github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/core/state/journal.go (about)

     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  
    17  package state
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/intfoundation/intchain/common"
    23  )
    24  
    25  type journalEntry interface {
    26  	undo(*StateDB)
    27  }
    28  
    29  type journal []journalEntry
    30  
    31  type (
    32  	// Changes to the account trie.
    33  	createObjectChange struct {
    34  		account *common.Address
    35  	}
    36  	resetObjectChange struct {
    37  		prev *stateObject
    38  	}
    39  	suicideChange struct {
    40  		account     *common.Address
    41  		prev        bool // whether account had already suicided
    42  		prevbalance *big.Int
    43  	}
    44  
    45  	// Changes to individual accounts.
    46  	balanceChange struct {
    47  		account *common.Address
    48  		prev    *big.Int
    49  	}
    50  	depositBalanceChange struct {
    51  		account *common.Address
    52  		prev    *big.Int
    53  	}
    54  	childChainDepositBalanceChange struct {
    55  		account *common.Address
    56  		chainId string
    57  		prev    *big.Int
    58  	}
    59  	chainBalanceChange struct {
    60  		account *common.Address
    61  		prev    *big.Int
    62  	}
    63  	delegateBalanceChange struct {
    64  		account *common.Address
    65  		prev    *big.Int
    66  	}
    67  	proxiedBalanceChange struct {
    68  		account *common.Address
    69  		prev    *big.Int
    70  	}
    71  	depositProxiedBalanceChange struct {
    72  		account *common.Address
    73  		prev    *big.Int
    74  	}
    75  	pendingRefundBalanceChange struct {
    76  		account *common.Address
    77  		prev    *big.Int
    78  	}
    79  	rewardBalanceChange struct {
    80  		account *common.Address
    81  		prev    *big.Int
    82  	}
    83  	//availableRewardBalanceChange struct {
    84  	//	account *common.Address
    85  	//	prev    *big.Int
    86  	//}
    87  
    88  	nonceChange struct {
    89  		account *common.Address
    90  		prev    uint64
    91  	}
    92  	storageChange struct {
    93  		account       *common.Address
    94  		key, prevalue common.Hash
    95  	}
    96  	addTX1Change struct {
    97  		account *common.Address
    98  		txHash  common.Hash
    99  	}
   100  	addTX3Change struct {
   101  		account *common.Address
   102  		txHash  common.Hash
   103  	}
   104  	accountProxiedBalanceChange struct {
   105  		account  *common.Address
   106  		key      common.Address
   107  		prevalue *accountProxiedBalance
   108  	}
   109  	delegateRewardBalanceChange struct {
   110  		account  *common.Address
   111  		key      common.Address
   112  		prevalue *big.Int
   113  	}
   114  
   115  	candidateChange struct {
   116  		account *common.Address
   117  		prev    bool
   118  	}
   119  
   120  	pubkeyChange struct {
   121  		account *common.Address
   122  		prev    string
   123  	}
   124  
   125  	commissionChange struct {
   126  		account *common.Address
   127  		prev    uint8
   128  	}
   129  
   130  	forbiddenChange struct {
   131  		account *common.Address
   132  		prev    bool
   133  	}
   134  
   135  	blockTimeChange struct {
   136  		account *common.Address
   137  		prev    *big.Int
   138  	}
   139  
   140  	forbiddenTimeChange struct {
   141  		account *common.Address
   142  		prev    *big.Int
   143  	}
   144  
   145  	fAddressChange struct {
   146  		account *common.Address
   147  		prev    common.Address
   148  	}
   149  
   150  	codeChange struct {
   151  		account            *common.Address
   152  		prevcode, prevhash []byte
   153  	}
   154  
   155  	// Changes to other state values.
   156  	refundChange struct {
   157  		prev uint64
   158  	}
   159  	addLogChange struct {
   160  		txhash common.Hash
   161  	}
   162  	addPreimageChange struct {
   163  		hash common.Hash
   164  	}
   165  	touchChange struct {
   166  		account   *common.Address
   167  		prev      bool
   168  		prevDirty bool
   169  	}
   170  )
   171  
   172  func (ch createObjectChange) undo(s *StateDB) {
   173  	delete(s.stateObjects, *ch.account)
   174  	delete(s.stateObjectsDirty, *ch.account)
   175  }
   176  
   177  func (ch resetObjectChange) undo(s *StateDB) {
   178  	s.setStateObject(ch.prev)
   179  }
   180  
   181  func (ch suicideChange) undo(s *StateDB) {
   182  	obj := s.getStateObject(*ch.account)
   183  	if obj != nil {
   184  		obj.suicided = ch.prev
   185  		obj.setBalance(ch.prevbalance)
   186  	}
   187  }
   188  
   189  var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
   190  
   191  func (ch touchChange) undo(s *StateDB) {
   192  	if !ch.prev && *ch.account != ripemd {
   193  		s.getStateObject(*ch.account).touched = ch.prev
   194  		if !ch.prevDirty {
   195  			delete(s.stateObjectsDirty, *ch.account)
   196  		}
   197  	}
   198  }
   199  
   200  func (ch balanceChange) undo(s *StateDB) {
   201  	s.getStateObject(*ch.account).setBalance(ch.prev)
   202  }
   203  
   204  func (ch depositBalanceChange) undo(s *StateDB) {
   205  	s.getStateObject(*ch.account).setDepositBalance(ch.prev)
   206  }
   207  
   208  func (ch childChainDepositBalanceChange) undo(s *StateDB) {
   209  	self := s.getStateObject(*ch.account)
   210  
   211  	var index = -1
   212  	for i := range self.data.ChildChainDepositBalance {
   213  		if self.data.ChildChainDepositBalance[i].ChainId == ch.chainId {
   214  			index = i
   215  			break
   216  		}
   217  	}
   218  	if index < 0 { // not found, we'll append
   219  		self.data.ChildChainDepositBalance = append(self.data.ChildChainDepositBalance, &childChainDepositBalance{
   220  			ChainId:        ch.chainId,
   221  			DepositBalance: new(big.Int),
   222  		})
   223  		index = len(self.data.ChildChainDepositBalance) - 1
   224  	}
   225  
   226  	self.setChildChainDepositBalance(index, ch.prev)
   227  }
   228  
   229  func (ch chainBalanceChange) undo(s *StateDB) {
   230  	s.getStateObject(*ch.account).setChainBalance(ch.prev)
   231  }
   232  
   233  func (ch delegateBalanceChange) undo(s *StateDB) {
   234  	s.getStateObject(*ch.account).setDelegateBalance(ch.prev)
   235  }
   236  
   237  func (ch proxiedBalanceChange) undo(s *StateDB) {
   238  	s.getStateObject(*ch.account).setProxiedBalance(ch.prev)
   239  }
   240  
   241  func (ch depositProxiedBalanceChange) undo(s *StateDB) {
   242  	s.getStateObject(*ch.account).setDepositProxiedBalance(ch.prev)
   243  }
   244  
   245  func (ch pendingRefundBalanceChange) undo(s *StateDB) {
   246  	s.getStateObject(*ch.account).setPendingRefundBalance(ch.prev)
   247  }
   248  
   249  func (ch rewardBalanceChange) undo(s *StateDB) {
   250  	s.getStateObject(*ch.account).setRewardBalance(ch.prev)
   251  }
   252  
   253  //func (ch availableRewardBalanceChange) undo(s *StateDB) {
   254  //	s.getStateObject(*ch.account).setAvailableRewardBalance(ch.prev)
   255  //}
   256  
   257  func (ch nonceChange) undo(s *StateDB) {
   258  	s.getStateObject(*ch.account).setNonce(ch.prev)
   259  }
   260  
   261  func (ch codeChange) undo(s *StateDB) {
   262  	s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
   263  }
   264  
   265  func (ch storageChange) undo(s *StateDB) {
   266  	s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
   267  }
   268  
   269  func (ch addTX1Change) undo(s *StateDB) {
   270  	s.getStateObject(*ch.account).removeTX1(ch.txHash)
   271  }
   272  
   273  func (ch addTX3Change) undo(s *StateDB) {
   274  	s.getStateObject(*ch.account).removeTX3(ch.txHash)
   275  }
   276  
   277  func (ch accountProxiedBalanceChange) undo(s *StateDB) {
   278  	s.getStateObject(*ch.account).setAccountProxiedBalance(ch.key, ch.prevalue)
   279  }
   280  
   281  func (ch delegateRewardBalanceChange) undo(s *StateDB) {
   282  	s.getStateObject(*ch.account).setDelegateRewardBalance(ch.key, ch.prevalue)
   283  }
   284  
   285  func (ch candidateChange) undo(s *StateDB) {
   286  	s.getStateObject(*ch.account).setCandidate(ch.prev)
   287  }
   288  
   289  func (ch pubkeyChange) undo(s *StateDB) {
   290  	s.getStateObject(*ch.account).setPubkey(ch.prev)
   291  }
   292  
   293  func (ch commissionChange) undo(s *StateDB) {
   294  	s.getStateObject(*ch.account).setCommission(ch.prev)
   295  }
   296  
   297  //func (ch forbiddenChange) undo(s *StateDB) {
   298  //	s.getStateObject(*ch.account).setForbidden(ch.prev)
   299  //}
   300  //
   301  //func (ch blockTimeChange) undo(s *StateDB) {
   302  //	s.getStateObject(*ch.account).setBlockTime(ch.prev)
   303  //}
   304  //
   305  //func (ch forbiddenTimeChange) undo(s *StateDB) {
   306  //	s.getStateObject(*ch.account).setForbiddenTime(ch.prev)
   307  //}
   308  
   309  func (ch fAddressChange) undo(s *StateDB) {
   310  	s.getStateObject(*ch.account).setAddress(ch.prev)
   311  }
   312  
   313  func (ch refundChange) undo(s *StateDB) {
   314  	s.refund = ch.prev
   315  }
   316  
   317  func (ch addLogChange) undo(s *StateDB) {
   318  	logs := s.logs[ch.txhash]
   319  	if len(logs) == 1 {
   320  		delete(s.logs, ch.txhash)
   321  	} else {
   322  		s.logs[ch.txhash] = logs[:len(logs)-1]
   323  	}
   324  	s.logSize--
   325  }
   326  
   327  func (ch addPreimageChange) undo(s *StateDB) {
   328  	delete(s.preimages, ch.hash)
   329  }