2022-04-29 07:56:31 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-17 18:52:56 +00:00
|
|
|
testDB "github.com/prysmaticlabs/prysm/v4/beacon-chain/db/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/util"
|
2022-04-29 07:56:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_getBlock(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
beaconDB := testDB.SetupDB(t)
|
|
|
|
s := setupBeaconChain(t, beaconDB)
|
|
|
|
b1 := util.NewBeaconBlock()
|
|
|
|
r1, err := b1.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
b2 := util.NewBeaconBlock()
|
|
|
|
b2.Block.Slot = 100
|
|
|
|
r2, err := b2.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// block not found
|
|
|
|
_, err = s.getBlock(ctx, [32]byte{})
|
|
|
|
require.ErrorIs(t, err, errBlockNotFoundInCacheOrDB)
|
|
|
|
|
|
|
|
// block in cache
|
2022-08-02 15:30:46 +00:00
|
|
|
b, err := blocks.NewSignedBeaconBlock(b1)
|
2022-04-29 07:56:31 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-11 20:12:05 +00:00
|
|
|
require.NoError(t, s.saveInitSyncBlock(ctx, r1, b))
|
2022-04-29 07:56:31 +00:00
|
|
|
got, err := s.getBlock(ctx, r1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepEqual(t, b, got)
|
|
|
|
|
|
|
|
// block in db
|
2022-06-24 17:22:39 +00:00
|
|
|
b = util.SaveBlock(t, ctx, s.cfg.BeaconDB, b2)
|
2022-04-29 07:56:31 +00:00
|
|
|
got, err = s.getBlock(ctx, r2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepEqual(t, b, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_hasBlockInInitSyncOrDB(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
beaconDB := testDB.SetupDB(t)
|
|
|
|
s := setupBeaconChain(t, beaconDB)
|
|
|
|
b1 := util.NewBeaconBlock()
|
|
|
|
r1, err := b1.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
b2 := util.NewBeaconBlock()
|
|
|
|
b2.Block.Slot = 100
|
|
|
|
r2, err := b2.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// block not found
|
|
|
|
require.Equal(t, false, s.hasBlockInInitSyncOrDB(ctx, [32]byte{}))
|
|
|
|
|
|
|
|
// block in cache
|
2022-08-02 15:30:46 +00:00
|
|
|
b, err := blocks.NewSignedBeaconBlock(b1)
|
2022-04-29 07:56:31 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-11 20:12:05 +00:00
|
|
|
require.NoError(t, s.saveInitSyncBlock(ctx, r1, b))
|
2022-04-29 07:56:31 +00:00
|
|
|
require.Equal(t, true, s.hasBlockInInitSyncOrDB(ctx, r1))
|
|
|
|
|
|
|
|
// block in db
|
2022-06-24 17:22:39 +00:00
|
|
|
util.SaveBlock(t, ctx, s.cfg.BeaconDB, b2)
|
2022-04-29 07:56:31 +00:00
|
|
|
require.Equal(t, true, s.hasBlockInInitSyncOrDB(ctx, r2))
|
|
|
|
}
|