Skip to content

Commit

Permalink
Add HeadGenesisValidatorRoot for DomainData (#5582)
Browse files Browse the repository at this point in the history
* Provide getters

* Using it in DomainData

* Fixed test

* Tests

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
terencechain and prylabs-bulldozer[bot] authored Apr 23, 2020
1 parent 8b058b5 commit 2f21249
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
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

0 comments on commit 2f21249

Please sign in to comment.