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