github.com/Gessiux/neatchain@v1.3.1/chain/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/Gessiux/neatchain/utilities/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  	sideChainDepositBalanceChange 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  	codeChange struct {
   146  		account            *common.Address
   147  		prevcode, prevhash []byte
   148  	}
   149  
   150  	// Changes to other state values.
   151  	refundChange struct {
   152  		prev uint64
   153  	}
   154  	addLogChange struct {
   155  		txhash common.Hash
   156  	}
   157  	addPreimageChange struct {
   158  		hash common.Hash
   159  	}
   160  	touchChange struct {
   161  		account   *common.Address
   162  		prev      bool
   163  		prevDirty bool
   164  	}
   165  )
   166  
   167  func (ch createObjectChange) undo(s *StateDB) {
   168  	delete(s.stateObjects, *ch.account)
   169  	delete(s.stateObjectsDirty, *ch.account)
   170  }
   171  
   172  func (ch resetObjectChange) undo(s *StateDB) {
   173  	s.setStateObject(ch.prev)
   174  }
   175  
   176  func (ch suicideChange) undo(s *StateDB) {
   177  	obj := s.getStateObject(*ch.account)
   178  	if obj != nil {
   179  		obj.suicided = ch.prev
   180  		obj.setBalance(ch.prevbalance)
   181  	}
   182  }
   183  
   184  var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
   185  
   186  func (ch touchChange) undo(s *StateDB) {
   187  	if !ch.prev && *ch.account != ripemd {
   188  		s.getStateObject(*ch.account).touched = ch.prev
   189  		if !ch.prevDirty {
   190  			delete(s.stateObjectsDirty, *ch.account)
   191  		}
   192  	}
   193  }
   194  
   195  func (ch balanceChange) undo(s *StateDB) {
   196  	s.getStateObject(*ch.account).setBalance(ch.prev)
   197  }
   198  
   199  func (ch depositBalanceChange) undo(s *StateDB) {
   200  	s.getStateObject(*ch.account).setDepositBalance(ch.prev)
   201  }
   202  
   203  func (ch sideChainDepositBalanceChange) undo(s *StateDB) {
   204  	self := s.getStateObject(*ch.account)
   205  
   206  	var index = -1
   207  	for i := range self.data.SideChainDepositBalance {
   208  		if self.data.SideChainDepositBalance[i].ChainId == ch.chainId {
   209  			index = i
   210  			break
   211  		}
   212  	}
   213  	if index < 0 { // not found, we'll append
   214  		self.data.SideChainDepositBalance = append(self.data.SideChainDepositBalance, &sideChainDepositBalance{
   215  			ChainId:        ch.chainId,
   216  			DepositBalance: new(big.Int),
   217  		})
   218  		index = len(self.data.SideChainDepositBalance) - 1
   219  	}
   220  
   221  	self.setSideChainDepositBalance(index, ch.prev)
   222  }
   223  
   224  func (ch chainBalanceChange) undo(s *StateDB) {
   225  	s.getStateObject(*ch.account).setChainBalance(ch.prev)
   226  }
   227  
   228  func (ch delegateBalanceChange) undo(s *StateDB) {
   229  	s.getStateObject(*ch.account).setDelegateBalance(ch.prev)
   230  }
   231  
   232  func (ch proxiedBalanceChange) undo(s *StateDB) {
   233  	s.getStateObject(*ch.account).setProxiedBalance(ch.prev)
   234  }
   235  
   236  func (ch depositProxiedBalanceChange) undo(s *StateDB) {
   237  	s.getStateObject(*ch.account).setDepositProxiedBalance(ch.prev)
   238  }
   239  
   240  func (ch pendingRefundBalanceChange) undo(s *StateDB) {
   241  	s.getStateObject(*ch.account).setPendingRefundBalance(ch.prev)
   242  }
   243  
   244  func (ch rewardBalanceChange) undo(s *StateDB) {
   245  	s.getStateObject(*ch.account).setRewardBalance(ch.prev)
   246  }
   247  
   248  func (ch availableRewardBalanceChange) undo(s *StateDB) {
   249  	s.getStateObject(*ch.account).setAvailableRewardBalance(ch.prev)
   250  }
   251  
   252  func (ch nonceChange) undo(s *StateDB) {
   253  	s.getStateObject(*ch.account).setNonce(ch.prev)
   254  }
   255  
   256  func (ch codeChange) undo(s *StateDB) {
   257  	s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
   258  }
   259  
   260  func (ch storageChange) undo(s *StateDB) {
   261  	s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
   262  }
   263  
   264  func (ch addTX1Change) undo(s *StateDB) {
   265  	s.getStateObject(*ch.account).removeTX1(ch.txHash)
   266  }
   267  
   268  func (ch addTX3Change) undo(s *StateDB) {
   269  	s.getStateObject(*ch.account).removeTX3(ch.txHash)
   270  }
   271  
   272  func (ch accountProxiedBalanceChange) undo(s *StateDB) {
   273  	s.getStateObject(*ch.account).setAccountProxiedBalance(ch.key, ch.prevalue)
   274  }
   275  
   276  func (ch delegateRewardBalanceChange) undo(s *StateDB) {
   277  	s.getStateObject(*ch.account).setDelegateRewardBalance(ch.key, ch.prevalue)
   278  }
   279  
   280  func (ch candidateChange) undo(s *StateDB) {
   281  	s.getStateObject(*ch.account).setCandidate(ch.prev)
   282  }
   283  
   284  func (ch pubkeyChange) undo(s *StateDB) {
   285  	s.getStateObject(*ch.account).setPubkey(ch.prev)
   286  }
   287  
   288  func (ch commissionChange) undo(s *StateDB) {
   289  	s.getStateObject(*ch.account).setCommission(ch.prev)
   290  }
   291  
   292  func (ch forbiddenChange) undo(s *StateDB) {
   293  	s.getStateObject(*ch.account).setForbidden(ch.prev)
   294  }
   295  
   296  func (ch blockTimeChange) undo(s *StateDB) {
   297  	s.getStateObject(*ch.account).setBlockTime(ch.prev)
   298  }
   299  
   300  func (ch forbiddenTimeChange) undo(s *StateDB) {
   301  	s.getStateObject(*ch.account).setForbiddenTime(ch.prev)
   302  }
   303  
   304  func (ch refundChange) undo(s *StateDB) {
   305  	s.refund = ch.prev
   306  }
   307  
   308  func (ch addLogChange) undo(s *StateDB) {
   309  	logs := s.logs[ch.txhash]
   310  	if len(logs) == 1 {
   311  		delete(s.logs, ch.txhash)
   312  	} else {
   313  		s.logs[ch.txhash] = logs[:len(logs)-1]
   314  	}
   315  	s.logSize--
   316  }
   317  
   318  func (ch addPreimageChange) undo(s *StateDB) {
   319  	delete(s.preimages, ch.hash)
   320  }