tx_noncer.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "sync"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/state"
  21. )
  22. // txNoncer is a tiny virtual state database to manage the executable nonces of
  23. // accounts in the pool, falling back to reading from a real state database if
  24. // an account is unknown.
  25. type txNoncer struct {
  26. fallback *state.StateDB
  27. nonces map[common.Address]uint64
  28. lock sync.Mutex
  29. }
  30. // newTxNoncer creates a new virtual state database to track the pool nonces.
  31. func newTxNoncer(statedb *state.StateDB) *txNoncer {
  32. return &txNoncer{
  33. fallback: statedb.Copy(),
  34. nonces: make(map[common.Address]uint64),
  35. }
  36. }
  37. // get returns the current nonce of an account, falling back to a real state
  38. // database if the account is unknown.
  39. func (txn *txNoncer) get(addr common.Address) uint64 {
  40. // We use mutex for get operation is the underlying
  41. // state will mutate db even for read access.
  42. txn.lock.Lock()
  43. defer txn.lock.Unlock()
  44. if _, ok := txn.nonces[addr]; !ok {
  45. txn.nonces[addr] = txn.fallback.GetNonce(addr)
  46. }
  47. return txn.nonces[addr]
  48. }
  49. // set inserts a new virtual nonce into the virtual state database to be returned
  50. // whenever the pool requests it instead of reaching into the real state database.
  51. func (txn *txNoncer) set(addr common.Address, nonce uint64) {
  52. txn.lock.Lock()
  53. defer txn.lock.Unlock()
  54. txn.nonces[addr] = nonce
  55. }
  56. // setIfLower updates a new virtual nonce into the virtual state database if the
  57. // the new one is lower.
  58. func (txn *txNoncer) setIfLower(addr common.Address, nonce uint64) {
  59. txn.lock.Lock()
  60. defer txn.lock.Unlock()
  61. if _, ok := txn.nonces[addr]; !ok {
  62. txn.nonces[addr] = txn.fallback.GetNonce(addr)
  63. }
  64. if txn.nonces[addr] <= nonce {
  65. return
  66. }
  67. txn.nonces[addr] = nonce
  68. }