journal.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Copyright 2016 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. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. )
  21. // journalEntry is a modification entry in the state change journal that can be
  22. // reverted on demand.
  23. type journalEntry interface {
  24. // revert undoes the changes introduced by this journal entry.
  25. revert(*StateDB)
  26. // dirtied returns the Ethereum address modified by this journal entry.
  27. dirtied() *common.Address
  28. }
  29. // journal contains the list of state modifications applied since the last state
  30. // commit. These are tracked to be able to be reverted in the case of an execution
  31. // exception or request for reversal.
  32. type journal struct {
  33. entries []journalEntry // Current changes tracked by the journal
  34. dirties map[common.Address]int // Dirty accounts and the number of changes
  35. }
  36. // newJournal creates a new initialized journal.
  37. func newJournal() *journal {
  38. return &journal{
  39. dirties: make(map[common.Address]int),
  40. }
  41. }
  42. // append inserts a new modification entry to the end of the change journal.
  43. func (j *journal) append(entry journalEntry) {
  44. j.entries = append(j.entries, entry)
  45. if addr := entry.dirtied(); addr != nil {
  46. j.dirties[*addr]++
  47. }
  48. }
  49. // revert undoes a batch of journalled modifications along with any reverted
  50. // dirty handling too.
  51. func (j *journal) revert(statedb *StateDB, snapshot int) {
  52. for i := len(j.entries) - 1; i >= snapshot; i-- {
  53. // Undo the changes made by the operation
  54. j.entries[i].revert(statedb)
  55. // Drop any dirty tracking induced by the change
  56. if addr := j.entries[i].dirtied(); addr != nil {
  57. if j.dirties[*addr]--; j.dirties[*addr] == 0 {
  58. delete(j.dirties, *addr)
  59. }
  60. }
  61. }
  62. j.entries = j.entries[:snapshot]
  63. }
  64. // dirty explicitly sets an address to dirty, even if the change entries would
  65. // otherwise suggest it as clean. This method is an ugly hack to handle the RIPEMD
  66. // precompile consensus exception.
  67. func (j *journal) dirty(addr common.Address) {
  68. j.dirties[addr]++
  69. }
  70. // length returns the current number of entries in the journal.
  71. func (j *journal) length() int {
  72. return len(j.entries)
  73. }
  74. type (
  75. // Changes to the account trie.
  76. createObjectChange struct {
  77. account *common.Address
  78. }
  79. resetObjectChange struct {
  80. prev *stateObject
  81. prevdestruct bool
  82. }
  83. suicideChange struct {
  84. account *common.Address
  85. prev bool // whether account had already suicided
  86. prevbalance *big.Int
  87. }
  88. // Changes to individual accounts.
  89. balanceChange struct {
  90. account *common.Address
  91. prev *big.Int
  92. }
  93. nonceChange struct {
  94. account *common.Address
  95. prev uint64
  96. }
  97. storageChange struct {
  98. account *common.Address
  99. key, prevalue common.Hash
  100. }
  101. codeChange struct {
  102. account *common.Address
  103. prevcode, prevhash []byte
  104. }
  105. // Changes to other state values.
  106. refundChange struct {
  107. prev uint64
  108. }
  109. addLogChange struct {
  110. txhash common.Hash
  111. }
  112. addPreimageChange struct {
  113. hash common.Hash
  114. }
  115. touchChange struct {
  116. account *common.Address
  117. }
  118. // Changes to the access list
  119. accessListAddAccountChange struct {
  120. address *common.Address
  121. }
  122. accessListAddSlotChange struct {
  123. address *common.Address
  124. slot *common.Hash
  125. }
  126. )
  127. func (ch createObjectChange) revert(s *StateDB) {
  128. delete(s.stateObjects, *ch.account)
  129. delete(s.stateObjectsDirty, *ch.account)
  130. }
  131. func (ch createObjectChange) dirtied() *common.Address {
  132. return ch.account
  133. }
  134. func (ch resetObjectChange) revert(s *StateDB) {
  135. s.setStateObject(ch.prev)
  136. if !ch.prevdestruct && s.snap != nil {
  137. delete(s.snapDestructs, ch.prev.addrHash)
  138. }
  139. }
  140. func (ch resetObjectChange) dirtied() *common.Address {
  141. return nil
  142. }
  143. func (ch suicideChange) revert(s *StateDB) {
  144. obj := s.getStateObject(*ch.account)
  145. if obj != nil {
  146. obj.suicided = ch.prev
  147. obj.setBalance(ch.prevbalance)
  148. }
  149. }
  150. func (ch suicideChange) dirtied() *common.Address {
  151. return ch.account
  152. }
  153. var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
  154. func (ch touchChange) revert(s *StateDB) {
  155. }
  156. func (ch touchChange) dirtied() *common.Address {
  157. return ch.account
  158. }
  159. func (ch balanceChange) revert(s *StateDB) {
  160. s.getStateObject(*ch.account).setBalance(ch.prev)
  161. }
  162. func (ch balanceChange) dirtied() *common.Address {
  163. return ch.account
  164. }
  165. func (ch nonceChange) revert(s *StateDB) {
  166. s.getStateObject(*ch.account).setNonce(ch.prev)
  167. }
  168. func (ch nonceChange) dirtied() *common.Address {
  169. return ch.account
  170. }
  171. func (ch codeChange) revert(s *StateDB) {
  172. s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
  173. }
  174. func (ch codeChange) dirtied() *common.Address {
  175. return ch.account
  176. }
  177. func (ch storageChange) revert(s *StateDB) {
  178. s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
  179. }
  180. func (ch storageChange) dirtied() *common.Address {
  181. return ch.account
  182. }
  183. func (ch refundChange) revert(s *StateDB) {
  184. s.refund = ch.prev
  185. }
  186. func (ch refundChange) dirtied() *common.Address {
  187. return nil
  188. }
  189. func (ch addLogChange) revert(s *StateDB) {
  190. logs := s.logs[ch.txhash]
  191. if len(logs) == 1 {
  192. delete(s.logs, ch.txhash)
  193. } else {
  194. s.logs[ch.txhash] = logs[:len(logs)-1]
  195. }
  196. s.logSize--
  197. }
  198. func (ch addLogChange) dirtied() *common.Address {
  199. return nil
  200. }
  201. func (ch addPreimageChange) revert(s *StateDB) {
  202. delete(s.preimages, ch.hash)
  203. }
  204. func (ch addPreimageChange) dirtied() *common.Address {
  205. return nil
  206. }
  207. func (ch accessListAddAccountChange) revert(s *StateDB) {
  208. /*
  209. One important invariant here, is that whenever a (addr, slot) is added, if the
  210. addr is not already present, the add causes two journal entries:
  211. - one for the address,
  212. - one for the (address,slot)
  213. Therefore, when unrolling the change, we can always blindly delete the
  214. (addr) at this point, since no storage adds can remain when come upon
  215. a single (addr) change.
  216. */
  217. s.accessList.DeleteAddress(*ch.address)
  218. }
  219. func (ch accessListAddAccountChange) dirtied() *common.Address {
  220. return nil
  221. }
  222. func (ch accessListAddSlotChange) revert(s *StateDB) {
  223. s.accessList.DeleteSlot(*ch.address, *ch.slot)
  224. }
  225. func (ch accessListAddSlotChange) dirtied() *common.Address {
  226. return nil
  227. }