Minor clean up to 6249 (#6253)

* Add helper to prevent zero hashes

* Test

* Comments

* Comments
This commit is contained in:
terence tsao 2020-06-14 10:43:25 -07:00 committed by GitHub
parent 1dfeb645b6
commit f4e9e2f49c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 20 deletions

View File

@ -238,11 +238,7 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified
return true, nil
}
var newJustifiedBlockSigned *ethpb.SignedBeaconBlock
justifiedRoot := bytesutil.ToBytes32(newJustifiedCheckpt.Root)
// Sets to the genesis root in the event we get a zero hash.
if justifiedRoot == params.BeaconConfig().ZeroHash {
justifiedRoot = s.genesisRoot
}
justifiedRoot := s.ensureRootNotZeros(bytesutil.ToBytes32(newJustifiedCheckpt.Root))
var err error
if !featureconfig.Get().NoInitSyncBatchSaveBlocks && s.hasInitSyncBlock(justifiedRoot) {
newJustifiedBlockSigned = s.getInitSyncBlock(justifiedRoot)
@ -261,11 +257,7 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified
return false, nil
}
var justifiedBlockSigned *ethpb.SignedBeaconBlock
cachedJustifiedRoot := bytesutil.ToBytes32(s.justifiedCheckpt.Root)
// Sets to the genesis root in the event we get a zero hash.
if cachedJustifiedRoot == params.BeaconConfig().ZeroHash {
cachedJustifiedRoot = s.genesisRoot
}
cachedJustifiedRoot := s.ensureRootNotZeros(bytesutil.ToBytes32(s.justifiedCheckpt.Root))
if !featureconfig.Get().NoInitSyncBatchSaveBlocks && s.hasInitSyncBlock(cachedJustifiedRoot) {
justifiedBlockSigned = s.getInitSyncBlock(cachedJustifiedRoot)
} else {
@ -305,11 +297,7 @@ func (s *Service) updateJustified(ctx context.Context, state *stateTrie.BeaconSt
}
if !featureconfig.Get().NewStateMgmt {
justifiedRoot := bytesutil.ToBytes32(cpt.Root)
if justifiedRoot == params.BeaconConfig().ZeroHash {
justifiedRoot = s.genesisRoot
}
justifiedRoot := s.ensureRootNotZeros(bytesutil.ToBytes32(cpt.Root))
justifiedState := s.initSyncState[justifiedRoot]
// If justified state is nil, resume back to normal syncing process and save
// justified check point.
@ -446,11 +434,7 @@ func (s *Service) finalizedImpliesNewJustified(ctx context.Context, state *state
}
finalizedBlk := finalizedBlkSigned.Block
justifiedRoot := bytesutil.ToBytes32(s.justifiedCheckpt.Root)
if justifiedRoot == params.BeaconConfig().ZeroHash {
justifiedRoot = s.genesisRoot
}
justifiedRoot := s.ensureRootNotZeros(bytesutil.ToBytes32(s.justifiedCheckpt.Root))
anc, err := s.ancestor(ctx, justifiedRoot[:], finalizedBlk.Slot)
if err != nil {
return err
@ -522,3 +506,12 @@ func (s *Service) deletePoolAtts(atts []*ethpb.Attestation) error {
return nil
}
// This ensures that the input root defaults to using genesis root instead of zero hashes. This is needed for handling
// fork choice justification routine.
func (s *Service) ensureRootNotZeros(root [32]byte) [32]byte {
if root == params.BeaconConfig().ZeroHash {
return s.genesisRoot
}
return root
}

View File

@ -833,3 +833,23 @@ func TestAncestor_HandleSkipSlot(t *testing.T) {
t.Error("Did not get correct root")
}
}
func TestEnsureRootNotZeroHashes(t *testing.T) {
ctx := context.Background()
cfg := &Config{}
service, err := NewService(ctx, cfg)
if err != nil {
t.Fatal(err)
}
service.genesisRoot = [32]byte{'a'}
r := service.ensureRootNotZeros(params.BeaconConfig().ZeroHash)
if r != service.genesisRoot {
t.Error("Did not get wanted justified root")
}
root := [32]byte{'b'}
r = service.ensureRootNotZeros(root)
if r != root {
t.Error("Did not get wanted justified root")
}
}