github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/txpool/orgchain/subtx_pool_collector_test.go (about) 1 package orgchain 2 3 import ( 4 "fmt" 5 "math/big" 6 "os" 7 "path" 8 "testing" 9 "time" 10 11 "github.com/sixexorg/magnetic-ring/log" 12 13 "github.com/sixexorg/magnetic-ring/common" 14 "github.com/sixexorg/magnetic-ring/config" 15 "github.com/sixexorg/magnetic-ring/core/orgchain/types" 16 "github.com/sixexorg/magnetic-ring/store/orgchain/states" 17 "github.com/sixexorg/magnetic-ring/store/orgchain/storages" 18 "github.com/sixexorg/magnetic-ring/store/orgchain/validation" 19 ) 20 21 var ( 22 balance1, balance2 = big.NewInt(1000), big.NewInt(2000) 23 energy1, energy2 = big.NewInt(5000), big.NewInt(4000) 24 nonce1, n1, n2, n3, n4, n5 uint64 = 50, 51, 52, 53, 54, 55 25 nonce2, m1, m2, m3, m4, m5 uint64 = 100, 101, 102, 103, 104, 105 26 fee1, fee2, fee3, fee4, fee5 = big.NewInt(20), big.NewInt(10), big.NewInt(2), big.NewInt(20), big.NewInt(5) 27 tx1a1, tx1a2, touta1_1, touta1_2, touta1_3 = big.NewInt(20), big.NewInt(108), big.NewInt(30), big.NewInt(50), big.NewInt(28) 28 tx2a1, tx2a2, touta2_1, touta2_2, touta2_3 = big.NewInt(80), big.NewInt(300), big.NewInt(50), big.NewInt(50), big.NewInt(280) 29 tx3a1, tx3a2, touta3_1, touta3_2, touta3_3 = big.NewInt(800), big.NewInt(500), big.NewInt(500), big.NewInt(300), big.NewInt(500) 30 tx4a1, tx4a2, touta4_1, touta4_2, touta4_3 = big.NewInt(200), big.NewInt(20), big.NewInt(20), big.NewInt(100), big.NewInt(100) 31 tx5a1, tx5a2, touta5_1, touta5_2, touta5_3 = big.NewInt(3000), big.NewInt(40), big.NewInt(1000), big.NewInt(1000), big.NewInt(1040) 32 33 leagueAddr, _ = common.ToAddress("ct2qK96vAkK6E8S7JgYUY3YY28Qhj6cmfda") 34 Address_1, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfda") 35 Address_2, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfdb") 36 Address_3, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfdc") 37 Address_4, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfdd") 38 Address_5, _ = common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfde") 39 40 height = uint64(50) 41 42 tx1 *types.Transaction 43 tx2 *types.Transaction 44 tx3 *types.Transaction 45 tx4 *types.Transaction 46 tx5 *types.Transaction 47 txs []*types.Transaction 48 ) 49 50 func init() { 51 p, err := os.Getwd() 52 if err != nil { 53 return 54 } 55 56 err = config.InitConfig(path.Join(p, "./config.yml")) 57 58 if err != nil { 59 fmt.Printf("init config error-->%v\n", err) 60 os.Exit(0) 61 return 62 } 63 fmt.Println("init config success") 64 } 65 66 func TestRun(t *testing.T) { 67 68 log.InitMagneticLog("temp.log") 69 70 froms := &common.TxIns{} 71 froms.Tis = append(froms.Tis, 72 &common.TxIn{ 73 Address: Address_1, 74 Nonce: n1, 75 Amount: tx1a1, 76 }, 77 &common.TxIn{ 78 Address: Address_2, 79 Nonce: m1, 80 Amount: tx1a2, 81 }, 82 ) 83 tos := &common.TxOuts{} 84 tos.Tos = append(tos.Tos, 85 &common.TxOut{ 86 Address: Address_3, 87 Amount: touta1_1, 88 }, 89 &common.TxOut{ 90 Address: Address_4, 91 Amount: touta1_2, 92 }, 93 &common.TxOut{ 94 Address: Address_5, 95 Amount: touta1_3, 96 }, 97 ) 98 txd1 := &types.TxData{ 99 Froms: froms, 100 Tos: tos, 101 Fee: fee1, 102 From: Address_1, 103 } 104 105 tx1 = &types.Transaction{ 106 Version: 0x01, 107 TxType: types.TransferUT, //trnasfer ut 108 TxData: txd1, 109 } 110 common.DeepCopy(&tx2, tx1) 111 tx2.TxData.Froms.Tis[0].Nonce = n2 112 tx2.TxData.Froms.Tis[0].Amount = tx2a1 113 tx2.TxData.Froms.Tis[1].Nonce = m2 114 tx2.TxData.Froms.Tis[1].Amount = tx2a2 115 tx2.TxData.Tos.Tos[0].Amount = touta2_1 116 tx2.TxData.Tos.Tos[1].Amount = touta2_2 117 tx2.TxData.Tos.Tos[2].Amount = touta2_3 118 tx2.TxData.Fee = fee2 119 common.DeepCopy(&tx3, tx1) 120 tx3.TxData.Froms.Tis[0].Nonce = n3 121 tx3.TxData.Froms.Tis[0].Amount = tx3a1 122 tx3.TxData.Froms.Tis[1].Nonce = m3 123 tx3.TxData.Froms.Tis[1].Amount = tx3a2 124 tx3.TxData.Tos.Tos[0].Amount = touta3_1 125 tx3.TxData.Tos.Tos[1].Amount = touta3_2 126 tx3.TxData.Tos.Tos[2].Amount = touta3_3 127 tx3.TxData.Fee = fee3 128 common.DeepCopy(&tx4, tx1) 129 tx4.TxData.Froms.Tis[0].Nonce = n4 130 tx4.TxData.Froms.Tis[0].Amount = tx4a1 131 tx4.TxData.Froms.Tis[1].Nonce = m4 132 tx4.TxData.Froms.Tis[1].Amount = tx4a2 133 tx4.TxData.Tos.Tos[0].Amount = touta4_1 134 tx4.TxData.Tos.Tos[1].Amount = touta4_2 135 tx4.TxData.Tos.Tos[2].Amount = touta4_3 136 tx4.TxData.Fee = fee4 137 common.DeepCopy(&tx5, tx1) 138 tx5.TxData.Froms.Tis[0].Nonce = n5 139 tx5.TxData.Froms.Tis[0].Amount = tx5a1 140 tx5.TxData.Froms.Tis[1].Nonce = m5 141 tx5.TxData.Froms.Tis[1].Amount = tx5a2 142 tx5.TxData.Tos.Tos[0].Amount = touta5_1 143 tx5.TxData.Tos.Tos[1].Amount = touta5_2 144 tx5.TxData.Tos.Tos[2].Amount = touta5_3 145 tx5.TxData.Fee = fee5 146 147 txs = append(txs, tx1, tx2, tx3, tx4, tx5) 148 149 addr_a_bal := big.NewInt(0).Set(balance1) 150 addr_a_bal.Sub(addr_a_bal, tx1a1) 151 addr_a_bal.Sub(addr_a_bal, tx2a1) 152 addr_a_bal.Sub(addr_a_bal, tx3a1) 153 addr_a_bal.Sub(addr_a_bal, tx4a1) 154 addr_a_bal.Sub(addr_a_bal, tx5a1) 155 156 addr_b_bal := big.NewInt(0).Set(balance2) 157 addr_b_bal.Sub(addr_b_bal, tx1a2) 158 addr_b_bal.Sub(addr_b_bal, tx2a2) 159 addr_b_bal.Sub(addr_b_bal, tx3a2) 160 addr_b_bal.Sub(addr_b_bal, tx4a2) 161 addr_b_bal.Sub(addr_b_bal, tx5a2) 162 163 datadir := "./tmp" 164 165 ledgerImpl, err := storages.NewLedgerStore(datadir) 166 if err != nil { 167 t.Error(err) 168 return 169 } 170 171 clctor := NewSubPool(ledgerImpl, leagueAddr) 172 173 //actstore, err := storages.NewAccountStore(datadir) 174 if err != nil { 175 t.Error(err) 176 return 177 } 178 179 //pool.AddPoolCollection(actstore, leagueAddr) 180 181 //clctor := pool.GetPoolCollector(leagueAddr) 182 183 setPool(clctor.stateValidator) 184 clctor.Start() 185 186 tx1.Raw = []byte("raw tx1") 187 tx2.Raw = []byte("raw tx2") 188 tx3.Raw = []byte("raw tx3") 189 tx4.Raw = []byte("raw tx4") 190 tx5.Raw = []byte("raw tx5") 191 192 err = clctor.TxEnqueue(tx1) 193 if err != nil { 194 t.Errorf("error occured,err=%v\n", err) 195 return 196 } 197 198 //pool.AddTx(tx2) 199 200 time.Sleep(time.Second) 201 202 blk := clctor.GenerateBlock(leagueAddr, 55, true) 203 204 t.Logf("===============================================block info bg===============================================") 205 206 for i, v := range blk.Transactions { 207 208 t.Logf("index=%d,\thash=%s,type=%d,address=%s,fee=%d\n", i, v.Hash(), v.TxType, v.TxData.From, v.TxData.Fee.Uint64()) 209 } 210 211 t.Logf("===============================================block info ed===============================================") 212 213 t.Logf("blk-->%+v\n", blk) 214 215 t.Log("-----------------------MemoAccountState---------------------------") 216 for k, v := range clctor.stateValidator.MemoAccountState { 217 t.Logf("address:%s, nonce:%v, balance:%d, energybalance:%d ", k.ToString(), v.Nonce(), v.Balance(), v.Energy().Uint64()) 218 } 219 220 t.Logf("===============================================") 221 blkInfo := clctor.Execute() 222 223 for _, v := range blkInfo.AccStates { 224 t.Logf("address:%s, nonce:%d, balance:%d, energybalance:%d ", v.Account().ToString(), v.Nonce(), v.Balance().Uint64(), v.Energy().Uint64()) 225 } 226 227 //c := make(chan os.Signal, 1) 228 //signal.Notify(c, os.Interrupt, os.Kill,syscall.SIGTERM) 229 //<-c 230 231 } 232 233 func setPool(stateValidate *validation.StateValidate) { 234 stateValidate.ParentBonusHeight = 1000 235 stateValidate.MemoAccountState[Address_1] = &states.AccountState{ 236 Address: Address_1, 237 Height: height, 238 Data: &states.Account{ 239 Nonce: nonce1, 240 Balance: balance1, 241 EnergyBalance: energy1, 242 BonusHeight: 50, 243 }, 244 } 245 stateValidate.MemoAccountState[Address_2] = &states.AccountState{ 246 Address: Address_2, 247 Height: height, 248 Data: &states.Account{ 249 Nonce: nonce2, 250 Balance: balance2, 251 EnergyBalance: energy2, 252 BonusHeight: 40, 253 }, 254 } 255 stateValidate.MemoAccountState[Address_3] = &states.AccountState{ 256 Address: Address_3, 257 Height: height, 258 Data: &states.Account{ 259 Nonce: 0, 260 Balance: big.NewInt(10000), 261 EnergyBalance: big.NewInt(20000), 262 BonusHeight: 0, 263 }, 264 } 265 stateValidate.MemoAccountState[Address_4] = &states.AccountState{ 266 Address: Address_4, 267 Height: height, 268 Data: &states.Account{ 269 Nonce: 0, 270 Balance: big.NewInt(1), 271 EnergyBalance: big.NewInt(1), 272 BonusHeight: 0, 273 }, 274 } 275 stateValidate.MemoAccountState[Address_5] = &states.AccountState{ 276 Address: Address_5, 277 Height: height, 278 Data: &states.Account{ 279 Nonce: 0, 280 Balance: big.NewInt(1), 281 EnergyBalance: big.NewInt(1), 282 BonusHeight: 0, 283 }, 284 } 285 286 for k, v := range stateValidate.MemoAccountState { 287 as := v.(*states.AccountState) 288 stateValidate.DirtyAccountState[k] = as 289 } 290 291 fmt.Println("init end") 292 293 }