Skip to content

Commit

Permalink
fix: race condition of x.signer in v1 consensus (#445)
Browse files Browse the repository at this point in the history
Co-authored-by: wjrjerome <wjrjerome@babylonchain.io>
  • Loading branch information
wjrjerome and jrwbabylonlab committed Mar 3, 2024
1 parent 317dc4c commit e324d7d
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions consensus/XDPoS/engines/engine_v1/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,9 @@ func (x *XDPoS_v1) Prepare(chain consensus.ChainReader, header *types.Header) er
if err != nil {
return err
}
if number%x.config.Epoch != 0 {
x.lock.RLock()

x.lock.RLock()
if number%x.config.Epoch != 0 {
// Gather all the proposals that make sense voting on
addresses := make([]common.Address, 0, len(x.proposals))
for address, authorize := range x.proposals {
Expand All @@ -727,14 +727,16 @@ func (x *XDPoS_v1) Prepare(chain consensus.ChainReader, header *types.Header) er
copy(header.Nonce[:], utils.NonceDropVote)
}
}
x.lock.RUnlock()
}
signer := x.signer
x.lock.RUnlock()

parent := chain.GetHeader(header.ParentHash, number-1)
if parent == nil {
return consensus.ErrUnknownAncestor
}
// Set the correct difficulty
header.Difficulty = x.calcDifficulty(chain, parent, x.signer)
header.Difficulty = x.calcDifficulty(chain, parent, signer)
log.Debug("CalcDifficulty ", "number", header.Number, "difficulty", header.Difficulty)
// Ensure the extra data has all it's components
if len(header.Extra) < utils.ExtraVanity {
Expand Down Expand Up @@ -956,7 +958,10 @@ func (x *XDPoS_v1) Seal(chain consensus.ChainReader, block *types.Block, stop <-
// that a new block should have based on the previous blocks in the chain and the
// current signer.
func (x *XDPoS_v1) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
return x.calcDifficulty(chain, parent, x.signer)
x.lock.Lock()
signer := x.signer
x.lock.Unlock()
return x.calcDifficulty(chain, parent, signer)
}

func (x *XDPoS_v1) calcDifficulty(chain consensus.ChainReader, parent *types.Header, signer common.Address) *big.Int {
Expand Down

0 comments on commit e324d7d

Please sign in to comment.