journal.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. type journalEntry interface {
  22. undo(*StateDB)
  23. getAccount() *common.Address
  24. }
  25. type journal struct {
  26. entries []journalEntry
  27. dirtyOverrides []common.Address
  28. }
  29. func (j *journal) append(entry journalEntry) {
  30. j.entries = append(j.entries, entry)
  31. }
  32. func (j *journal) flatten() map[common.Address]struct{} {
  33. dirtyObjects := make(map[common.Address]struct{})
  34. for _, journalEntry := range j.entries {
  35. if addr := journalEntry.getAccount(); addr != nil {
  36. dirtyObjects[*addr] = struct{}{}
  37. }
  38. }
  39. for _, addr := range j.dirtyOverrides {
  40. dirtyObjects[addr] = struct{}{}
  41. }
  42. return dirtyObjects
  43. }
  44. // Length returns the number of journal entries in the journal
  45. func (j *journal) Length() int {
  46. return len(j.entries)
  47. }
  48. func (j *journal) dirtyOverride(address common.Address) {
  49. j.dirtyOverrides = append(j.dirtyOverrides, address)
  50. }
  51. type (
  52. // Changes to the account trie.
  53. createObjectChange struct {
  54. account *common.Address
  55. }
  56. resetObjectChange struct {
  57. prev *stateObject
  58. }
  59. suicideChange struct {
  60. account *common.Address
  61. prev bool // whether account had already suicided
  62. prevbalance *big.Int
  63. }
  64. // Changes to individual accounts.
  65. balanceChange struct {
  66. account *common.Address
  67. prev *big.Int
  68. }
  69. nonceChange struct {
  70. account *common.Address
  71. prev uint64
  72. }
  73. storageChange struct {
  74. account *common.Address
  75. key, prevalue common.Hash
  76. }
  77. codeChange struct {
  78. account *common.Address
  79. prevcode, prevhash []byte
  80. }
  81. // Changes to other state values.
  82. refundChange struct {
  83. prev uint64
  84. }
  85. addLogChange struct {
  86. txhash common.Hash
  87. }
  88. addPreimageChange struct {
  89. hash common.Hash
  90. }
  91. touchChange struct {
  92. account *common.Address
  93. prev bool
  94. prevDirty bool
  95. }
  96. )
  97. func (ch createObjectChange) undo(s *StateDB) {
  98. delete(s.stateObjects, *ch.account)
  99. delete(s.stateObjectsDirty, *ch.account)
  100. }
  101. func (ch createObjectChange) getAccount() *common.Address {
  102. return ch.account
  103. }
  104. func (ch resetObjectChange) undo(s *StateDB) {
  105. s.setStateObject(ch.prev)
  106. }
  107. func (ch resetObjectChange) getAccount() *common.Address {
  108. return nil
  109. }
  110. func (ch suicideChange) undo(s *StateDB) {
  111. obj := s.getStateObject(*ch.account)
  112. if obj != nil {
  113. obj.suicided = ch.prev
  114. obj.setBalance(ch.prevbalance)
  115. }
  116. }
  117. func (ch suicideChange) getAccount() *common.Address {
  118. return ch.account
  119. }
  120. var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
  121. func (ch touchChange) undo(s *StateDB) {
  122. }
  123. func (ch touchChange) getAccount() *common.Address {
  124. return ch.account
  125. }
  126. func (ch balanceChange) undo(s *StateDB) {
  127. s.getStateObject(*ch.account).setBalance(ch.prev)
  128. }
  129. func (ch balanceChange) getAccount() *common.Address {
  130. return ch.account
  131. }
  132. func (ch nonceChange) undo(s *StateDB) {
  133. s.getStateObject(*ch.account).setNonce(ch.prev)
  134. }
  135. func (ch nonceChange) getAccount() *common.Address {
  136. return ch.account
  137. }
  138. func (ch codeChange) undo(s *StateDB) {
  139. s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
  140. }
  141. func (ch codeChange) getAccount() *common.Address {
  142. return ch.account
  143. }
  144. func (ch storageChange) undo(s *StateDB) {
  145. s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
  146. }
  147. func (ch storageChange) getAccount() *common.Address {
  148. return ch.account
  149. }
  150. func (ch refundChange) undo(s *StateDB) {
  151. s.refund = ch.prev
  152. }
  153. func (ch refundChange) getAccount() *common.Address {
  154. return nil
  155. }
  156. func (ch addLogChange) undo(s *StateDB) {
  157. logs := s.logs[ch.txhash]
  158. if len(logs) == 1 {
  159. delete(s.logs, ch.txhash)
  160. } else {
  161. s.logs[ch.txhash] = logs[:len(logs)-1]
  162. }
  163. s.logSize--
  164. }
  165. func (ch addLogChange) getAccount() *common.Address {
  166. return nil
  167. }
  168. func (ch addPreimageChange) undo(s *StateDB) {
  169. delete(s.preimages, ch.hash)
  170. }
  171. func (ch addPreimageChange) getAccount() *common.Address {
  172. return nil
  173. }