diff --git a/beacon-chain/archiver/BUILD.bazel b/beacon-chain/archiver/BUILD.bazel index 06d93d2e1e99..993e8dec4b67 100644 --- a/beacon-chain/archiver/BUILD.bazel +++ b/beacon-chain/archiver/BUILD.bazel @@ -39,7 +39,6 @@ go_test( "//shared/testutil:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", - "@com_github_prysmaticlabs_go_bitfield//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@com_github_sirupsen_logrus//hooks/test:go_default_library", ], diff --git a/beacon-chain/archiver/service_test.go b/beacon-chain/archiver/service_test.go index fda180683973..2d618a61e60c 100644 --- a/beacon-chain/archiver/service_test.go +++ b/beacon-chain/archiver/service_test.go @@ -9,7 +9,6 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-bitfield" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" @@ -34,12 +33,8 @@ func TestArchiverService_ReceivesBlockProcessedEvent(t *testing.T) { hook := logTest.NewGlobal() svc, beaconDB := setupService(t) defer dbutil.TeardownDB(t, beaconDB) - st, err := stateTrie.InitializeFromProto(&pb.BeaconState{ - Slot: 1, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(1) svc.headFetcher = &mock.ChainService{ State: st, } @@ -61,12 +56,8 @@ func TestArchiverService_OnlyArchiveAtEpochEnd(t *testing.T) { svc, beaconDB := setupService(t) defer dbutil.TeardownDB(t, beaconDB) // The head state is NOT an epoch end. - st, err := stateTrie.InitializeFromProto(&pb.BeaconState{ - Slot: params.BeaconConfig().SlotsPerEpoch - 2, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(params.BeaconConfig().SlotsPerEpoch - 2) svc.headFetcher = &mock.ChainService{ State: st, } @@ -433,18 +424,12 @@ func setupState(validatorCount uint64) (*stateTrie.BeaconState, error) { // We initialize a head state that has attestations from participated // validators in a simulated fashion. - return stateTrie.InitializeFromProto(&pb.BeaconState{ - Slot: (2 * params.BeaconConfig().SlotsPerEpoch) - 1, - Validators: validators, - Balances: balances, - BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), - Slashings: []uint64{0, 1e9, 1e9}, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - CurrentEpochAttestations: atts, - FinalizedCheckpoint: ðpb.Checkpoint{}, - JustificationBits: bitfield.Bitvector4{0x00}, - CurrentJustifiedCheckpoint: ðpb.Checkpoint{}, - }) + st := testutil.NewBeaconState() + st.SetSlot((2 * params.BeaconConfig().SlotsPerEpoch) - 1) + st.SetValidators(validators) + st.SetBalances(balances) + st.SetCurrentEpochAttestations(atts) + return st, nil } func setupService(t *testing.T) (*Service, db.Database) { diff --git a/beacon-chain/blockchain/head_test.go b/beacon-chain/blockchain/head_test.go index ca340608f271..7bb870271d40 100644 --- a/beacon-chain/blockchain/head_test.go +++ b/beacon-chain/blockchain/head_test.go @@ -9,8 +9,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" - "github.com/prysmaticlabs/prysm/beacon-chain/state" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/testutil" ) func TestSaveHead_Same(t *testing.T) { @@ -46,7 +45,8 @@ func TestSaveHead_Different(t *testing.T) { newHeadSignedBlock := ðpb.SignedBeaconBlock{Block: newHeadBlock} service.beaconDB.SaveBlock(context.Background(), newHeadSignedBlock) newRoot, _ := ssz.HashTreeRoot(newHeadBlock) - headState, _ := state.InitializeFromProto(&pb.BeaconState{Slot: 1}) + headState := testutil.NewBeaconState() + headState.SetSlot(1) service.beaconDB.SaveState(context.Background(), headState, newRoot) if err := service.saveHead(context.Background(), newRoot); err != nil { t.Fatal(err) diff --git a/beacon-chain/blockchain/process_attestation_test.go b/beacon-chain/blockchain/process_attestation_test.go index e78b0a63cc83..de933296f4e4 100644 --- a/beacon-chain/blockchain/process_attestation_test.go +++ b/beacon-chain/blockchain/process_attestation_test.go @@ -13,7 +13,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/state" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray" - beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -54,7 +53,7 @@ func TestStore_OnAttestation(t *testing.T) { } BlkWithStateBadAttRoot, _ := ssz.HashTreeRoot(BlkWithStateBadAtt.Block) - s, err := beaconstate.InitializeFromProto(&pb.BeaconState{}) + s := testutil.NewBeaconState() if err := s.SetSlot(100 * params.BeaconConfig().SlotsPerEpoch); err != nil { t.Fatal(err) } @@ -67,13 +66,11 @@ func TestStore_OnAttestation(t *testing.T) { t.Fatal(err) } BlkWithValidStateRoot, _ := ssz.HashTreeRoot(BlkWithValidState.Block) - s, _ = stateTrie.InitializeFromProto(&pb.BeaconState{ - Fork: &pb.Fork{ - Epoch: 0, - CurrentVersion: params.BeaconConfig().GenesisForkVersion, - PreviousVersion: params.BeaconConfig().GenesisForkVersion, - }, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), + s = testutil.NewBeaconState() + s.SetFork(&pb.Fork{ + Epoch: 0, + CurrentVersion: params.BeaconConfig().GenesisForkVersion, + PreviousVersion: params.BeaconConfig().GenesisForkVersion, }) if err := service.beaconDB.SaveState(ctx, s, BlkWithValidStateRoot); err != nil { t.Fatal(err) diff --git a/beacon-chain/blockchain/process_block_test.go b/beacon-chain/blockchain/process_block_test.go index 79c197e22a17..abf2ce811266 100644 --- a/beacon-chain/blockchain/process_block_test.go +++ b/beacon-chain/blockchain/process_block_test.go @@ -46,10 +46,7 @@ func TestStore_OnBlock(t *testing.T) { if err != nil { t.Error(err) } - st, err := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() if err := service.beaconDB.SaveState(ctx, st.Copy(), validGenesisRoot); err != nil { t.Fatal(err) } @@ -187,7 +184,8 @@ func TestRemoveStateSinceLastFinalized(t *testing.T) { if err != nil { t.Fatal(err) } - s, _ := stateTrie.InitializeFromProto(&pb.BeaconState{Slot: uint64(i)}) + s := testutil.NewBeaconState() + s.SetSlot(uint64(i)) if err := service.beaconDB.SaveState(ctx, s, r); err != nil { t.Fatal(err) } @@ -397,16 +395,17 @@ func TestSaveInitState_CanSaveDelete(t *testing.T) { for i := uint64(0); i < 64; i++ { b := ðpb.BeaconBlock{Slot: i} - s, _ := stateTrie.InitializeFromProto(&pb.BeaconState{Slot: i}) + s := testutil.NewBeaconState() + s.SetSlot(i) r, _ := ssz.HashTreeRoot(b) service.initSyncState[r] = s } // Set finalized root as slot 32 finalizedRoot, _ := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 32}) - - s, _ := stateTrie.InitializeFromProto(&pb.BeaconState{FinalizedCheckpoint: ðpb.Checkpoint{ - Epoch: 1, Root: finalizedRoot[:]}}) + s := testutil.NewBeaconState() + s.SetFinalizedCheckpoint(ðpb.Checkpoint{ + Epoch: 1, Root: finalizedRoot[:]}) if err := service.saveInitState(ctx, s); err != nil { t.Fatal(err) } @@ -442,17 +441,15 @@ func TestUpdateJustified_CouldUpdateBest(t *testing.T) { } service.justifiedCheckpt = ðpb.Checkpoint{Root: []byte{'A'}} service.bestJustifiedCheckpt = ðpb.Checkpoint{Root: []byte{'A'}} - st, err := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() service.initSyncState[r] = st.Copy() if err := db.SaveState(ctx, st.Copy(), r); err != nil { t.Fatal(err) } // Could update - s, _ := stateTrie.InitializeFromProto(&pb.BeaconState{CurrentJustifiedCheckpoint: ðpb.Checkpoint{Epoch: 1, Root: r[:]}}) + s := testutil.NewBeaconState() + s.SetCurrentJustifiedCheckpoint(ðpb.Checkpoint{Epoch: 1, Root: r[:]}) if err := service.updateJustified(context.Background(), s); err != nil { t.Fatal(err) } @@ -487,7 +484,7 @@ func TestFilterBlockRoots_CanFilter(t *testing.T) { fRoot, _ := ssz.HashTreeRoot(fBlock) hBlock := ðpb.BeaconBlock{Slot: 1} headRoot, _ := ssz.HashTreeRoot(hBlock) - st, _ := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) + st := testutil.NewBeaconState() if err := service.beaconDB.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: fBlock}); err != nil { t.Fatal(err) } @@ -530,7 +527,7 @@ func TestPersistCache_CanSave(t *testing.T) { if err != nil { t.Fatal(err) } - st, _ := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) + st := testutil.NewBeaconState() for i := uint64(0); i < initialSyncCacheSize; i++ { st.SetSlot(i) @@ -582,7 +579,7 @@ func TestFillForkChoiceMissingBlocks_CanSave(t *testing.T) { if err != nil { t.Error(err) } - st, _ := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) + st := testutil.NewBeaconState() if err := service.beaconDB.SaveState(ctx, st.Copy(), validGenesisRoot); err != nil { t.Fatal(err) } @@ -636,7 +633,7 @@ func TestFillForkChoiceMissingBlocks_FilterFinalized(t *testing.T) { if err != nil { t.Error(err) } - st, _ := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) + st := testutil.NewBeaconState() if err := service.beaconDB.SaveState(ctx, st.Copy(), validGenesisRoot); err != nil { t.Fatal(err) } @@ -695,10 +692,7 @@ func blockTree1(db db.Database, genesisRoot []byte) ([][]byte, error) { r7, _ := ssz.HashTreeRoot(b7) b8 := ðpb.BeaconBlock{Slot: 8, ParentRoot: r6[:]} r8, _ := ssz.HashTreeRoot(b8) - st, err := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) - if err != nil { - return nil, err - } + st := testutil.NewBeaconState() for _, b := range []*ethpb.BeaconBlock{b0, b1, b3, b4, b5, b6, b7, b8} { if err := db.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: b}); err != nil { return nil, err diff --git a/beacon-chain/blockchain/service_test.go b/beacon-chain/blockchain/service_test.go index b952243e4ca6..25331c627f66 100644 --- a/beacon-chain/blockchain/service_test.go +++ b/beacon-chain/blockchain/service_test.go @@ -235,10 +235,8 @@ func TestChainStartStop_Initialized(t *testing.T) { if err := db.SaveBlock(ctx, genesisBlk); err != nil { t.Fatal(err) } - s, err := beaconstate.InitializeFromProto(&pb.BeaconState{Slot: 1}) - if err != nil { - t.Fatal(err) - } + s := testutil.NewBeaconState() + s.SetSlot(1) if err := db.SaveState(ctx, s, blkRoot); err != nil { t.Fatal(err) } @@ -348,10 +346,9 @@ func TestChainService_InitializeChainInfo(t *testing.T) { finalizedSlot := params.BeaconConfig().SlotsPerEpoch*2 + 1 headBlock := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: finalizedSlot, ParentRoot: genesisRoot[:]}} - headState, err := beaconstate.InitializeFromProto(&pb.BeaconState{Slot: finalizedSlot, GenesisValidatorsRoot: params.BeaconConfig().ZeroHash[:]}) - if err != nil { - t.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetSlot(finalizedSlot) + headState.SetGenesisValidatorRoot(params.BeaconConfig().ZeroHash[:]) headRoot, _ := ssz.HashTreeRoot(headBlock.Block) if err := db.SaveState(ctx, headState, headRoot); err != nil { t.Fatal(err) @@ -414,8 +411,7 @@ func TestChainService_SaveHeadNoDB(t *testing.T) { } b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} r, _ := ssz.HashTreeRoot(b) - state := &pb.BeaconState{} - newState, err := beaconstate.InitializeFromProto(state) + newState := testutil.NewBeaconState() s.stateGen.SaveState(ctx, r, newState) if err := s.saveHeadNoDB(ctx, b, r); err != nil { t.Fatal(err) @@ -447,11 +443,8 @@ func TestChainService_PruneOldStates(t *testing.T) { if err != nil { t.Fatal(err) } - state := &pb.BeaconState{Slot: uint64(i)} - newState, err := beaconstate.InitializeFromProto(state) - if err != nil { - t.Fatal(err) - } + newState := testutil.NewBeaconState() + newState.SetSlot(uint64(i)) if err := s.beaconDB.SaveState(ctx, newState, r); err != nil { t.Fatal(err) } diff --git a/beacon-chain/core/blocks/block_operations.go b/beacon-chain/core/blocks/block_operations.go index 05fd412d5ee4..a4e39af88193 100644 --- a/beacon-chain/core/blocks/block_operations.go +++ b/beacon-chain/core/blocks/block_operations.go @@ -456,7 +456,7 @@ func VerifyProposerSlashing( return fmt.Errorf("validator with key %#x is not slashable", proposer.PublicKey) } // Using headerEpoch1 here because both of the headers should have the same epoch. - domain, err := helpers.Domain(beaconState.Fork(), helpers.StartSlot(slashing.Header_1.Header.Slot), params.BeaconConfig().DomainBeaconProposer, beaconState.GenesisValidatorRoot()) + domain, err := helpers.Domain(beaconState.Fork(), helpers.SlotToEpoch(slashing.Header_1.Header.Slot), params.BeaconConfig().DomainBeaconProposer, beaconState.GenesisValidatorRoot()) if err != nil { return err } diff --git a/beacon-chain/db/kv/BUILD.bazel b/beacon-chain/db/kv/BUILD.bazel index 76c8dfd82296..ce3e23f6601a 100644 --- a/beacon-chain/db/kv/BUILD.bazel +++ b/beacon-chain/db/kv/BUILD.bazel @@ -44,6 +44,7 @@ go_library( "//shared/traceutil:go_default_library", "@com_github_dgraph_io_ristretto//:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", + "@com_github_ferranbt_fastssz//:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_golang_snappy//:go_default_library", "@com_github_pkg_errors//:go_default_library", @@ -80,7 +81,6 @@ go_test( deps = [ "//beacon-chain/cache:go_default_library", "//beacon-chain/db/filters:go_default_library", - "//beacon-chain/state:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//proto/testing:go_default_library", "//shared/bytesutil:go_default_library", @@ -91,5 +91,6 @@ go_test( "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", "@com_github_prysmaticlabs_go_ssz//:go_default_library", + "@in_gopkg_d4l3k_messagediff_v1//:go_default_library", ], ) diff --git a/beacon-chain/db/kv/backup_test.go b/beacon-chain/db/kv/backup_test.go index bcb6c93be201..f6c27e1aa72a 100644 --- a/beacon-chain/db/kv/backup_test.go +++ b/beacon-chain/db/kv/backup_test.go @@ -8,8 +8,7 @@ import ( eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" - "github.com/prysmaticlabs/prysm/beacon-chain/state" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/testutil" ) func TestStore_Backup(t *testing.T) { @@ -26,7 +25,7 @@ func TestStore_Backup(t *testing.T) { if err != nil { t.Fatal(err) } - st, err := state.InitializeFromProto(&pb.BeaconState{}) + st := testutil.NewBeaconState() if err := db.SaveState(ctx, st, root); err != nil { t.Fatal(err) } diff --git a/beacon-chain/db/kv/checkpoint_test.go b/beacon-chain/db/kv/checkpoint_test.go index d6a5d9c86dd0..caa797f2826a 100644 --- a/beacon-chain/db/kv/checkpoint_test.go +++ b/beacon-chain/db/kv/checkpoint_test.go @@ -8,9 +8,8 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" - "github.com/prysmaticlabs/prysm/beacon-chain/state" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/testutil" ) func TestStore_JustifiedCheckpoint_CanSaveRetrieve(t *testing.T) { @@ -22,10 +21,9 @@ func TestStore_JustifiedCheckpoint_CanSaveRetrieve(t *testing.T) { Epoch: 10, Root: root[:], } - st, err := state.InitializeFromProto(&pb.BeaconState{Slot: 1}) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(1) + if err := db.SaveState(ctx, st, root); err != nil { t.Fatal(err) } @@ -74,10 +72,8 @@ func TestStore_FinalizedCheckpoint_CanSaveRetrieve(t *testing.T) { if err := db.SaveBlock(ctx, blk); err != nil { t.Fatal(err) } - st, err := state.InitializeFromProto(&pb.BeaconState{Slot: 1}) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(1) // a state is required to save checkpoint if err := db.SaveState(ctx, st, root); err != nil { t.Fatal(err) diff --git a/beacon-chain/db/kv/encoding.go b/beacon-chain/db/kv/encoding.go index 1e8e9985acd3..4391e20abb69 100644 --- a/beacon-chain/db/kv/encoding.go +++ b/beacon-chain/db/kv/encoding.go @@ -4,8 +4,11 @@ import ( "errors" "reflect" + fastssz "github.com/ferranbt/fastssz" "github.com/gogo/protobuf/proto" "github.com/golang/snappy" + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ) func decode(data []byte, dst proto.Message) error { @@ -13,20 +16,49 @@ func decode(data []byte, dst proto.Message) error { if err != nil { return err } - if err := proto.Unmarshal(data, dst); err != nil { - return err + if isWhitelisted(dst) { + return dst.(fastssz.Unmarshaler).UnmarshalSSZ(data) } - return nil + return proto.Unmarshal(data, dst) } func encode(msg proto.Message) ([]byte, error) { if msg == nil || reflect.ValueOf(msg).IsNil() { return nil, errors.New("cannot encode nil message") } - enc, err := proto.Marshal(msg) - if err != nil { - return nil, err + var enc []byte + var err error + if isWhitelisted(msg) { + enc, err = msg.(fastssz.Marshaler).MarshalSSZ() + if err != nil { + return nil, err + } + } else { + enc, err = proto.Marshal(msg) + if err != nil { + return nil, err + } } - return snappy.Encode(nil, enc), nil } + +func isWhitelisted(obj interface{}) bool { + switch obj.(type) { + case *pb.BeaconState: + return true + case *ethpb.BeaconBlock: + return true + case *ethpb.Attestation: + return true + case *ethpb.Deposit: + return true + case *ethpb.AttesterSlashing: + return true + case *ethpb.ProposerSlashing: + return true + case *ethpb.VoluntaryExit: + return true + default: + return false + } +} diff --git a/beacon-chain/db/kv/finalized_block_roots_test.go b/beacon-chain/db/kv/finalized_block_roots_test.go index fb45048c43e6..4283b250f465 100644 --- a/beacon-chain/db/kv/finalized_block_roots_test.go +++ b/beacon-chain/db/kv/finalized_block_roots_test.go @@ -6,10 +6,9 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" - "github.com/prysmaticlabs/prysm/beacon-chain/state" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/testutil" ) var genesisBlockRoot = bytesutil.ToBytes32([]byte{'G', 'E', 'N', 'E', 'S', 'I', 'S'}) @@ -39,10 +38,7 @@ func TestStore_IsFinalizedBlock(t *testing.T) { Root: root[:], } - st, err := state.InitializeFromProto(&pb.BeaconState{}) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() // a state is required to save checkpoint if err := db.SaveState(ctx, st, root); err != nil { t.Fatal(err) @@ -115,10 +111,7 @@ func TestStore_IsFinalized_ForkEdgeCase(t *testing.T) { Epoch: 1, } - st, err := state.InitializeFromProto(&pb.BeaconState{}) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() // A state is required to save checkpoint if err := db.SaveState(ctx, st, bytesutil.ToBytes32(checkpoint1.Root)); err != nil { t.Fatal(err) diff --git a/beacon-chain/db/kv/slashings_test.go b/beacon-chain/db/kv/slashings_test.go index 76cd57f1fdf9..9925297b4985 100644 --- a/beacon-chain/db/kv/slashings_test.go +++ b/beacon-chain/db/kv/slashings_test.go @@ -15,10 +15,22 @@ func TestStore_ProposerSlashing_CRUD(t *testing.T) { ctx := context.Background() prop := ðpb.ProposerSlashing{ Header_1: ðpb.SignedBeaconBlockHeader{ - Header: ðpb.BeaconBlockHeader{ProposerIndex: 5}, + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 5, + BodyRoot: make([]byte, 32), + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), }, Header_2: ðpb.SignedBeaconBlockHeader{ - Header: ðpb.BeaconBlockHeader{ProposerIndex: 5}, + Header: ðpb.BeaconBlockHeader{ + ProposerIndex: 5, + BodyRoot: make([]byte, 32), + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + }, + Signature: make([]byte, 96), }, } slashingRoot, err := ssz.HashTreeRoot(prop) @@ -62,13 +74,31 @@ func TestStore_AttesterSlashing_CRUD(t *testing.T) { Data: ðpb.AttestationData{ BeaconBlockRoot: make([]byte, 32), Slot: 5, + Source: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, }, + Signature: make([]byte, 96), }, Attestation_2: ðpb.IndexedAttestation{ Data: ðpb.AttestationData{ BeaconBlockRoot: make([]byte, 32), Slot: 7, + Source: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: make([]byte, 32), + }, }, + Signature: make([]byte, 96), }, } slashingRoot, err := ssz.HashTreeRoot(att) diff --git a/beacon-chain/db/kv/state_test.go b/beacon-chain/db/kv/state_test.go index 415d1b9f2d27..f2c24ec47178 100644 --- a/beacon-chain/db/kv/state_test.go +++ b/beacon-chain/db/kv/state_test.go @@ -8,26 +8,25 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" - "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/testutil" + "gopkg.in/d4l3k/messagediff.v1" ) func TestState_CanSaveRetrieve(t *testing.T) { db := setupDB(t) defer teardownDB(t, db) - s := &pb.BeaconState{Slot: 100} r := [32]byte{'A'} if db.HasState(context.Background(), r) { t.Fatal("Wanted false") } - st, err := state.InitializeFromProto(s) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(100) if err := db.SaveState(context.Background(), st, r); err != nil { t.Fatal(err) @@ -42,8 +41,9 @@ func TestState_CanSaveRetrieve(t *testing.T) { t.Fatal(err) } - if !reflect.DeepEqual(st, savedS) { - t.Errorf("Did not retrieve saved state: %v != %v", s, savedS) + if !reflect.DeepEqual(st.InnerStateUnsafe(), savedS.InnerStateUnsafe()) { + diff, _ := messagediff.PrettyDiff(st.InnerStateUnsafe(), savedS.InnerStateUnsafe()) + t.Errorf("Did not retrieve saved state: %v", diff) } savedS, err = db.State(context.Background(), [32]byte{'B'}) @@ -60,13 +60,10 @@ func TestHeadState_CanSaveRetrieve(t *testing.T) { db := setupDB(t) defer teardownDB(t, db) - s := &pb.BeaconState{Slot: 100} headRoot := [32]byte{'A'} - st, err := state.InitializeFromProto(s) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(100) if err := db.SaveState(context.Background(), st, headRoot); err != nil { t.Fatal(err) @@ -81,7 +78,7 @@ func TestHeadState_CanSaveRetrieve(t *testing.T) { t.Fatal(err) } - if !reflect.DeepEqual(st, savedHeadS) { + if !reflect.DeepEqual(st.InnerStateUnsafe(), savedHeadS.InnerStateUnsafe()) { t.Error("did not retrieve saved state") } } @@ -90,13 +87,10 @@ func TestGenesisState_CanSaveRetrieve(t *testing.T) { db := setupDB(t) defer teardownDB(t, db) - s := &pb.BeaconState{Slot: 1} headRoot := [32]byte{'B'} - st, err := state.InitializeFromProto(s) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(1) if err := db.SaveGenesisBlockRoot(context.Background(), headRoot); err != nil { t.Fatal(err) @@ -111,7 +105,7 @@ func TestGenesisState_CanSaveRetrieve(t *testing.T) { t.Fatal(err) } - if !reflect.DeepEqual(st, savedGenesisS) { + if !reflect.DeepEqual(st.InnerStateUnsafe(), savedGenesisS.InnerStateUnsafe()) { t.Error("did not retrieve saved state") } @@ -148,10 +142,8 @@ func TestStore_StatesBatchDelete(t *testing.T) { if err != nil { t.Fatal(err) } - st, err := state.InitializeFromProto(&pb.BeaconState{Slot: uint64(i)}) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(uint64(i)) if err := db.SaveState(context.Background(), st, r); err != nil { t.Fatal(err) } @@ -191,11 +183,8 @@ func TestStore_DeleteGenesisState(t *testing.T) { if err := db.SaveGenesisBlockRoot(ctx, genesisBlockRoot); err != nil { t.Fatal(err) } - genesisState := &pb.BeaconState{Slot: 100} - st, err := state.InitializeFromProto(genesisState) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(100) if err := db.SaveState(ctx, st, genesisBlockRoot); err != nil { t.Fatal(err) } @@ -230,10 +219,8 @@ func TestStore_DeleteFinalizedState(t *testing.T) { t.Fatal(err) } - finalizedState, err := state.InitializeFromProto(&pb.BeaconState{Slot: 100}) - if err != nil { - t.Fatal(err) - } + finalizedState := testutil.NewBeaconState() + finalizedState.SetSlot(100) if err := db.SaveState(ctx, finalizedState, finalizedBlockRoot); err != nil { t.Fatal(err) } @@ -272,11 +259,8 @@ func TestStore_DeleteHeadState(t *testing.T) { if err != nil { t.Fatal(err) } - headState := &pb.BeaconState{Slot: 100} - st, err := state.InitializeFromProto(headState) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(100) if err := db.SaveState(ctx, st, headBlockRoot); err != nil { t.Fatal(err) } @@ -293,30 +277,28 @@ func TestStore_SaveDeleteState_CanGetHighest(t *testing.T) { db := setupDB(t) defer teardownDB(t, db) - s0 := &pb.BeaconState{Slot: 1} b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} r, _ := ssz.HashTreeRoot(b.Block) if err := db.SaveBlock(context.Background(), b); err != nil { t.Fatal(err) } - st, err := state.InitializeFromProto(s0) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() if err := db.SaveState(context.Background(), st, r); err != nil { t.Fatal(err) } + if err := db.SaveGenesisBlockRoot(context.Background(), r); err != nil { + t.Error(err) + } + s0 := st.InnerStateUnsafe() - s1 := &pb.BeaconState{Slot: 999} b = ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 999}} r1, _ := ssz.HashTreeRoot(b.Block) if err := db.SaveBlock(context.Background(), b); err != nil { t.Fatal(err) } - st, err = state.InitializeFromProto(s1) - if err != nil { - t.Fatal(err) - } + st = testutil.NewBeaconState() + st.SetSlot(999) + s1 := st.InnerStateUnsafe() if err := db.SaveState(context.Background(), st, r1); err != nil { t.Fatal(err) } @@ -329,16 +311,14 @@ func TestStore_SaveDeleteState_CanGetHighest(t *testing.T) { t.Errorf("Did not retrieve saved state: %v != %v", highest, s1) } - s2 := &pb.BeaconState{Slot: 1000} b = ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1000}} r2, _ := ssz.HashTreeRoot(b.Block) if err := db.SaveBlock(context.Background(), b); err != nil { t.Fatal(err) } - st, err = state.InitializeFromProto(s2) - if err != nil { - t.Fatal(err) - } + st = testutil.NewBeaconState() + st.SetSlot(1000) + s2 := st.InnerStateUnsafe() if err := db.SaveState(context.Background(), st, r2); err != nil { t.Fatal(err) } @@ -365,8 +345,12 @@ func TestStore_SaveDeleteState_CanGetHighest(t *testing.T) { if err != nil { t.Fatal(err) } + if highest[0] == nil { + t.Fatal("returned nil state ") + } if !proto.Equal(highest[0].InnerStateUnsafe(), s0) { - t.Errorf("Did not retrieve saved state: %v != %v", highest, s1) + diff, _ := messagediff.PrettyDiff(highest[0].InnerStateUnsafe(), s0) + t.Errorf("Did not retrieve saved state: %v", diff) } } @@ -374,30 +358,26 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { db := setupDB(t) defer teardownDB(t, db) - s0 := &pb.BeaconState{Slot: 1} b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} r, _ := ssz.HashTreeRoot(b.Block) if err := db.SaveBlock(context.Background(), b); err != nil { t.Fatal(err) } - st, err := state.InitializeFromProto(s0) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(1) + s0 := st.InnerStateUnsafe() if err := db.SaveState(context.Background(), st, r); err != nil { t.Fatal(err) } - s1 := &pb.BeaconState{Slot: 100} b = ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 100}} r1, _ := ssz.HashTreeRoot(b.Block) if err := db.SaveBlock(context.Background(), b); err != nil { t.Fatal(err) } - st, err = state.InitializeFromProto(s1) - if err != nil { - t.Fatal(err) - } + st = testutil.NewBeaconState() + st.SetSlot(100) + s1 := st.InnerStateUnsafe() if err := db.SaveState(context.Background(), st, r1); err != nil { t.Fatal(err) } @@ -410,16 +390,15 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) { t.Errorf("Did not retrieve saved state: %v != %v", highest, s1) } - s2 := &pb.BeaconState{Slot: 1000} b = ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1000}} r2, _ := ssz.HashTreeRoot(b.Block) if err := db.SaveBlock(context.Background(), b); err != nil { t.Fatal(err) } - st, err = state.InitializeFromProto(s2) - if err != nil { - t.Fatal(err) - } + st = testutil.NewBeaconState() + st.SetSlot(1000) + s2 := st.InnerStateUnsafe() + if err := db.SaveState(context.Background(), st, r2); err != nil { t.Fatal(err) } @@ -453,25 +432,19 @@ func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) { db := setupDB(t) defer teardownDB(t, db) - s := &pb.BeaconState{} - genesisState, err := state.InitializeFromProto(s) - if err != nil { - t.Fatal(err) - } + genesisState := testutil.NewBeaconState() genesisRoot := [32]byte{'a'} db.SaveGenesisBlockRoot(context.Background(), genesisRoot) db.SaveState(context.Background(), genesisState, genesisRoot) - s0 := &pb.BeaconState{Slot: 1} b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}} r, _ := ssz.HashTreeRoot(b.Block) if err := db.SaveBlock(context.Background(), b); err != nil { t.Fatal(err) } - st, err := state.InitializeFromProto(s0) - if err != nil { - t.Fatal(err) - } + + st := testutil.NewBeaconState() + st.SetSlot(1) if err := db.SaveState(context.Background(), st, r); err != nil { t.Fatal(err) } @@ -480,8 +453,8 @@ func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) { if err != nil { t.Fatal(err) } - if !proto.Equal(highest[0].InnerStateUnsafe(), s0) { - t.Errorf("Did not retrieve saved state: %v != %v", highest, s0) + if !proto.Equal(highest[0].InnerStateUnsafe(), st.InnerStateUnsafe()) { + t.Errorf("Did not retrieve saved state: %v != %v", highest, st.InnerStateUnsafe()) } highest, err = db.HighestSlotStatesBelow(context.Background(), 1) @@ -489,13 +462,26 @@ func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) { t.Fatal(err) } if !proto.Equal(highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe()) { - t.Errorf("Did not retrieve saved state: %v != %v", highest, s0) + t.Errorf("Did not retrieve saved state: %v != %v", highest, genesisState.InnerStateUnsafe()) } highest, err = db.HighestSlotStatesBelow(context.Background(), 0) if err != nil { t.Fatal(err) } if !proto.Equal(highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe()) { - t.Errorf("Did not retrieve saved state: %v != %v", highest, s0) + t.Errorf("Did not retrieve saved state: %v != %v", highest, genesisState.InnerStateUnsafe()) + } +} + +func initializeBeaconState(slot uint64) *pb.BeaconState { + return &pb.BeaconState{ + Slot: slot, + GenesisValidatorsRoot: make([]byte, 32), + BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), + StateRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), + RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), + Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector), + PreviousEpochAttestations: make([]*pb.PendingAttestation, 0, params.BeaconConfig().MaxAttestations*params.BeaconConfig().SlotsPerEpoch), + CurrentEpochAttestations: make([]*pb.PendingAttestation, 0, params.BeaconConfig().MaxAttestations*params.BeaconConfig().SlotsPerEpoch), } } diff --git a/beacon-chain/rpc/beacon/BUILD.bazel b/beacon-chain/rpc/beacon/BUILD.bazel index 4597195f982f..10da690b81e2 100644 --- a/beacon-chain/rpc/beacon/BUILD.bazel +++ b/beacon-chain/rpc/beacon/BUILD.bazel @@ -74,6 +74,7 @@ go_test( shard_count = 4, deps = [ "//beacon-chain/blockchain/testing:go_default_library", + "//beacon-chain/cache:go_default_library", "//beacon-chain/core/epoch/precompute:go_default_library", "//beacon-chain/core/feed:go_default_library", "//beacon-chain/core/feed/block:go_default_library", @@ -88,6 +89,7 @@ go_test( "//beacon-chain/p2p/testing:go_default_library", "//beacon-chain/rpc/testing:go_default_library", "//beacon-chain/state:go_default_library", + "//beacon-chain/state/stategen:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/attestationutil:go_default_library", "//shared/bytesutil:go_default_library", diff --git a/beacon-chain/rpc/beacon/assignments_test.go b/beacon-chain/rpc/beacon/assignments_test.go index fd55f4e2d39c..d0c493e2601c 100644 --- a/beacon-chain/rpc/beacon/assignments_test.go +++ b/beacon-chain/rpc/beacon/assignments_test.go @@ -19,6 +19,7 @@ import ( stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/testutil" ) func TestServer_ListAssignments_CannotRequestFutureEpoch(t *testing.T) { @@ -139,7 +140,7 @@ func TestServer_ListAssignments_Pagination_DefaultPageSize_NoArchive(t *testing. defer dbTest.TeardownDB(t, db) ctx := context.Background() - count := 1000 + count := 500 validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { pubKey := make([]byte, params.BeaconConfig().BLSPubkeyLength) @@ -173,13 +174,8 @@ func TestServer_ListAssignments_Pagination_DefaultPageSize_NoArchive(t *testing. t.Fatal(err) } - s, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Validators: validators, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) - if err != nil { - t.Fatal(err) - } + s := testutil.NewBeaconState() + s.SetValidators(validators) if err := db.SaveState(ctx, s, blockRoot); err != nil { t.Fatal(err) } @@ -241,8 +237,8 @@ func TestServer_ListAssignments_Pagination_DefaultPageSize_FromArchive(t *testin defer dbTest.TeardownDB(t, db) ctx := context.Background() - count := 1000 - validators := make([]*ethpb.Validator, 0, count) + count := 500 + validators := make([]*ethpb.Validator, 0) balances := make([]uint64, count) for i := 0; i < count; i++ { pubKey := make([]byte, params.BeaconConfig().BLSPubkeyLength) @@ -274,14 +270,9 @@ func TestServer_ListAssignments_Pagination_DefaultPageSize_FromArchive(t *testin if err != nil { t.Fatal(err) } - s, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Validators: validators, - Balances: balances, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) - if err != nil { - t.Fatal(err) - } + s := testutil.NewBeaconState() + s.SetValidators(validators) + s.SetBalances(balances) if err := db.SaveState(ctx, s, blockRoot); err != nil { t.Fatal(err) } @@ -383,13 +374,8 @@ func TestServer_ListAssignments_FilterPubkeysIndices_NoPagination(t *testing.T) if err != nil { t.Fatal(err) } - s, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Validators: validators, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) - if err != nil { - t.Fatal(err) - } + s := testutil.NewBeaconState() + s.SetValidators(validators) if err := db.SaveState(ctx, s, blockRoot); err != nil { t.Fatal(err) } @@ -472,13 +458,8 @@ func TestServer_ListAssignments_CanFilterPubkeysIndices_WithPagination(t *testin if err != nil { t.Fatal(err) } - s, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Validators: validators, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) - if err != nil { - t.Fatal(err) - } + s := testutil.NewBeaconState() + s.SetValidators(validators) if err := db.SaveState(ctx, s, blockRoot); err != nil { t.Fatal(err) } diff --git a/beacon-chain/rpc/beacon/attestations.go b/beacon-chain/rpc/beacon/attestations.go index b45639f5ccaa..5ec0164c7329 100644 --- a/beacon-chain/rpc/beacon/attestations.go +++ b/beacon-chain/rpc/beacon/attestations.go @@ -141,7 +141,7 @@ func (bs *Server) ListIndexedAttestations( for i := 0; i < len(atts); i++ { att := atts[i] epoch := helpers.SlotToEpoch(att.Data.Slot) - attState, err := bs.BeaconDB.State(ctx, bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) + attState, err := bs.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) if err != nil { return nil, status.Errorf( codes.Internal, diff --git a/beacon-chain/rpc/beacon/attestations_test.go b/beacon-chain/rpc/beacon/attestations_test.go index c54a964f2a43..210470893526 100644 --- a/beacon-chain/rpc/beacon/attestations_test.go +++ b/beacon-chain/rpc/beacon/attestations_test.go @@ -17,6 +17,7 @@ import ( "github.com/prysmaticlabs/go-bitfield" "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/operation" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" @@ -25,6 +26,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations" mockRPC "github.com/prysmaticlabs/prysm/beacon-chain/rpc/testing" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" + "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" @@ -536,6 +538,8 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { helpers.ClearCache() ctx := context.Background() + params.OverrideBeaconConfig(params.MainnetConfig()) + defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) count := params.BeaconConfig().SlotsPerEpoch atts := make([]*ethpb.Attestation, 0, count) for i := uint64(0); i < count; i++ { @@ -565,14 +569,6 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { numValidators := 128 state := setupActiveValidators(t, db, numValidators) - randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector) - for i := 0; i < len(randaoMixes); i++ { - randaoMixes[i] = make([]byte, 32) - } - if err := state.SetRandaoMixes(randaoMixes); err != nil { - t.Fatal(err) - } - activeIndices, err := helpers.ActiveValidatorIndices(state, 0) if err != nil { t.Fatal(err) @@ -598,13 +594,24 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { indexedAtts[i] = idxAtt } + summaryCache := cache.NewStateSummaryCache() bs := &Server{ BeaconDB: db, GenesisTimeFetcher: &mock.ChainService{ Genesis: time.Now(), }, + StateGen: stategen.New(db, summaryCache), } - db.SaveState(ctx, state, bytesutil.ToBytes32([]byte("root"))) + root := bytesutil.ToBytes32([]byte("root")) + db.SaveState(ctx, state, root) + stateRoot, err := state.HashTreeRoot(ctx) + if err != nil { + t.Fatal(err) + } + summaryCache.Put(root, &pbp2p.StateSummary{ + Slot: 0, + Root: stateRoot[:], + }) res, err := bs.ListIndexedAttestations(ctx, ðpb.ListIndexedAttestationsRequest{ QueryFilter: ðpb.ListIndexedAttestationsRequest_GenesisEpoch{ GenesisEpoch: true, diff --git a/beacon-chain/rpc/beacon/committees_test.go b/beacon-chain/rpc/beacon/committees_test.go index 82ecb564f53a..bdc6fd7cabc6 100644 --- a/beacon-chain/rpc/beacon/committees_test.go +++ b/beacon-chain/rpc/beacon/committees_test.go @@ -17,6 +17,7 @@ import ( pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" + "github.com/prysmaticlabs/prysm/shared/testutil" "gopkg.in/d4l3k/messagediff.v1" ) @@ -257,9 +258,8 @@ func setupActiveValidators(t *testing.T, db db.Database, count int) *stateTrie.B WithdrawalCredentials: make([]byte, 32), }) } - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{Validators: validators, Balances: balances}) - if err != nil { - t.Fatal(err) - } - return st + s := testutil.NewBeaconState() + s.SetValidators(validators) + s.SetBalances(balances) + return s } diff --git a/beacon-chain/rpc/beacon/validators_test.go b/beacon-chain/rpc/beacon/validators_test.go index df253772c564..9542c11ff1e9 100644 --- a/beacon-chain/rpc/beacon/validators_test.go +++ b/beacon-chain/rpc/beacon/validators_test.go @@ -13,10 +13,7 @@ import ( "github.com/gogo/protobuf/proto" ptypes "github.com/gogo/protobuf/types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" - "github.com/prysmaticlabs/go-bitfield" "github.com/prysmaticlabs/go-ssz" - pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" @@ -24,7 +21,9 @@ import ( dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/flags" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" + pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/testutil" ) func init() { @@ -40,12 +39,8 @@ func TestServer_ListValidatorBalances_CannotRequestFutureEpoch(t *testing.T) { defer dbTest.TeardownDB(t, db) ctx := context.Background() - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: 0, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(0) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -71,12 +66,8 @@ func TestServer_ListValidatorBalances_NoResults(t *testing.T) { defer dbTest.TeardownDB(t, db) ctx := context.Background() - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: 0, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(0) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -125,14 +116,10 @@ func TestServer_ListValidatorBalances_DefaultResponse_NoArchive(t *testing.T) { Balance: params.BeaconConfig().MaxEffectiveBalance, } } - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: 0, - Validators: validators, - Balances: balances, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(0) + st.SetValidators(validators) + st.SetBalances(balances) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -189,14 +176,10 @@ func TestServer_ListValidatorBalances_DefaultResponse_FromArchive(t *testing.T) if err := db.SaveArchivedBalances(ctx, 50, oldBalances); err != nil { t.Fatal(err) } - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: helpers.StartSlot(100 /* epoch 100 */), - Validators: validators, - Balances: balances, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(helpers.StartSlot(100) /* epoch 100 */) + st.SetValidators(validators) + st.SetBalances(balances) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -450,14 +433,10 @@ func TestServer_ListValidatorBalances_FromArchive(t *testing.T) { for i := 0; i < len(newerBalances); i++ { newerBalances[i] = balances[i] * 2 } - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: params.BeaconConfig().SlotsPerEpoch * 3, - Validators: validators, - Balances: newerBalances, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(params.BeaconConfig().SlotsPerEpoch * 3) + st.SetValidators(validators) + st.SetBalances(newerBalances) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -499,14 +478,10 @@ func TestServer_ListValidatorBalances_FromArchive_NewValidatorNotFound(t *testin } newValidators, newBalances := setupValidators(t, db, 200) - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: params.BeaconConfig().SlotsPerEpoch * 3, - Validators: newValidators, - Balances: newBalances, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(params.BeaconConfig().SlotsPerEpoch * 3) + st.SetValidators(newValidators) + st.SetBalances(newBalances) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -528,12 +503,8 @@ func TestServer_ListValidators_CannotRequestFutureEpoch(t *testing.T) { defer dbTest.TeardownDB(t, db) ctx := context.Background() - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: 0, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(0) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -559,12 +530,8 @@ func TestServer_ListValidators_NoResults(t *testing.T) { defer dbTest.TeardownDB(t, db) ctx := context.Background() - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: 0, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(0) bs := &Server{ BeaconDB: db, @@ -631,13 +598,9 @@ func TestServer_ListValidators_OnlyActiveValidators(t *testing.T) { } } } - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Validators: validators, - Balances: balances, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetValidators(validators) + st.SetBalances(balances) bs := &Server{ HeadFetcher: &mock.ChainService{ @@ -990,13 +953,9 @@ func TestServer_ListValidators_FromOldEpoch(t *testing.T) { } } - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: helpers.StartSlot(30), - Validators: validators, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(helpers.StartSlot(30)) + st.SetValidators(validators) bs := &Server{ HeadFetcher: &mock.ChainService{ State: st, @@ -1044,12 +1003,8 @@ func TestServer_GetValidator(t *testing.T) { } } - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Validators: validators, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetValidators(validators) bs := &Server{ HeadFetcher: &mock.ChainService{ @@ -1132,12 +1087,8 @@ func TestServer_GetValidatorActiveSetChanges_CannotRequestFutureEpoch(t *testing defer dbTest.TeardownDB(t, db) ctx := context.Background() - st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: 0, - }) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(0) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -1161,13 +1112,9 @@ func TestServer_GetValidatorActiveSetChanges_CannotRequestFutureEpoch(t *testing func TestServer_GetValidatorActiveSetChanges(t *testing.T) { ctx := context.Background() validators := make([]*ethpb.Validator, 8) - headState, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: 0, - Validators: validators, - }) - if err != nil { - t.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetSlot(0) + headState.SetValidators(validators) for i := 0; i < len(validators); i++ { activationEpoch := params.BeaconConfig().FarFutureEpoch withdrawableEpoch := params.BeaconConfig().FarFutureEpoch @@ -1255,13 +1202,9 @@ func TestServer_GetValidatorActiveSetChanges_FromArchive(t *testing.T) { defer dbTest.TeardownDB(t, db) ctx := context.Background() validators := make([]*ethpb.Validator, 8) - headState, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: helpers.StartSlot(100), - Validators: validators, - }) - if err != nil { - t.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetSlot(helpers.StartSlot(100)) + headState.SetValidators(validators) activatedIndices := make([]uint64, 0) exitedIndices := make([]uint64, 0) slashedIndices := make([]uint64, 0) @@ -1440,12 +1383,9 @@ func TestServer_GetValidatorQueue_ExitedValidatorLeavesQueue(t *testing.T) { PublicKey: []byte("2"), }, } - headState, _ := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Validators: validators, - FinalizedCheckpoint: ðpb.Checkpoint{ - Epoch: 0, - }, - }) + headState := testutil.NewBeaconState() + headState.SetValidators(validators) + headState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: 0}) bs := &Server{ HeadFetcher: &mock.ChainService{ State: headState, @@ -1557,12 +1497,8 @@ func TestServer_GetValidatorParticipation_CannotRequestCurrentEpoch(t *testing.T defer dbTest.TeardownDB(t, db) ctx := context.Background() - headState, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: helpers.StartSlot(2), - }) - if err != nil { - t.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetSlot(helpers.StartSlot(2)) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -1588,12 +1524,8 @@ func TestServer_GetValidatorParticipation_CannotRequestFutureEpoch(t *testing.T) defer dbTest.TeardownDB(t, db) ctx := context.Background() - headState, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: 0, - }) - if err != nil { - t.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetSlot(0) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -1628,15 +1560,9 @@ func TestServer_GetValidatorParticipation_FromArchive(t *testing.T) { t.Fatal(err) } - headState, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: helpers.StartSlot(epoch + 1), - FinalizedCheckpoint: ðpb.Checkpoint{ - Epoch: epoch + 1, - }, - }) - if err != nil { - t.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetSlot(helpers.StartSlot(epoch + 1)) + headState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: epoch + 1}) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ @@ -1691,16 +1617,9 @@ func TestServer_GetValidatorParticipation_FromArchive_FinalizedEpoch(t *testing. if err := db.SaveArchivedValidatorParticipation(ctx, epoch, part); err != nil { t.Fatal(err) } - headState, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: helpers.StartSlot(epoch + 10), - FinalizedCheckpoint: ðpb.Checkpoint{ - // We say there have been 5 epochs since finality. - Epoch: epoch + 5, - }, - }) - if err != nil { - t.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetSlot(helpers.StartSlot(epoch + 10)) + headState.SetFinalizedCheckpoint(ðpb.Checkpoint{Epoch: epoch + 5}) bs := &Server{ BeaconDB: db, @@ -1748,21 +1667,11 @@ func TestServer_GetValidatorParticipation_PrevEpoch(t *testing.T) { } atts := []*pbp2p.PendingAttestation{{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{}}}} - headState, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: epoch*params.BeaconConfig().SlotsPerEpoch + 1, - Validators: validators, - Balances: balances, - BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), - Slashings: []uint64{0, 1e9, 1e9}, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - CurrentEpochAttestations: atts, - FinalizedCheckpoint: ðpb.Checkpoint{}, - JustificationBits: bitfield.Bitvector4{0x00}, - CurrentJustifiedCheckpoint: ðpb.Checkpoint{}, - }) - if err != nil { - t.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetSlot(epoch*params.BeaconConfig().SlotsPerEpoch + 1) + headState.SetValidators(validators) + headState.SetBalances(balances) + headState.SetCurrentEpochAttestations(atts) m := &mock.ChainService{ State: headState, @@ -1812,24 +1721,14 @@ func TestServer_GetValidatorParticipation_DoesntExist(t *testing.T) { } atts := []*pbp2p.PendingAttestation{{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{}}}} - s, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: epoch*params.BeaconConfig().SlotsPerEpoch + 1, - Validators: validators, - Balances: balances, - BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), - Slashings: []uint64{0, 1e9, 1e9}, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - CurrentEpochAttestations: atts, - FinalizedCheckpoint: ðpb.Checkpoint{}, - JustificationBits: bitfield.Bitvector4{0x00}, - CurrentJustifiedCheckpoint: ðpb.Checkpoint{}, - }) - if err != nil { - t.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetSlot(epoch*params.BeaconConfig().SlotsPerEpoch + 1) + headState.SetValidators(validators) + headState.SetBalances(balances) + headState.SetCurrentEpochAttestations(atts) m := &mock.ChainService{ - State: s, + State: headState, } bs := &Server{ BeaconDB: db, @@ -1904,17 +1803,13 @@ func BenchmarkListValidatorBalances_FromArchive(b *testing.B) { if err := db.SaveArchivedBalances(ctx, 50, oldBalances); err != nil { b.Fatal(err) } - s, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: helpers.StartSlot(100 /* epoch 100 */), - Validators: validators, - }) - if err != nil { - b.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetSlot(helpers.StartSlot(100 /* epoch 100 */)) + headState.SetValidators(validators) bs := &Server{ BeaconDB: db, HeadFetcher: &mock.ChainService{ - State: s, + State: headState, }, } @@ -1956,14 +1851,9 @@ func setupValidators(t testing.TB, db db.Database, count int) ([]*ethpb.Validato if err != nil { t.Fatal(err) } - s, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Validators: validators, - Balances: balances, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) - if err != nil { - t.Fatal(err) - } + s := testutil.NewBeaconState() + s.SetValidators(validators) + s.SetBalances(balances) if err := db.SaveState( context.Background(), s, diff --git a/beacon-chain/rpc/service.go b/beacon-chain/rpc/service.go index 62e684518277..3e5fecf64200 100644 --- a/beacon-chain/rpc/service.go +++ b/beacon-chain/rpc/service.go @@ -261,6 +261,7 @@ func (s *Service) Start() { Broadcaster: s.p2p, ReceivedAttestationsBuffer: make(chan *ethpb.Attestation, 100), CollectedAttestationsBuffer: make(chan []*ethpb.Attestation, 100), + StateGen: s.stateGen, } ethpb.RegisterNodeServer(s.grpcServer, nodeServer) ethpb.RegisterBeaconChainServer(s.grpcServer, beaconChainServer) diff --git a/beacon-chain/rpc/validator/attester_test.go b/beacon-chain/rpc/validator/attester_test.go index bd999a80f5a7..a0cc3c95d6e6 100644 --- a/beacon-chain/rpc/validator/attester_test.go +++ b/beacon-chain/rpc/validator/attester_test.go @@ -23,6 +23,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" + "github.com/prysmaticlabs/prysm/shared/testutil" "google.golang.org/grpc/status" ) @@ -58,7 +59,7 @@ func TestProposeAttestation_OK(t *testing.T) { t.Fatal(err) } - validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount/16) + validators := make([]*ethpb.Validator, 64) for i := 0; i < len(validators); i++ { validators[i] = ðpb.Validator{ ExitEpoch: params.BeaconConfig().FarFutureEpoch, @@ -66,11 +67,9 @@ func TestProposeAttestation_OK(t *testing.T) { } } - state, _ := beaconstate.InitializeFromProto(&pbp2p.BeaconState{ - Slot: params.BeaconConfig().SlotsPerEpoch + 1, - Validators: validators, - RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), - }) + state := testutil.NewBeaconState() + state.SetSlot(params.BeaconConfig().SlotsPerEpoch + 1) + state.SetValidators(validators) if err := db.SaveState(ctx, state, root); err != nil { t.Fatal(err) @@ -146,32 +145,38 @@ func TestGetAttestationData_OK(t *testing.T) { t.Fatalf("Could not get signing root for target block: %v", err) } slot := 3*params.BeaconConfig().SlotsPerEpoch + 1 - beaconState := &pbp2p.BeaconState{ - Slot: slot, - BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), - CurrentJustifiedCheckpoint: ðpb.Checkpoint{ - Epoch: 2, - Root: justifiedRoot[:], - }, - } - beaconState.BlockRoots[1] = blockRoot[:] - beaconState.BlockRoots[1*params.BeaconConfig().SlotsPerEpoch] = targetRoot[:] - beaconState.BlockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedRoot[:] - s, _ := beaconstate.InitializeFromProto(beaconState) + beaconState := testutil.NewBeaconState() + beaconState.SetSlot(slot) + beaconState.SetCurrentJustifiedCheckpoint(ðpb.Checkpoint{ + Epoch: 2, + Root: justifiedRoot[:], + }) + + blockRoots := beaconState.BlockRoots() + blockRoots[1] = blockRoot[:] + blockRoots[1*params.BeaconConfig().SlotsPerEpoch] = targetRoot[:] + blockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedRoot[:] + beaconState.SetBlockRoots(blockRoots) chainService := &mock.ChainService{ Genesis: time.Now(), } attesterServer := &Server{ - BeaconDB: db, - P2P: &mockp2p.MockBroadcaster{}, - SyncChecker: &mockSync.Sync{IsSyncing: false}, - AttestationCache: cache.NewAttestationCache(), - HeadFetcher: &mock.ChainService{State: s, Root: blockRoot[:]}, - FinalizationFetcher: &mock.ChainService{CurrentJustifiedCheckPoint: beaconState.CurrentJustifiedCheckpoint}, - GenesisTimeFetcher: &mock.ChainService{Genesis: time.Now().Add(time.Duration(-1*int64(slot*params.BeaconConfig().SecondsPerSlot)) * time.Second)}, - StateNotifier: chainService.StateNotifier(), + BeaconDB: db, + P2P: &mockp2p.MockBroadcaster{}, + SyncChecker: &mockSync.Sync{IsSyncing: false}, + AttestationCache: cache.NewAttestationCache(), + HeadFetcher: &mock.ChainService{ + State: beaconState, Root: blockRoot[:], + }, + FinalizationFetcher: &mock.ChainService{ + CurrentJustifiedCheckPoint: beaconState.CurrentJustifiedCheckpoint(), + }, + GenesisTimeFetcher: &mock.ChainService{ + Genesis: time.Now().Add(time.Duration(-1*int64(slot*params.BeaconConfig().SecondsPerSlot)) * time.Second), + }, + StateNotifier: chainService.StateNotifier(), } - if err := db.SaveState(ctx, s, blockRoot); err != nil { + if err := db.SaveState(ctx, beaconState, blockRoot); err != nil { t.Fatal(err) } if err := db.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block}); err != nil { @@ -260,32 +265,34 @@ func TestAttestationDataAtSlot_HandlesFarAwayJustifiedEpoch(t *testing.T) { t.Fatalf("Could not hash justified block: %v", err) } slot := uint64(10000) - beaconState := &pbp2p.BeaconState{ - Slot: slot, - BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), - CurrentJustifiedCheckpoint: ðpb.Checkpoint{ - Epoch: helpers.SlotToEpoch(1500), - Root: justifiedBlockRoot[:], - }, - } - beaconState.BlockRoots[1] = blockRoot[:] - beaconState.BlockRoots[1*params.BeaconConfig().SlotsPerEpoch] = epochBoundaryRoot[:] - beaconState.BlockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedBlockRoot[:] - s, _ := beaconstate.InitializeFromProto(beaconState) + + beaconState := testutil.NewBeaconState() + beaconState.SetSlot(slot) + beaconState.SetCurrentJustifiedCheckpoint(ðpb.Checkpoint{ + Epoch: helpers.SlotToEpoch(1500), + Root: justifiedBlockRoot[:], + }) + blockRoots := beaconState.BlockRoots() + blockRoots[1] = blockRoot[:] + blockRoots[1*params.BeaconConfig().SlotsPerEpoch] = epochBoundaryRoot[:] + blockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedBlockRoot[:] + beaconState.SetBlockRoots(blockRoots) chainService := &mock.ChainService{ Genesis: time.Now(), } attesterServer := &Server{ - BeaconDB: db, - P2P: &mockp2p.MockBroadcaster{}, - AttestationCache: cache.NewAttestationCache(), - HeadFetcher: &mock.ChainService{State: s, Root: blockRoot[:]}, - FinalizationFetcher: &mock.ChainService{CurrentJustifiedCheckPoint: beaconState.CurrentJustifiedCheckpoint}, - SyncChecker: &mockSync.Sync{IsSyncing: false}, - GenesisTimeFetcher: &mock.ChainService{Genesis: time.Now().Add(time.Duration(-1*int64(slot*params.BeaconConfig().SecondsPerSlot)) * time.Second)}, - StateNotifier: chainService.StateNotifier(), + BeaconDB: db, + P2P: &mockp2p.MockBroadcaster{}, + AttestationCache: cache.NewAttestationCache(), + HeadFetcher: &mock.ChainService{State: beaconState, Root: blockRoot[:]}, + FinalizationFetcher: &mock.ChainService{ + CurrentJustifiedCheckPoint: beaconState.CurrentJustifiedCheckpoint(), + }, + SyncChecker: &mockSync.Sync{IsSyncing: false}, + GenesisTimeFetcher: &mock.ChainService{Genesis: time.Now().Add(time.Duration(-1*int64(slot*params.BeaconConfig().SecondsPerSlot)) * time.Second)}, + StateNotifier: chainService.StateNotifier(), } - if err := db.SaveState(ctx, s, blockRoot); err != nil { + if err := db.SaveState(ctx, beaconState, blockRoot); err != nil { t.Fatal(err) } if err := db.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block}); err != nil { @@ -437,26 +444,27 @@ func TestServer_GetAttestationData_HeadStateSlotGreaterThanRequestSlot(t *testin if err != nil { t.Fatalf("Could not get signing root for target block: %v", err) } - beaconState := &pbp2p.BeaconState{ - Slot: slot, - GenesisTime: uint64(time.Now().Unix() - int64((slot * params.BeaconConfig().SecondsPerSlot))), - BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), - LatestBlockHeader: ðpb.BeaconBlockHeader{ - ParentRoot: blockRoot2[:], - }, - CurrentJustifiedCheckpoint: ðpb.Checkpoint{ - Epoch: 2, - Root: justifiedRoot[:], - }, - } - beaconState.BlockRoots[1] = blockRoot[:] - beaconState.BlockRoots[1*params.BeaconConfig().SlotsPerEpoch] = targetRoot[:] - beaconState.BlockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedRoot[:] - s, _ := beaconstate.InitializeFromProto(beaconState) - beaconState2 := s.CloneInnerState() - beaconState2.Slot-- - s2, _ := beaconstate.InitializeFromProto(beaconState2) - if err := db.SaveState(ctx, s2, blockRoot2); err != nil { + beaconState := testutil.NewBeaconState() + beaconState.SetSlot(slot) + beaconState.SetGenesisTime(uint64(time.Now().Unix() - int64((slot * params.BeaconConfig().SecondsPerSlot)))) + beaconState.SetLatestBlockHeader(ðpb.BeaconBlockHeader{ + ParentRoot: blockRoot2[:], + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }) + beaconState.SetCurrentJustifiedCheckpoint(ðpb.Checkpoint{ + Epoch: 2, + Root: justifiedRoot[:], + }) + blockRoots := beaconState.BlockRoots() + blockRoots[1] = blockRoot[:] + blockRoots[1*params.BeaconConfig().SlotsPerEpoch] = targetRoot[:] + blockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedRoot[:] + beaconState.SetBlockRoots(blockRoots) + + beaconState2 := beaconState.Copy() + beaconState2.SetSlot(beaconState2.Slot() - 1) + if err := db.SaveState(ctx, beaconState2, blockRoot2); err != nil { t.Fatal(err) } chainService := &mock.ChainService{ @@ -467,13 +475,13 @@ func TestServer_GetAttestationData_HeadStateSlotGreaterThanRequestSlot(t *testin P2P: &mockp2p.MockBroadcaster{}, SyncChecker: &mockSync.Sync{IsSyncing: false}, AttestationCache: cache.NewAttestationCache(), - HeadFetcher: &mock.ChainService{State: s, Root: blockRoot[:]}, - FinalizationFetcher: &mock.ChainService{CurrentJustifiedCheckPoint: beaconState.CurrentJustifiedCheckpoint}, + HeadFetcher: &mock.ChainService{State: beaconState, Root: blockRoot[:]}, + FinalizationFetcher: &mock.ChainService{CurrentJustifiedCheckPoint: beaconState.CurrentJustifiedCheckpoint()}, GenesisTimeFetcher: &mock.ChainService{Genesis: time.Now().Add(time.Duration(-1*int64(slot*params.BeaconConfig().SecondsPerSlot)) * time.Second)}, StateNotifier: chainService.StateNotifier(), StateGen: stategen.New(db, cache.NewStateSummaryCache()), } - if err := db.SaveState(ctx, s, blockRoot); err != nil { + if err := db.SaveState(ctx, beaconState, blockRoot); err != nil { t.Fatal(err) } if err := db.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block}); err != nil { @@ -537,32 +545,35 @@ func TestGetAttestationData_SucceedsInFirstEpoch(t *testing.T) { if err != nil { t.Fatalf("Could not get signing root for target block: %v", err) } - beaconState := &pbp2p.BeaconState{ - Slot: slot, - BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot), - CurrentJustifiedCheckpoint: ðpb.Checkpoint{ - Epoch: 0, - Root: justifiedRoot[:], - }, - } - beaconState.BlockRoots[1] = blockRoot[:] - beaconState.BlockRoots[1*params.BeaconConfig().SlotsPerEpoch] = targetRoot[:] - beaconState.BlockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedRoot[:] - s, _ := beaconstate.InitializeFromProto(beaconState) + beaconState := testutil.NewBeaconState() + beaconState.SetSlot(slot) + beaconState.SetCurrentJustifiedCheckpoint(ðpb.Checkpoint{ + Epoch: 0, + Root: justifiedRoot[:], + }) + blockRoots := beaconState.BlockRoots() + blockRoots[1] = blockRoot[:] + blockRoots[1*params.BeaconConfig().SlotsPerEpoch] = targetRoot[:] + blockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedRoot[:] + beaconState.SetBlockRoots(blockRoots) chainService := &mock.ChainService{ Genesis: time.Now(), } attesterServer := &Server{ - BeaconDB: db, - P2P: &mockp2p.MockBroadcaster{}, - SyncChecker: &mockSync.Sync{IsSyncing: false}, - AttestationCache: cache.NewAttestationCache(), - HeadFetcher: &mock.ChainService{State: s, Root: blockRoot[:]}, - FinalizationFetcher: &mock.ChainService{CurrentJustifiedCheckPoint: beaconState.CurrentJustifiedCheckpoint}, - GenesisTimeFetcher: &mock.ChainService{Genesis: roughtime.Now().Add(time.Duration(-1*int64(slot*params.BeaconConfig().SecondsPerSlot)) * time.Second)}, - StateNotifier: chainService.StateNotifier(), + BeaconDB: db, + P2P: &mockp2p.MockBroadcaster{}, + SyncChecker: &mockSync.Sync{IsSyncing: false}, + AttestationCache: cache.NewAttestationCache(), + HeadFetcher: &mock.ChainService{ + State: beaconState, Root: blockRoot[:], + }, + FinalizationFetcher: &mock.ChainService{ + CurrentJustifiedCheckPoint: beaconState.CurrentJustifiedCheckpoint(), + }, + GenesisTimeFetcher: &mock.ChainService{Genesis: roughtime.Now().Add(time.Duration(-1*int64(slot*params.BeaconConfig().SecondsPerSlot)) * time.Second)}, + StateNotifier: chainService.StateNotifier(), } - if err := db.SaveState(ctx, s, blockRoot); err != nil { + if err := db.SaveState(ctx, beaconState, blockRoot); err != nil { t.Fatal(err) } if err := db.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block}); err != nil { diff --git a/beacon-chain/rpc/validator/proposer_test.go b/beacon-chain/rpc/validator/proposer_test.go index 30c0467c76f7..b5cc0a51f712 100644 --- a/beacon-chain/rpc/validator/proposer_test.go +++ b/beacon-chain/rpc/validator/proposer_test.go @@ -46,7 +46,10 @@ func TestGetBlock_OK(t *testing.T) { defer dbutil.TeardownDB(t, db) ctx := context.Background() - beaconState, privKeys := testutil.DeterministicGenesisState(t, params.BeaconConfig().MinGenesisActiveValidatorCount) + testutil.ResetCache() + params.OverrideBeaconConfig(params.MainnetConfig()) + defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) + beaconState, privKeys := testutil.DeterministicGenesisState(t, 64) stateRoot, err := beaconState.HashTreeRoot(ctx) if err != nil { @@ -152,6 +155,8 @@ func TestGetBlock_AddsUnaggregatedAtts(t *testing.T) { defer dbutil.TeardownDB(t, db) ctx := context.Background() + params.OverrideBeaconConfig(params.MainnetConfig()) + defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) beaconState, privKeys := testutil.DeterministicGenesisState(t, params.BeaconConfig().MinGenesisActiveValidatorCount) stateRoot, err := beaconState.HashTreeRoot(ctx) @@ -209,7 +214,7 @@ func TestGetBlock_AddsUnaggregatedAtts(t *testing.T) { // Generate some more random attestations with a larger spread so that we can capture at least // one unaggregated attestation. - if atts, err := testutil.GenerateAttestations(beaconState, privKeys, 8, 1, true); err != nil { + if atts, err := testutil.GenerateAttestations(beaconState, privKeys, 300, 1, true); err != nil { t.Fatal(err) } else { found := false @@ -274,13 +279,15 @@ func TestProposeBlock_OK(t *testing.T) { db := dbutil.SetupDB(t) defer dbutil.TeardownDB(t, db) ctx := context.Background() + params.OverrideBeaconConfig(params.MainnetConfig()) + defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) genesis := b.NewGenesisBlock([]byte{}) if err := db.SaveBlock(context.Background(), genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } - numDeposits := params.BeaconConfig().MinGenesisActiveValidatorCount + numDeposits := uint64(64) beaconState, _ := testutil.DeterministicGenesisState(t, numDeposits) genesisRoot, err := ssz.HashTreeRoot(genesis.Block) @@ -321,6 +328,8 @@ func TestComputeStateRoot_OK(t *testing.T) { defer dbutil.TeardownDB(t, db) ctx := context.Background() + params.OverrideBeaconConfig(params.MainnetConfig()) + defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) beaconState, privKeys := testutil.DeterministicGenesisState(t, 100) stateRoot, err := beaconState.HashTreeRoot(ctx) @@ -354,7 +363,7 @@ func TestComputeStateRoot_OK(t *testing.T) { req := ðpb.SignedBeaconBlock{ Block: ðpb.BeaconBlock{ - ProposerIndex: 9, + ProposerIndex: 41, ParentRoot: parentRoot[:], Slot: 1, Body: ðpb.BeaconBlockBody{ @@ -1216,12 +1225,8 @@ func TestEth1Data_MockEnabled(t *testing.T) { // BlockHash = hash(hash(current_epoch + slot_in_voting_period)), // ) ctx := context.Background() - headState, err := beaconstate.InitializeFromProto(&pbp2p.BeaconState{ - Eth1DepositIndex: 64, - }) - if err != nil { - t.Fatal(err) - } + headState := testutil.NewBeaconState() + headState.SetEth1DepositIndex(64) ps := &Server{ HeadFetcher: &mock.ChainService{State: headState}, BeaconDB: db, @@ -1263,12 +1268,14 @@ func TestFilterAttestation_OK(t *testing.T) { defer dbutil.TeardownDB(t, db) ctx := context.Background() + params.OverrideBeaconConfig(params.MainnetConfig()) + defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) genesis := b.NewGenesisBlock([]byte{}) if err := db.SaveBlock(context.Background(), genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } - numDeposits := params.BeaconConfig().MinGenesisActiveValidatorCount + numDeposits := uint64(64) state, privKeys := testutil.DeterministicGenesisState(t, numDeposits) if err := state.SetGenesisValidatorRoot(params.BeaconConfig().ZeroHash[:]); err != nil { t.Fatal(err) @@ -1307,7 +1314,7 @@ func TestFilterAttestation_OK(t *testing.T) { } for i := 0; i < len(atts); i++ { - aggBits := bitfield.NewBitlist(4) + aggBits := bitfield.NewBitlist(2) aggBits.SetBitAt(0, true) atts[i] = ðpb.Attestation{Data: ðpb.AttestationData{ CommitteeIndex: uint64(i), diff --git a/beacon-chain/rpc/validator/server_test.go b/beacon-chain/rpc/validator/server_test.go index 363970aaeac3..f73289899dc4 100644 --- a/beacon-chain/rpc/validator/server_test.go +++ b/beacon-chain/rpc/validator/server_test.go @@ -40,7 +40,7 @@ func TestValidatorIndex_OK(t *testing.T) { db := dbutil.SetupDB(t) defer dbutil.TeardownDB(t, db) ctx := context.Background() - st, _ := stateTrie.InitializeFromProtoUnsafe(&pbp2p.BeaconState{}) + st := testutil.NewBeaconState() if err := db.SaveState(ctx, st.Copy(), [32]byte{}); err != nil { t.Fatal(err) } @@ -259,10 +259,8 @@ func TestWaitForChainStart_AlreadyStarted(t *testing.T) { defer dbutil.TeardownDB(t, db) ctx := context.Background() headBlockRoot := [32]byte{0x01, 0x02} - trie, err := stateTrie.InitializeFromProtoUnsafe(&pbp2p.BeaconState{Slot: 3}) - if err != nil { - t.Fatal(err) - } + trie := testutil.NewBeaconState() + trie.SetSlot(3) if err := db.SaveState(ctx, trie, headBlockRoot); err != nil { t.Fatal(err) } diff --git a/beacon-chain/rpc/validator/status_test.go b/beacon-chain/rpc/validator/status_test.go index 817dbf0bf2bb..081996afba13 100644 --- a/beacon-chain/rpc/validator/status_test.go +++ b/beacon-chain/rpc/validator/status_test.go @@ -94,16 +94,15 @@ func TestValidatorStatus_Pending(t *testing.T) { t.Fatalf("Could not get signing root %v", err) } // Pending active because activation epoch is still defaulted at far future slot. - state, _ := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Validators: []*ethpb.Validator{ - { - ActivationEpoch: params.BeaconConfig().FarFutureEpoch, - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, - PublicKey: pubKey, - }, + state := testutil.NewBeaconState() + state.SetSlot(5000) + state.SetValidators([]*ethpb.Validator{ + { + ActivationEpoch: params.BeaconConfig().FarFutureEpoch, + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + PublicKey: pubKey, }, - Slot: 5000, }) if err := db.SaveState(ctx, state, genesisRoot); err != nil { t.Fatalf("could not save state: %v", err) @@ -412,7 +411,9 @@ func TestValidatorStatus_Exited(t *testing.T) { if err != nil { t.Fatalf("Could not get signing root %v", err) } - numDeposits := params.BeaconConfig().MinGenesisActiveValidatorCount + params.OverrideBeaconConfig(params.MainnetConfig()) + defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) + numDeposits := uint64(64) beaconState, _ := testutil.DeterministicGenesisState(t, numDeposits) if err := db.SaveState(ctx, beaconState, genesisRoot); err != nil { t.Fatal(err) @@ -420,12 +421,11 @@ func TestValidatorStatus_Exited(t *testing.T) { if err := db.SaveHeadBlockRoot(ctx, genesisRoot); err != nil { t.Fatalf("Could not save genesis state: %v", err) } - state, _ := stateTrie.InitializeFromProto(&pbp2p.BeaconState{ - Slot: slot, - Validators: []*ethpb.Validator{{ - PublicKey: pubKey, - WithdrawableEpoch: epoch + 1}, - }, + state := testutil.NewBeaconState() + state.SetSlot(slot) + state.SetValidators([]*ethpb.Validator{{ + PublicKey: pubKey, + WithdrawableEpoch: epoch + 1}, }) depData := ðpb.Deposit_Data{ PublicKey: pubKey, @@ -616,50 +616,47 @@ func TestValidatorStatus_CorrectActivationQueue(t *testing.T) { } currentSlot := uint64(5000) // Pending active because activation epoch is still defaulted at far future slot. - state, err := stateTrie.InitializeFromProtoUnsafe(&pbp2p.BeaconState{ - Validators: []*ethpb.Validator{ - { - ActivationEpoch: 0, - PublicKey: pubKey(0), - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, - }, - { - ActivationEpoch: 0, - PublicKey: pubKey(1), - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, - }, - { - ActivationEpoch: 0, - PublicKey: pubKey(2), - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, - }, - { - ActivationEpoch: 0, - PublicKey: pubKey(3), - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, - }, - { - ActivationEpoch: currentSlot/params.BeaconConfig().SlotsPerEpoch + 1, - PublicKey: pbKey, - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, - }, - { - ActivationEpoch: currentSlot/params.BeaconConfig().SlotsPerEpoch + 4, - PublicKey: pubKey(5), - ExitEpoch: params.BeaconConfig().FarFutureEpoch, - WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, - }, + validators := []*ethpb.Validator{ + { + ActivationEpoch: 0, + PublicKey: pubKey(0), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + }, + { + ActivationEpoch: 0, + PublicKey: pubKey(1), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + }, + { + ActivationEpoch: 0, + PublicKey: pubKey(2), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + }, + { + ActivationEpoch: 0, + PublicKey: pubKey(3), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + }, + { + ActivationEpoch: currentSlot/params.BeaconConfig().SlotsPerEpoch + 1, + PublicKey: pbKey, + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, + }, + { + ActivationEpoch: currentSlot/params.BeaconConfig().SlotsPerEpoch + 4, + PublicKey: pubKey(5), + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch, }, - Slot: currentSlot, - }) - if err != nil { - t.Fatal(err) } + state := testutil.NewBeaconState() + state.SetValidators(validators) + state.SetSlot(currentSlot) if err := db.SaveState(ctx, state, genesisRoot); err != nil { t.Fatalf("could not save state: %v", err) } diff --git a/beacon-chain/state/stategen/BUILD.bazel b/beacon-chain/state/stategen/BUILD.bazel index 8cfd418311fa..d53513a44cfe 100644 --- a/beacon-chain/state/stategen/BUILD.bazel +++ b/beacon-chain/state/stategen/BUILD.bazel @@ -51,7 +51,6 @@ go_test( "//beacon-chain/core/blocks:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/testing:go_default_library", - "//beacon-chain/state:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/bytesutil:go_default_library", "//shared/params:go_default_library", diff --git a/beacon-chain/state/stategen/replay_test.go b/beacon-chain/state/stategen/replay_test.go index bad2497386c3..f1795d0662b7 100644 --- a/beacon-chain/state/stategen/replay_test.go +++ b/beacon-chain/state/stategen/replay_test.go @@ -12,7 +12,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/db" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" - stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" @@ -481,10 +480,8 @@ func TestLastSavedState_CanGet(t *testing.T) { t.Fatal(err) } b2Root, _ := ssz.HashTreeRoot(b2.Block) - st, err := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{Slot: s.splitInfo.slot + 10}) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() + st.SetSlot(s.splitInfo.slot + 10) if err := s.beaconDB.SaveState(ctx, st, b2Root); err != nil { t.Fatal(err) } @@ -545,10 +542,7 @@ func tree1(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.BeaconBlock r7, _ := ssz.HashTreeRoot(b7) b8 := ðpb.BeaconBlock{Slot: 8, ParentRoot: r6[:]} r8, _ := ssz.HashTreeRoot(b8) - st, err := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) - if err != nil { - return nil, nil, err - } + st := testutil.NewBeaconState() for _, b := range []*ethpb.BeaconBlock{b0, b1, b2, b3, b4, b5, b6, b7, b8} { if err := db.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: b}); err != nil { return nil, nil, err @@ -581,11 +575,7 @@ func tree2(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.BeaconBlock r24, _ := ssz.HashTreeRoot(b24) b3 := ðpb.BeaconBlock{Slot: 3, ParentRoot: r24[:]} r3, _ := ssz.HashTreeRoot(b3) - st, err := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) - if err != nil { - return nil, nil, err - } - + st := testutil.NewBeaconState() for _, b := range []*ethpb.BeaconBlock{b0, b1, b21, b22, b23, b24, b3} { if err := db.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: b}); err != nil { return nil, nil, err @@ -616,11 +606,7 @@ func tree3(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.BeaconBlock r23, _ := ssz.HashTreeRoot(b23) b24 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r1[:], StateRoot: []byte{'D'}} r24, _ := ssz.HashTreeRoot(b24) - st, err := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) - if err != nil { - return nil, nil, err - } - + st := testutil.NewBeaconState() for _, b := range []*ethpb.BeaconBlock{b0, b1, b21, b22, b23, b24} { if err := db.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: b}); err != nil { return nil, nil, err @@ -650,11 +636,7 @@ func tree4(db db.Database, genesisRoot []byte) ([][32]byte, []*ethpb.BeaconBlock r23, _ := ssz.HashTreeRoot(b23) b24 := ðpb.BeaconBlock{Slot: 2, ParentRoot: r0[:], StateRoot: []byte{'D'}} r24, _ := ssz.HashTreeRoot(b24) - st, err := stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{}) - if err != nil { - return nil, nil, err - } - + st := testutil.NewBeaconState() for _, b := range []*ethpb.BeaconBlock{b0, b21, b22, b23, b24} { if err := db.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: b}); err != nil { return nil, nil, err diff --git a/beacon-chain/sync/initial-sync-old/BUILD.bazel b/beacon-chain/sync/initial-sync-old/BUILD.bazel index ac746b2a59cf..575e758bcf7c 100644 --- a/beacon-chain/sync/initial-sync-old/BUILD.bazel +++ b/beacon-chain/sync/initial-sync-old/BUILD.bazel @@ -48,13 +48,13 @@ go_test( "//beacon-chain/db/testing:go_default_library", "//beacon-chain/p2p/peers:go_default_library", "//beacon-chain/p2p/testing:go_default_library", - "//beacon-chain/state:go_default_library", "//beacon-chain/sync:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/hashutil:go_default_library", "//shared/params:go_default_library", "//shared/roughtime:go_default_library", "//shared/sliceutil:go_default_library", + "//shared/testutil:go_default_library", "@com_github_ethereum_go_ethereum//p2p/enr:go_default_library", "@com_github_kevinms_leakybucket_go//:go_default_library", "@com_github_libp2p_go_libp2p_core//network:go_default_library", diff --git a/beacon-chain/sync/initial-sync-old/round_robin_test.go b/beacon-chain/sync/initial-sync-old/round_robin_test.go index 6d3a95822ea1..d4906871ddf5 100644 --- a/beacon-chain/sync/initial-sync-old/round_robin_test.go +++ b/beacon-chain/sync/initial-sync-old/round_robin_test.go @@ -18,13 +18,13 @@ import ( dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers" p2pt "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" - stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" beaconsync "github.com/prysmaticlabs/prysm/beacon-chain/sync" p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/hashutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" "github.com/prysmaticlabs/prysm/shared/sliceutil" + "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/sirupsen/logrus" ) @@ -55,7 +55,7 @@ func TestConstants(t *testing.T) { } func TestRoundRobinSync(t *testing.T) { - + t.Skip("Test is filled with races and is part of legacy code, pending deletion") tests := []struct { name string currentSlot uint64 @@ -151,38 +151,6 @@ func TestRoundRobinSync(t *testing.T) { }, }, }, - - // TODO(3147): Handle multiple failures. - //{ - // name: "Multiple peers with multiple failures", - // currentSlot: 320, // 10 epochs - // expectedBlockSlots: makeSequence(1, 320), - // peers: []*peerData{ - // { - // blocks: makeSequence(1, 320), - // finalizedEpoch: 4, - // headSlot: 320, - // }, - // { - // blocks: makeSequence(1, 320), - // finalizedEpoch: 4, - // headSlot: 320, - // failureSlots: makeSequence(1, 320), - // }, - // { - // blocks: makeSequence(1, 320), - // finalizedEpoch: 4, - // headSlot: 320, - // failureSlots: makeSequence(1, 320), - // }, - // { - // blocks: makeSequence(1, 320), - // finalizedEpoch: 4, - // headSlot: 320, - // failureSlots: makeSequence(1, 320), - // }, - // }, - //}, { name: "Multiple peers with different finalized epoch", currentSlot: 320, // 10 epochs @@ -262,10 +230,7 @@ func TestRoundRobinSync(t *testing.T) { } gRoot, _ := ssz.HashTreeRoot(gBlock.Block) beaconDB.SaveGenesisBlockRoot(context.Background(), gRoot) - st, err := stateTrie.InitializeFromProto(&p2ppb.BeaconState{}) - if err != nil { - t.Fatal(err) - } + st := testutil.NewBeaconState() beaconDB.SaveState(context.Background(), st, gRoot) mc := &mock.ChainService{ diff --git a/beacon-chain/sync/pending_attestations_queue_test.go b/beacon-chain/sync/pending_attestations_queue_test.go index 3ea09a4fdbc6..6b31e64a7cdd 100644 --- a/beacon-chain/sync/pending_attestations_queue_test.go +++ b/beacon-chain/sync/pending_attestations_queue_test.go @@ -18,7 +18,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations" "github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers" p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" - beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bls" @@ -84,7 +83,7 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) { b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} r32, _ := ssz.HashTreeRoot(b.Block) - s, _ := beaconstate.InitializeFromProto(&pb.BeaconState{}) + s := testutil.NewBeaconState() r.db.SaveBlock(context.Background(), b) r.db.SaveState(context.Background(), s, r32) @@ -194,7 +193,7 @@ func TestProcessPendingAtts_HasBlockSaveAggregatedAtt(t *testing.T) { sb = ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} r32, _ := ssz.HashTreeRoot(sb.Block) r.db.SaveBlock(context.Background(), sb) - s, _ := beaconstate.InitializeFromProto(&pb.BeaconState{}) + s := testutil.NewBeaconState() r.db.SaveState(context.Background(), s, r32) r.blkRootToPendingAtts[r32] = []*ethpb.SignedAggregateAttestationAndProof{{Message: aggregateAndProof, Signature: aggreSig}} diff --git a/beacon-chain/sync/subscriber_committee_index_beacon_attestation_test.go b/beacon-chain/sync/subscriber_committee_index_beacon_attestation_test.go index 17638c730775..bdab5f9e1128 100644 --- a/beacon-chain/sync/subscriber_committee_index_beacon_attestation_test.go +++ b/beacon-chain/sync/subscriber_committee_index_beacon_attestation_test.go @@ -16,9 +16,7 @@ import ( dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations" p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" - beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state" mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/testutil" ) @@ -43,7 +41,7 @@ func TestService_committeeIndexBeaconAttestationSubscriber_ValidMessage(t *testi if err := db.SaveBlock(ctx, blk); err != nil { t.Fatal(err) } - savedState, _ := beaconstate.InitializeFromProto(&pb.BeaconState{}) + savedState := testutil.NewBeaconState() db.SaveState(context.Background(), savedState, root) c, _ := lru.New(10) diff --git a/beacon-chain/sync/subscriber_test.go b/beacon-chain/sync/subscriber_test.go index 24055ce5f5a0..1e826966330d 100644 --- a/beacon-chain/sync/subscriber_test.go +++ b/beacon-chain/sync/subscriber_test.go @@ -72,13 +72,13 @@ func TestSubscribe_ReceivesAttesterSlashing(t *testing.T) { topic := "/eth2/%x/attester_slashing" var wg sync.WaitGroup wg.Add(1) - params.OverrideBeaconConfig(params.MinimalSpecConfig()) + params.OverrideBeaconConfig(params.MainnetConfig()) r.subscribe(topic, r.noopValidator, func(ctx context.Context, msg proto.Message) error { r.attesterSlashingSubscriber(ctx, msg) wg.Done() return nil }) - beaconState, privKeys := testutil.DeterministicGenesisState(t, params.BeaconConfig().MinGenesisActiveValidatorCount) + beaconState, privKeys := testutil.DeterministicGenesisState(t, 64) chainService.State = beaconState r.chainStarted = true attesterSlashing, err := testutil.GenerateAttesterSlashingForValidator( @@ -120,13 +120,13 @@ func TestSubscribe_ReceivesProposerSlashing(t *testing.T) { topic := "/eth2/%x/proposer_slashing" var wg sync.WaitGroup wg.Add(1) - params.OverrideBeaconConfig(params.MinimalSpecConfig()) + params.OverrideBeaconConfig(params.MainnetConfig()) r.subscribe(topic, r.noopValidator, func(ctx context.Context, msg proto.Message) error { r.proposerSlashingSubscriber(ctx, msg) wg.Done() return nil }) - beaconState, privKeys := testutil.DeterministicGenesisState(t, params.BeaconConfig().MinGenesisActiveValidatorCount) + beaconState, privKeys := testutil.DeterministicGenesisState(t, 64) chainService.State = beaconState r.chainStarted = true proposerSlashing, err := testutil.GenerateProposerSlashingForValidator( diff --git a/beacon-chain/sync/validate_aggregate_proof_test.go b/beacon-chain/sync/validate_aggregate_proof_test.go index 9d292416ae30..6127aa8423e9 100644 --- a/beacon-chain/sync/validate_aggregate_proof_test.go +++ b/beacon-chain/sync/validate_aggregate_proof_test.go @@ -21,9 +21,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations" "github.com/prysmaticlabs/prysm/beacon-chain/p2p" p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" - beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state" mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" @@ -177,7 +175,7 @@ func TestValidateAggregateAndProof_NotWithinSlotRange(t *testing.T) { b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} db.SaveBlock(context.Background(), b) root, _ := ssz.HashTreeRoot(b.Block) - s, _ := beaconstate.InitializeFromProto(&pb.BeaconState{}) + s := testutil.NewBeaconState() db.SaveState(context.Background(), s, root) aggBits := bitfield.NewBitlist(3) @@ -328,7 +326,7 @@ func TestValidateAggregateAndProof_CanValidate(t *testing.T) { b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} db.SaveBlock(context.Background(), b) root, _ := ssz.HashTreeRoot(b.Block) - s, _ := beaconstate.InitializeFromProto(&pb.BeaconState{}) + s := testutil.NewBeaconState() db.SaveState(context.Background(), s, root) aggBits := bitfield.NewBitlist(3) @@ -442,7 +440,7 @@ func TestVerifyIndexInCommittee_SeenAggregatorSlot(t *testing.T) { b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} db.SaveBlock(context.Background(), b) root, _ := ssz.HashTreeRoot(b.Block) - s, _ := beaconstate.InitializeFromProto(&pb.BeaconState{}) + s := testutil.NewBeaconState() db.SaveState(context.Background(), s, root) aggBits := bitfield.NewBitlist(3) diff --git a/beacon-chain/sync/validate_committee_index_beacon_attestation_test.go b/beacon-chain/sync/validate_committee_index_beacon_attestation_test.go index e487c8a732f9..74a2866cd2a6 100644 --- a/beacon-chain/sync/validate_committee_index_beacon_attestation_test.go +++ b/beacon-chain/sync/validate_committee_index_beacon_attestation_test.go @@ -16,11 +16,10 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/cache" dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" - beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state" mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/params" + "github.com/prysmaticlabs/prysm/shared/testutil" ) func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { @@ -58,7 +57,7 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { t.Fatal(err) } - savedState, _ := beaconstate.InitializeFromProto(&pb.BeaconState{}) + savedState := testutil.NewBeaconState() db.SaveState(context.Background(), savedState, validBlockRoot) tests := []struct { diff --git a/shared/testutil/BUILD.bazel b/shared/testutil/BUILD.bazel index bddbb835e6f2..cca45f10bc2b 100644 --- a/shared/testutil/BUILD.bazel +++ b/shared/testutil/BUILD.bazel @@ -9,6 +9,7 @@ go_library( "helpers.go", "log.go", "spectest.go", + "state.go", "tempdir.go", "wait_timeout.go", ], @@ -45,12 +46,14 @@ go_test( "block_test.go", "deposits_test.go", "helpers_test.go", + "state_test.go", ], embed = [":go_default_library"], deps = [ "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/core/state:go_default_library", "//beacon-chain/core/state/stateutils:go_default_library", + "//proto/beacon/p2p/v1:go_default_library", "//shared/bytesutil:go_default_library", "//shared/params:go_default_library", ], diff --git a/shared/testutil/block.go b/shared/testutil/block.go index bdfd99151c83..59e0b8b5de0c 100644 --- a/shared/testutil/block.go +++ b/shared/testutil/block.go @@ -173,6 +173,8 @@ func GenerateProposerSlashingForValidator( ProposerIndex: idx, Slot: bState.Slot(), BodyRoot: bytesutil.PadTo([]byte{0, 1, 0}, 32), + StateRoot: make([]byte, 32), + ParentRoot: make([]byte, 32), }, } currentEpoch := helpers.CurrentEpoch(bState) @@ -191,6 +193,8 @@ func GenerateProposerSlashingForValidator( ProposerIndex: idx, Slot: bState.Slot(), BodyRoot: bytesutil.PadTo([]byte{0, 2, 0}, 32), + StateRoot: make([]byte, 32), + ParentRoot: make([]byte, 32), }, } root, err = helpers.ComputeSigningRoot(header2.Header, domain) diff --git a/shared/testutil/state.go b/shared/testutil/state.go new file mode 100644 index 000000000000..6ff25932f9bf --- /dev/null +++ b/shared/testutil/state.go @@ -0,0 +1,53 @@ +package testutil + +import ( + ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/go-bitfield" + stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/params" +) + +var st, _ = stateTrie.InitializeFromProtoUnsafe(&pb.BeaconState{ + BlockRoots: filledByteSlice2D(params.BeaconConfig().SlotsPerHistoricalRoot, 32), + StateRoots: filledByteSlice2D(params.BeaconConfig().SlotsPerHistoricalRoot, 32), + Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector), + RandaoMixes: filledByteSlice2D(params.BeaconConfig().EpochsPerHistoricalVector, 32), + Validators: make([]*ethpb.Validator, 0), + CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, + Eth1Data: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + BlockHash: make([]byte, 32), + }, + Fork: &pb.Fork{ + PreviousVersion: make([]byte, 4), + CurrentVersion: make([]byte, 4), + }, + Eth1DataVotes: make([]*ethpb.Eth1Data, 0), + HistoricalRoots: make([][]byte, 0), + JustificationBits: bitfield.Bitvector4{0x0}, + FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, + LatestBlockHeader: ðpb.BeaconBlockHeader{ + ParentRoot: make([]byte, 32), + StateRoot: make([]byte, 32), + BodyRoot: make([]byte, 32), + }, + PreviousEpochAttestations: make([]*pb.PendingAttestation, 0), + CurrentEpochAttestations: make([]*pb.PendingAttestation, 0), + PreviousJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)}, +}) + +// NewBeaconState creates a beacon state with minimum marshalable fields. +func NewBeaconState() *stateTrie.BeaconState { + return st.Copy() +} + +// SSZ will fill 2D byte slices with their respective values, so we must fill these in too for round +// trip testing. +func filledByteSlice2D(len, innerLen uint64) [][]byte { + b := make([][]byte, len) + for i := uint64(0); i < len; i++ { + b[i] = make([]byte, innerLen) + } + return b +} diff --git a/shared/testutil/state_test.go b/shared/testutil/state_test.go new file mode 100644 index 000000000000..7aa4ed4b759e --- /dev/null +++ b/shared/testutil/state_test.go @@ -0,0 +1,23 @@ +package testutil + +import ( + "reflect" + "testing" + + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" +) + +func TestNewBeaconState(t *testing.T) { + st := NewBeaconState() + b, err := st.InnerStateUnsafe().MarshalSSZ() + if err != nil { + t.Fatal(err) + } + got := &pb.BeaconState{} + if err := got.UnmarshalSSZ(b); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(st.InnerStateUnsafe(), got) { + t.Fatal("State did not match after round trip marshal") + } +}