2020-01-27 21:48:16 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-05 01:33:45 +00:00
|
|
|
"fmt"
|
2021-02-10 16:35:44 +00:00
|
|
|
"math/big"
|
|
|
|
"strconv"
|
2020-01-27 21:48:16 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-02-09 20:57:22 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-06-10 21:13:15 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
2021-01-15 13:42:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache"
|
2020-01-27 21:48:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
2020-07-07 04:16:12 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
2020-01-27 21:48:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2020-02-05 17:05:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray"
|
2021-03-08 22:37:33 +00:00
|
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
2020-04-14 20:27:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
|
2021-06-30 15:06:19 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
2020-01-27 21:48:16 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2021-06-02 23:49:52 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
2021-07-06 15:34:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/proto/eth/v1alpha1/wrapper"
|
|
|
|
"github.com/prysmaticlabs/prysm/proto/interfaces"
|
2020-06-17 13:50:10 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/attestationutil"
|
2020-01-27 21:48:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-02-05 17:05:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-14 16:20:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2020-07-16 12:11:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-09-22 11:49:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/timeutils"
|
2020-01-27 21:48:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore_OnBlock(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
cfg := &Config{
|
2020-12-18 19:12:30 +00:00
|
|
|
BeaconDB: beaconDB,
|
|
|
|
StateGen: stategen.New(beaconDB),
|
2020-07-28 22:29:34 +00:00
|
|
|
ForkChoiceStore: protoarray.New(0, 0, [32]byte{}),
|
2020-04-14 20:27:03 +00:00
|
|
|
}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-31 20:57:01 +00:00
|
|
|
genesisStateRoot := [32]byte{}
|
2020-01-27 21:48:16 +00:00
|
|
|
genesis := blocks.NewGenesisBlock(genesisStateRoot[:])
|
2021-07-06 15:34:05 +00:00
|
|
|
assert.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(genesis)))
|
2020-08-27 18:13:32 +00:00
|
|
|
validGenesisRoot, err := genesis.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-08 20:00:09 +00:00
|
|
|
st, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(ctx, st.Copy(), validGenesisRoot))
|
2021-02-08 20:00:09 +00:00
|
|
|
roots, err := blockTree1(t, beaconDB, validGenesisRoot[:])
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 18:13:32 +00:00
|
|
|
random := testutil.NewBeaconBlock()
|
|
|
|
random.Block.Slot = 1
|
|
|
|
random.Block.ParentRoot = validGenesisRoot[:]
|
2021-07-06 15:34:05 +00:00
|
|
|
assert.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(random)))
|
2020-08-27 18:13:32 +00:00
|
|
|
randomParentRoot, err := random.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.NoError(t, err)
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: st.Slot(), Root: randomParentRoot[:]}))
|
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(ctx, st.Copy(), randomParentRoot))
|
2020-01-27 21:48:16 +00:00
|
|
|
randomParentRoot2 := roots[1]
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: st.Slot(), Root: randomParentRoot2}))
|
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(ctx, st.Copy(), bytesutil.ToBytes32(randomParentRoot2)))
|
2020-01-27 21:48:16 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2020-08-27 18:13:32 +00:00
|
|
|
blk *ethpb.SignedBeaconBlock
|
2021-03-08 22:37:33 +00:00
|
|
|
s iface.BeaconState
|
2020-01-27 21:48:16 +00:00
|
|
|
time uint64
|
|
|
|
wantErrString string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "parent block root does not have a state",
|
2020-08-27 18:13:32 +00:00
|
|
|
blk: testutil.NewBeaconBlock(),
|
2020-02-13 20:34:50 +00:00
|
|
|
s: st.Copy(),
|
2020-06-23 00:19:33 +00:00
|
|
|
wantErrString: "could not reconstruct parent state",
|
2020-01-27 21:48:16 +00:00
|
|
|
},
|
|
|
|
{
|
2020-08-27 18:13:32 +00:00
|
|
|
name: "block is from the future",
|
|
|
|
blk: func() *ethpb.SignedBeaconBlock {
|
|
|
|
b := testutil.NewBeaconBlock()
|
2020-09-02 18:38:03 +00:00
|
|
|
b.Block.ParentRoot = randomParentRoot2
|
2021-02-16 07:45:34 +00:00
|
|
|
b.Block.Slot = params.BeaconConfig().FarFutureSlot
|
2020-08-27 18:13:32 +00:00
|
|
|
return b
|
|
|
|
}(),
|
2020-02-13 20:34:50 +00:00
|
|
|
s: st.Copy(),
|
2020-09-02 18:38:03 +00:00
|
|
|
wantErrString: "is in the far distant future",
|
2020-01-27 21:48:16 +00:00
|
|
|
},
|
|
|
|
{
|
2020-08-27 18:13:32 +00:00
|
|
|
name: "could not get finalized block",
|
|
|
|
blk: func() *ethpb.SignedBeaconBlock {
|
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block.ParentRoot = randomParentRoot[:]
|
|
|
|
return b
|
|
|
|
}(),
|
2020-02-13 20:34:50 +00:00
|
|
|
s: st.Copy(),
|
2020-07-11 00:16:52 +00:00
|
|
|
wantErrString: "is not a descendent of the current finalized block",
|
2020-01-27 21:48:16 +00:00
|
|
|
},
|
|
|
|
{
|
2020-08-27 18:13:32 +00:00
|
|
|
name: "same slot as finalized block",
|
|
|
|
blk: func() *ethpb.SignedBeaconBlock {
|
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block.Slot = 0
|
|
|
|
b.Block.ParentRoot = randomParentRoot2
|
|
|
|
return b
|
|
|
|
}(),
|
2020-02-13 20:34:50 +00:00
|
|
|
s: st.Copy(),
|
2020-01-27 21:48:16 +00:00
|
|
|
wantErrString: "block is equal or earlier than finalized block, slot 0 < slot 0",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
service.justifiedCheckpt = ðpb.Checkpoint{Root: validGenesisRoot[:]}
|
|
|
|
service.bestJustifiedCheckpt = ðpb.Checkpoint{Root: validGenesisRoot[:]}
|
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{Root: validGenesisRoot[:]}
|
|
|
|
service.prevFinalizedCheckpt = ðpb.Checkpoint{Root: validGenesisRoot[:]}
|
|
|
|
service.finalizedCheckpt.Root = roots[0]
|
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
root, err := tt.blk.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.NoError(t, err)
|
2021-07-06 15:34:05 +00:00
|
|
|
err = service.onBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(tt.blk), root)
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.ErrorContains(t, tt.wantErrString, err)
|
2020-01-27 21:48:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:16:12 +00:00
|
|
|
func TestStore_OnBlockBatch(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-07-07 04:16:12 +00:00
|
|
|
|
|
|
|
cfg := &Config{
|
2020-12-18 19:12:30 +00:00
|
|
|
BeaconDB: beaconDB,
|
|
|
|
StateGen: stategen.New(beaconDB),
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-07 04:16:12 +00:00
|
|
|
|
|
|
|
genesisStateRoot := [32]byte{}
|
|
|
|
genesis := blocks.NewGenesisBlock(genesisStateRoot[:])
|
2021-07-06 15:34:05 +00:00
|
|
|
assert.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(genesis)))
|
2020-09-02 18:38:03 +00:00
|
|
|
gRoot, err := genesis.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{
|
|
|
|
Root: gRoot[:],
|
|
|
|
}
|
2021-03-17 18:36:56 +00:00
|
|
|
service.cfg.ForkChoiceStore = protoarray.New(0, 0, [32]byte{})
|
2021-07-06 15:34:05 +00:00
|
|
|
service.saveInitSyncBlock(gRoot, wrapper.WrappedPhase0SignedBeaconBlock(genesis))
|
2020-07-07 04:16:12 +00:00
|
|
|
|
|
|
|
st, keys := testutil.DeterministicGenesisState(t, 64)
|
|
|
|
|
|
|
|
bState := st.Copy()
|
|
|
|
|
2021-05-26 16:19:54 +00:00
|
|
|
var blks []interfaces.SignedBeaconBlock
|
2020-10-12 08:11:05 +00:00
|
|
|
var blkRoots [][32]byte
|
2021-03-08 22:37:33 +00:00
|
|
|
var firstState iface.BeaconState
|
2020-07-07 04:16:12 +00:00
|
|
|
for i := 1; i < 10; i++ {
|
2021-02-16 07:45:34 +00:00
|
|
|
b, err := testutil.GenerateFullBlock(bState, keys, testutil.DefaultBlockGenConfig(), types.Slot(i))
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-06 15:34:05 +00:00
|
|
|
bState, err = state.ExecuteStateTransition(ctx, bState, wrapper.WrappedPhase0SignedBeaconBlock(b))
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-07 04:16:12 +00:00
|
|
|
if i == 1 {
|
|
|
|
firstState = bState.Copy()
|
|
|
|
}
|
2020-08-27 18:13:32 +00:00
|
|
|
root, err := b.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-06 15:34:05 +00:00
|
|
|
service.saveInitSyncBlock(root, wrapper.WrappedPhase0SignedBeaconBlock(b))
|
|
|
|
blks = append(blks, wrapper.WrappedPhase0SignedBeaconBlock(b))
|
2020-07-07 04:16:12 +00:00
|
|
|
blkRoots = append(blkRoots, root)
|
|
|
|
}
|
2020-09-02 18:38:03 +00:00
|
|
|
|
2021-05-26 16:19:54 +00:00
|
|
|
rBlock, err := blks[0].PbPhase0Block()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rBlock.Block.ParentRoot = gRoot[:]
|
2020-12-18 19:12:30 +00:00
|
|
|
require.NoError(t, beaconDB.SaveBlock(context.Background(), blks[0]))
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.StateGen.SaveState(ctx, blkRoots[0], firstState))
|
2020-07-20 19:35:30 +00:00
|
|
|
_, _, err = service.onBlockBatch(ctx, blks[1:], blkRoots[1:])
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 21:48:16 +00:00
|
|
|
func TestRemoveStateSinceLastFinalized_EmptyStartSlot(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-01-27 21:48:16 +00:00
|
|
|
params.UseMinimalConfig()
|
|
|
|
defer params.UseMainnetConfig()
|
|
|
|
|
2020-12-18 19:12:30 +00:00
|
|
|
cfg := &Config{BeaconDB: beaconDB, ForkChoiceStore: protoarray.New(0, 0, [32]byte{})}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-27 21:48:16 +00:00
|
|
|
service.genesisTime = time.Now()
|
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
update, err := service.shouldUpdateCurrentJustified(ctx, ðpb.Checkpoint{Root: make([]byte, 32)})
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, update, "Should be able to update justified")
|
2020-06-09 22:40:48 +00:00
|
|
|
lastJustifiedBlk := testutil.NewBeaconBlock()
|
|
|
|
lastJustifiedBlk.Block.ParentRoot = bytesutil.PadTo([]byte{'G'}, 32)
|
2020-08-27 18:13:32 +00:00
|
|
|
lastJustifiedRoot, err := lastJustifiedBlk.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-09 22:40:48 +00:00
|
|
|
newJustifiedBlk := testutil.NewBeaconBlock()
|
|
|
|
newJustifiedBlk.Block.Slot = 1
|
|
|
|
newJustifiedBlk.Block.ParentRoot = bytesutil.PadTo(lastJustifiedRoot[:], 32)
|
2020-08-27 18:13:32 +00:00
|
|
|
newJustifiedRoot, err := newJustifiedBlk.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(newJustifiedBlk)))
|
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(lastJustifiedBlk)))
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2021-02-16 07:45:34 +00:00
|
|
|
diff := params.BeaconConfig().SlotsPerEpoch.Sub(1).Mul(params.BeaconConfig().SecondsPerSlot)
|
2020-01-27 21:48:16 +00:00
|
|
|
service.genesisTime = time.Unix(time.Now().Unix()-int64(diff), 0)
|
|
|
|
service.justifiedCheckpt = ðpb.Checkpoint{Root: lastJustifiedRoot[:]}
|
|
|
|
update, err = service.shouldUpdateCurrentJustified(ctx, ðpb.Checkpoint{Root: newJustifiedRoot[:]})
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, update, "Should be able to update justified")
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldUpdateJustified_ReturnFalse(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-01-27 21:48:16 +00:00
|
|
|
params.UseMinimalConfig()
|
|
|
|
defer params.UseMainnetConfig()
|
|
|
|
|
2020-12-18 19:12:30 +00:00
|
|
|
cfg := &Config{BeaconDB: beaconDB}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-09 22:40:48 +00:00
|
|
|
lastJustifiedBlk := testutil.NewBeaconBlock()
|
|
|
|
lastJustifiedBlk.Block.ParentRoot = bytesutil.PadTo([]byte{'G'}, 32)
|
2020-08-27 18:13:32 +00:00
|
|
|
lastJustifiedRoot, err := lastJustifiedBlk.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-09 22:40:48 +00:00
|
|
|
newJustifiedBlk := testutil.NewBeaconBlock()
|
|
|
|
newJustifiedBlk.Block.ParentRoot = bytesutil.PadTo(lastJustifiedRoot[:], 32)
|
2020-08-27 18:13:32 +00:00
|
|
|
newJustifiedRoot, err := newJustifiedBlk.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(newJustifiedBlk)))
|
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(lastJustifiedBlk)))
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2021-02-16 07:45:34 +00:00
|
|
|
diff := params.BeaconConfig().SlotsPerEpoch.Sub(1).Mul(params.BeaconConfig().SecondsPerSlot)
|
2020-01-27 21:48:16 +00:00
|
|
|
service.genesisTime = time.Unix(time.Now().Unix()-int64(diff), 0)
|
|
|
|
service.justifiedCheckpt = ðpb.Checkpoint{Root: lastJustifiedRoot[:]}
|
|
|
|
|
|
|
|
update, err := service.shouldUpdateCurrentJustified(ctx, ðpb.Checkpoint{Root: newJustifiedRoot[:]})
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, update, "Should not be able to update justified, received true")
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
func TestCachedPreState_CanGetFromStateSummary(t *testing.T) {
|
2020-01-27 21:48:16 +00:00
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
cfg := &Config{
|
2020-12-18 19:12:30 +00:00
|
|
|
BeaconDB: beaconDB,
|
|
|
|
StateGen: stategen.New(beaconDB),
|
2020-04-14 20:27:03 +00:00
|
|
|
}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2021-06-30 15:06:19 +00:00
|
|
|
s, err := v1.InitializeFromProto(&pb.BeaconState{Slot: 1, GenesisValidatorsRoot: params.BeaconConfig().ZeroHash[:]})
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-02 18:38:03 +00:00
|
|
|
|
|
|
|
genesisStateRoot := [32]byte{}
|
|
|
|
genesis := blocks.NewGenesisBlock(genesisStateRoot[:])
|
2021-07-06 15:34:05 +00:00
|
|
|
assert.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(genesis)))
|
2020-09-02 18:38:03 +00:00
|
|
|
gRoot, err := genesis.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{
|
|
|
|
Root: gRoot[:],
|
|
|
|
}
|
2021-03-17 18:36:56 +00:00
|
|
|
service.cfg.ForkChoiceStore = protoarray.New(0, 0, [32]byte{})
|
2021-07-06 15:34:05 +00:00
|
|
|
service.saveInitSyncBlock(gRoot, wrapper.WrappedPhase0SignedBeaconBlock(genesis))
|
2020-09-02 18:38:03 +00:00
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block.Slot = 1
|
2020-09-02 18:38:03 +00:00
|
|
|
b.Block.ParentRoot = gRoot[:]
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 1, Root: gRoot[:]}))
|
|
|
|
require.NoError(t, service.cfg.StateGen.SaveState(ctx, gRoot, s))
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.verifyBlkPreState(ctx, wrapper.WrappedPhase0BeaconBlock(b.Block)))
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCachedPreState_CanGetFromDB(t *testing.T) {
|
2020-06-23 20:40:55 +00:00
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-06-23 20:40:55 +00:00
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
cfg := &Config{
|
2020-12-18 19:12:30 +00:00
|
|
|
BeaconDB: beaconDB,
|
|
|
|
StateGen: stategen.New(beaconDB),
|
2020-04-14 20:27:03 +00:00
|
|
|
}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2020-09-02 18:38:03 +00:00
|
|
|
genesisStateRoot := [32]byte{}
|
|
|
|
genesis := blocks.NewGenesisBlock(genesisStateRoot[:])
|
2021-07-06 15:34:05 +00:00
|
|
|
assert.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(genesis)))
|
2020-09-02 18:38:03 +00:00
|
|
|
gRoot, err := genesis.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{
|
|
|
|
Root: gRoot[:],
|
|
|
|
}
|
2021-03-17 18:36:56 +00:00
|
|
|
service.cfg.ForkChoiceStore = protoarray.New(0, 0, [32]byte{})
|
2021-07-06 15:34:05 +00:00
|
|
|
service.saveInitSyncBlock(gRoot, wrapper.WrappedPhase0SignedBeaconBlock(genesis))
|
2020-09-02 18:38:03 +00:00
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block.Slot = 1
|
2020-09-02 18:38:03 +00:00
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{Root: gRoot[:]}
|
2021-07-06 15:34:05 +00:00
|
|
|
err = service.verifyBlkPreState(ctx, wrapper.WrappedPhase0BeaconBlock(b.Block))
|
2020-04-18 00:32:45 +00:00
|
|
|
wanted := "could not reconstruct parent state"
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.ErrorContains(t, wanted, err)
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2020-09-02 18:38:03 +00:00
|
|
|
b.Block.ParentRoot = gRoot[:]
|
2021-06-30 15:06:19 +00:00
|
|
|
s, err := v1.InitializeFromProto(&pb.BeaconState{Slot: 1})
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 1, Root: gRoot[:]}))
|
|
|
|
require.NoError(t, service.cfg.StateGen.SaveState(ctx, gRoot, s))
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.verifyBlkPreState(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b).Block()))
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateJustified_CouldUpdateBest(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2020-12-18 19:12:30 +00:00
|
|
|
cfg := &Config{BeaconDB: beaconDB, StateGen: stategen.New(beaconDB)}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
signedBlock := testutil.NewBeaconBlock()
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(signedBlock)))
|
2020-08-27 18:13:32 +00:00
|
|
|
r, err := signedBlock.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-27 21:48:16 +00:00
|
|
|
service.justifiedCheckpt = ðpb.Checkpoint{Root: []byte{'A'}}
|
|
|
|
service.bestJustifiedCheckpt = ðpb.Checkpoint{Root: []byte{'A'}}
|
2021-02-08 20:00:09 +00:00
|
|
|
st, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-12-18 19:12:30 +00:00
|
|
|
require.NoError(t, beaconDB.SaveState(ctx, st.Copy(), r))
|
2020-01-27 21:48:16 +00:00
|
|
|
|
|
|
|
// Could update
|
2021-02-08 20:00:09 +00:00
|
|
|
s, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, s.SetCurrentJustifiedCheckpoint(ðpb.Checkpoint{Epoch: 1, Root: r[:]}))
|
|
|
|
require.NoError(t, service.updateJustified(context.Background(), s))
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.Equal(t, s.CurrentJustifiedCheckpoint().Epoch, service.bestJustifiedCheckpt.Epoch, "Incorrect justified epoch in service")
|
2020-01-27 21:48:16 +00:00
|
|
|
|
|
|
|
// Could not update
|
|
|
|
service.bestJustifiedCheckpt.Epoch = 2
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, service.updateJustified(context.Background(), s))
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2021-02-09 10:05:22 +00:00
|
|
|
assert.Equal(t, types.Epoch(2), service.bestJustifiedCheckpt.Epoch, "Incorrect justified epoch in service")
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 17:05:51 +00:00
|
|
|
func TestFillForkChoiceMissingBlocks_CanSave(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-02-05 17:05:51 +00:00
|
|
|
|
2020-12-18 19:12:30 +00:00
|
|
|
cfg := &Config{BeaconDB: beaconDB}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-17 18:36:56 +00:00
|
|
|
service.cfg.ForkChoiceStore = protoarray.New(0, 0, [32]byte{'A'})
|
2020-08-27 18:13:32 +00:00
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{Root: make([]byte, 32)}
|
2020-02-05 17:05:51 +00:00
|
|
|
|
|
|
|
genesisStateRoot := [32]byte{}
|
|
|
|
genesis := blocks.NewGenesisBlock(genesisStateRoot[:])
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(genesis)))
|
2020-08-27 18:13:32 +00:00
|
|
|
validGenesisRoot, err := genesis.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-08 20:00:09 +00:00
|
|
|
st, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-04-14 20:27:03 +00:00
|
|
|
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(ctx, st.Copy(), validGenesisRoot))
|
2021-02-08 20:00:09 +00:00
|
|
|
roots, err := blockTree1(t, beaconDB, validGenesisRoot[:])
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-05 17:05:51 +00:00
|
|
|
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-08-27 18:13:32 +00:00
|
|
|
block := testutil.NewBeaconBlock()
|
|
|
|
block.Block.Slot = 9
|
|
|
|
block.Block.ParentRoot = roots[8]
|
2020-02-05 17:05:51 +00:00
|
|
|
|
2020-07-16 12:11:39 +00:00
|
|
|
err = service.fillInForkChoiceMissingBlocks(
|
2021-07-06 15:34:05 +00:00
|
|
|
context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(block).Block(), beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint())
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-05 17:05:51 +00:00
|
|
|
|
2020-07-16 12:11:39 +00:00
|
|
|
// 5 nodes from the block tree 1. B0 - B3 - B4 - B6 - B8
|
2021-03-17 18:36:56 +00:00
|
|
|
assert.Equal(t, 5, len(service.cfg.ForkChoiceStore.Nodes()), "Miss match nodes")
|
|
|
|
assert.Equal(t, true, service.cfg.ForkChoiceStore.HasNode(bytesutil.ToBytes32(roots[4])), "Didn't save node")
|
|
|
|
assert.Equal(t, true, service.cfg.ForkChoiceStore.HasNode(bytesutil.ToBytes32(roots[6])), "Didn't save node")
|
|
|
|
assert.Equal(t, true, service.cfg.ForkChoiceStore.HasNode(bytesutil.ToBytes32(roots[8])), "Didn't save node")
|
2020-02-05 17:05:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 01:33:45 +00:00
|
|
|
func TestFillForkChoiceMissingBlocks_RootsMatch(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-11-05 01:33:45 +00:00
|
|
|
|
2020-12-18 19:12:30 +00:00
|
|
|
cfg := &Config{BeaconDB: beaconDB}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-11-05 01:33:45 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-17 18:36:56 +00:00
|
|
|
service.cfg.ForkChoiceStore = protoarray.New(0, 0, [32]byte{'A'})
|
2020-11-05 01:33:45 +00:00
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{Root: make([]byte, 32)}
|
|
|
|
|
|
|
|
genesisStateRoot := [32]byte{}
|
|
|
|
genesis := blocks.NewGenesisBlock(genesisStateRoot[:])
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(genesis)))
|
2020-11-05 01:33:45 +00:00
|
|
|
validGenesisRoot, err := genesis.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
2021-02-08 20:00:09 +00:00
|
|
|
st, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-11-05 01:33:45 +00:00
|
|
|
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(ctx, st.Copy(), validGenesisRoot))
|
2021-02-08 20:00:09 +00:00
|
|
|
roots, err := blockTree1(t, beaconDB, validGenesisRoot[:])
|
2020-11-05 01:33:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
block := testutil.NewBeaconBlock()
|
|
|
|
block.Block.Slot = 9
|
|
|
|
block.Block.ParentRoot = roots[8]
|
|
|
|
|
|
|
|
err = service.fillInForkChoiceMissingBlocks(
|
2021-07-06 15:34:05 +00:00
|
|
|
context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(block).Block(), beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint())
|
2020-11-05 01:33:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 5 nodes from the block tree 1. B0 - B3 - B4 - B6 - B8
|
2021-03-17 18:36:56 +00:00
|
|
|
assert.Equal(t, 5, len(service.cfg.ForkChoiceStore.Nodes()), "Miss match nodes")
|
2020-11-05 01:33:45 +00:00
|
|
|
// Ensure all roots and their respective blocks exist.
|
|
|
|
wantedRoots := [][]byte{roots[0], roots[3], roots[4], roots[6], roots[8]}
|
|
|
|
for i, rt := range wantedRoots {
|
2021-03-17 18:36:56 +00:00
|
|
|
assert.Equal(t, true, service.cfg.ForkChoiceStore.HasNode(bytesutil.ToBytes32(rt)), fmt.Sprintf("Didn't save node: %d", i))
|
|
|
|
assert.Equal(t, true, service.cfg.BeaconDB.HasBlock(context.Background(), bytesutil.ToBytes32(rt)))
|
2020-11-05 01:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 20:13:38 +00:00
|
|
|
func TestFillForkChoiceMissingBlocks_FilterFinalized(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-02-05 20:13:38 +00:00
|
|
|
|
2020-12-18 19:12:30 +00:00
|
|
|
cfg := &Config{BeaconDB: beaconDB}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-17 18:36:56 +00:00
|
|
|
service.cfg.ForkChoiceStore = protoarray.New(0, 0, [32]byte{'A'})
|
2020-02-05 20:13:38 +00:00
|
|
|
// Set finalized epoch to 1.
|
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{Epoch: 1}
|
|
|
|
|
|
|
|
genesisStateRoot := [32]byte{}
|
|
|
|
genesis := blocks.NewGenesisBlock(genesisStateRoot[:])
|
2021-07-06 15:34:05 +00:00
|
|
|
assert.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(genesis)))
|
2020-08-27 18:13:32 +00:00
|
|
|
validGenesisRoot, err := genesis.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-08 20:00:09 +00:00
|
|
|
st, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-04-14 20:27:03 +00:00
|
|
|
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(ctx, st.Copy(), validGenesisRoot))
|
2020-02-05 20:13:38 +00:00
|
|
|
|
|
|
|
// Define a tree branch, slot 63 <- 64 <- 65
|
2020-08-27 18:13:32 +00:00
|
|
|
b63 := testutil.NewBeaconBlock()
|
|
|
|
b63.Block.Slot = 63
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b63)))
|
2020-08-27 18:13:32 +00:00
|
|
|
r63, err := b63.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 18:13:32 +00:00
|
|
|
b64 := testutil.NewBeaconBlock()
|
|
|
|
b64.Block.Slot = 64
|
|
|
|
b64.Block.ParentRoot = r63[:]
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b64)))
|
2020-08-27 18:13:32 +00:00
|
|
|
r64, err := b64.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 18:13:32 +00:00
|
|
|
b65 := testutil.NewBeaconBlock()
|
|
|
|
b65.Block.Slot = 65
|
|
|
|
b65.Block.ParentRoot = r64[:]
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b65)))
|
2020-02-05 20:13:38 +00:00
|
|
|
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-07-16 12:11:39 +00:00
|
|
|
err = service.fillInForkChoiceMissingBlocks(
|
2021-07-06 15:34:05 +00:00
|
|
|
context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(b65).Block(), beaconState.FinalizedCheckpoint(), beaconState.CurrentJustifiedCheckpoint())
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-05 20:13:38 +00:00
|
|
|
|
|
|
|
// There should be 2 nodes, block 65 and block 64.
|
2021-03-17 18:36:56 +00:00
|
|
|
assert.Equal(t, 2, len(service.cfg.ForkChoiceStore.Nodes()), "Miss match nodes")
|
2020-02-05 20:13:38 +00:00
|
|
|
|
|
|
|
// Block with slot 63 should be in fork choice because it's less than finalized epoch 1.
|
2021-03-17 18:36:56 +00:00
|
|
|
assert.Equal(t, true, service.cfg.ForkChoiceStore.HasNode(r63), "Didn't save node")
|
2020-02-05 20:13:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 21:48:16 +00:00
|
|
|
// blockTree1 constructs the following tree:
|
|
|
|
// /- B1
|
|
|
|
// B0 /- B5 - B7
|
|
|
|
// \- B3 - B4 - B6 - B8
|
|
|
|
// (B1, and B3 are all from the same slots)
|
2021-02-08 20:00:09 +00:00
|
|
|
func blockTree1(t *testing.T, beaconDB db.Database, genesisRoot []byte) ([][]byte, error) {
|
2020-08-27 18:13:32 +00:00
|
|
|
genesisRoot = bytesutil.PadTo(genesisRoot, 32)
|
|
|
|
b0 := testutil.NewBeaconBlock()
|
|
|
|
b0.Block.Slot = 0
|
|
|
|
b0.Block.ParentRoot = genesisRoot
|
|
|
|
r0, err := b0.Block.HashTreeRoot()
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-27 18:13:32 +00:00
|
|
|
b1 := testutil.NewBeaconBlock()
|
|
|
|
b1.Block.Slot = 1
|
|
|
|
b1.Block.ParentRoot = r0[:]
|
|
|
|
r1, err := b1.Block.HashTreeRoot()
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-27 18:13:32 +00:00
|
|
|
b3 := testutil.NewBeaconBlock()
|
|
|
|
b3.Block.Slot = 3
|
|
|
|
b3.Block.ParentRoot = r0[:]
|
|
|
|
r3, err := b3.Block.HashTreeRoot()
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-27 18:13:32 +00:00
|
|
|
b4 := testutil.NewBeaconBlock()
|
|
|
|
b4.Block.Slot = 4
|
|
|
|
b4.Block.ParentRoot = r3[:]
|
|
|
|
r4, err := b4.Block.HashTreeRoot()
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-27 18:13:32 +00:00
|
|
|
b5 := testutil.NewBeaconBlock()
|
|
|
|
b5.Block.Slot = 5
|
|
|
|
b5.Block.ParentRoot = r4[:]
|
|
|
|
r5, err := b5.Block.HashTreeRoot()
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-27 18:13:32 +00:00
|
|
|
b6 := testutil.NewBeaconBlock()
|
|
|
|
b6.Block.Slot = 6
|
|
|
|
b6.Block.ParentRoot = r4[:]
|
|
|
|
r6, err := b6.Block.HashTreeRoot()
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-27 18:13:32 +00:00
|
|
|
b7 := testutil.NewBeaconBlock()
|
|
|
|
b7.Block.Slot = 7
|
|
|
|
b7.Block.ParentRoot = r5[:]
|
|
|
|
r7, err := b7.Block.HashTreeRoot()
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-27 18:13:32 +00:00
|
|
|
b8 := testutil.NewBeaconBlock()
|
|
|
|
b8.Block.Slot = 8
|
|
|
|
b8.Block.ParentRoot = r6[:]
|
|
|
|
r8, err := b8.Block.HashTreeRoot()
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-08 20:00:09 +00:00
|
|
|
st, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-04-14 20:27:03 +00:00
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
for _, b := range []*ethpb.SignedBeaconBlock{b0, b1, b3, b4, b5, b6, b7, b8} {
|
2020-06-09 22:40:48 +00:00
|
|
|
beaconBlock := testutil.NewBeaconBlock()
|
2020-08-27 18:13:32 +00:00
|
|
|
beaconBlock.Block.Slot = b.Block.Slot
|
|
|
|
beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32)
|
2021-07-06 15:34:05 +00:00
|
|
|
if err := beaconDB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(beaconBlock)); err != nil {
|
2020-01-27 21:48:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-18 19:12:30 +00:00
|
|
|
if err := beaconDB.SaveState(context.Background(), st.Copy(), bytesutil.ToBytes32(beaconBlock.Block.ParentRoot)); err != nil {
|
2020-08-27 18:13:32 +00:00
|
|
|
return nil, errors.Wrap(err, "could not save state")
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-18 19:12:30 +00:00
|
|
|
if err := beaconDB.SaveState(context.Background(), st.Copy(), r1); err != nil {
|
2020-01-27 21:48:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-18 19:12:30 +00:00
|
|
|
if err := beaconDB.SaveState(context.Background(), st.Copy(), r7); err != nil {
|
2020-01-27 21:48:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-18 19:12:30 +00:00
|
|
|
if err := beaconDB.SaveState(context.Background(), st.Copy(), r8); err != nil {
|
2020-01-27 21:48:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return [][]byte{r0[:], r1[:], nil, r3[:], r4[:], r5[:], r6[:], r7[:], r8[:]}, nil
|
|
|
|
}
|
2020-04-17 03:18:07 +00:00
|
|
|
|
|
|
|
func TestCurrentSlot_HandlesOverflow(t *testing.T) {
|
2020-09-22 11:49:58 +00:00
|
|
|
svc := Service{genesisTime: timeutils.Now().Add(1 * time.Hour)}
|
2020-04-17 03:18:07 +00:00
|
|
|
|
|
|
|
slot := svc.CurrentSlot()
|
2021-02-16 07:45:34 +00:00
|
|
|
require.Equal(t, types.Slot(0), slot, "Unexpected slot")
|
2020-04-17 03:18:07 +00:00
|
|
|
}
|
2020-10-10 00:07:56 +00:00
|
|
|
func TestAncestorByDB_CtxErr(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, &Config{})
|
2020-10-10 00:07:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
_, err = service.ancestorByDB(ctx, [32]byte{}, 0)
|
|
|
|
require.ErrorContains(t, "context canceled", err)
|
|
|
|
}
|
2020-06-14 06:58:36 +00:00
|
|
|
|
|
|
|
func TestAncestor_HandleSkipSlot(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-06-14 06:58:36 +00:00
|
|
|
|
2020-12-18 19:12:30 +00:00
|
|
|
cfg := &Config{BeaconDB: beaconDB, ForkChoiceStore: protoarray.New(0, 0, [32]byte{})}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 06:58:36 +00:00
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
b1 := testutil.NewBeaconBlock()
|
|
|
|
b1.Block.Slot = 1
|
|
|
|
b1.Block.ParentRoot = bytesutil.PadTo([]byte{'a'}, 32)
|
|
|
|
r1, err := b1.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 18:13:32 +00:00
|
|
|
b100 := testutil.NewBeaconBlock()
|
|
|
|
b100.Block.Slot = 100
|
|
|
|
b100.Block.ParentRoot = r1[:]
|
|
|
|
r100, err := b100.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 18:13:32 +00:00
|
|
|
b200 := testutil.NewBeaconBlock()
|
|
|
|
b200.Block.Slot = 200
|
|
|
|
b200.Block.ParentRoot = r100[:]
|
|
|
|
r200, err := b200.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 18:13:32 +00:00
|
|
|
for _, b := range []*ethpb.SignedBeaconBlock{b1, b100, b200} {
|
2020-06-14 06:58:36 +00:00
|
|
|
beaconBlock := testutil.NewBeaconBlock()
|
2020-08-27 18:13:32 +00:00
|
|
|
beaconBlock.Block.Slot = b.Block.Slot
|
|
|
|
beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32)
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, beaconDB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(beaconBlock)))
|
2020-06-14 06:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Slots 100 to 200 are skip slots. Requesting root at 150 will yield root at 100. The last physical block.
|
|
|
|
r, err := service.ancestor(context.Background(), r200[:], 150)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 06:58:36 +00:00
|
|
|
if bytesutil.ToBytes32(r) != r100 {
|
|
|
|
t.Error("Did not get correct root")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slots 1 to 100 are skip slots. Requesting root at 50 will yield root at 1. The last physical block.
|
|
|
|
r, err = service.ancestor(context.Background(), r200[:], 50)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 06:58:36 +00:00
|
|
|
if bytesutil.ToBytes32(r) != r1 {
|
|
|
|
t.Error("Did not get correct root")
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 17:43:25 +00:00
|
|
|
|
2020-10-10 00:07:56 +00:00
|
|
|
func TestAncestor_CanUseForkchoice(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
cfg := &Config{ForkChoiceStore: protoarray.New(0, 0, [32]byte{})}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-10-10 00:07:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b1 := testutil.NewBeaconBlock()
|
|
|
|
b1.Block.Slot = 1
|
|
|
|
b1.Block.ParentRoot = bytesutil.PadTo([]byte{'a'}, 32)
|
|
|
|
r1, err := b1.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
b100 := testutil.NewBeaconBlock()
|
|
|
|
b100.Block.Slot = 100
|
|
|
|
b100.Block.ParentRoot = r1[:]
|
|
|
|
r100, err := b100.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
b200 := testutil.NewBeaconBlock()
|
|
|
|
b200.Block.Slot = 200
|
|
|
|
b200.Block.ParentRoot = r100[:]
|
|
|
|
r200, err := b200.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, b := range []*ethpb.SignedBeaconBlock{b1, b100, b200} {
|
|
|
|
beaconBlock := testutil.NewBeaconBlock()
|
|
|
|
beaconBlock.Block.Slot = b.Block.Slot
|
|
|
|
beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32)
|
|
|
|
r, err := b.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.ForkChoiceStore.ProcessBlock(context.Background(), b.Block.Slot, r, bytesutil.ToBytes32(b.Block.ParentRoot), [32]byte{}, 0, 0)) // Saves blocks to fork choice store.
|
2020-10-10 00:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r, err := service.ancestor(context.Background(), r200[:], 150)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if bytesutil.ToBytes32(r) != r100 {
|
|
|
|
t.Error("Did not get correct root")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAncestor_CanUseDB(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-10-10 00:07:56 +00:00
|
|
|
|
2020-12-18 19:12:30 +00:00
|
|
|
cfg := &Config{BeaconDB: beaconDB, ForkChoiceStore: protoarray.New(0, 0, [32]byte{})}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-10-10 00:07:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b1 := testutil.NewBeaconBlock()
|
|
|
|
b1.Block.Slot = 1
|
|
|
|
b1.Block.ParentRoot = bytesutil.PadTo([]byte{'a'}, 32)
|
|
|
|
r1, err := b1.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
b100 := testutil.NewBeaconBlock()
|
|
|
|
b100.Block.Slot = 100
|
|
|
|
b100.Block.ParentRoot = r1[:]
|
|
|
|
r100, err := b100.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
b200 := testutil.NewBeaconBlock()
|
|
|
|
b200.Block.Slot = 200
|
|
|
|
b200.Block.ParentRoot = r100[:]
|
|
|
|
r200, err := b200.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, b := range []*ethpb.SignedBeaconBlock{b1, b100, b200} {
|
|
|
|
beaconBlock := testutil.NewBeaconBlock()
|
|
|
|
beaconBlock.Block.Slot = b.Block.Slot
|
|
|
|
beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32)
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, beaconDB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(beaconBlock))) // Saves blocks to DB.
|
2020-10-10 00:07:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.ForkChoiceStore.ProcessBlock(context.Background(), 200, r200, r200, [32]byte{}, 0, 0))
|
2020-10-10 00:07:56 +00:00
|
|
|
|
|
|
|
r, err := service.ancestor(context.Background(), r200[:], 150)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if bytesutil.ToBytes32(r) != r100 {
|
|
|
|
t.Error("Did not get correct root")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 17:43:25 +00:00
|
|
|
func TestEnsureRootNotZeroHashes(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
cfg := &Config{}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-14 17:43:25 +00:00
|
|
|
service.genesisRoot = [32]byte{'a'}
|
|
|
|
|
|
|
|
r := service.ensureRootNotZeros(params.BeaconConfig().ZeroHash)
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.Equal(t, service.genesisRoot, r, "Did not get wanted justified root")
|
2020-06-14 17:43:25 +00:00
|
|
|
root := [32]byte{'b'}
|
|
|
|
r = service.ensureRootNotZeros(root)
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.Equal(t, root, r, "Did not get wanted justified root")
|
2020-06-14 17:43:25 +00:00
|
|
|
}
|
2020-06-17 13:50:10 +00:00
|
|
|
|
|
|
|
func TestFinalizedImpliesNewJustified(t *testing.T) {
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-06-17 13:50:10 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
type args struct {
|
|
|
|
cachedCheckPoint *ethpb.Checkpoint
|
|
|
|
stateCheckPoint *ethpb.Checkpoint
|
|
|
|
diffFinalizedCheckPoint bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *ethpb.Checkpoint
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Same justified, do nothing",
|
|
|
|
args: args{
|
|
|
|
cachedCheckPoint: ðpb.Checkpoint{Epoch: 1, Root: []byte{'a'}},
|
|
|
|
stateCheckPoint: ðpb.Checkpoint{Epoch: 1, Root: []byte{'a'}},
|
|
|
|
},
|
|
|
|
want: ðpb.Checkpoint{Epoch: 1, Root: []byte{'a'}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Different justified, higher epoch, cache new justified",
|
|
|
|
args: args{
|
|
|
|
cachedCheckPoint: ðpb.Checkpoint{Epoch: 1, Root: []byte{'a'}},
|
|
|
|
stateCheckPoint: ðpb.Checkpoint{Epoch: 2, Root: []byte{'b'}},
|
|
|
|
},
|
|
|
|
want: ðpb.Checkpoint{Epoch: 2, Root: []byte{'b'}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "finalized has different justified, cache new justified",
|
|
|
|
args: args{
|
|
|
|
cachedCheckPoint: ðpb.Checkpoint{Epoch: 1, Root: []byte{'a'}},
|
|
|
|
stateCheckPoint: ðpb.Checkpoint{Epoch: 1, Root: []byte{'b'}},
|
|
|
|
diffFinalizedCheckPoint: true,
|
|
|
|
},
|
|
|
|
want: ðpb.Checkpoint{Epoch: 1, Root: []byte{'b'}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2021-02-08 20:00:09 +00:00
|
|
|
beaconState, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(test.args.stateCheckPoint))
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, &Config{BeaconDB: beaconDB, StateGen: stategen.New(beaconDB), ForkChoiceStore: protoarray.New(0, 0, [32]byte{})})
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-17 13:50:10 +00:00
|
|
|
service.justifiedCheckpt = test.args.cachedCheckPoint
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: bytesutil.PadTo(test.want.Root, 32)}))
|
2021-02-08 20:00:09 +00:00
|
|
|
genesisState, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(ctx, genesisState, bytesutil.ToBytes32(test.want.Root)))
|
2020-06-17 13:50:10 +00:00
|
|
|
|
|
|
|
if test.args.diffFinalizedCheckPoint {
|
2020-08-27 18:13:32 +00:00
|
|
|
b1 := testutil.NewBeaconBlock()
|
|
|
|
b1.Block.Slot = 1
|
|
|
|
b1.Block.ParentRoot = bytesutil.PadTo([]byte{'a'}, 32)
|
|
|
|
r1, err := b1.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 18:13:32 +00:00
|
|
|
b100 := testutil.NewBeaconBlock()
|
|
|
|
b100.Block.Slot = 100
|
|
|
|
b100.Block.ParentRoot = r1[:]
|
|
|
|
r100, err := b100.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 18:13:32 +00:00
|
|
|
for _, b := range []*ethpb.SignedBeaconBlock{b1, b100} {
|
2020-06-17 13:50:10 +00:00
|
|
|
beaconBlock := testutil.NewBeaconBlock()
|
2020-08-27 18:13:32 +00:00
|
|
|
beaconBlock.Block.Slot = b.Block.Slot
|
|
|
|
beaconBlock.Block.ParentRoot = bytesutil.PadTo(b.Block.ParentRoot, 32)
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(beaconBlock)))
|
2020-06-17 13:50:10 +00:00
|
|
|
}
|
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{Root: []byte{'c'}, Epoch: 1}
|
|
|
|
service.justifiedCheckpt.Root = r100[:]
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, service.finalizedImpliesNewJustified(ctx, beaconState))
|
|
|
|
assert.Equal(t, true, attestationutil.CheckPointIsEqual(test.want, service.justifiedCheckpt), "Did not get wanted check point")
|
2020-06-17 13:50:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 20:14:36 +00:00
|
|
|
|
|
|
|
func TestVerifyBlkDescendant(t *testing.T) {
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-07-13 20:14:36 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
b.Block.Slot = 1
|
2020-08-27 18:13:32 +00:00
|
|
|
r, err := b.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b)))
|
2020-07-13 20:14:36 +00:00
|
|
|
|
|
|
|
b1 := testutil.NewBeaconBlock()
|
|
|
|
b1.Block.Slot = 1
|
|
|
|
b1.Block.Body.Graffiti = bytesutil.PadTo([]byte{'a'}, 32)
|
2020-08-27 18:13:32 +00:00
|
|
|
r1, err := b1.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, beaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b1)))
|
2020-07-13 20:14:36 +00:00
|
|
|
|
|
|
|
type args struct {
|
|
|
|
parentRoot [32]byte
|
|
|
|
finalizedRoot [32]byte
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2020-10-01 14:38:53 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantedErr string
|
2020-07-13 20:14:36 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "could not get finalized block in block service cache",
|
|
|
|
args: args{
|
|
|
|
finalizedRoot: [32]byte{'a'},
|
|
|
|
},
|
2020-10-01 14:38:53 +00:00
|
|
|
wantedErr: "nil finalized block",
|
2020-07-13 20:14:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "could not get finalized block root in DB",
|
|
|
|
args: args{
|
|
|
|
finalizedRoot: r,
|
|
|
|
parentRoot: [32]byte{'a'},
|
|
|
|
},
|
2020-10-01 14:38:53 +00:00
|
|
|
wantedErr: "could not get finalized block root",
|
2020-07-13 20:14:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "is not descendant",
|
|
|
|
args: args{
|
|
|
|
finalizedRoot: r1,
|
|
|
|
parentRoot: r,
|
|
|
|
},
|
2020-10-01 14:38:53 +00:00
|
|
|
wantedErr: "is not a descendent of the current finalized block slot",
|
2020-07-13 20:14:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "is descendant",
|
|
|
|
args: args{
|
|
|
|
finalizedRoot: r,
|
|
|
|
parentRoot: r,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-01 14:38:53 +00:00
|
|
|
for _, tt := range tests {
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, &Config{BeaconDB: beaconDB, StateGen: stategen.New(beaconDB), ForkChoiceStore: protoarray.New(0, 0, [32]byte{})})
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-13 20:14:36 +00:00
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{
|
2020-10-01 14:38:53 +00:00
|
|
|
Root: tt.args.finalizedRoot[:],
|
2020-07-13 20:14:36 +00:00
|
|
|
}
|
2020-10-01 14:38:53 +00:00
|
|
|
err = service.VerifyBlkDescendant(ctx, tt.args.parentRoot)
|
|
|
|
if tt.wantedErr != "" {
|
|
|
|
assert.ErrorContains(t, tt.wantedErr, err)
|
2020-07-13 20:14:36 +00:00
|
|
|
} else if err != nil {
|
2020-10-01 14:38:53 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-13 20:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 16:20:25 +00:00
|
|
|
|
|
|
|
func TestUpdateJustifiedInitSync(t *testing.T) {
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-07-14 16:20:25 +00:00
|
|
|
ctx := context.Background()
|
2020-12-18 19:12:30 +00:00
|
|
|
cfg := &Config{BeaconDB: beaconDB}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-14 16:20:25 +00:00
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
gBlk := testutil.NewBeaconBlock()
|
|
|
|
gRoot, err := gBlk.Block.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(gBlk)))
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveGenesisBlockRoot(ctx, gRoot))
|
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(ctx, &pb.StateSummary{Root: gRoot[:]}))
|
2020-07-14 16:20:25 +00:00
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(ctx, beaconState, gRoot))
|
2020-07-14 16:20:25 +00:00
|
|
|
service.genesisRoot = gRoot
|
|
|
|
currentCp := ðpb.Checkpoint{Epoch: 1}
|
|
|
|
service.justifiedCheckpt = currentCp
|
|
|
|
newCp := ðpb.Checkpoint{Epoch: 2, Root: gRoot[:]}
|
|
|
|
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, service.updateJustifiedInitSync(ctx, newCp))
|
2020-07-14 16:20:25 +00:00
|
|
|
|
2021-02-09 20:57:22 +00:00
|
|
|
assert.DeepSSZEqual(t, currentCp, service.prevJustifiedCheckpt, "Incorrect previous justified checkpoint")
|
|
|
|
assert.DeepSSZEqual(t, newCp, service.CurrentJustifiedCheckpt(), "Incorrect current justified checkpoint in cache")
|
2021-03-17 18:36:56 +00:00
|
|
|
cp, err := service.cfg.BeaconDB.JustifiedCheckpoint(ctx)
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-09 20:57:22 +00:00
|
|
|
assert.DeepSSZEqual(t, newCp, cp, "Incorrect current justified checkpoint in db")
|
2020-07-14 16:20:25 +00:00
|
|
|
}
|
2020-10-20 19:14:24 +00:00
|
|
|
|
|
|
|
func TestHandleEpochBoundary_BadMetrics(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
cfg := &Config{}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-10-20 19:14:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-08 20:00:09 +00:00
|
|
|
s, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-10-20 19:14:24 +00:00
|
|
|
require.NoError(t, s.SetSlot(1))
|
2021-06-30 15:06:19 +00:00
|
|
|
service.head = &head{state: (*v1.BeaconState)(nil)}
|
2021-03-08 22:37:33 +00:00
|
|
|
|
2020-10-20 19:14:24 +00:00
|
|
|
require.ErrorContains(t, "failed to initialize precompute: nil inner state", service.handleEpochBoundary(ctx, s))
|
|
|
|
}
|
2020-12-15 21:19:02 +00:00
|
|
|
|
|
|
|
func TestHandleEpochBoundary_UpdateFirstSlot(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
cfg := &Config{}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2020-12-15 21:19:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s, _ := testutil.DeterministicGenesisState(t, 1024)
|
|
|
|
service.head = &head{state: s}
|
|
|
|
require.NoError(t, s.SetSlot(2*params.BeaconConfig().SlotsPerEpoch))
|
|
|
|
require.NoError(t, service.handleEpochBoundary(ctx, s))
|
|
|
|
require.Equal(t, 3*params.BeaconConfig().SlotsPerEpoch, service.nextEpochBoundarySlot)
|
|
|
|
}
|
2021-01-15 13:42:49 +00:00
|
|
|
|
|
|
|
func TestOnBlock_CanFinalize(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
beaconDB := testDB.SetupDB(t)
|
|
|
|
depositCache, err := depositcache.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
cfg := &Config{
|
|
|
|
BeaconDB: beaconDB,
|
|
|
|
StateGen: stategen.New(beaconDB),
|
|
|
|
ForkChoiceStore: protoarray.New(0, 0, [32]byte{}),
|
|
|
|
DepositCache: depositCache,
|
2021-06-10 21:13:15 +00:00
|
|
|
StateNotifier: &mock.MockStateNotifier{},
|
2021-01-15 13:42:49 +00:00
|
|
|
}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2021-01-15 13:42:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gs, keys := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
require.NoError(t, service.saveGenesisData(ctx, gs))
|
2021-03-17 18:36:56 +00:00
|
|
|
gBlk, err := service.cfg.BeaconDB.GenesisBlock(ctx)
|
2021-01-15 13:42:49 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-26 16:19:54 +00:00
|
|
|
gRoot, err := gBlk.Block().HashTreeRoot()
|
2021-01-15 13:42:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{Root: gRoot[:]}
|
|
|
|
|
|
|
|
testState := gs.Copy()
|
2021-02-16 07:45:34 +00:00
|
|
|
for i := types.Slot(1); i <= 4*params.BeaconConfig().SlotsPerEpoch; i++ {
|
2021-01-15 13:42:49 +00:00
|
|
|
blk, err := testutil.GenerateFullBlock(testState, keys, testutil.DefaultBlockGenConfig(), i)
|
|
|
|
require.NoError(t, err)
|
|
|
|
r, err := blk.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.onBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(blk), r))
|
2021-03-17 18:36:56 +00:00
|
|
|
testState, err = service.cfg.StateGen.StateByRoot(ctx, r)
|
2021-01-15 13:42:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2021-02-09 10:05:22 +00:00
|
|
|
require.Equal(t, types.Epoch(3), service.CurrentJustifiedCheckpt().Epoch)
|
|
|
|
require.Equal(t, types.Epoch(2), service.FinalizedCheckpt().Epoch)
|
2021-01-15 13:42:49 +00:00
|
|
|
}
|
2021-02-10 16:35:44 +00:00
|
|
|
|
|
|
|
func TestInsertFinalizedDeposits(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
beaconDB := testDB.SetupDB(t)
|
|
|
|
depositCache, err := depositcache.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
cfg := &Config{
|
|
|
|
BeaconDB: beaconDB,
|
|
|
|
StateGen: stategen.New(beaconDB),
|
|
|
|
ForkChoiceStore: protoarray.New(0, 0, [32]byte{}),
|
|
|
|
DepositCache: depositCache,
|
|
|
|
}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2021-02-10 16:35:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gs, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
require.NoError(t, service.saveGenesisData(ctx, gs))
|
2021-03-17 18:36:56 +00:00
|
|
|
gBlk, err := service.cfg.BeaconDB.GenesisBlock(ctx)
|
2021-02-10 16:35:44 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-26 16:19:54 +00:00
|
|
|
gRoot, err := gBlk.Block().HashTreeRoot()
|
2021-02-10 16:35:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{Root: gRoot[:]}
|
|
|
|
gs = gs.Copy()
|
|
|
|
assert.NoError(t, gs.SetEth1Data(ðpb.Eth1Data{DepositCount: 10}))
|
2021-03-17 18:36:56 +00:00
|
|
|
assert.NoError(t, service.cfg.StateGen.SaveState(ctx, [32]byte{'m', 'o', 'c', 'k'}, gs))
|
2021-02-10 16:35:44 +00:00
|
|
|
zeroSig := [96]byte{}
|
2021-02-16 07:45:34 +00:00
|
|
|
for i := uint64(0); i < uint64(4*params.BeaconConfig().SlotsPerEpoch); i++ {
|
2021-02-10 16:35:44 +00:00
|
|
|
root := []byte(strconv.Itoa(int(i)))
|
2021-04-29 12:24:13 +00:00
|
|
|
assert.NoError(t, depositCache.InsertDeposit(ctx, ðpb.Deposit{Data: ðpb.Deposit_Data{
|
2021-02-10 16:35:44 +00:00
|
|
|
PublicKey: bytesutil.FromBytes48([48]byte{}),
|
|
|
|
WithdrawalCredentials: params.BeaconConfig().ZeroHash[:],
|
|
|
|
Amount: 0,
|
|
|
|
Signature: zeroSig[:],
|
2021-04-29 12:24:13 +00:00
|
|
|
}, Proof: [][]byte{root}}, 100+i, int64(i), bytesutil.ToBytes32(root)))
|
2021-02-10 16:35:44 +00:00
|
|
|
}
|
|
|
|
assert.NoError(t, service.insertFinalizedDeposits(ctx, [32]byte{'m', 'o', 'c', 'k'}))
|
|
|
|
fDeposits := depositCache.FinalizedDeposits(ctx)
|
|
|
|
assert.Equal(t, 9, int(fDeposits.MerkleTrieIndex), "Finalized deposits not inserted correctly")
|
|
|
|
deps := depositCache.AllDeposits(ctx, big.NewInt(109))
|
|
|
|
for _, d := range deps {
|
|
|
|
assert.DeepEqual(t, [][]byte(nil), d.Proof, "Proofs are not empty")
|
|
|
|
}
|
|
|
|
}
|