Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition of x.signer in v1 consensus #445

Merged
merged 1 commit into from Mar 3, 2024

Conversation

wjrjerome
Copy link
Collaborator

@wjrjerome wjrjerome commented Feb 25, 2024

Proposed changes

Fix a small bug where race condition could happen when concurrently update x.signer

Types of changes

What types of changes does your code introduce to XDC network?
Put an in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)
  • Regular KTLO or any of the maintaince work. e.g code style
  • CICD Improvement

Impacted Components

Which part of the codebase this PR will touch base on,

Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

@wjrjerome wjrjerome merged commit e324d7d into dev-upgrade Mar 3, 2024
@wjrjerome wjrjerome deleted the fix-race-condition-in-v1-x.signer branch March 3, 2024 11:27
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)
Copy link
Collaborator

@gzliudan gzliudan Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this version ?

	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 {
			if snap.validVote(address, authorize) {
				addresses = append(addresses, address)
			}
		}
		// If there's pending proposals, cast a vote on them
		if len(addresses) > 0 {
			header.Coinbase = addresses[rand.Intn(len(addresses))]
			if x.proposals[header.Coinbase] {
				copy(header.Nonce[:], utils.NonceAuthVote)
			} else {
				copy(header.Nonce[:], utils.NonceDropVote)
			}
		}
	}

	parent := chain.GetHeader(header.ParentHash, number-1)
	if parent == nil {
		return consensus.ErrUnknownAncestor
	}

	// Set the correct difficulty
	x.lock.RLock()
	signer := x.signer
	x.lock.RUnlock()
	header.Difficulty = x.calcDifficulty(chain, parent, signer)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The locks are already in the code before, i'm not sure what values it's locking, probably x.proposals. I prefer not to change the original logic as it would involve unnecessary riskks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can refer the struct and other codes:

lock   sync.RWMutex    // Protects the signer fields

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure i understand what you mean above.
What's i'm referring to is that the lock is already within the original if statement.
image

I do not intend to change the behaviour of the original code hence move the signer := x.signer right before the Runlock in the original code. This would be the smallest change that would be required to achieve the same goal

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lock field protects signer field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants