github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/state/journal.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package state 19 20 import ( 21 "math/big" 22 23 "github.com/AigarNetwork/aigar/common" 24 ) 25 26 // journalEntry is a modification entry in the state change journal that can be 27 // reverted on demand. 28 type journalEntry interface { 29 // revert undoes the changes introduced by this journal entry. 30 revert(*StateDB) 31 32 // dirtied returns the Ethereum address modified by this journal entry. 33 dirtied() *common.Address 34 } 35 36 // journal contains the list of state modifications applied since the last state 37 // commit. These are tracked to be able to be reverted in case of an execution 38 // exception or revertal request. 39 type journal struct { 40 entries []journalEntry // Current changes tracked by the journal 41 dirties map[common.Address]int // Dirty accounts and the number of changes 42 } 43 44 // newJournal create a new initialized journal. 45 func newJournal() *journal { 46 return &journal{ 47 dirties: make(map[common.Address]int), 48 } 49 } 50 51 // append inserts a new modification entry to the end of the change journal. 52 func (j *journal) append(entry journalEntry) { 53 j.entries = append(j.entries, entry) 54 if addr := entry.dirtied(); addr != nil { 55 j.dirties[*addr]++ 56 } 57 } 58 59 // revert undoes a batch of journalled modifications along with any reverted 60 // dirty handling too. 61 func (j *journal) revert(statedb *StateDB, snapshot int) { 62 for i := len(j.entries) - 1; i >= snapshot; i-- { 63 // Undo the changes made by the operation 64 j.entries[i].revert(statedb) 65 66 // Drop any dirty tracking induced by the change 67 if addr := j.entries[i].dirtied(); addr != nil { 68 if j.dirties[*addr]--; j.dirties[*addr] == 0 { 69 delete(j.dirties, *addr) 70 } 71 } 72 } 73 j.entries = j.entries[:snapshot] 74 } 75 76 // dirty explicitly sets an address to dirty, even if the change entries would 77 // otherwise suggest it as clean. This method is an ugly hack to handle the RIPEMD 78 // precompile consensus exception. 79 func (j *journal) dirty(addr common.Address) { 80 j.dirties[addr]++ 81 } 82 83 // length returns the current number of entries in the journal. 84 func (j *journal) length() int { 85 return len(j.entries) 86 } 87 88 type ( 89 // Changes to the account trie. 90 createObjectChange struct { 91 account *common.Address 92 } 93 resetObjectChange struct { 94 prev *stateObject 95 } 96 suicideChange struct { 97 account *common.Address 98 prev bool // whether account had already suicided 99 prevbalance *big.Int 100 } 101 102 // Changes to individual accounts. 103 balanceChange struct { 104 account *common.Address 105 prev *big.Int 106 } 107 nonceChange struct { 108 account *common.Address 109 prev uint64 110 } 111 storageChange struct { 112 account *common.Address 113 key, prevalue common.Hash 114 } 115 codeChange struct { 116 account *common.Address 117 prevcode, prevhash []byte 118 } 119 120 // Changes to other state values. 121 refundChange struct { 122 prev uint64 123 } 124 addLogChange struct { 125 txhash common.Hash 126 } 127 addPreimageChange struct { 128 hash common.Hash 129 } 130 touchChange struct { 131 account *common.Address 132 prev bool 133 prevDirty bool 134 } 135 ) 136 137 func (ch createObjectChange) revert(s *StateDB) { 138 delete(s.stateObjects, *ch.account) 139 delete(s.stateObjectsDirty, *ch.account) 140 } 141 142 func (ch createObjectChange) dirtied() *common.Address { 143 return ch.account 144 } 145 146 func (ch resetObjectChange) revert(s *StateDB) { 147 s.setStateObject(ch.prev) 148 } 149 150 func (ch resetObjectChange) dirtied() *common.Address { 151 return nil 152 } 153 154 func (ch suicideChange) revert(s *StateDB) { 155 obj := s.getStateObject(*ch.account) 156 if obj != nil { 157 obj.suicided = ch.prev 158 obj.setBalance(ch.prevbalance) 159 } 160 } 161 162 func (ch suicideChange) dirtied() *common.Address { 163 return ch.account 164 } 165 166 var ripemd = common.HexToAddress("0000000000000000000000000000000000000003") 167 168 func (ch touchChange) revert(s *StateDB) { 169 } 170 171 func (ch touchChange) dirtied() *common.Address { 172 return ch.account 173 } 174 175 func (ch balanceChange) revert(s *StateDB) { 176 s.getStateObject(*ch.account).setBalance(ch.prev) 177 } 178 179 func (ch balanceChange) dirtied() *common.Address { 180 return ch.account 181 } 182 183 func (ch nonceChange) revert(s *StateDB) { 184 s.getStateObject(*ch.account).setNonce(ch.prev) 185 } 186 187 func (ch nonceChange) dirtied() *common.Address { 188 return ch.account 189 } 190 191 func (ch codeChange) revert(s *StateDB) { 192 s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode) 193 } 194 195 func (ch codeChange) dirtied() *common.Address { 196 return ch.account 197 } 198 199 func (ch storageChange) revert(s *StateDB) { 200 s.getStateObject(*ch.account).setState(ch.key, ch.prevalue) 201 } 202 203 func (ch storageChange) dirtied() *common.Address { 204 return ch.account 205 } 206 207 func (ch refundChange) revert(s *StateDB) { 208 s.refund = ch.prev 209 } 210 211 func (ch refundChange) dirtied() *common.Address { 212 return nil 213 } 214 215 func (ch addLogChange) revert(s *StateDB) { 216 logs := s.logs[ch.txhash] 217 if len(logs) == 1 { 218 delete(s.logs, ch.txhash) 219 } else { 220 s.logs[ch.txhash] = logs[:len(logs)-1] 221 } 222 s.logSize-- 223 } 224 225 func (ch addLogChange) dirtied() *common.Address { 226 return nil 227 } 228 229 func (ch addPreimageChange) revert(s *StateDB) { 230 delete(s.preimages, ch.hash) 231 } 232 233 func (ch addPreimageChange) dirtied() *common.Address { 234 return nil 235 }