managed_state.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2015 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 state
  17. import (
  18. "sync"
  19. "github.com/ethereum/go-ethereum/common"
  20. )
  21. type account struct {
  22. stateObject *stateObject
  23. nstart uint64
  24. nonces []bool
  25. }
  26. type ManagedState struct {
  27. *StateDB
  28. mu sync.RWMutex
  29. accounts map[common.Address]*account
  30. }
  31. // ManagedState returns a new managed state with the statedb as it's backing layer
  32. func ManageState(statedb *StateDB) *ManagedState {
  33. return &ManagedState{
  34. StateDB: statedb.Copy(),
  35. accounts: make(map[common.Address]*account),
  36. }
  37. }
  38. // SetState sets the backing layer of the managed state
  39. func (ms *ManagedState) SetState(statedb *StateDB) {
  40. ms.mu.Lock()
  41. defer ms.mu.Unlock()
  42. ms.StateDB = statedb
  43. }
  44. // RemoveNonce removed the nonce from the managed state and all future pending nonces
  45. func (ms *ManagedState) RemoveNonce(addr common.Address, n uint64) {
  46. if ms.hasAccount(addr) {
  47. ms.mu.Lock()
  48. defer ms.mu.Unlock()
  49. account := ms.getAccount(addr)
  50. if n-account.nstart <= uint64(len(account.nonces)) {
  51. reslice := make([]bool, n-account.nstart)
  52. copy(reslice, account.nonces[:n-account.nstart])
  53. account.nonces = reslice
  54. }
  55. }
  56. }
  57. // NewNonce returns the new canonical nonce for the managed account
  58. func (ms *ManagedState) NewNonce(addr common.Address) uint64 {
  59. ms.mu.Lock()
  60. defer ms.mu.Unlock()
  61. account := ms.getAccount(addr)
  62. for i, nonce := range account.nonces {
  63. if !nonce {
  64. return account.nstart + uint64(i)
  65. }
  66. }
  67. account.nonces = append(account.nonces, true)
  68. return uint64(len(account.nonces)-1) + account.nstart
  69. }
  70. // GetNonce returns the canonical nonce for the managed or unmanaged account.
  71. //
  72. // Because GetNonce mutates the DB, we must take a write lock.
  73. func (ms *ManagedState) GetNonce(addr common.Address) uint64 {
  74. ms.mu.Lock()
  75. defer ms.mu.Unlock()
  76. if ms.hasAccount(addr) {
  77. account := ms.getAccount(addr)
  78. return uint64(len(account.nonces)) + account.nstart
  79. } else {
  80. return ms.StateDB.GetNonce(addr)
  81. }
  82. }
  83. // SetNonce sets the new canonical nonce for the managed state
  84. func (ms *ManagedState) SetNonce(addr common.Address, nonce uint64) {
  85. ms.mu.Lock()
  86. defer ms.mu.Unlock()
  87. so := ms.GetOrNewStateObject(addr)
  88. so.SetNonce(nonce)
  89. ms.accounts[addr] = newAccount(so)
  90. }
  91. // HasAccount returns whether the given address is managed or not
  92. func (ms *ManagedState) HasAccount(addr common.Address) bool {
  93. ms.mu.RLock()
  94. defer ms.mu.RUnlock()
  95. return ms.hasAccount(addr)
  96. }
  97. func (ms *ManagedState) hasAccount(addr common.Address) bool {
  98. _, ok := ms.accounts[addr]
  99. return ok
  100. }
  101. // populate the managed state
  102. func (ms *ManagedState) getAccount(addr common.Address) *account {
  103. if account, ok := ms.accounts[addr]; !ok {
  104. so := ms.GetOrNewStateObject(addr)
  105. ms.accounts[addr] = newAccount(so)
  106. } else {
  107. // Always make sure the state account nonce isn't actually higher
  108. // than the tracked one.
  109. so := ms.StateDB.getStateObject(addr)
  110. if so != nil && uint64(len(account.nonces))+account.nstart < so.Nonce() {
  111. ms.accounts[addr] = newAccount(so)
  112. }
  113. }
  114. return ms.accounts[addr]
  115. }
  116. func newAccount(so *stateObject) *account {
  117. return &account{so, so.Nonce(), nil}
  118. }