Skip to content

Commit

Permalink
consortium/v2: add function to get seal hash before finality vote
Browse files Browse the repository at this point in the history
As we delay assembling finality vote until Seal function, it means the seal hash
can be updated after the FinalizeAndAssemble. This breaks the worker which uses
seal hash to store and look up seal tasks between FinalizeAndAssemble and Seal
function. This commit creates a function SealHashBeforeFinalityVote to calculate
the seal hash before assembling finality vote to be used by worker to look up
the seal tasks.
  • Loading branch information
minh-bq committed Jul 31, 2023
1 parent a7b6bf1 commit ee77594
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ type FastFinalityPoSA interface {
SetVotePool(votePool VotePool)

GetActiveValidatorAt(chain ChainHeaderReader, blockNumber uint64, blockHash common.Hash) []finality.ValidatorWithBlsPub

SealHashBeforeFinalityVote(header *types.Header) common.Hash
}

type VotePool interface {
Expand Down
9 changes: 9 additions & 0 deletions consensus/consortium/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ func (c *Consortium) SealHash(header *types.Header) common.Hash {
return c.v1.SealHash(header)
}

// SealHashBeforeFinalityVote returns the seal hash before assembling finality vote
// this is use for worker to look up seal tasks
func (c *Consortium) SealHashBeforeFinalityVote(header *types.Header) common.Hash {
if c.chainConfig.IsShillin(header.Number) {
return c.v2.SealHashBeforeFinalityVote(header)
}
return c.SealHash(header)
}

// Close implements consensus.Engine. It's a noop for Consortium as there are no background threads.
func (c *Consortium) Close() error {
return nil
Expand Down
11 changes: 11 additions & 0 deletions consensus/consortium/v2/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,17 @@ func (c *Consortium) SealHash(header *types.Header) common.Hash {
return SealHash(header, c.chainConfig.ChainID)
}

// SealHashBeforeFinalityVote returns the seal hash before assembling finality vote
// this is use for worker to look up seal tasks
func (c *Consortium) SealHashBeforeFinalityVote(header *types.Header) common.Hash {
copyHeader := types.CopyHeader(header)

extraData, _ := finality.DecodeExtra(copyHeader.Extra, true)
extraData.HasFinalityVote = 0
copyHeader.Extra = extraData.Encode(true)
return SealHash(copyHeader, c.chainConfig.ChainID)
}

// Close implements consensus.Engine. It's a noop for Consortium as there are no background threads.
func (c *Consortium) Close() error {
return nil
Expand Down
8 changes: 7 additions & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,15 @@ func (w *worker) resultLoop() {
continue
}
var (
sealhash = w.engine.SealHash(block.Header())
sealhash common.Hash
hash = block.Hash()
)
if engine, ok := w.engine.(consensus.FastFinalityPoSA); ok {
sealhash = engine.SealHashBeforeFinalityVote(block.Header())
} else {
sealhash = w.engine.SealHash(block.Header())
}

w.pendingMu.RLock()
task, exist := w.pendingTasks[sealhash]
w.pendingMu.RUnlock()
Expand Down

0 comments on commit ee77594

Please sign in to comment.