journal.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. deleteAccountChange 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. )
  63. func (ch createObjectChange) undo(s *StateDB) {
  64. s.GetStateObject(*ch.account).deleted = true
  65. delete(s.stateObjects, *ch.account)
  66. delete(s.stateObjectsDirty, *ch.account)
  67. }
  68. func (ch resetObjectChange) undo(s *StateDB) {
  69. s.setStateObject(ch.prev)
  70. }
  71. func (ch deleteAccountChange) undo(s *StateDB) {
  72. obj := s.GetStateObject(*ch.account)
  73. if obj != nil {
  74. obj.remove = ch.prev
  75. obj.setBalance(ch.prevbalance)
  76. }
  77. }
  78. func (ch balanceChange) undo(s *StateDB) {
  79. s.GetStateObject(*ch.account).setBalance(ch.prev)
  80. }
  81. func (ch nonceChange) undo(s *StateDB) {
  82. s.GetStateObject(*ch.account).setNonce(ch.prev)
  83. }
  84. func (ch codeChange) undo(s *StateDB) {
  85. s.GetStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
  86. }
  87. func (ch storageChange) undo(s *StateDB) {
  88. s.GetStateObject(*ch.account).setState(ch.key, ch.prevalue)
  89. }
  90. func (ch refundChange) undo(s *StateDB) {
  91. s.refund = ch.prev
  92. }
  93. func (ch addLogChange) undo(s *StateDB) {
  94. logs := s.logs[ch.txhash]
  95. if len(logs) == 1 {
  96. delete(s.logs, ch.txhash)
  97. } else {
  98. s.logs[ch.txhash] = logs[:len(logs)-1]
  99. }
  100. }