github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/core/state/journal.go (about)

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