github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/core/state/privileged_state_object.go (about) 1 package state 2 3 import ( 4 "errors" 5 "github.com/insight-chain/inb-go/params" 6 "math/big" 7 ) 8 9 type MortgageManager interface { 10 afterMortgageNet(mortgagtionNumber *big.Int) error 11 afterUnmortgageNet(unMortgagtionNumber *big.Int) error 12 } 13 14 type manager struct { 15 } 16 17 func (a *manager) afterUnMortgageCpu(mortgagtionNumber *big.Int) error { 18 PrivilegedSateObject.SubNet(mortgagtionNumber) 19 return errors.New("") 20 } 21 func (a *manager) afterUnMortgageNet(mortgagtionNumber *big.Int) error { 22 PrivilegedSateObject.SubNet(mortgagtionNumber) 23 return errors.New("") 24 } 25 26 //User's mortgage and unmortgage operations 27 //mortgage 28 //func (self *stateObject) MortgageCpu(amount *big.Int) { 29 // //self.updateAccountCpuAndNet() 30 // if (amount.Cmp(big.NewInt(0)) == 0) || (amount.Cmp(self.data.Balance) == 1) { 31 // return 32 // } 33 // 34 // //Add CPU and net to the account 35 // self.AddCpu(amount) 36 // 37 // self.db.Finalise(false) 38 // root := self.db.IntermediateRoot(false) 39 // self.db.Commit(false) 40 // self.db.Database().TrieDB().Commit(root, true) 41 //} 42 43 // 44 //func (self *stateObject) MortgageNet(amount *big.Int) { 45 // 46 // //self.updateAccountCpuAndNet() 47 // if (amount.Cmp(big.NewInt(0)) == 0) || (amount.Cmp(self.data.Balance) == 1) { 48 // return 49 // } 50 // 51 // //Add CPU and net to the account 52 // self.AddNet(amount) 53 // 54 // self.db.Finalise(true) 55 // root := self.db.IntermediateRoot(false) 56 // self.db.Commit(false) 57 // self.db.Database().TrieDB().Commit(root, true) 58 //} 59 60 //UnMortgage 61 //func (self *stateObject) UnMortgageCpu(amount *big.Int) { 62 // 63 // usableCpu := self.data.Resources.CPU.Usableness 64 // usableNet := self.data.Resources.NET.Usableness 65 // mortgageCpuOfINB := self.data.Resources.CPU.MortgagteINB 66 // 67 // //You need to convert number to the type of big 68 // expendCpuFromUnMortgageCpu := big.NewInt(params.TxConfig.UseCpu) 69 // expendNetFromUnMortgageNet := big.NewInt(params.TxConfig.UseNet) 70 // 71 // //self.updateAccountCpuAndNet() 72 // if (amount.Cmp(big.NewInt(0)) == 0) || (amount.Cmp(self.data.Resources.CPU.MortgagteINB) == 1) { 73 // return 74 // } 75 // if expendNetFromUnMortgageNet.Cmp(usableNet) == 1 { 76 // return 77 // } 78 // if amount.Cmp(self.data.Resources.CPU.MortgagteINB) == 0 { 79 // self.setCpu(big.NewInt(0), big.NewInt(0), big.NewInt(0)) 80 // self.AddBalance(amount) 81 // } else { 82 // //Make sure unmarshalling CPU consumes enough CPU 83 // residueMorgageCpuOfInb := mortgageCpuOfINB.Sub(mortgageCpuOfINB, amount) 84 // residueCpu := usableCpu.Mul(usableCpu, residueMorgageCpuOfInb).Div(usableCpu.Mul(usableCpu, residueMorgageCpuOfInb), mortgageCpuOfINB) 85 // if expendCpuFromUnMortgageCpu.Cmp(residueCpu) == 1 { 86 // return 87 // } 88 // 89 // self.SubCpu(amount) 90 // } 91 //} 92 //func (self *stateObject) UnMortgageNet(amount *big.Int) { 93 // 94 // //usableCpu := self.data.Resources.CPU.Usableness 95 // usableNet := self.data.Resources.NET.Usableness 96 // mortgageNetOfINB := self.data.Resources.NET.MortgagteINB 97 // 98 // //You need to convert number to the type of big 99 // //expendCpuFromUnMortgageCpu := big.NewInt(params.TxConfig.UseCpu) 100 // expendNetFromUnMortgageNet := big.NewInt(params.TxConfig.UseNet) 101 // 102 // //self.updateAccountCpuAndNet() 103 // if (amount.Cmp(big.NewInt(0)) == 0) || (amount.Cmp(self.data.Resources.NET.MortgagteINB) == 1) { 104 // return 105 // } 106 // //if expendCpuFromUnMortgageCpu.Cmp(usableCpu) == 1 { 107 // // return 108 // //} 109 // if amount.Cmp(self.data.Resources.NET.MortgagteINB) == 0 { 110 // self.setNet(big.NewInt(0), big.NewInt(0), big.NewInt(0)) 111 // self.AddBalance(amount) 112 // } else { 113 // //Make sure unmarshalling Net consumes enough Net 114 // residueMorgageNetOfInb := mortgageNetOfINB.Sub(mortgageNetOfINB, amount) 115 // residueNet := usableNet.Mul(usableNet, residueMorgageNetOfInb).Div(usableNet.Mul(usableNet, residueMorgageNetOfInb), mortgageNetOfINB) 116 // if expendNetFromUnMortgageNet.Cmp(residueNet) == 1 { 117 // return 118 // } 119 // 120 // self.SubNet(amount) 121 // } 122 //} 123 124 //Increase or decrease the user's CPU or net 125 //Mortgage 126 func (c *stateObject) AddNet(amount *big.Int) { 127 gainNumberOfNet := c.db.ConvertToNets(amount) 128 used := c.data.Res.Used 129 usable := c.AddUsableNet(gainNumberOfNet) 130 mortgagetion := c.AddMortgageINBOfNet(amount) 131 c.setRes(used, usable, mortgagetion) 132 c.db.GetMortgageStateObject() 133 PrivilegedSateObject.AddMortgageINBOfNet(amount) 134 } 135 136 func (c *stateObject) SubNet(amount *big.Int) { 137 //expendCpuFromUnMortgageCpu := big.NewInt(50) 138 expendNetFromUnMortgageNet := big.NewInt(params.TxConfig.UseNet) 139 140 used := c.AddUsableNet(expendNetFromUnMortgageNet) 141 mortgagetion := c.SubMortgageINBOfNet(amount) 142 calculateNetNumber := c.db.ConvertToNets(c.data.Res.StakingValue) 143 remainingNetNumber := calculateNetNumber.Sub(calculateNetNumber, expendNetFromUnMortgageNet) 144 usable := c.AddUsableNet(remainingNetNumber) 145 c.SetRes(used, usable, mortgagetion) 146 147 //achilles replace gas with net 148 //cpuUsed := c.AddUsedCpu(expendCpuFromUnMortgageCpu) 149 //cpuUsable := c.SubUsableCpu(expendCpuFromUnMortgageCpu) 150 //c.SetCpu(cpuUsed, cpuUsable, c.data.Resources.CPU.MortgagteINB) 151 152 c.db.GetMortgageStateObject() 153 PrivilegedSateObject.SubMortgageINBOfNet(amount) 154 } 155 156 //achilles replace gas with net 157 func (c *stateObject) UserRes(bytes *big.Int) { 158 usable := c.SubUsableNet(bytes) 159 used := c.AddUsedNet(bytes) 160 c.SetRes(used, usable, c.data.Res.StakingValue) 161 } 162 163 //func (self *stateObject) SetNet(usedAmount *big.Int, usableAmount *big.Int, mortgageInb *big.Int) { 164 // 165 // self.db.journal.append(netChange{ 166 // account: &self.address, 167 // Used: new(big.Int).Set(self.data.Resources.NET.Used), 168 // Usableness: new(big.Int).Set(self.data.Resources.NET.Usableness), 169 // MortgagteINB: new(big.Int).Set(self.data.Resources.NET.MortgagteINB), 170 // }) 171 // self.setNet(usedAmount, usableAmount, mortgageInb) 172 //} 173 //func (self *stateObject) setNet(usedAmount *big.Int, usableAmount *big.Int, mortgageInb *big.Int) { 174 // self.data.Resources.NET.Used = usedAmount 175 // self.data.Resources.NET.Usableness = usableAmount 176 // self.data.Resources.NET.MortgagteINB = mortgageInb 177 //} 178 179 //Usable, usable, and mortgage are used in the CPU or Net 180 181 func (self *stateObject) AddUsedNet(amout *big.Int) *big.Int { 182 return self.data.Res.Used.Add(self.data.Res.Used, amout) 183 } 184 185 func (self *stateObject) AddUsableNet(amout *big.Int) *big.Int { 186 return self.data.Res.Usable.Add(self.data.Res.Usable, amout) 187 } 188 func (self *stateObject) SubUsableNet(amout *big.Int) *big.Int { 189 return self.data.Res.Usable.Sub(self.data.Res.Usable, amout) 190 } 191 192 //Mortgage 193 194 func (self *stateObject) AddMortgageINBOfNet(amout *big.Int) *big.Int { 195 return self.data.Res.StakingValue.Add(self.data.Res.StakingValue, amout) 196 } 197 func (self *stateObject) SubMortgageINBOfNet(amout *big.Int) *big.Int { 198 return self.data.Res.StakingValue.Sub(self.data.Res.StakingValue, amout) 199 } 200 201 //Updates the user's CPU and net 24 hours a day 202 //func (self *stateObject) updateAccountCpuAndNet() { 203 // for { 204 // now := time.Now() 205 // next := now.Add(time.Hour * 24) 206 // next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location()) 207 // t := time.NewTimer(next.Sub(now)) 208 // <-t.C 209 // //Update the CPU and Net owned by the user 210 // if self.data.Resources.CPU.MortgagteINB != big.NewInt(0) { 211 // self.SetCpu(big.NewInt(0), self.db.GainNumberOfCpu(self.data.Resources.CPU.MortgagteINB), self.data.Resources.CPU.MortgagteINB) 212 // } else if self.data.Resources.NET.MortgagteINB != big.NewInt(0) { 213 // self.SetNet(big.NewInt(0), self.db.ConvertToNets(self.data.Resources.NET.MortgagteINB), self.data.Resources.NET.MortgagteINB) 214 // } 215 // } 216 // 217 //}