7 "github.com/davecgh/go-spew/spew"
9 "github.com/vapor/consensus"
10 "github.com/vapor/database/storage"
11 "github.com/vapor/event"
12 "github.com/vapor/protocol/bc"
13 "github.com/vapor/protocol/bc/types"
14 "github.com/vapor/protocol/state"
15 "github.com/vapor/testutil"
18 var testTxs = []*types.Tx{
20 types.NewTx(types.TxData{
22 Inputs: []*types.TxInput{
23 types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 1, 1, []byte{0x51}),
25 Outputs: []*types.TxOutput{
26 types.NewIntraChainOutput(*consensus.BTMAssetID, 1, []byte{0x6a}),
30 types.NewTx(types.TxData{
32 Inputs: []*types.TxInput{
33 types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 1, 1, []byte{0x51}),
35 Outputs: []*types.TxOutput{
36 types.NewIntraChainOutput(*consensus.BTMAssetID, 1, []byte{0x6b}),
40 types.NewTx(types.TxData{
43 Inputs: []*types.TxInput{
44 types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 1, 1, []byte{0x51}),
45 types.NewSpendInput(nil, bc.NewHash([32]byte{0x02}), bc.NewAssetID([32]byte{0xa1}), 4, 1, []byte{0x51}),
47 Outputs: []*types.TxOutput{
48 types.NewIntraChainOutput(*consensus.BTMAssetID, 1, []byte{0x6b}),
49 types.NewIntraChainOutput(bc.NewAssetID([32]byte{0xa1}), 4, []byte{0x61}),
53 types.NewTx(types.TxData{
56 Inputs: []*types.TxInput{
57 types.NewSpendInput(nil, testutil.MustDecodeHash("7d3f8e8474775f9fab2a7370529f0569a2199b22a5a83d235a036f50de3e8c84"), bc.NewAssetID([32]byte{0xa1}), 4, 1, []byte{0x61}),
59 Outputs: []*types.TxOutput{
60 types.NewIntraChainOutput(bc.NewAssetID([32]byte{0xa1}), 3, []byte{0x62}),
61 types.NewIntraChainOutput(bc.NewAssetID([32]byte{0xa1}), 1, []byte{0x63}),
65 types.NewTx(types.TxData{
67 Inputs: []*types.TxInput{
68 types.NewSpendInput(nil, testutil.MustDecodeHash("9a26cde504a5d7190dbed119280276f9816d9c2b7d20c768b312be57930fe840"), bc.NewAssetID([32]byte{0xa1}), 3, 0, []byte{0x62}),
70 Outputs: []*types.TxOutput{
71 types.NewIntraChainOutput(bc.NewAssetID([32]byte{0xa1}), 2, []byte{0x64}),
72 types.NewIntraChainOutput(bc.NewAssetID([32]byte{0xa1}), 1, []byte{0x65}),
76 types.NewTx(types.TxData{
78 Inputs: []*types.TxInput{
79 types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 1, 1, []byte{0x51}),
81 Outputs: []*types.TxOutput{
82 types.NewIntraChainOutput(*consensus.BTMAssetID, 0, []byte{0x51}),
86 types.NewTx(types.TxData{
88 Inputs: []*types.TxInput{
89 types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 3, 1, []byte{0x51}),
90 types.NewSpendInput(nil, testutil.MustDecodeHash("9a26cde504a5d7190dbed119280276f9816d9c2b7d20c768b312be57930fe840"), bc.NewAssetID([32]byte{0xa1}), 3, 0, []byte{0x62}),
92 Outputs: []*types.TxOutput{
93 types.NewIntraChainOutput(*consensus.BTMAssetID, 2, []byte{0x51}),
94 types.NewIntraChainOutput(bc.NewAssetID([32]byte{0xa1}), 0, []byte{0x65}),
98 types.NewTx(types.TxData{
101 Inputs: []*types.TxInput{
102 types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 1, 1, []byte{0x51}),
103 types.NewSpendInput(nil, bc.NewHash([32]byte{0x02}), bc.NewAssetID([32]byte{0xa1}), 4, 1, []byte{0x51}),
105 Outputs: []*types.TxOutput{
106 types.NewIntraChainOutput(*consensus.BTMAssetID, 1, []byte{0x6b}),
107 types.NewVoteOutput(bc.NewAssetID([32]byte{0xa1}), 4, []byte{0x61}, []byte("a8f410b9f7cd9ce352d215ed17c85559c351dc8d18ed89ad403ca28cfc423f612e04a1c9584f945c286c47ec1e5b8405c65ff56e31f44a2627aca4f77e03936f")),
112 type mockStore struct{}
114 func (s *mockStore) BlockExist(hash *bc.Hash) bool { return false }
115 func (s *mockStore) GetBlock(*bc.Hash) (*types.Block, error) { return nil, nil }
116 func (s *mockStore) GetStoreStatus() *BlockStoreState { return nil }
117 func (s *mockStore) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
118 func (s *mockStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error { return nil }
119 func (s *mockStore) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
120 func (s *mockStore) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
121 func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
122 func (s *mockStore) SaveChainStatus(*state.BlockNode, *state.UtxoViewpoint) error { return nil }
124 func TestAddOrphan(t *testing.T) {
129 requireParents []*bc.Hash
133 orphans: map[bc.Hash]*orphanTx{},
134 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
137 orphans: map[bc.Hash]*orphanTx{
144 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
145 testTxs[0].SpentOutputIDs[0]: {
154 addOrphan: &TxDesc{Tx: testTxs[0]},
155 requireParents: []*bc.Hash{&testTxs[0].SpentOutputIDs[0]},
159 orphans: map[bc.Hash]*orphanTx{
166 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
167 testTxs[0].SpentOutputIDs[0]: {
177 orphans: map[bc.Hash]*orphanTx{
189 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
190 testTxs[0].SpentOutputIDs[0]: {
204 addOrphan: &TxDesc{Tx: testTxs[1]},
205 requireParents: []*bc.Hash{&testTxs[1].SpentOutputIDs[0]},
209 orphans: map[bc.Hash]*orphanTx{},
210 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
213 orphans: map[bc.Hash]*orphanTx{
220 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
221 testTxs[2].SpentOutputIDs[1]: {
230 addOrphan: &TxDesc{Tx: testTxs[2]},
231 requireParents: []*bc.Hash{&testTxs[2].SpentOutputIDs[1]},
235 for i, c := range cases {
236 c.before.addOrphan(c.addOrphan, c.requireParents)
237 for _, orphan := range c.before.orphans {
238 orphan.expiration = time.Time{}
240 for _, orphans := range c.before.orphansByPrev {
241 for _, orphan := range orphans {
242 orphan.expiration = time.Time{}
245 if !testutil.DeepEqual(c.before, c.after) {
246 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
251 func TestAddTransaction(t *testing.T) {
252 dispatcher := event.NewDispatcher()
260 pool: map[bc.Hash]*TxDesc{},
261 utxo: map[bc.Hash]*types.Tx{},
262 eventDispatcher: dispatcher,
265 pool: map[bc.Hash]*TxDesc{
271 utxo: map[bc.Hash]*types.Tx{
272 *testTxs[2].ResultIds[0]: testTxs[2],
273 *testTxs[2].ResultIds[1]: testTxs[2],
283 pool: map[bc.Hash]*TxDesc{},
284 utxo: map[bc.Hash]*types.Tx{},
285 eventDispatcher: dispatcher,
288 pool: map[bc.Hash]*TxDesc{
294 utxo: map[bc.Hash]*types.Tx{
295 *testTxs[2].ResultIds[0]: testTxs[2],
305 pool: map[bc.Hash]*TxDesc{},
306 utxo: map[bc.Hash]*types.Tx{},
307 eventDispatcher: dispatcher,
310 pool: map[bc.Hash]*TxDesc{
316 utxo: map[bc.Hash]*types.Tx{
317 *testTxs[7].ResultIds[0]: testTxs[7],
318 *testTxs[7].ResultIds[1]: testTxs[7],
328 pool: map[bc.Hash]*TxDesc{},
329 utxo: map[bc.Hash]*types.Tx{},
330 eventDispatcher: dispatcher,
333 pool: map[bc.Hash]*TxDesc{
339 utxo: map[bc.Hash]*types.Tx{
340 *testTxs[7].ResultIds[0]: testTxs[7],
350 for i, c := range cases {
351 c.before.addTransaction(c.addTx)
352 for _, txD := range c.before.pool {
353 txD.Added = time.Time{}
355 if !testutil.DeepEqual(c.before.pool, c.after.pool) {
356 t.Errorf("case %d: got %v want %v", i, c.before.pool, c.after.pool)
358 if !testutil.DeepEqual(c.before.utxo, c.after.utxo) {
359 t.Errorf("case %d: got %v want %v", i, c.before.utxo, c.after.utxo)
364 func TestExpireOrphan(t *testing.T) {
366 orphans: map[bc.Hash]*orphanTx{
368 expiration: time.Unix(1533489701, 0),
374 expiration: time.Unix(1633489701, 0),
380 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
381 testTxs[0].SpentOutputIDs[0]: {
383 expiration: time.Unix(1533489701, 0),
389 expiration: time.Unix(1633489701, 0),
399 orphans: map[bc.Hash]*orphanTx{
401 expiration: time.Unix(1633489701, 0),
407 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
408 testTxs[0].SpentOutputIDs[0]: {
410 expiration: time.Unix(1633489701, 0),
419 before.ExpireOrphan(time.Unix(1633479701, 0))
420 if !testutil.DeepEqual(before, want) {
421 t.Errorf("got %v want %v", before, want)
425 func TestProcessOrphans(t *testing.T) {
426 dispatcher := event.NewDispatcher()
434 pool: map[bc.Hash]*TxDesc{},
435 utxo: map[bc.Hash]*types.Tx{},
436 eventDispatcher: dispatcher,
437 orphans: map[bc.Hash]*orphanTx{
444 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
445 testTxs[3].SpentOutputIDs[0]: {
455 pool: map[bc.Hash]*TxDesc{
461 utxo: map[bc.Hash]*types.Tx{
462 *testTxs[3].ResultIds[0]: testTxs[3],
463 *testTxs[3].ResultIds[1]: testTxs[3],
465 eventDispatcher: dispatcher,
466 orphans: map[bc.Hash]*orphanTx{},
467 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
469 processTx: &TxDesc{Tx: testTxs[2]},
473 pool: map[bc.Hash]*TxDesc{},
474 utxo: map[bc.Hash]*types.Tx{},
475 eventDispatcher: dispatcher,
476 orphans: map[bc.Hash]*orphanTx{
488 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
489 testTxs[3].SpentOutputIDs[0]: {
496 testTxs[4].SpentOutputIDs[0]: {
506 pool: map[bc.Hash]*TxDesc{
516 utxo: map[bc.Hash]*types.Tx{
517 *testTxs[3].ResultIds[0]: testTxs[3],
518 *testTxs[3].ResultIds[1]: testTxs[3],
519 *testTxs[4].ResultIds[0]: testTxs[4],
520 *testTxs[4].ResultIds[1]: testTxs[4],
522 eventDispatcher: dispatcher,
523 orphans: map[bc.Hash]*orphanTx{},
524 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
526 processTx: &TxDesc{Tx: testTxs[2]},
530 for i, c := range cases {
531 c.before.store = &mockStore{}
532 c.before.addTransaction(c.processTx)
533 c.before.processOrphans(c.processTx)
534 c.before.RemoveTransaction(&c.processTx.Tx.ID)
536 c.before.lastUpdated = 0
537 for _, txD := range c.before.pool {
538 txD.Added = time.Time{}
541 if !testutil.DeepEqual(c.before, c.after) {
542 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
547 func TestRemoveOrphan(t *testing.T) {
551 removeHashes []*bc.Hash
555 orphans: map[bc.Hash]*orphanTx{
557 expiration: time.Unix(1533489701, 0),
563 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
564 testTxs[0].SpentOutputIDs[0]: {
566 expiration: time.Unix(1533489701, 0),
575 orphans: map[bc.Hash]*orphanTx{},
576 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
578 removeHashes: []*bc.Hash{
584 orphans: map[bc.Hash]*orphanTx{
586 expiration: time.Unix(1533489701, 0),
592 expiration: time.Unix(1533489701, 0),
598 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
599 testTxs[0].SpentOutputIDs[0]: {
601 expiration: time.Unix(1533489701, 0),
607 expiration: time.Unix(1533489701, 0),
616 orphans: map[bc.Hash]*orphanTx{
618 expiration: time.Unix(1533489701, 0),
624 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
625 testTxs[0].SpentOutputIDs[0]: {
627 expiration: time.Unix(1533489701, 0),
635 removeHashes: []*bc.Hash{
641 for i, c := range cases {
642 for _, hash := range c.removeHashes {
643 c.before.removeOrphan(hash)
645 if !testutil.DeepEqual(c.before, c.after) {
646 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
651 type mockStore1 struct{}
653 func (s *mockStore1) BlockExist(hash *bc.Hash) bool { return false }
654 func (s *mockStore1) GetBlock(*bc.Hash) (*types.Block, error) { return nil, nil }
655 func (s *mockStore1) GetStoreStatus() *BlockStoreState { return nil }
656 func (s *mockStore1) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
657 func (s *mockStore1) GetTransactionsUtxo(utxoView *state.UtxoViewpoint, tx []*bc.Tx) error {
658 for _, hash := range testTxs[2].SpentOutputIDs {
659 utxoView.Entries[hash] = &storage.UtxoEntry{IsCoinBase: false, Spent: false}
663 func (s *mockStore1) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
664 func (s *mockStore1) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
665 func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
666 func (s *mockStore1) SaveChainStatus(*state.BlockNode, *state.UtxoViewpoint) error { return nil }
668 func TestProcessTransaction(t *testing.T) {
670 pool: make(map[bc.Hash]*TxDesc),
671 utxo: make(map[bc.Hash]*types.Tx),
672 orphans: make(map[bc.Hash]*orphanTx),
673 orphansByPrev: make(map[bc.Hash]map[bc.Hash]*orphanTx),
674 store: &mockStore1{},
675 eventDispatcher: event.NewDispatcher(),
716 pool: map[bc.Hash]*TxDesc{
723 utxo: map[bc.Hash]*types.Tx{
724 *testTxs[2].ResultIds[0]: testTxs[2],
725 *testTxs[2].ResultIds[1]: testTxs[2],
735 for i, c := range cases {
736 txPool.ProcessTransaction(c.addTx.Tx, c.addTx.StatusFail, 0, 0)
737 for _, txD := range txPool.pool {
738 txD.Added = time.Time{}
740 for _, txD := range txPool.orphans {
741 txD.Added = time.Time{}
742 txD.expiration = time.Time{}
745 if !testutil.DeepEqual(txPool.pool, c.want.pool) {
746 t.Errorf("case %d: test ProcessTransaction pool mismatch got %s want %s", i, spew.Sdump(txPool.pool), spew.Sdump(c.want.pool))
748 if !testutil.DeepEqual(txPool.utxo, c.want.utxo) {
749 t.Errorf("case %d: test ProcessTransaction utxo mismatch got %s want %s", i, spew.Sdump(txPool.utxo), spew.Sdump(c.want.utxo))
751 if !testutil.DeepEqual(txPool.orphans, c.want.orphans) {
752 t.Errorf("case %d: test ProcessTransaction orphans mismatch got %s want %s", i, spew.Sdump(txPool.orphans), spew.Sdump(c.want.orphans))
754 if !testutil.DeepEqual(txPool.orphansByPrev, c.want.orphansByPrev) {
755 t.Errorf("case %d: test ProcessTransaction orphansByPrev mismatch got %s want %s", i, spew.Sdump(txPool.orphansByPrev), spew.Sdump(c.want.orphansByPrev))