journal.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. }
  24. type journal []journalEntry
  25. type (
  26. // Changes to the account trie.
  27. createObjectChange struct {
  28. account *common.Address
  29. }
  30. resetObjectChange struct {
  31. prev *stateObject
  32. }
  33. suicideChange struct {
  34. account *common.Address
  35. prev bool // whether account had already suicided
  36. prevbalance *big.Int
  37. }
  38. // Changes to individual accounts.
  39. balanceChange struct {
  40. account *common.Address
  41. prev *big.Int
  42. }
  43. nonceChange struct {
  44. account *common.Address
  45. prev uint64
  46. }
  47. storageChange struct {
  48. account *common.Address
  49. key, prevalue common.Hash
  50. }
  51. codeChange struct {
  52. account *common.Address
  53. prevcode, prevhash []byte
  54. }
  55. // Changes to other state values.
  56. refundChange struct {
  57. prev *big.Int
  58. }
  59. addLogChange struct {
  60. txhash common.Hash
  61. }
  62. addPreimageChange struct {
  63. hash common.Hash
  64. }
  65. touchChange struct {
  66. account *common.Address
  67. prev bool
  68. prevDirty bool
  69. }
  70. )
  71. func (ch createObjectChange) undo(s *StateDB) {
  72. delete(s.stateObjects, *ch.account)
  73. delete(s.stateObjectsDirty, *ch.account)
  74. }
  75. func (ch resetObjectChange) undo(s *StateDB) {
  76. s.setStateObject(ch.prev)
  77. }
  78. func (ch suicideChange) undo(s *StateDB) {
  79. obj := s.getStateObject(*ch.account)
  80. if obj != nil {
  81. obj.suicided = ch.prev
  82. obj.setBalance(ch.prevbalance)
  83. // if the object wasn't suicided before, remove
  84. // it from the list of destructed objects as well.
  85. if !obj.suicided {
  86. delete(s.stateObjectsDestructed, *ch.account)
  87. }
  88. }
  89. }
  90. var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
  91. func (ch touchChange) undo(s *StateDB) {
  92. if !ch.prev && *ch.account != ripemd {
  93. s.getStateObject(*ch.account).touched = ch.prev
  94. if !ch.prevDirty {
  95. delete(s.stateObjectsDirty, *ch.account)
  96. }
  97. }
  98. }
  99. func (ch balanceChange) undo(s *StateDB) {
  100. s.getStateObject(*ch.account).setBalance(ch.prev)
  101. }
  102. func (ch nonceChange) undo(s *StateDB) {
  103. s.getStateObject(*ch.account).setNonce(ch.prev)
  104. }
  105. func (ch codeChange) undo(s *StateDB) {
  106. s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
  107. }
  108. func (ch storageChange) undo(s *StateDB) {
  109. s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
  110. }
  111. func (ch refundChange) undo(s *StateDB) {
  112. s.refund = ch.prev
  113. }
  114. func (ch addLogChange) undo(s *StateDB) {
  115. logs := s.logs[ch.txhash]
  116. if len(logs) == 1 {
  117. delete(s.logs, ch.txhash)
  118. } else {
  119. s.logs[ch.txhash] = logs[:len(logs)-1]
  120. }
  121. }
  122. func (ch addPreimageChange) undo(s *StateDB) {
  123. delete(s.preimages, ch.hash)
  124. }