Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HeadGenesisValidatorRoot for DomainData #5582

Merged
merged 6 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions beacon-chain/blockchain/chain_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions beacon-chain/blockchain/chain_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
8 changes: 8 additions & 0 deletions beacon-chain/blockchain/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/blockchain/testing/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
7 changes: 2 additions & 5 deletions beacon-chain/rpc/validator/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down