Skip to content

Commit

Permalink
consensus/parlia: fix nextForkHash in Extra filed of block header (bn…
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBSC authored and whw188 committed Dec 6, 2023
1 parent bb30cb2 commit 58e5b14
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ func (p *Parlia) Prepare(chain consensus.ChainHeaderReader, header *types.Header
}

header.Extra = header.Extra[:extraVanity-nextForkHashSize]
nextForkHash := forkid.NewID(p.chainConfig, p.genesisHash, number, header.Time).Hash
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, number, header.Time)
header.Extra = append(header.Extra, nextForkHash[:]...)

if err := p.prepareValidators(header); err != nil {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
if err != nil {
return err
}
nextForkHash := forkid.NewID(p.chainConfig, p.genesisHash, number, header.Time).Hash
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, number, header.Time)
if !snap.isMajorityFork(hex.EncodeToString(nextForkHash[:])) {
log.Debug("there is a possible fork, and your client is not the majority. Please check...", "nextForkHash", hex.EncodeToString(nextForkHash[:]))
}
Expand Down
26 changes: 26 additions & 0 deletions core/forkid/forkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ func NewID(config *params.ChainConfig, genesis common.Hash, head, time uint64) I
return ID{Hash: checksumToBytes(hash), Next: 0}
}

// NextForkHash calculates the forkHash from genesis to the next fork block number or time
func NextForkHash(config *params.ChainConfig, genesis common.Hash, head uint64, time uint64) [4]byte {
// Calculate the starting checksum from the genesis hash
hash := crc32.ChecksumIEEE(genesis[:])

// Calculate the next fork checksum
forksByBlock, forksByTime := gatherForks(config)
for _, fork := range forksByBlock {
if fork > head {
// Checksum the previous hash and nextFork number and return
return checksumToBytes(checksumUpdate(hash, fork))
}
// Fork already passed, checksum the previous hash and the fork number
hash = checksumUpdate(hash, fork)
}
for _, fork := range forksByTime {
if fork > time {
// Checksum the previous hash and nextFork time and return
return checksumToBytes(checksumUpdate(hash, fork))
}
// Fork already passed, checksum the previous hash and the fork time
hash = checksumUpdate(hash, fork)
}
return checksumToBytes(hash)
}

// NewIDWithChain calculates the Ethereum fork ID from an existing chain instance.
func NewIDWithChain(chain Blockchain) ID {
head := chain.CurrentHeader()
Expand Down

0 comments on commit 58e5b14

Please sign in to comment.