|
| 1 | +package ethutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/ethereum/go-ethereum/accounts/abi/bind" |
| 7 | + "github.com/ethereum/go-ethereum/common" |
| 8 | +) |
| 9 | + |
| 10 | +// NonceManager tracks the nonce for the account and allows to update it after |
| 11 | +// each successfully submitted transaction. Tracking the nonce locall is |
| 12 | +// required when transactions are submitted from multiple goroutines or when |
| 13 | +// multiple Ethereum clients are deployed behind a load balancer, there are no |
| 14 | +// sticky sessions and mempool synchronization between them takes some time. |
| 15 | +// |
| 16 | +// NonceManager provides no synchronization and is NOT safe for concurrent use. |
| 17 | +// It is up to the client code to implement the required synchronization. |
| 18 | +// |
| 19 | +// An example execution might work as follows: |
| 20 | +// 1. Obtain transaction lock, |
| 21 | +// 2. Calculate CurrentNonce(), |
| 22 | +// 3. Submit transaction with the calculated nonce, |
| 23 | +// 4. Call IncrementNonce(), |
| 24 | +// 5. Release transaction lock. |
| 25 | +type NonceManager struct { |
| 26 | + account common.Address |
| 27 | + transactor bind.ContractTransactor |
| 28 | + localNonce uint64 |
| 29 | +} |
| 30 | + |
| 31 | +// NewNonceManager creates NonceManager instance for the provided account using |
| 32 | +// the provided contract transactor. Contract transactor is used for every |
| 33 | +// CurrentNonce execution to check the pending nonce value as seen by the |
| 34 | +// Ethereum client. |
| 35 | +func NewNonceManager( |
| 36 | + account common.Address, |
| 37 | + transactor bind.ContractTransactor, |
| 38 | +) *NonceManager { |
| 39 | + return &NonceManager{ |
| 40 | + account: account, |
| 41 | + transactor: transactor, |
| 42 | + localNonce: 0, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// CurrentNonce returns the nonce value that should be used for the next |
| 47 | +// transaction. The nonce is evaluated as the higher value from the local |
| 48 | +// nonce and pending nonce fetched from the Ethereum client. |
| 49 | +// |
| 50 | +// CurrentNonce is NOT safe for concurrent use. It is up to the code using this |
| 51 | +// function to provide the required synchronization, optionally including |
| 52 | +// IncrementNonce call as well. |
| 53 | +func (nm *NonceManager) CurrentNonce() (uint64, error) { |
| 54 | + pendingNonce, err := nm.transactor.PendingNonceAt( |
| 55 | + context.TODO(), |
| 56 | + nm.account, |
| 57 | + ) |
| 58 | + if err != nil { |
| 59 | + return 0, err |
| 60 | + } |
| 61 | + |
| 62 | + if pendingNonce < nm.localNonce { |
| 63 | + logger.Infof( |
| 64 | + "local nonce [%v] is higher than pending [%v]; using the local one", |
| 65 | + nm.localNonce, |
| 66 | + pendingNonce, |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + if pendingNonce > nm.localNonce { |
| 71 | + logger.Infof( |
| 72 | + "local nonce [%v] is lower than pending [%v]; updating", |
| 73 | + nm.localNonce, |
| 74 | + pendingNonce, |
| 75 | + ) |
| 76 | + |
| 77 | + nm.localNonce = pendingNonce |
| 78 | + } |
| 79 | + |
| 80 | + return nm.localNonce, nil |
| 81 | +} |
| 82 | + |
| 83 | +// IncrementNonce increments the value of the nonce kept locally by one. |
| 84 | +// This function is NOT safe for concurrent use. It is up to the client code |
| 85 | +// using this function to provide the required synchronization. |
| 86 | +func (nm *NonceManager) IncrementNonce() uint64 { |
| 87 | + nm.localNonce++ |
| 88 | + return nm.localNonce |
| 89 | +} |
0 commit comments