Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Commit

Permalink
fix test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan-kwon committed Dec 7, 2023
1 parent 8a69068 commit d5e484f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 10 additions & 8 deletions consensus/istanbul/backend/randao.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ func (sb *backend) CalcRandao(number *big.Int, prevMixHash []byte) ([]byte, []by
if sb.blsSecretKey == nil {
return nil, nil, errNoBlsKey
}
parentMixHash := prevMixHash
if prevMixHash == nil {
prevMixHash = params.ZeroMixHash
parentMixHash = params.ZeroMixHash
}
if len(prevMixHash) != 32 {
logger.Error("invalid prevMixHash", "number", number.Uint64(), "prevMixHash", hexutil.Encode(prevMixHash))
if len(parentMixHash) != 32 {
logger.Error("invalid prevMixHash", "number", number.Uint64(), "prevMixHash", hexutil.Encode(parentMixHash))
return nil, nil, errInvalidRandaoFields
}

Expand All @@ -123,7 +124,7 @@ func (sb *backend) CalcRandao(number *big.Int, prevMixHash []byte) ([]byte, []by
randomReveal := bls.Sign(sb.blsSecretKey, msg[:]).Marshal()

// calc_mix_hash() = xor(prevMixHash, keccak256(randomReveal))
mixHash := calcMixHash(randomReveal, prevMixHash)
mixHash := calcMixHash(randomReveal, parentMixHash)

return randomReveal, mixHash, nil
}
Expand All @@ -136,10 +137,11 @@ func (sb *backend) VerifyRandao(chain consensus.ChainReader, header *types.Heade
if header.RandomReveal == nil || header.MixHash == nil {
return errInvalidRandaoFields
}
// The following condition is only true when header's block number is the Randao hardfork block number.
// The following if condition is only true when header's block number is the Randao hardfork block number.
// Because of the above condition, prevMixHash cannot be nil after Randao hardfork block.
if prevMixHash == nil {
prevMixHash = params.ZeroMixHash
parentMixHash := prevMixHash
if parentMixHash == nil {
parentMixHash = params.ZeroMixHash
}

proposer, err := sb.Author(header)
Expand All @@ -165,7 +167,7 @@ func (sb *backend) VerifyRandao(chain consensus.ChainReader, header *types.Heade
}

// if not newHeader.mixHash == calc_mix_hash(prevMixHash, newHeader.randomReveal): return False
mixHash := calcMixHash(header.RandomReveal, prevMixHash)
mixHash := calcMixHash(header.RandomReveal, parentMixHash)
if !bytes.Equal(header.MixHash, mixHash) {
return errInvalidRandaoFields
}
Expand Down
5 changes: 4 additions & 1 deletion consensus/istanbul/backend/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ func (s *Snapshot) apply(headers []*types.Header, gov governance.Engine, addr co

snap.ValSet.SetBlockNum(snap.Number)
snap.ValSet.SetSubGroupSize(snap.CommitteeSize)
snap.ValSet.SetMixHash(lastHeader.MixHash)

if chain.Config().IsRandaoForkEnabled(lastHeader.Number) {
snap.ValSet.SetMixHash(lastHeader.MixHash)
}

if writable {
gov.SetTotalVotingPower(snap.ValSet.TotalVotingPower())
Expand Down

0 comments on commit d5e484f

Please sign in to comment.