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

     1  package state
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/neatio-net/neatio/utilities/common"
     7  )
     8  
     9  // sideChainDepositBalance
    10  type sideChainDepositBalance struct {
    11  	ChainId        string
    12  	DepositBalance *big.Int
    13  }
    14  
    15  // AddDepositBalance add amount to the deposit balance.
    16  func (c *stateObject) AddDepositBalance(amount *big.Int) {
    17  	// EIP158: We must check emptiness for the objects such that the account
    18  	// clearing (0,0,0 objects) can take effect.
    19  	if amount.Cmp(common.Big0) == 0 {
    20  		if c.empty() {
    21  			c.touch()
    22  		}
    23  
    24  		return
    25  	}
    26  
    27  	c.SetDepositBalance(new(big.Int).Add(c.DepositBalance(), amount))
    28  }
    29  
    30  // SubDepositBalance removes amount from c's deposit balance.
    31  func (c *stateObject) SubDepositBalance(amount *big.Int) {
    32  	if amount.Cmp(common.Big0) == 0 {
    33  		return
    34  	}
    35  	c.SetDepositBalance(new(big.Int).Sub(c.DepositBalance(), amount))
    36  }
    37  
    38  func (self *stateObject) SetDepositBalance(amount *big.Int) {
    39  	self.db.journal = append(self.db.journal, depositBalanceChange{
    40  		account: &self.address,
    41  		prev:    new(big.Int).Set(self.data.DepositBalance),
    42  	})
    43  	self.setDepositBalance(amount)
    44  }
    45  
    46  func (self *stateObject) setDepositBalance(amount *big.Int) {
    47  	self.data.DepositBalance = amount
    48  	if self.onDirty != nil {
    49  		self.onDirty(self.Address())
    50  		self.onDirty = nil
    51  	}
    52  }
    53  
    54  // AddSideChainDepositBalance add amount to the side chain deposit balance
    55  func (c *stateObject) AddSideChainDepositBalance(chainId string, amount *big.Int) {
    56  	// EIP158: We must check emptiness for the objects such that the account
    57  	// clearing (0,0,0 objects) can take effect.
    58  	if amount.Cmp(common.Big0) == 0 {
    59  		if c.empty() {
    60  			c.touch()
    61  		}
    62  
    63  		return
    64  	}
    65  
    66  	c.SetSideChainDepositBalance(chainId, new(big.Int).Add(c.SideChainDepositBalance(chainId), amount))
    67  }
    68  
    69  // SubSideChainDepositBalance removes amount from c's side chain deposit balance
    70  func (c *stateObject) SubSideChainDepositBalance(chainId string, amount *big.Int) {
    71  	if amount.Cmp(common.Big0) == 0 {
    72  		return
    73  	}
    74  	c.SetSideChainDepositBalance(chainId, new(big.Int).Sub(c.SideChainDepositBalance(chainId), amount))
    75  }
    76  
    77  func (self *stateObject) SetSideChainDepositBalance(chainId string, amount *big.Int) {
    78  	var index = -1
    79  	for i := range self.data.SideChainDepositBalance {
    80  		if self.data.SideChainDepositBalance[i].ChainId == chainId {
    81  			index = i
    82  			break
    83  		}
    84  	}
    85  	if index < 0 { // not found, we'll append
    86  		self.data.SideChainDepositBalance = append(self.data.SideChainDepositBalance, &sideChainDepositBalance{
    87  			ChainId:        chainId,
    88  			DepositBalance: new(big.Int),
    89  		})
    90  		index = len(self.data.SideChainDepositBalance) - 1
    91  	}
    92  
    93  	self.db.journal = append(self.db.journal, sideChainDepositBalanceChange{
    94  		account: &self.address,
    95  		chainId: chainId,
    96  		prev:    new(big.Int).Set(self.data.SideChainDepositBalance[index].DepositBalance),
    97  	})
    98  	self.setSideChainDepositBalance(index, amount)
    99  }
   100  
   101  func (self *stateObject) setSideChainDepositBalance(index int, amount *big.Int) {
   102  	self.data.SideChainDepositBalance[index].DepositBalance = amount
   103  	if self.onDirty != nil {
   104  		self.onDirty(self.Address())
   105  		self.onDirty = nil
   106  	}
   107  }
   108  
   109  // AddChainBalance add amount to the locked balance.
   110  func (c *stateObject) AddChainBalance(amount *big.Int) {
   111  	// EIP158: We must check emptiness for the objects such that the account
   112  	// clearing (0,0,0 objects) can take effect.
   113  	if amount.Cmp(common.Big0) == 0 {
   114  		if c.empty() {
   115  			c.touch()
   116  		}
   117  
   118  		return
   119  	}
   120  
   121  	c.SetChainBalance(new(big.Int).Add(c.ChainBalance(), amount))
   122  }
   123  
   124  // SubChainBalance removes amount from c's chain balance.
   125  func (c *stateObject) SubChainBalance(amount *big.Int) {
   126  	if amount.Cmp(common.Big0) == 0 {
   127  		return
   128  	}
   129  	c.SetChainBalance(new(big.Int).Sub(c.ChainBalance(), amount))
   130  }
   131  
   132  func (self *stateObject) SetChainBalance(amount *big.Int) {
   133  	self.db.journal = append(self.db.journal, chainBalanceChange{
   134  		account: &self.address,
   135  		prev:    new(big.Int).Set(self.data.ChainBalance),
   136  	})
   137  	self.setChainBalance(amount)
   138  }
   139  
   140  func (self *stateObject) setChainBalance(amount *big.Int) {
   141  	self.data.ChainBalance = amount
   142  	if self.onDirty != nil {
   143  		self.onDirty(self.Address())
   144  		self.onDirty = nil
   145  	}
   146  }
   147  
   148  func (self *stateObject) DepositBalance() *big.Int {
   149  	return self.data.DepositBalance
   150  }
   151  
   152  func (self *stateObject) SideChainDepositBalance(chainId string) *big.Int {
   153  	for i := range self.data.SideChainDepositBalance {
   154  		if self.data.SideChainDepositBalance[i].ChainId == chainId {
   155  			return self.data.SideChainDepositBalance[i].DepositBalance
   156  		}
   157  	}
   158  	return common.Big0
   159  }
   160  
   161  func (self *stateObject) ChainBalance() *big.Int {
   162  	return self.data.ChainBalance
   163  }