From 6b5528df22f2fc195f388c6e30c64918a480096b Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Mon, 27 Apr 2020 17:03:39 +0300 Subject: [PATCH 01/10] fixes segfault --- beacon-chain/rpc/beacon/attestations_test.go | 19 ++++--------------- beacon-chain/rpc/beacon/validators.go | 6 ++++++ beacon-chain/rpc/beacon/validators_test.go | 12 +++++++++++- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/beacon-chain/rpc/beacon/attestations_test.go b/beacon-chain/rpc/beacon/attestations_test.go index cc1f825984dc..2aefc4f5218f 100644 --- a/beacon-chain/rpc/beacon/attestations_test.go +++ b/beacon-chain/rpc/beacon/attestations_test.go @@ -612,8 +612,8 @@ func TestServer_ListIndexedAttestations_NewStateManagnmentDisabled(t *testing.T) func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { params.OverrideBeaconConfig(params.MainnetConfig()) defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) - cfg := assertNewStateMgmtIsEnabled() - defer featureconfig.Init(cfg) + featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) + defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) helpers.ClearCache() @@ -749,8 +749,8 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { func TestServer_ListIndexedAttestations_OldEpoch(t *testing.T) { params.OverrideBeaconConfig(params.MainnetConfig()) defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) - cfg := assertNewStateMgmtIsEnabled() - defer featureconfig.Init(cfg) + featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) + defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) helpers.ClearCache() @@ -1232,14 +1232,3 @@ func TestServer_StreamAttestations_OnSlotTick(t *testing.T) { } <-exitRoutine } - -// assertNewStateMgmtIsEnabled asserts that state management feature is enabled. -func assertNewStateMgmtIsEnabled() *featureconfig.Flags { - cfg := featureconfig.Get() - if cfg.NewStateMgmt { - cfgUpd := cfg.Copy() - cfgUpd.NewStateMgmt = true - featureconfig.Init(cfgUpd) - } - return cfg -} diff --git a/beacon-chain/rpc/beacon/validators.go b/beacon-chain/rpc/beacon/validators.go index af1549f999ac..d9b13c4ea53b 100644 --- a/beacon-chain/rpc/beacon/validators.go +++ b/beacon-chain/rpc/beacon/validators.go @@ -449,10 +449,16 @@ func (bs *Server) GetValidatorActiveSetChanges( func (bs *Server) getValidatorActiveSetChangesUsingOldArchival( ctx context.Context, req *ethpb.GetValidatorActiveSetChangesRequest, ) (*ethpb.ActiveSetChanges, error) { + if bs.HeadFetcher == nil { + return nil, status.Error(codes.Internal, "Nil head state") + } headState, err := bs.HeadFetcher.HeadState(ctx) if err != nil { return nil, status.Error(codes.Internal, "Could not get head state") } + if bs.BeaconDB == nil { + return nil, status.Error(codes.Internal, "Nil beacon DB") + } currentEpoch := helpers.CurrentEpoch(headState) requestedEpoch := currentEpoch requestingGenesis := false diff --git a/beacon-chain/rpc/beacon/validators_test.go b/beacon-chain/rpc/beacon/validators_test.go index 4ede5e2f555c..dc61751b11eb 100644 --- a/beacon-chain/rpc/beacon/validators_test.go +++ b/beacon-chain/rpc/beacon/validators_test.go @@ -37,12 +37,20 @@ func init() { } func TestServer_GetValidatorActiveSetChanges_CannotRequestFutureEpoch(t *testing.T) { + db := dbTest.SetupDB(t) + defer dbTest.TeardownDB(t, db) ctx := context.Background() st := testutil.NewBeaconState() if err := st.SetSlot(0); err != nil { t.Fatal(err) } - bs := &Server{GenesisTimeFetcher: &mock.ChainService{}} + bs := &Server{ + GenesisTimeFetcher: &mock.ChainService{}, + HeadFetcher: &mock.ChainService{ + State: testutil.NewBeaconState(), + }, + BeaconDB: db, + } wanted := "Cannot retrieve information about an epoch in the future" if _, err := bs.GetValidatorActiveSetChanges( @@ -1147,6 +1155,8 @@ func TestServer_GetValidator(t *testing.T) { func TestServer_GetValidatorActiveSetChanges(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) + featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) + defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) ctx := context.Background() validators := make([]*ethpb.Validator, 8) From 82708a3c42c3f677916e4d82c9c364e30c9e6c6e Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Mon, 27 Apr 2020 17:11:34 +0300 Subject: [PATCH 02/10] upd state --- beacon-chain/rpc/beacon/validators_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/rpc/beacon/validators_test.go b/beacon-chain/rpc/beacon/validators_test.go index 87119ea78c1b..fa8678a12428 100644 --- a/beacon-chain/rpc/beacon/validators_test.go +++ b/beacon-chain/rpc/beacon/validators_test.go @@ -47,7 +47,7 @@ func TestServer_GetValidatorActiveSetChanges_CannotRequestFutureEpoch(t *testing bs := &Server{ GenesisTimeFetcher: &mock.ChainService{}, HeadFetcher: &mock.ChainService{ - State: testutil.NewBeaconState(), + State: st, }, BeaconDB: db, } From 001604d484dbd48d0fb3004895d3bea608acac20 Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Mon, 27 Apr 2020 17:37:56 +0300 Subject: [PATCH 03/10] updates shard=1 --- beacon-chain/rpc/beacon/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/rpc/beacon/BUILD.bazel b/beacon-chain/rpc/beacon/BUILD.bazel index c1faa77ef12c..0f2ce2a15227 100644 --- a/beacon-chain/rpc/beacon/BUILD.bazel +++ b/beacon-chain/rpc/beacon/BUILD.bazel @@ -71,7 +71,7 @@ go_test( "validators_test.go", ], embed = [":go_default_library"], - shard_count = 4, + shard_count = 1, deps = [ "//beacon-chain/blockchain/testing:go_default_library", "//beacon-chain/cache:go_default_library", From e320829bef052bdc72222ee5798f2ad4e0aa906d Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Mon, 27 Apr 2020 20:36:20 +0300 Subject: [PATCH 04/10] fixes tests --- beacon-chain/rpc/beacon/assignments_test.go | 28 ++++---- beacon-chain/rpc/beacon/blocks_test.go | 1 + beacon-chain/rpc/beacon/committees_test.go | 2 + beacon-chain/rpc/beacon/validators.go | 6 ++ beacon-chain/rpc/beacon/validators_test.go | 77 ++++++++++----------- beacon-chain/state/stateutil/state_root.go | 2 +- shared/featureconfig/config.go | 10 +++ 7 files changed, 70 insertions(+), 56 deletions(-) diff --git a/beacon-chain/rpc/beacon/assignments_test.go b/beacon-chain/rpc/beacon/assignments_test.go index e92308620aef..2795267c4fbd 100644 --- a/beacon-chain/rpc/beacon/assignments_test.go +++ b/beacon-chain/rpc/beacon/assignments_test.go @@ -24,10 +24,8 @@ import ( ) func TestServer_ListAssignments_CannotRequestFutureEpoch(t *testing.T) { - config := &featureconfig.Flags{ - NewStateMgmt: true, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) @@ -52,10 +50,8 @@ func TestServer_ListAssignments_CannotRequestFutureEpoch(t *testing.T) { } func TestServer_ListAssignments_NoResults(t *testing.T) { - config := &featureconfig.Flags{ - NewStateMgmt: true, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) @@ -105,10 +101,8 @@ func TestServer_ListAssignments_NoResults(t *testing.T) { } func TestServer_ListAssignments_Pagination_InputOutOfRange(t *testing.T) { - config := &featureconfig.Flags{ - NewStateMgmt: true, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) @@ -269,6 +263,8 @@ func TestServer_ListAssignments_Pagination_DefaultPageSize_FromArchive(t *testin helpers.ClearCache() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() ctx := context.Background() count := 500 @@ -368,6 +364,8 @@ func TestServer_ListAssignments_FilterPubkeysIndices_NoPagination(t *testing.T) helpers.ClearCache() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() ctx := context.Background() count := 100 @@ -449,10 +447,8 @@ func TestServer_ListAssignments_FilterPubkeysIndices_NoPagination(t *testing.T) } func TestServer_ListAssignments_CanFilterPubkeysIndices_WithPagination(t *testing.T) { - config := &featureconfig.Flags{ - NewStateMgmt: true, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) diff --git a/beacon-chain/rpc/beacon/blocks_test.go b/beacon-chain/rpc/beacon/blocks_test.go index 73ab0dc81a8e..8474a5de9d87 100644 --- a/beacon-chain/rpc/beacon/blocks_test.go +++ b/beacon-chain/rpc/beacon/blocks_test.go @@ -194,6 +194,7 @@ func TestServer_ListBlocks_Pagination(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) ctx := context.Background() + t.Skip("re-init") count := uint64(100) blks := make([]*ethpb.SignedBeaconBlock, count) diff --git a/beacon-chain/rpc/beacon/committees_test.go b/beacon-chain/rpc/beacon/committees_test.go index 6a0cfa2eba2c..a5a22ec533ad 100644 --- a/beacon-chain/rpc/beacon/committees_test.go +++ b/beacon-chain/rpc/beacon/committees_test.go @@ -29,6 +29,8 @@ func TestServer_ListBeaconCommittees_CurrentEpoch(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) helpers.ClearCache() + featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) + defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) numValidators := 128 ctx := context.Background() diff --git a/beacon-chain/rpc/beacon/validators.go b/beacon-chain/rpc/beacon/validators.go index 8201864e42dc..4016536c87c7 100644 --- a/beacon-chain/rpc/beacon/validators.go +++ b/beacon-chain/rpc/beacon/validators.go @@ -35,6 +35,9 @@ func (bs *Server) ListValidatorBalances( return bs.listValidatorsBalancesUsingOldArchival(ctx, req) } + if bs.GenesisTimeFetcher == nil { + return nil, status.Errorf(codes.Internal, "Nil genesis time fetcher") + } currentEpoch := helpers.SlotToEpoch(bs.GenesisTimeFetcher.CurrentSlot()) requestedEpoch := currentEpoch switch q := req.QueryFilter.(type) { @@ -163,6 +166,9 @@ func (bs *Server) listValidatorsBalancesUsingOldArchival( res := make([]*ethpb.ValidatorBalances_Balance, 0) filtered := map[uint64]bool{} // Track filtered validators to prevent duplication in the response. + if bs.HeadFetcher == nil { + return nil, status.Error(codes.Internal, "Nil head state") + } headState, err := bs.HeadFetcher.HeadState(ctx) if err != nil { return nil, status.Error(codes.Internal, "Could not get head state") diff --git a/beacon-chain/rpc/beacon/validators_test.go b/beacon-chain/rpc/beacon/validators_test.go index fa8678a12428..2db918fa3b45 100644 --- a/beacon-chain/rpc/beacon/validators_test.go +++ b/beacon-chain/rpc/beacon/validators_test.go @@ -98,9 +98,8 @@ func TestServer_ListValidatorBalances_CannotRequestFutureEpoch(t *testing.T) { func TestServer_ListValidatorBalances_NoResults(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) - - featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) - defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() ctx := context.Background() st := testutil.NewBeaconState() @@ -197,6 +196,9 @@ func TestServer_ListValidatorBalances_DefaultResponse_NoArchive(t *testing.T) { bs := &Server{ GenesisTimeFetcher: &mock.ChainService{}, StateGen: stategen.New(db, cache.NewStateSummaryCache()), + HeadFetcher: &mock.ChainService{ + State: st, + }, } res, err := bs.ListValidatorBalances( ctx, @@ -236,6 +238,9 @@ func TestServer_ListValidatorBalances_PaginationOutOfRange(t *testing.T) { bs := &Server{ GenesisTimeFetcher: &mock.ChainService{}, StateGen: stategen.New(db, cache.NewStateSummaryCache()), + HeadFetcher: &mock.ChainService{ + State: st, + }, } req := ðpb.ListValidatorBalancesRequest{PageToken: strconv.Itoa(1), PageSize: 100, QueryFilter: ðpb.ListValidatorBalancesRequest_Epoch{Epoch: 0}} @@ -269,8 +274,6 @@ func pubKey(i uint64) []byte { func TestServer_ListValidatorBalances_Pagination_Default(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) - featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) - defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) ctx := context.Background() setupValidators(t, db, 100) @@ -293,6 +296,9 @@ func TestServer_ListValidatorBalances_Pagination_Default(t *testing.T) { bs := &Server{ GenesisTimeFetcher: &mock.ChainService{}, StateGen: stategen.New(db, cache.NewStateSummaryCache()), + HeadFetcher: &mock.ChainService{ + State: headState, + }, } tests := []struct { @@ -385,6 +391,9 @@ func TestServer_ListValidatorBalances_Pagination_CustomPageSizes(t *testing.T) { bs := &Server{ GenesisTimeFetcher: &mock.ChainService{}, StateGen: stategen.New(db, cache.NewStateSummaryCache()), + HeadFetcher: &mock.ChainService{ + State: headState, + }, } tests := []struct { @@ -440,6 +449,8 @@ func TestServer_ListValidatorBalances_Pagination_CustomPageSizes(t *testing.T) { func TestServer_ListValidatorBalances_OutOfRange(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() ctx := context.Background() setupValidators(t, db, 1) @@ -462,6 +473,9 @@ func TestServer_ListValidatorBalances_OutOfRange(t *testing.T) { bs := &Server{ GenesisTimeFetcher: &mock.ChainService{}, StateGen: stategen.New(db, cache.NewStateSummaryCache()), + HeadFetcher: &mock.ChainService{ + State: headState, + }, } req := ðpb.ListValidatorBalancesRequest{Indices: []uint64{uint64(1)}, QueryFilter: ðpb.ListValidatorBalancesRequest_Epoch{Epoch: 0}} @@ -504,11 +518,8 @@ func TestServer_ListValidatorBalances_FromArchive(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) ctx := context.Background() - - config := &featureconfig.Flags{ - NewStateMgmt: false, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: false}) + defer resetCfg() epoch := uint64(0) validators, balances := setupValidators(t, db, 100) @@ -536,6 +547,7 @@ func TestServer_ListValidatorBalances_FromArchive(t *testing.T) { HeadFetcher: &mock.ChainService{ State: st, }, + GenesisTimeFetcher: &mock.ChainService{}, } req := ðpb.ListValidatorBalancesRequest{ @@ -565,10 +577,8 @@ func TestServer_ListValidatorBalances_FromArchive_NewValidatorNotFound(t *testin defer dbTest.TeardownDB(t, db) ctx := context.Background() - config := &featureconfig.Flags{ - NewStateMgmt: false, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: false}) + defer resetCfg() epoch := uint64(0) _, balances := setupValidators(t, db, 100) @@ -608,8 +618,8 @@ func TestServer_ListValidators_NoResults(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) - featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) - defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() ctx := context.Background() st := testutil.NewBeaconState() @@ -1176,11 +1186,8 @@ func TestServer_GetValidator(t *testing.T) { func TestServer_GetValidatorActiveSetChanges(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) - featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) - defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) - - featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) - defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() ctx := context.Background() validators := make([]*ethpb.Validator, 8) @@ -1291,10 +1298,8 @@ func TestServer_GetValidatorActiveSetChanges(t *testing.T) { } func TestServer_GetValidatorActiveSetChanges_FromArchive(t *testing.T) { - config := &featureconfig.Flags{ - NewStateMgmt: false, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: false}) + defer resetCfg() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) @@ -1640,10 +1645,8 @@ func TestServer_GetValidatorParticipation_CannotRequestFutureEpoch(t *testing.T) db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) - config := &featureconfig.Flags{ - NewStateMgmt: false, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: false}) + defer resetCfg() ctx := context.Background() headState := testutil.NewBeaconState() @@ -1676,10 +1679,8 @@ func TestServer_GetValidatorParticipation_FromArchive(t *testing.T) { defer dbTest.TeardownDB(t, db) ctx := context.Background() - config := &featureconfig.Flags{ - NewStateMgmt: false, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: false}) + defer resetCfg() epoch := uint64(4) part := ðpb.ValidatorParticipation{ @@ -1741,8 +1742,8 @@ func TestServer_GetValidatorParticipation_FromArchive(t *testing.T) { func TestServer_GetValidatorParticipation_PrevEpoch(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) - featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) - defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() ctx := context.Background() validatorCount := uint64(100) @@ -1850,10 +1851,8 @@ func TestServer_GetValidatorParticipation_FromArchive_FinalizedEpoch(t *testing. defer dbTest.TeardownDB(t, db) ctx := context.Background() - config := &featureconfig.Flags{ - NewStateMgmt: false, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: false}) + defer resetCfg() part := ðpb.ValidatorParticipation{ GlobalParticipationRate: 1.0, diff --git a/beacon-chain/state/stateutil/state_root.go b/beacon-chain/state/stateutil/state_root.go index db41f8817eb1..ec2e025f692c 100644 --- a/beacon-chain/state/stateutil/state_root.go +++ b/beacon-chain/state/stateutil/state_root.go @@ -251,7 +251,7 @@ func ForkRoot(fork *pb.Fork) ([32]byte, error) { } // CheckpointRoot computes the HashTreeRoot Merkleization of -// a Checkpoint struct value according to the eth2 +// a InitWithReset struct value according to the eth2 // Simple Serialize specification. func CheckpointRoot(hasher HashFn, checkpoint *ethpb.Checkpoint) ([32]byte, error) { fieldRoots := make([][]byte, 2) diff --git a/shared/featureconfig/config.go b/shared/featureconfig/config.go index 4e34f0002e1b..cccb84c4c960 100644 --- a/shared/featureconfig/config.go +++ b/shared/featureconfig/config.go @@ -86,6 +86,16 @@ func Init(c *Flags) { featureConfig = c } +// InitWithReset sets the global config and returns function that is used to reset configuration back. +func InitWithReset(c *Flags) func() { + origConfig := featureConfig + resetFunc := func() { + Init(origConfig) + } + Init(c) + return resetFunc +} + // Copy returns copy of the config object. func (c *Flags) Copy() *Flags { return &Flags{ From b1374e9be952e9430a224e0519573b6d39578536 Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Mon, 27 Apr 2020 20:57:30 +0300 Subject: [PATCH 05/10] reset config to nil --- shared/featureconfig/config.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/shared/featureconfig/config.go b/shared/featureconfig/config.go index cccb84c4c960..b111f18e03a1 100644 --- a/shared/featureconfig/config.go +++ b/shared/featureconfig/config.go @@ -86,11 +86,10 @@ func Init(c *Flags) { featureConfig = c } -// InitWithReset sets the global config and returns function that is used to reset configuration back. +// InitWithReset sets the global config and returns function that is used to reset configuration. func InitWithReset(c *Flags) func() { - origConfig := featureConfig resetFunc := func() { - Init(origConfig) + Init(&Flags{}) } Init(c) return resetFunc From 6d40006ddf58b93440d776d5013cf6994d753a5d Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Mon, 27 Apr 2020 21:04:24 +0300 Subject: [PATCH 06/10] add flag assertion to other beacon-chain/rpc files --- beacon-chain/rpc/beacon/attestations_test.go | 14 ++++++-------- beacon-chain/rpc/beacon/committees_test.go | 16 ++++++---------- beacon-chain/rpc/beacon/slashings_test.go | 18 ++++-------------- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/beacon-chain/rpc/beacon/attestations_test.go b/beacon-chain/rpc/beacon/attestations_test.go index 2aefc4f5218f..6e2f5b4ce2cb 100644 --- a/beacon-chain/rpc/beacon/attestations_test.go +++ b/beacon-chain/rpc/beacon/attestations_test.go @@ -574,10 +574,8 @@ func TestServer_mapAttestationToTargetRoot(t *testing.T) { } func TestServer_ListIndexedAttestations_NewStateManagnmentDisabled(t *testing.T) { - config := &featureconfig.Flags{ - NewStateMgmt: false, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt:false}) + defer resetCfg() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) @@ -612,8 +610,8 @@ func TestServer_ListIndexedAttestations_NewStateManagnmentDisabled(t *testing.T) func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { params.OverrideBeaconConfig(params.MainnetConfig()) defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) - featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) - defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt:true}) + defer resetCfg() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) helpers.ClearCache() @@ -749,8 +747,8 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { func TestServer_ListIndexedAttestations_OldEpoch(t *testing.T) { params.OverrideBeaconConfig(params.MainnetConfig()) defer params.OverrideBeaconConfig(params.MinimalSpecConfig()) - featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) - defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt:true}) + defer resetCfg() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) helpers.ClearCache() diff --git a/beacon-chain/rpc/beacon/committees_test.go b/beacon-chain/rpc/beacon/committees_test.go index a5a22ec533ad..516db9b36e1b 100644 --- a/beacon-chain/rpc/beacon/committees_test.go +++ b/beacon-chain/rpc/beacon/committees_test.go @@ -29,8 +29,8 @@ func TestServer_ListBeaconCommittees_CurrentEpoch(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) helpers.ClearCache() - featureconfig.Init(&featureconfig.Flags{NewStateMgmt: true}) - defer featureconfig.Init(&featureconfig.Flags{NewStateMgmt: false}) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt:true}) + defer resetCfg() numValidators := 128 ctx := context.Background() @@ -89,10 +89,8 @@ func TestServer_ListBeaconCommittees_CurrentEpoch(t *testing.T) { } func TestServer_ListBeaconCommittees_PreviousEpoch(t *testing.T) { - config := &featureconfig.Flags{ - NewStateMgmt: false, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt:false}) + defer resetCfg() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) @@ -164,10 +162,8 @@ func TestServer_ListBeaconCommittees_PreviousEpoch(t *testing.T) { } func TestServer_ListBeaconCommittees_FromArchive(t *testing.T) { - config := &featureconfig.Flags{ - NewStateMgmt: false, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt:false}) + defer resetCfg() db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) diff --git a/beacon-chain/rpc/beacon/slashings_test.go b/beacon-chain/rpc/beacon/slashings_test.go index 87dc11bc471e..2217a43a49e6 100644 --- a/beacon-chain/rpc/beacon/slashings_test.go +++ b/beacon-chain/rpc/beacon/slashings_test.go @@ -71,13 +71,8 @@ func TestServer_SubmitProposerSlashing(t *testing.T) { func TestServer_SubmitProposerSlashingBroadcast(t *testing.T) { ctx := context.Background() - cfg := featureconfig.Get() - cfg.BroadcastSlashings = true - featureconfig.Init(cfg) - defer func() { - cfg.BroadcastSlashings = false - featureconfig.Init(cfg) - }() + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{BroadcastSlashings:true}) + defer resetCfg() st, privs := testutil.DeterministicGenesisState(t, 64) slashedVal, err := st.ValidatorAtIndex(5) @@ -175,13 +170,8 @@ func TestServer_SubmitAttesterSlashing(t *testing.T) { func TestServer_SubmitAttesterSlashingBroadcast(t *testing.T) { ctx := context.Background() - cfg := featureconfig.Get() - cfg.BroadcastSlashings = true - featureconfig.Init(cfg) - defer func() { - cfg.BroadcastSlashings = false - featureconfig.Init(cfg) - }() + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{BroadcastSlashings:true}) + defer resetCfg() // We mark the validators at index 5, 6 as already slashed. st, privs := testutil.DeterministicGenesisState(t, 64) slashedVal, err := st.ValidatorAtIndex(5) From d43588a4f32226114006f5c630f9c1b80249383d Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Mon, 27 Apr 2020 22:00:46 +0300 Subject: [PATCH 07/10] fix the rest featureconfig.init calls --- beacon-chain/blockchain/process_block_test.go | 8 ++-- beacon-chain/core/helpers/committee_test.go | 9 ---- beacon-chain/powchain/log_processing_test.go | 6 +-- beacon-chain/state/references_test.go | 48 ++++--------------- beacon-chain/state/stateutil/blocks_test.go | 5 +- .../sync/pending_attestations_queue_test.go | 3 ++ ...committee_index_beacon_attestation_test.go | 5 +- .../sync/validate_aggregate_proof_test.go | 6 +-- 8 files changed, 23 insertions(+), 67 deletions(-) diff --git a/beacon-chain/blockchain/process_block_test.go b/beacon-chain/blockchain/process_block_test.go index 0ac10eabc4ff..4c290fa4cbbc 100644 --- a/beacon-chain/blockchain/process_block_test.go +++ b/beacon-chain/blockchain/process_block_test.go @@ -301,10 +301,8 @@ func TestShouldUpdateJustified_ReturnFalse(t *testing.T) { } func TestCachedPreState_CanGetFromStateSummary(t *testing.T) { - config := &featureconfig.Flags{ - NewStateMgmt: true, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() ctx := context.Background() db := testDB.SetupDB(t) @@ -345,6 +343,8 @@ func TestCachedPreState_CanGetFromDB(t *testing.T) { ctx := context.Background() db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() cfg := &Config{ BeaconDB: db, diff --git a/beacon-chain/core/helpers/committee_test.go b/beacon-chain/core/helpers/committee_test.go index 22e8d751e574..7ff246bcc4d6 100644 --- a/beacon-chain/core/helpers/committee_test.go +++ b/beacon-chain/core/helpers/committee_test.go @@ -12,7 +12,6 @@ import ( pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" - "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/hashutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/sliceutil" @@ -434,10 +433,6 @@ func TestShuffledIndices_ShuffleRightLength(t *testing.T) { func TestUpdateCommitteeCache_CanUpdate(t *testing.T) { ClearCache() - c := featureconfig.Get() - featureconfig.Init(c) - defer featureconfig.Init(nil) - validatorCount := int(params.BeaconConfig().MinGenesisActiveValidatorCount) validators := make([]*ethpb.Validator, validatorCount) indices := make([]uint64, validatorCount) @@ -679,10 +674,6 @@ func BenchmarkComputeCommittee4000000_WithOutCache(b *testing.B) { } func TestBeaconCommitteeFromState_UpdateCacheForPreviousEpoch(t *testing.T) { - c := featureconfig.Get() - featureconfig.Init(c) - defer featureconfig.Init(nil) - committeeSize := uint64(16) validators := make([]*ethpb.Validator, committeeSize*params.BeaconConfig().SlotsPerEpoch) for i := 0; i < len(validators); i++ { diff --git a/beacon-chain/powchain/log_processing_test.go b/beacon-chain/powchain/log_processing_test.go index 066432b684bc..6848940d9afd 100644 --- a/beacon-chain/powchain/log_processing_test.go +++ b/beacon-chain/powchain/log_processing_test.go @@ -350,10 +350,8 @@ func TestProcessETH2GenesisLog_8DuplicatePubkeys(t *testing.T) { } func TestProcessETH2GenesisLog(t *testing.T) { - config := &featureconfig.Flags{ - CustomGenesisDelay: 0, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{CustomGenesisDelay:0}) + defer resetCfg() hook := logTest.NewGlobal() testAcc, err := contracts.Setup() if err != nil { diff --git a/beacon-chain/state/references_test.go b/beacon-chain/state/references_test.go index 8351bd4d74f4..7df1b9302a67 100644 --- a/beacon-chain/state/references_test.go +++ b/beacon-chain/state/references_test.go @@ -51,16 +51,8 @@ func TestStateReferenceSharing_Finalizer(t *testing.T) { } func TestStateReferenceCopy_NoUnexpectedValidatorMutation(t *testing.T) { - // Assert that feature is enabled. - if cfg := featureconfig.Get(); !cfg.EnableStateRefCopy { - cfg.EnableStateRefCopy = true - featureconfig.Init(cfg) - defer func() { - cfg := featureconfig.Get() - cfg.EnableStateRefCopy = false - featureconfig.Init(cfg) - }() - } + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableStateRefCopy: true}) + defer resetCfg() a, err := InitializeFromProtoUnsafe(&p2ppb.BeaconState{}) if err != nil { @@ -164,16 +156,8 @@ func TestStateReferenceCopy_NoUnexpectedValidatorMutation(t *testing.T) { } func TestStateReferenceCopy_NoUnexpectedRootsMutation(t *testing.T) { - // Assert that feature is enabled. - if cfg := featureconfig.Get(); !cfg.EnableStateRefCopy { - cfg.EnableStateRefCopy = true - featureconfig.Init(cfg) - defer func() { - cfg := featureconfig.Get() - cfg.EnableStateRefCopy = false - featureconfig.Init(cfg) - }() - } + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableStateRefCopy: true}) + defer resetCfg() root1, root2 := bytesutil.ToBytes32([]byte("foo")), bytesutil.ToBytes32([]byte("bar")) a, err := InitializeFromProtoUnsafe(&p2ppb.BeaconState{ @@ -263,16 +247,8 @@ func TestStateReferenceCopy_NoUnexpectedRootsMutation(t *testing.T) { } func TestStateReferenceCopy_NoUnexpectedRandaoMutation(t *testing.T) { - // Assert that feature is enabled. - if cfg := featureconfig.Get(); !cfg.EnableStateRefCopy { - cfg.EnableStateRefCopy = true - featureconfig.Init(cfg) - defer func() { - cfg := featureconfig.Get() - cfg.EnableStateRefCopy = false - featureconfig.Init(cfg) - }() - } + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableStateRefCopy: true}) + defer resetCfg() val1, val2 := []byte("foo"), []byte("bar") a, err := InitializeFromProtoUnsafe(&p2ppb.BeaconState{ @@ -331,16 +307,8 @@ func TestStateReferenceCopy_NoUnexpectedRandaoMutation(t *testing.T) { } func TestStateReferenceCopy_NoUnexpectedAttestationsMutation(t *testing.T) { - // Assert that feature is enabled. - if cfg := featureconfig.Get(); !cfg.EnableStateRefCopy { - cfg.EnableStateRefCopy = true - featureconfig.Init(cfg) - defer func() { - cfg := featureconfig.Get() - cfg.EnableStateRefCopy = false - featureconfig.Init(cfg) - }() - } + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableStateRefCopy: true}) + defer resetCfg() assertAttFound := func(vals []*p2ppb.PendingAttestation, val uint64) { for i := range vals { diff --git a/beacon-chain/state/stateutil/blocks_test.go b/beacon-chain/state/stateutil/blocks_test.go index 72bacf46717d..88c9f5aaafde 100644 --- a/beacon-chain/state/stateutil/blocks_test.go +++ b/beacon-chain/state/stateutil/blocks_test.go @@ -10,9 +10,8 @@ import ( ) func TestBlockRoot(t *testing.T) { - f := featureconfig.Get() - f.EnableBlockHTR = true - featureconfig.Init(f) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableBlockHTR: true}) + defer resetCfg() genState, keys := testutil.DeterministicGenesisState(t, 100) blk, err := testutil.GenerateFullBlock(genState, keys, testutil.DefaultBlockGenConfig(), 10) if err != nil { diff --git a/beacon-chain/sync/pending_attestations_queue_test.go b/beacon-chain/sync/pending_attestations_queue_test.go index f9fac4cae260..e511efddcdb4 100644 --- a/beacon-chain/sync/pending_attestations_queue_test.go +++ b/beacon-chain/sync/pending_attestations_queue_test.go @@ -22,6 +22,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -64,6 +65,8 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) { db := dbtest.SetupDB(t) defer dbtest.TeardownDB(t, db) p1 := p2ptest.NewTestP2P(t) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() r := &Service{ p2p: p1, 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 364f979fbbaa..e462afe59812 100644 --- a/beacon-chain/sync/subscriber_committee_index_beacon_attestation_test.go +++ b/beacon-chain/sync/subscriber_committee_index_beacon_attestation_test.go @@ -23,9 +23,8 @@ import ( func TestService_committeeIndexBeaconAttestationSubscriber_ValidMessage(t *testing.T) { p := p2ptest.NewTestP2P(t) - fc := featureconfig.Get() - fc.DisableDynamicCommitteeSubnets = true - featureconfig.Init(fc) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{DisableDynamicCommitteeSubnets: true}) + defer resetCfg() ctx := context.Background() db := dbtest.SetupDB(t) diff --git a/beacon-chain/sync/validate_aggregate_proof_test.go b/beacon-chain/sync/validate_aggregate_proof_test.go index d7c5de556708..bbfad31971dc 100644 --- a/beacon-chain/sync/validate_aggregate_proof_test.go +++ b/beacon-chain/sync/validate_aggregate_proof_test.go @@ -338,10 +338,8 @@ func TestValidateAggregateAndProof_ExistedInPool(t *testing.T) { } func TestValidateAggregateAndProofWithNewStateMgmt_CanValidate(t *testing.T) { - config := &featureconfig.Flags{ - NewStateMgmt: true, - } - featureconfig.Init(config) + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true}) + defer resetCfg() db := dbtest.SetupDB(t) defer dbtest.TeardownDB(t, db) From 775b903931b9c6c42b1ddec7d0438592bde9bb12 Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Mon, 27 Apr 2020 22:13:30 +0300 Subject: [PATCH 08/10] remove config setting from init --- beacon-chain/cache/feature_flag_test.go | 15 ++++++++++----- beacon-chain/state/stateutil/state_root_test.go | 10 +++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/beacon-chain/cache/feature_flag_test.go b/beacon-chain/cache/feature_flag_test.go index 2a75a515b6ba..c70098917e1e 100644 --- a/beacon-chain/cache/feature_flag_test.go +++ b/beacon-chain/cache/feature_flag_test.go @@ -1,9 +1,14 @@ package cache -import "github.com/prysmaticlabs/prysm/shared/featureconfig" +import ( + "os" + "testing" -func init() { - featureconfig.Init(&featureconfig.Flags{ - EnableEth1DataVoteCache: true, - }) + "github.com/prysmaticlabs/prysm/shared/featureconfig" +) + +func TestMain(m *testing.M) { + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableEth1DataVoteCache: true}) + defer resetCfg() + os.Exit(m.Run()) } diff --git a/beacon-chain/state/stateutil/state_root_test.go b/beacon-chain/state/stateutil/state_root_test.go index 6eaf0f538c7d..cefa291d3217 100644 --- a/beacon-chain/state/stateutil/state_root_test.go +++ b/beacon-chain/state/stateutil/state_root_test.go @@ -1,6 +1,7 @@ package stateutil_test import ( + "os" "reflect" "strconv" "strings" @@ -15,11 +16,10 @@ import ( "github.com/prysmaticlabs/prysm/shared/params" ) -func init() { - config := &featureconfig.Flags{ - EnableSSZCache: true, - } - featureconfig.Init(config) +func TestMain(m *testing.M) { + resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableSSZCache: true}) + defer resetCfg() + os.Exit(m.Run()) } func TestState_FieldCount(t *testing.T) { From 459c1f855efa55dadaa89c53b0ea3eee8a099b4e Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Mon, 27 Apr 2020 22:19:03 +0300 Subject: [PATCH 09/10] gazelle --- beacon-chain/core/helpers/BUILD.bazel | 1 - beacon-chain/rpc/beacon/blocks_test.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/beacon-chain/core/helpers/BUILD.bazel b/beacon-chain/core/helpers/BUILD.bazel index bfa53e958ff7..2d94eba1653a 100644 --- a/beacon-chain/core/helpers/BUILD.bazel +++ b/beacon-chain/core/helpers/BUILD.bazel @@ -65,7 +65,6 @@ go_test( "//shared/attestationutil:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", - "//shared/featureconfig:go_default_library", "//shared/hashutil:go_default_library", "//shared/params:go_default_library", "//shared/sliceutil:go_default_library", diff --git a/beacon-chain/rpc/beacon/blocks_test.go b/beacon-chain/rpc/beacon/blocks_test.go index 8474a5de9d87..c1e560170a48 100644 --- a/beacon-chain/rpc/beacon/blocks_test.go +++ b/beacon-chain/rpc/beacon/blocks_test.go @@ -194,7 +194,7 @@ func TestServer_ListBlocks_Pagination(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) ctx := context.Background() - t.Skip("re-init") + t.Skip("Re-check after PR#5650 is merged") count := uint64(100) blks := make([]*ethpb.SignedBeaconBlock, count) From 646705c87fe789ebba7d570547030e39be2ea4d5 Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Mon, 27 Apr 2020 22:26:16 +0300 Subject: [PATCH 10/10] reset shard back to 4 --- beacon-chain/rpc/beacon/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/rpc/beacon/BUILD.bazel b/beacon-chain/rpc/beacon/BUILD.bazel index db03e67784d4..387259b17cfa 100644 --- a/beacon-chain/rpc/beacon/BUILD.bazel +++ b/beacon-chain/rpc/beacon/BUILD.bazel @@ -71,7 +71,7 @@ go_test( "validators_test.go", ], embed = [":go_default_library"], - shard_count = 1, + shard_count = 4, deps = [ "//beacon-chain/blockchain/testing:go_default_library", "//beacon-chain/cache:go_default_library",