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

Local nonce manager #35

Merged
merged 5 commits into from
May 13, 2020
Merged

Local nonce manager #35

merged 5 commits into from
May 13, 2020

Conversation

pdyraga
Copy link
Member

@pdyraga pdyraga commented May 12, 2020

The nonce manager is used by the generated contracts to track the current nonce and update it after each successfully submitted transaction. Tracking the nonce locally is required when transactions are submitted from multiple goroutines or when Ethereum clients are deployed behind a load balancer and their mempools are not always in sync.

One important aspect that might not be clear at first is that we need to call IncrementNonce() only when the transaction has been successfully submitted to the mempool. Also, the nonce manager has to be a single instance stored between all contracts.

NonceManager itself does not provide any synchronization. All generated contracts use the transaction lock provided by the chain and that transaction lock makes nonces manipulation safe.

NonceManager considers the local nonce value as no longer valid and updates it to the pending nonce value as seen by the chain after 5 seconds of inactivity in computing nonces. This is to let us recover from potential mempool crashes before the last TX has been propagated to other mempools. However, as long as we submit transactions one after another, the local nonce value is what determines the nonce used in transaction.

I was considering keeping NonceManager in contracts package so that CurrentNonce() and IncrementNonce() (especially this one) are not accessible from anywhere else than contracts but it does not look right to me that the chain would have to create contracts.NonceManager and pass it to each contract as a constructor parameter. Given that we have the error resolver in ethutil, NonceManager fits better in ethutil IMO.

pdyraga added 2 commits May 12, 2020 14:44
Nonce manager is used by the generated contracts to track the current
nonce and update it after each successfully submitted transaction.
Tracking the nonce locally is required when transactions are submitted
from multiple goroutines for when Ethereum clients are deployed behind a
load balancer and their mempools are not always in sync.
pdyraga added 3 commits May 12, 2020 17:08
We want to be able to recover from a situation when mempool crashes. To
achieve it, we cache the last local nonce value only for 5 seconds.
@nkuba nkuba merged commit 8fbb5b3 into master May 13, 2020
@nkuba nkuba deleted the nonce-management branch May 13, 2020 10:38
nkuba added a commit to keep-network/keep-core that referenced this pull request May 13, 2020
See keep-network/keep-common#35

The local nonce manager from keep-common is used by the generated contracts to track the current nonce and update it after each successfully submitted transaction. Tracking the nonce locally is required when transactions are submitted from multiple goroutines or when Ethereum clients are deployed behind a load balancer and their mempools are not always in sync. The nonce manager is used by the generated contracts to track the current nonce and update it after each successfully submitted transaction. Tracking the nonce locally is required when transactions are submitted from multiple goroutines for when Ethereum clients are deployed behind a load balancer and their mempools are not always in sync.

In our case, we are having problems with ticket submission against both Infura and Alchemy, and we have a feeling that all load-balanced Ethereum APIs are affected. To be 100% safe, we need to handle nonce on our side.
@pdyraga pdyraga added this to the 1.0.0 milestone May 17, 2020
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.

2 participants