Golang implemented sidechain for Bytom
Revision | 6c9119c5017db9dcb13a0d1f1b74a4441bc5eaf4 (tree) |
---|---|
Time | 2020-01-19 17:16:42 |
Author | shenao78 <shenao.78@163....> |
Commiter | shenao78 |
complement mov test
@@ -207,6 +207,30 @@ var ( | ||
207 | 207 | }), |
208 | 208 | } |
209 | 209 | |
210 | + Eos2EtcMakerTxs = []*types.Tx{ | |
211 | + // Eos2Etc[0] | |
212 | + types.NewTx(types.TxData{ | |
213 | + Inputs: []*types.TxInput{types.NewSpendInput(nil, *Eos2EtcOrders[0].Utxo.SourceID, *Eos2EtcOrders[0].FromAssetID, Eos2EtcOrders[0].Utxo.Amount, Eos2EtcOrders[0].Utxo.SourcePos, []byte{0x51})}, | |
214 | + Outputs: []*types.TxOutput{types.NewIntraChainOutput(*Eos2EtcOrders[0].FromAssetID, Eos2EtcOrders[0].Utxo.Amount, Eos2EtcOrders[0].Utxo.ControlProgram)}, | |
215 | + }), | |
216 | + } | |
217 | + | |
218 | + Etc2EosMakerTxs = []*types.Tx{ | |
219 | + // Etc2Eos[0] | |
220 | + types.NewTx(types.TxData{ | |
221 | + Inputs: []*types.TxInput{types.NewSpendInput(nil, *Etc2EosOrders[0].Utxo.SourceID, *Etc2EosOrders[0].FromAssetID, Etc2EosOrders[0].Utxo.Amount, Etc2EosOrders[0].Utxo.SourcePos, []byte{0x51})}, | |
222 | + Outputs: []*types.TxOutput{types.NewIntraChainOutput(*Etc2EosOrders[0].FromAssetID, Etc2EosOrders[0].Utxo.Amount, Etc2EosOrders[0].Utxo.ControlProgram)}, | |
223 | + }), | |
224 | + } | |
225 | + | |
226 | + Eth2EosMakerTxs = []*types.Tx{ | |
227 | + // Eth2Eos[0] | |
228 | + types.NewTx(types.TxData{ | |
229 | + Inputs: []*types.TxInput{types.NewSpendInput(nil, *Eth2EosOrders[0].Utxo.SourceID, *Eth2EosOrders[0].FromAssetID, Eth2EosOrders[0].Utxo.Amount, Eth2EosOrders[0].Utxo.SourcePos, []byte{0x51})}, | |
230 | + Outputs: []*types.TxOutput{types.NewIntraChainOutput(*Eth2EosOrders[0].FromAssetID, Eth2EosOrders[0].Utxo.Amount, Eth2EosOrders[0].Utxo.ControlProgram)}, | |
231 | + }), | |
232 | + } | |
233 | + | |
210 | 234 | MatchedTxs = []*types.Tx{ |
211 | 235 | // partial matched transaction from Btc2EthOrders[0], Eth2BtcOrders[1] |
212 | 236 | types.NewTx(types.TxData{ |
@@ -280,11 +304,11 @@ var ( | ||
280 | 304 | }, |
281 | 305 | }), |
282 | 306 | |
283 | - // full matched transaction from Btc2Eth Eth2Eos Eos2Btc | |
307 | + // full matched transaction from Eos2Etc[0] Etc2Eos[0] | |
284 | 308 | types.NewTx(types.TxData{ |
285 | 309 | Inputs: []*types.TxInput{ |
286 | - types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, *Eos2EtcOrders[0].Utxo.SourceID, *Eos2EtcOrders[0].FromAssetID, Eos2EtcOrders[0].Utxo.Amount, Eos2EtcOrders[0].Utxo.SourcePos, Eos2EtcOrders[0].Utxo.ControlProgram), | |
287 | - types.NewSpendInput([][]byte{vm.Int64Bytes(1), vm.Int64Bytes(1)}, *Etc2EosOrders[0].Utxo.SourceID, *Etc2EosOrders[0].FromAssetID, Etc2EosOrders[0].Utxo.Amount, Etc2EosOrders[0].Utxo.SourcePos, Etc2EosOrders[0].Utxo.ControlProgram), | |
310 | + types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, *MustNewOrderFromOutput(Eos2EtcMakerTxs[0], 0).Utxo.SourceID, *Eos2EtcOrders[0].FromAssetID, Eos2EtcOrders[0].Utxo.Amount, Eos2EtcOrders[0].Utxo.SourcePos, Eos2EtcOrders[0].Utxo.ControlProgram), | |
311 | + types.NewSpendInput([][]byte{vm.Int64Bytes(1), vm.Int64Bytes(1)}, *MustNewOrderFromOutput(Etc2EosMakerTxs[0], 0).Utxo.SourceID, *Etc2EosOrders[0].FromAssetID, Etc2EosOrders[0].Utxo.Amount, Etc2EosOrders[0].Utxo.SourcePos, Etc2EosOrders[0].Utxo.ControlProgram), | |
288 | 312 | }, |
289 | 313 | Outputs: []*types.TxOutput{ |
290 | 314 | types.NewIntraChainOutput(*Eos2EtcOrders[0].ToAssetID, 50, testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19255")), |
@@ -292,7 +316,7 @@ var ( | ||
292 | 316 | }, |
293 | 317 | }), |
294 | 318 | |
295 | - // cycle matched | |
319 | + // cycle matched from Btc2Eth Eth2Eos Eos2Btc | |
296 | 320 | types.NewTx(types.TxData{ |
297 | 321 | Inputs: []*types.TxInput{ |
298 | 322 | types.NewSpendInput([][]byte{vm.Int64Bytes(0), vm.Int64Bytes(1)}, *Btc2EthOrders[0].Utxo.SourceID, *Btc2EthOrders[0].FromAssetID, Btc2EthOrders[0].Utxo.Amount, Btc2EthOrders[0].Utxo.SourcePos, Btc2EthOrders[0].Utxo.ControlProgram), |
@@ -16,15 +16,9 @@ import ( | ||
16 | 16 | "github.com/bytom/vapor/testutil" |
17 | 17 | ) |
18 | 18 | |
19 | -/* | |
20 | - @addTest:BeforeProposalBlock: will gas affect generate tx? will be packed tx affect generate tx? | |
21 | - @addTest:TestApplyBlock: one block has two different trade pairs & different trade pair won't affect each order(attach & detach) | |
22 | - @addTest:TestApplyBlock: node packed maker tx and match transaction in random order(attach & detach) | |
23 | - @addTest:TestValidateBlock: one tx has trade input and cancel input mixed | |
24 | - @addTest:TestValidateBlock: regular match transaction's seller program is also a P2WMCProgram | |
25 | -*/ | |
19 | +var initBlockHeader = &types.BlockHeader{Height: 1, PreviousBlockHash: bc.Hash{}} | |
20 | + | |
26 | 21 | func TestApplyBlock(t *testing.T) { |
27 | - initBlockHeader := &types.BlockHeader{Height: 1, PreviousBlockHash: bc.Hash{}} | |
28 | 22 | cases := []struct { |
29 | 23 | desc string |
30 | 24 | block *types.Block |
@@ -47,6 +41,26 @@ func TestApplyBlock(t *testing.T) { | ||
47 | 41 | wantDBState: &common.MovDatabaseState{Height: 2, Hash: hashPtr(testutil.MustDecodeHash("88dbcde57bb2b53b107d7494f20f1f1a892307a019705980c3510890449c0020"))}, |
48 | 42 | }, |
49 | 43 | { |
44 | + desc: "apply block has two different trade pairs & different trade pair won't affect each order", | |
45 | + block: &types.Block{ | |
46 | + BlockHeader: types.BlockHeader{Height: 2, PreviousBlockHash: initBlockHeader.Hash()}, | |
47 | + Transactions: []*types.Tx{ | |
48 | + mock.Btc2EthMakerTxs[0], | |
49 | + mock.Eth2BtcMakerTxs[0], | |
50 | + mock.Eos2EtcMakerTxs[0], | |
51 | + mock.Eth2EosMakerTxs[0], | |
52 | + }, | |
53 | + }, | |
54 | + blockFunc: applyBlock, | |
55 | + wantOrders: []*common.Order{ | |
56 | + mock.MustNewOrderFromOutput(mock.Btc2EthMakerTxs[0], 0), | |
57 | + mock.MustNewOrderFromOutput(mock.Eth2BtcMakerTxs[0], 0), | |
58 | + mock.MustNewOrderFromOutput(mock.Eos2EtcMakerTxs[0], 0), | |
59 | + mock.MustNewOrderFromOutput(mock.Eth2EosMakerTxs[0], 0), | |
60 | + }, | |
61 | + wantDBState: &common.MovDatabaseState{Height: 2, Hash: hashPtr(testutil.MustDecodeHash("88dbcde57bb2b53b107d7494f20f1f1a892307a019705980c3510890449c0020"))}, | |
62 | + }, | |
63 | + { | |
50 | 64 | desc: "apply block has full matched transaction", |
51 | 65 | block: &types.Block{ |
52 | 66 | BlockHeader: types.BlockHeader{Height: 2, PreviousBlockHash: initBlockHeader.Hash()}, |
@@ -101,6 +115,28 @@ func TestApplyBlock(t *testing.T) { | ||
101 | 115 | wantDBState: &common.MovDatabaseState{Height: 2, Hash: hashPtr(testutil.MustDecodeHash("88dbcde57bb2b53b107d7494f20f1f1a892307a019705980c3510890449c0020"))}, |
102 | 116 | }, |
103 | 117 | { |
118 | + desc: "apply block which node packed maker tx and match transaction in random orde", | |
119 | + block: &types.Block{ | |
120 | + BlockHeader: types.BlockHeader{Height: 2, PreviousBlockHash: initBlockHeader.Hash()}, | |
121 | + Transactions: []*types.Tx{ | |
122 | + mock.Eos2EtcMakerTxs[0], | |
123 | + mock.Btc2EthMakerTxs[0], | |
124 | + mock.MatchedTxs[4], | |
125 | + mock.Eth2EosMakerTxs[0], | |
126 | + mock.Eth2BtcMakerTxs[1], | |
127 | + mock.MatchedTxs[5], | |
128 | + mock.Etc2EosMakerTxs[0], | |
129 | + }, | |
130 | + }, | |
131 | + blockFunc: applyBlock, | |
132 | + initOrders: []*common.Order{}, | |
133 | + wantOrders: []*common.Order{ | |
134 | + mock.MustNewOrderFromOutput(mock.MatchedTxs[4], 1), | |
135 | + mock.MustNewOrderFromOutput(mock.Eth2EosMakerTxs[0], 0), | |
136 | + }, | |
137 | + wantDBState: &common.MovDatabaseState{Height: 2, Hash: hashPtr(testutil.MustDecodeHash("88dbcde57bb2b53b107d7494f20f1f1a892307a019705980c3510890449c0020"))}, | |
138 | + }, | |
139 | + { | |
104 | 140 | desc: "detach block has pending order transaction", |
105 | 141 | block: &types.Block{ |
106 | 142 | BlockHeader: *initBlockHeader, |
@@ -114,6 +150,27 @@ func TestApplyBlock(t *testing.T) { | ||
114 | 150 | wantDBState: &common.MovDatabaseState{Height: 0, Hash: &bc.Hash{}}, |
115 | 151 | }, |
116 | 152 | { |
153 | + desc: "detach block has two different trade pairs & different trade pair won't affect each order", | |
154 | + block: &types.Block{ | |
155 | + BlockHeader: *initBlockHeader, | |
156 | + Transactions: []*types.Tx{ | |
157 | + mock.Btc2EthMakerTxs[0], | |
158 | + mock.Eth2BtcMakerTxs[0], | |
159 | + mock.Eos2EtcMakerTxs[0], | |
160 | + mock.Eth2EosMakerTxs[0], | |
161 | + }, | |
162 | + }, | |
163 | + blockFunc: detachBlock, | |
164 | + initOrders: []*common.Order{ | |
165 | + mock.MustNewOrderFromOutput(mock.Btc2EthMakerTxs[0], 0), | |
166 | + mock.MustNewOrderFromOutput(mock.Eth2BtcMakerTxs[0], 0), | |
167 | + mock.MustNewOrderFromOutput(mock.Eos2EtcMakerTxs[0], 0), | |
168 | + mock.MustNewOrderFromOutput(mock.Eth2EosMakerTxs[0], 0), | |
169 | + }, | |
170 | + wantOrders: []*common.Order{}, | |
171 | + wantDBState: &common.MovDatabaseState{Height: 0, Hash: &bc.Hash{}}, | |
172 | + }, | |
173 | + { | |
117 | 174 | desc: "detach block has full matched transaction", |
118 | 175 | block: &types.Block{ |
119 | 176 | BlockHeader: *initBlockHeader, |
@@ -152,6 +209,28 @@ func TestApplyBlock(t *testing.T) { | ||
152 | 209 | wantOrders: []*common.Order{mock.Btc2EthOrders[0], mock.Btc2EthOrders[1], mock.Eth2BtcOrders[2]}, |
153 | 210 | wantDBState: &common.MovDatabaseState{Height: 0, Hash: &bc.Hash{}}, |
154 | 211 | }, |
212 | + { | |
213 | + desc: "detach block which node packed maker tx and match transaction in random orde", | |
214 | + block: &types.Block{ | |
215 | + BlockHeader: *initBlockHeader, | |
216 | + Transactions: []*types.Tx{ | |
217 | + mock.Eos2EtcMakerTxs[0], | |
218 | + mock.Btc2EthMakerTxs[0], | |
219 | + mock.MatchedTxs[4], | |
220 | + mock.Eth2EosMakerTxs[0], | |
221 | + mock.Eth2BtcMakerTxs[1], | |
222 | + mock.MatchedTxs[5], | |
223 | + mock.Etc2EosMakerTxs[0], | |
224 | + }, | |
225 | + }, | |
226 | + blockFunc: detachBlock, | |
227 | + initOrders: []*common.Order{ | |
228 | + mock.MustNewOrderFromOutput(mock.MatchedTxs[4], 1), | |
229 | + mock.MustNewOrderFromOutput(mock.Eth2EosMakerTxs[0], 0), | |
230 | + }, | |
231 | + wantOrders: []*common.Order{}, | |
232 | + wantDBState: &common.MovDatabaseState{Height: 0, Hash: &bc.Hash{}}, | |
233 | + }, | |
155 | 234 | } |
156 | 235 | |
157 | 236 | defer os.RemoveAll("temp") |
@@ -411,6 +490,83 @@ func TestValidateBlock(t *testing.T) { | ||
411 | 490 | } |
412 | 491 | } |
413 | 492 | |
493 | +func TestBeforeProposalBlock(t *testing.T) { | |
494 | + cases := []struct { | |
495 | + desc string | |
496 | + initOrders []*common.Order | |
497 | + gasLeft int64 | |
498 | + wantMatchedTxs []*types.Tx | |
499 | + }{ | |
500 | + { | |
501 | + desc: "has matched tx, but gas left is zero", | |
502 | + initOrders: []*common.Order{mock.Btc2EthOrders[0], mock.Eth2BtcOrders[0]}, | |
503 | + gasLeft: 0, | |
504 | + wantMatchedTxs: []*types.Tx{}, | |
505 | + }, | |
506 | + { | |
507 | + desc: "has one matched tx, and gas is sufficient", | |
508 | + initOrders: []*common.Order{mock.Btc2EthOrders[0], mock.Eth2BtcOrders[0]}, | |
509 | + gasLeft: 2000, | |
510 | + wantMatchedTxs: []*types.Tx{mock.MatchedTxs[1]}, | |
511 | + }, | |
512 | + { | |
513 | + desc: "has two matched tx, but gas is only enough to pack a matched tx", | |
514 | + initOrders: []*common.Order{ | |
515 | + mock.Btc2EthOrders[0], | |
516 | + mock.Btc2EthOrders[1], | |
517 | + mock.Eth2BtcOrders[2], | |
518 | + }, | |
519 | + gasLeft: 2000, | |
520 | + wantMatchedTxs: []*types.Tx{mock.MatchedTxs[2]}, | |
521 | + }, | |
522 | + { | |
523 | + desc: "has two matched tx, and gas left is sufficient", | |
524 | + initOrders: []*common.Order{ | |
525 | + mock.Btc2EthOrders[0], | |
526 | + mock.Btc2EthOrders[1], | |
527 | + mock.Eth2BtcOrders[2], | |
528 | + }, | |
529 | + gasLeft: 4000, | |
530 | + wantMatchedTxs: []*types.Tx{mock.MatchedTxs[2], mock.MatchedTxs[3]}, | |
531 | + }, | |
532 | + } | |
533 | + | |
534 | + for i, c := range cases { | |
535 | + testDB := dbm.NewDB("testdb", "leveldb", "temp") | |
536 | + store := database.NewLevelDBMovStore(testDB) | |
537 | + if err := store.InitDBState(0, &bc.Hash{}); err != nil { | |
538 | + t.Fatal(err) | |
539 | + } | |
540 | + | |
541 | + if err := store.ProcessOrders(c.initOrders, nil, initBlockHeader); err != nil { | |
542 | + t.Fatal(err) | |
543 | + } | |
544 | + | |
545 | + movCore := &MovCore{movStore: store} | |
546 | + gotMatchedTxs, err := movCore.BeforeProposalBlock(nil, []byte{0x51}, 2, c.gasLeft, func() bool { return false }) | |
547 | + if err != nil { | |
548 | + t.Fatal(err) | |
549 | + } | |
550 | + | |
551 | + gotMatchedTxMap := make(map[string]interface{}) | |
552 | + for _, matchedTx := range gotMatchedTxs { | |
553 | + gotMatchedTxMap[matchedTx.ID.String()] = nil | |
554 | + } | |
555 | + | |
556 | + wantMatchedTxMap := make(map[string]interface{}) | |
557 | + for _, matchedTx := range c.wantMatchedTxs { | |
558 | + wantMatchedTxMap[matchedTx.ID.String()] = nil | |
559 | + } | |
560 | + | |
561 | + if !testutil.DeepEqual(gotMatchedTxMap, wantMatchedTxMap) { | |
562 | + t.Errorf("#%d(%s):want matched tx(%v) is not equals got matched tx(%v)", i, c.desc, c.wantMatchedTxs, gotMatchedTxs) | |
563 | + } | |
564 | + | |
565 | + testDB.Close() | |
566 | + os.RemoveAll("temp") | |
567 | + } | |
568 | +} | |
569 | + | |
414 | 570 | type testFun func(movCore *MovCore, block *types.Block) error |
415 | 571 | |
416 | 572 | func applyBlock(movCore *MovCore, block *types.Block) error { |