diff --git a/beacon-chain/blockchain/chain_info.go b/beacon-chain/blockchain/chain_info.go index 538eb7c6e74f..aad74cbabbac 100644 --- a/beacon-chain/blockchain/chain_info.go +++ b/beacon-chain/blockchain/chain_info.go @@ -43,6 +43,7 @@ type HeadFetcher interface { HeadState(ctx context.Context) (*state.BeaconState, error) HeadValidatorsIndices(epoch uint64) ([]uint64, error) HeadSeed(epoch uint64) ([32]byte, error) + HeadGenesisValidatorRoot() [32]byte } // ForkFetcher retrieves the current fork information of the Ethereum beacon chain. @@ -180,6 +181,15 @@ func (s *Service) HeadSeed(epoch uint64) ([32]byte, error) { return helpers.Seed(s.headState(), epoch, params.BeaconConfig().DomainBeaconAttester) } +// HeadGenesisValidatorRoot returns genesis validator root of the head state. +func (s *Service) HeadGenesisValidatorRoot() [32]byte { + if !s.hasHeadState() { + return [32]byte{} + } + + return s.headGenesisValidatorRoot() +} + // GenesisTime returns the genesis time of beacon chain. func (s *Service) GenesisTime() time.Time { return s.genesisTime diff --git a/beacon-chain/blockchain/chain_info_test.go b/beacon-chain/blockchain/chain_info_test.go index e74bace82275..9db4ff8b1187 100644 --- a/beacon-chain/blockchain/chain_info_test.go +++ b/beacon-chain/blockchain/chain_info_test.go @@ -201,3 +201,20 @@ func TestCurrentFork_CanRetrieve(t *testing.T) { t.Error("Received incorrect fork version") } } + +func TestGenesisValidatorRoot_CanRetrieve(t *testing.T) { + // Should not panic if head state is nil. + c := &Service{} + if c.GenesisValidatorRoot() != [32]byte{} { + t.Error("Did not get correct genesis validator root") + } + + s, err := state.InitializeFromProto(&pb.BeaconState{GenesisValidatorsRoot: []byte{'a'}}) + if err != nil { + t.Fatal(err) + } + c.head = &head{state: s} + if c.GenesisValidatorRoot() != [32]byte{'a'} { + t.Error("Did not get correct genesis validator root") + } +} diff --git a/beacon-chain/blockchain/head.go b/beacon-chain/blockchain/head.go index 7c70d0616908..ac7cafa53a20 100644 --- a/beacon-chain/blockchain/head.go +++ b/beacon-chain/blockchain/head.go @@ -202,6 +202,14 @@ func (s *Service) headState() *state.BeaconState { return s.head.state.Copy() } +// This returns the genesis validator root of the head state. +func (s *Service) headGenesisValidatorRoot() [32]byte { + s.headLock.RLock() + defer s.headLock.RUnlock() + + return bytesutil.ToBytes32(s.head.state.GenesisValidatorRoot()) +} + // Returns true if head state exists. func (s *Service) hasHeadState() bool { s.headLock.RLock() diff --git a/beacon-chain/blockchain/testing/mock.go b/beacon-chain/blockchain/testing/mock.go index 9b242f0aa6b7..08a40710b9b8 100644 --- a/beacon-chain/blockchain/testing/mock.go +++ b/beacon-chain/blockchain/testing/mock.go @@ -245,3 +245,8 @@ func (ms *ChainService) ClearCachedStates() {} func (ms *ChainService) HasInitSyncBlock(root [32]byte) bool { return false } + +// HeadGenesisValidatorRoot mocks HeadGenesisValidatorRoot method in chain service. +func (ms *ChainService) HeadGenesisValidatorRoot() [32]byte { + return [32]byte{} +} diff --git a/beacon-chain/rpc/validator/server.go b/beacon-chain/rpc/validator/server.go index 5fb68764e653..5c886ee03039 100644 --- a/beacon-chain/rpc/validator/server.go +++ b/beacon-chain/rpc/validator/server.go @@ -131,11 +131,8 @@ func (vs *Server) ValidatorIndex(ctx context.Context, req *ethpb.ValidatorIndexR // DomainData fetches the current domain version information from the beacon state. func (vs *Server) DomainData(ctx context.Context, request *ethpb.DomainRequest) (*ethpb.DomainResponse, error) { fork := vs.ForkFetcher.CurrentFork() - s, err := vs.HeadFetcher.HeadState(ctx) - if err != nil { - return nil, err - } - dv, err := helpers.Domain(fork, request.Epoch, bytesutil.ToBytes4(request.Domain), s.GenesisValidatorRoot()) + headGenesisValidatorRoot := vs.HeadFetcher.HeadGenesisValidatorRoot() + dv, err := helpers.Domain(fork, request.Epoch, bytesutil.ToBytes4(request.Domain), headGenesisValidatorRoot[:]) if err != nil { return nil, err }