|
| 1 | +package ethutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/accounts/abi/bind" |
| 8 | + "github.com/ethereum/go-ethereum/common" |
| 9 | +) |
| 10 | + |
| 11 | +// The inactivity time after which the local nonce is refreshed with the value |
| 12 | +// from the chain. The local value is invalidated after the certain duration to |
| 13 | +// let the nonce recover in case the mempool crashed before propagating the last |
| 14 | +// transaction sent. |
| 15 | +const localNonceTrustDuration = 5 * time.Second |
| 16 | + |
| 17 | +// NonceManager tracks the nonce for the account and allows to update it after |
| 18 | +// each successfully submitted transaction. Tracking the nonce locally is |
| 19 | +// required when transactions are submitted from multiple goroutines or when |
| 20 | +// multiple Ethereum clients are deployed behind a load balancer, there are no |
| 21 | +// sticky sessions and mempool synchronization between them takes some time. |
| 22 | +// |
| 23 | +// NonceManager provides no synchronization and is NOT safe for concurrent use. |
| 24 | +// It is up to the client code to implement the required synchronization. |
| 25 | +// |
| 26 | +// An example execution might work as follows: |
| 27 | +// 1. Obtain transaction lock, |
| 28 | +// 2. Calculate CurrentNonce(), |
| 29 | +// 3. Submit transaction with the calculated nonce, |
| 30 | +// 4. Call IncrementNonce(), |
| 31 | +// 5. Release transaction lock. |
| 32 | +type NonceManager struct { |
| 33 | + account common.Address |
| 34 | + transactor bind.ContractTransactor |
| 35 | + localNonce uint64 |
| 36 | + expirationDate time.Time |
| 37 | +} |
| 38 | + |
| 39 | +// NewNonceManager creates NonceManager instance for the provided account using |
| 40 | +// the provided contract transactor. Contract transactor is used for every |
| 41 | +// CurrentNonce execution to check the pending nonce value as seen by the |
| 42 | +// Ethereum client. |
| 43 | +func NewNonceManager( |
| 44 | + account common.Address, |
| 45 | + transactor bind.ContractTransactor, |
| 46 | +) *NonceManager { |
| 47 | + return &NonceManager{ |
| 48 | + account: account, |
| 49 | + transactor: transactor, |
| 50 | + localNonce: 0, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// CurrentNonce returns the nonce value that should be used for the next |
| 55 | +// transaction. The nonce is evaluated as the higher value from the local |
| 56 | +// nonce and pending nonce fetched from the Ethereum client. The local nonce |
| 57 | +// is cached for the specific duration. If the local nonce expired, the pending |
| 58 | +// nonce returned from the chain is used. |
| 59 | +// |
| 60 | +// CurrentNonce is NOT safe for concurrent use. It is up to the code using this |
| 61 | +// function to provide the required synchronization, optionally including |
| 62 | +// IncrementNonce call as well. |
| 63 | +func (nm *NonceManager) CurrentNonce() (uint64, error) { |
| 64 | + pendingNonce, err := nm.transactor.PendingNonceAt( |
| 65 | + context.TODO(), |
| 66 | + nm.account, |
| 67 | + ) |
| 68 | + if err != nil { |
| 69 | + return 0, err |
| 70 | + } |
| 71 | + |
| 72 | + now := time.Now() |
| 73 | + |
| 74 | + if pendingNonce < nm.localNonce { |
| 75 | + if now.Before(nm.expirationDate) { |
| 76 | + logger.Infof( |
| 77 | + "local nonce [%v] is higher than pending [%v]; using the local one", |
| 78 | + nm.localNonce, |
| 79 | + pendingNonce, |
| 80 | + ) |
| 81 | + } else { |
| 82 | + logger.Infof( |
| 83 | + "local nonce [%v] is higher than pending [%v] but local "+ |
| 84 | + "nonce expired; updating local nonce", |
| 85 | + nm.localNonce, |
| 86 | + pendingNonce, |
| 87 | + ) |
| 88 | + |
| 89 | + nm.localNonce = pendingNonce |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // After localNonceTrustDuration of inactivity (no CurrentNonce() calls), |
| 94 | + // the local copy is considered as no longer up-to-date and it's always |
| 95 | + // reset to the pending nonce value as seen by the chain. |
| 96 | + // |
| 97 | + // We do it to recover from potential mempool crashes. |
| 98 | + // |
| 99 | + // Keep in mind, the local copy is considered valid as long as transactions |
| 100 | + // are submitted one after another. |
| 101 | + nm.expirationDate = now.Add(localNonceTrustDuration) |
| 102 | + |
| 103 | + if pendingNonce > nm.localNonce { |
| 104 | + logger.Infof( |
| 105 | + "local nonce [%v] is lower than pending [%v]; updating local nonce", |
| 106 | + nm.localNonce, |
| 107 | + pendingNonce, |
| 108 | + ) |
| 109 | + |
| 110 | + nm.localNonce = pendingNonce |
| 111 | + } |
| 112 | + |
| 113 | + return nm.localNonce, nil |
| 114 | +} |
| 115 | + |
| 116 | +// IncrementNonce increments the value of the nonce kept locally by one. |
| 117 | +// This function is NOT safe for concurrent use. It is up to the client code |
| 118 | +// using this function to provide the required synchronization. |
| 119 | +func (nm *NonceManager) IncrementNonce() uint64 { |
| 120 | + nm.localNonce++ |
| 121 | + return nm.localNonce |
| 122 | +} |
0 commit comments