managed_state.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. 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[string]*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[string]*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 unmanged account
  71. func (ms *ManagedState) GetNonce(addr common.Address) uint64 {
  72. ms.mu.RLock()
  73. defer ms.mu.RUnlock()
  74. if ms.hasAccount(addr) {
  75. account := ms.getAccount(addr)
  76. return uint64(len(account.nonces)) + account.nstart
  77. } else {
  78. return ms.StateDB.GetNonce(addr)
  79. }
  80. }
  81. // SetNonce sets the new canonical nonce for the managed state
  82. func (ms *ManagedState) SetNonce(addr common.Address, nonce uint64) {
  83. ms.mu.Lock()
  84. defer ms.mu.Unlock()
  85. so := ms.GetOrNewStateObject(addr)
  86. so.SetNonce(nonce)
  87. ms.accounts[addr.Str()] = newAccount(so)
  88. }
  89. // HasAccount returns whether the given address is managed or not
  90. func (ms *ManagedState) HasAccount(addr common.Address) bool {
  91. ms.mu.RLock()
  92. defer ms.mu.RUnlock()
  93. return ms.hasAccount(addr)
  94. }
  95. func (ms *ManagedState) hasAccount(addr common.Address) bool {
  96. _, ok := ms.accounts[addr.Str()]
  97. return ok
  98. }
  99. // populate the managed state
  100. func (ms *ManagedState) getAccount(addr common.Address) *account {
  101. straddr := addr.Str()
  102. if account, ok := ms.accounts[straddr]; !ok {
  103. so := ms.GetOrNewStateObject(addr)
  104. ms.accounts[straddr] = newAccount(so)
  105. } else {
  106. // Always make sure the state account nonce isn't actually higher
  107. // than the tracked one.
  108. so := ms.StateDB.GetStateObject(addr)
  109. if so != nil && uint64(len(account.nonces))+account.nstart < so.nonce {
  110. ms.accounts[straddr] = newAccount(so)
  111. }
  112. }
  113. return ms.accounts[straddr]
  114. }
  115. func newAccount(so *StateObject) *account {
  116. return &account{so, so.nonce, nil}
  117. }