journal.go 6.8 KB

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