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