journal.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. }
  69. )
  70. func (ch createObjectChange) undo(s *StateDB) {
  71. delete(s.stateObjects, *ch.account)
  72. delete(s.stateObjectsDirty, *ch.account)
  73. }
  74. func (ch resetObjectChange) undo(s *StateDB) {
  75. s.setStateObject(ch.prev)
  76. }
  77. func (ch suicideChange) undo(s *StateDB) {
  78. obj := s.GetStateObject(*ch.account)
  79. if obj != nil {
  80. obj.suicided = ch.prev
  81. obj.setBalance(ch.prevbalance)
  82. }
  83. }
  84. var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
  85. func (ch touchChange) undo(s *StateDB) {
  86. if !ch.prev && *ch.account != ripemd {
  87. delete(s.stateObjects, *ch.account)
  88. delete(s.stateObjectsDirty, *ch.account)
  89. }
  90. }
  91. func (ch balanceChange) undo(s *StateDB) {
  92. s.GetStateObject(*ch.account).setBalance(ch.prev)
  93. }
  94. func (ch nonceChange) undo(s *StateDB) {
  95. s.GetStateObject(*ch.account).setNonce(ch.prev)
  96. }
  97. func (ch codeChange) undo(s *StateDB) {
  98. s.GetStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
  99. }
  100. func (ch storageChange) undo(s *StateDB) {
  101. s.GetStateObject(*ch.account).setState(ch.key, ch.prevalue)
  102. }
  103. func (ch refundChange) undo(s *StateDB) {
  104. s.refund = ch.prev
  105. }
  106. func (ch addLogChange) undo(s *StateDB) {
  107. logs := s.logs[ch.txhash]
  108. if len(logs) == 1 {
  109. delete(s.logs, ch.txhash)
  110. } else {
  111. s.logs[ch.txhash] = logs[:len(logs)-1]
  112. }
  113. }
  114. func (ch addPreimageChange) undo(s *StateDB) {
  115. delete(s.preimages, ch.hash)
  116. }