github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/state/journal.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package state
    13  
    14  import (
    15  	"math/big"
    16  
    17  	"github.com/Sberex/go-sberex/common"
    18  )
    19  
    20  type journalEntry interface {
    21  	undo(*StateDB)
    22  }
    23  
    24  type journal []journalEntry
    25  
    26  type (
    27  	// Changes to the account trie.
    28  	createObjectChange struct {
    29  		account *common.Address
    30  	}
    31  	resetObjectChange struct {
    32  		prev *stateObject
    33  	}
    34  	suicideChange struct {
    35  		account     *common.Address
    36  		prev        bool // whether account had already suicided
    37  		prevbalance *big.Int
    38  	}
    39  
    40  	// Changes to individual accounts.
    41  	balanceChange struct {
    42  		account *common.Address
    43  		prev    *big.Int
    44  	}
    45  	nonceChange struct {
    46  		account *common.Address
    47  		prev    uint64
    48  	}
    49  	storageChange struct {
    50  		account       *common.Address
    51  		key, prevalue common.Hash
    52  	}
    53  	codeChange struct {
    54  		account            *common.Address
    55  		prevcode, prevhash []byte
    56  	}
    57  
    58  	// Changes to other state values.
    59  	refundChange struct {
    60  		prev uint64
    61  	}
    62  	addLogChange struct {
    63  		txhash common.Hash
    64  	}
    65  	addPreimageChange struct {
    66  		hash common.Hash
    67  	}
    68  	touchChange struct {
    69  		account   *common.Address
    70  		prev      bool
    71  		prevDirty bool
    72  	}
    73  )
    74  
    75  func (ch createObjectChange) undo(s *StateDB) {
    76  	delete(s.stateObjects, *ch.account)
    77  	delete(s.stateObjectsDirty, *ch.account)
    78  }
    79  
    80  func (ch resetObjectChange) undo(s *StateDB) {
    81  	s.setStateObject(ch.prev)
    82  }
    83  
    84  func (ch suicideChange) undo(s *StateDB) {
    85  	obj := s.getStateObject(*ch.account)
    86  	if obj != nil {
    87  		obj.suicided = ch.prev
    88  		obj.setBalance(ch.prevbalance)
    89  	}
    90  }
    91  
    92  var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
    93  
    94  func (ch touchChange) undo(s *StateDB) {
    95  	if !ch.prev && *ch.account != ripemd {
    96  		s.getStateObject(*ch.account).touched = ch.prev
    97  		if !ch.prevDirty {
    98  			delete(s.stateObjectsDirty, *ch.account)
    99  		}
   100  	}
   101  }
   102  
   103  func (ch balanceChange) undo(s *StateDB) {
   104  	s.getStateObject(*ch.account).setBalance(ch.prev)
   105  }
   106  
   107  func (ch nonceChange) undo(s *StateDB) {
   108  	s.getStateObject(*ch.account).setNonce(ch.prev)
   109  }
   110  
   111  func (ch codeChange) undo(s *StateDB) {
   112  	s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
   113  }
   114  
   115  func (ch storageChange) undo(s *StateDB) {
   116  	s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
   117  }
   118  
   119  func (ch refundChange) undo(s *StateDB) {
   120  	s.refund = ch.prev
   121  }
   122  
   123  func (ch addLogChange) undo(s *StateDB) {
   124  	logs := s.logs[ch.txhash]
   125  	if len(logs) == 1 {
   126  		delete(s.logs, ch.txhash)
   127  	} else {
   128  		s.logs[ch.txhash] = logs[:len(logs)-1]
   129  	}
   130  	s.logSize--
   131  }
   132  
   133  func (ch addPreimageChange) undo(s *StateDB) {
   134  	delete(s.preimages, ch.hash)
   135  }