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) GetBlockHeader(*bc.Hash) (*types.BlockHeader, error) { return nil, nil }
117 func (s *mockStore) GetStoreStatus() *BlockStoreState { return nil }
118 func (s *mockStore) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
119 func (s *mockStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error { return nil }
120 func (s *mockStore) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
121 func (s *mockStore) GetConsensusResult(uint64) (*state.ConsensusResult, error) { return nil, nil }
122 func (s *mockStore) GetMainChainHash(uint64) (*bc.Hash, error) { return nil, nil }
123 func (s *mockStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil }
124 func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
125 func (s *mockStore) SaveBlockHeader(*types.BlockHeader) error { return nil }
126 func (s *mockStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
130 func TestAddOrphan(t *testing.T) {
135 requireParents []*bc.Hash
139 orphans: map[bc.Hash]*orphanTx{},
140 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
143 orphans: map[bc.Hash]*orphanTx{
150 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
151 testTxs[0].SpentOutputIDs[0]: {
160 addOrphan: &TxDesc{Tx: testTxs[0]},
161 requireParents: []*bc.Hash{&testTxs[0].SpentOutputIDs[0]},
165 orphans: map[bc.Hash]*orphanTx{
172 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
173 testTxs[0].SpentOutputIDs[0]: {
183 orphans: map[bc.Hash]*orphanTx{
195 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
196 testTxs[0].SpentOutputIDs[0]: {
210 addOrphan: &TxDesc{Tx: testTxs[1]},
211 requireParents: []*bc.Hash{&testTxs[1].SpentOutputIDs[0]},
215 orphans: map[bc.Hash]*orphanTx{},
216 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
219 orphans: map[bc.Hash]*orphanTx{
226 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
227 testTxs[2].SpentOutputIDs[1]: {
236 addOrphan: &TxDesc{Tx: testTxs[2]},
237 requireParents: []*bc.Hash{&testTxs[2].SpentOutputIDs[1]},
241 for i, c := range cases {
242 c.before.addOrphan(c.addOrphan, c.requireParents)
243 for _, orphan := range c.before.orphans {
244 orphan.expiration = time.Time{}
246 for _, orphans := range c.before.orphansByPrev {
247 for _, orphan := range orphans {
248 orphan.expiration = time.Time{}
251 if !testutil.DeepEqual(c.before, c.after) {
252 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
257 func TestAddTransaction(t *testing.T) {
258 dispatcher := event.NewDispatcher()
266 pool: map[bc.Hash]*TxDesc{},
267 utxo: map[bc.Hash]*types.Tx{},
268 eventDispatcher: dispatcher,
271 pool: map[bc.Hash]*TxDesc{
277 utxo: map[bc.Hash]*types.Tx{
278 *testTxs[2].ResultIds[0]: testTxs[2],
279 *testTxs[2].ResultIds[1]: testTxs[2],
289 pool: map[bc.Hash]*TxDesc{},
290 utxo: map[bc.Hash]*types.Tx{},
291 eventDispatcher: dispatcher,
294 pool: map[bc.Hash]*TxDesc{
300 utxo: map[bc.Hash]*types.Tx{
301 *testTxs[2].ResultIds[0]: testTxs[2],
311 pool: map[bc.Hash]*TxDesc{},
312 utxo: map[bc.Hash]*types.Tx{},
313 eventDispatcher: dispatcher,
316 pool: map[bc.Hash]*TxDesc{
322 utxo: map[bc.Hash]*types.Tx{
323 *testTxs[7].ResultIds[0]: testTxs[7],
324 *testTxs[7].ResultIds[1]: testTxs[7],
334 pool: map[bc.Hash]*TxDesc{},
335 utxo: map[bc.Hash]*types.Tx{},
336 eventDispatcher: dispatcher,
339 pool: map[bc.Hash]*TxDesc{
345 utxo: map[bc.Hash]*types.Tx{
346 *testTxs[7].ResultIds[0]: testTxs[7],
356 for i, c := range cases {
357 c.before.addTransaction(c.addTx)
358 for _, txD := range c.before.pool {
359 txD.Added = time.Time{}
361 if !testutil.DeepEqual(c.before.pool, c.after.pool) {
362 t.Errorf("case %d: got %v want %v", i, c.before.pool, c.after.pool)
364 if !testutil.DeepEqual(c.before.utxo, c.after.utxo) {
365 t.Errorf("case %d: got %v want %v", i, c.before.utxo, c.after.utxo)
370 func TestExpireOrphan(t *testing.T) {
372 orphans: map[bc.Hash]*orphanTx{
374 expiration: time.Unix(1533489701, 0),
380 expiration: time.Unix(1633489701, 0),
386 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
387 testTxs[0].SpentOutputIDs[0]: {
389 expiration: time.Unix(1533489701, 0),
395 expiration: time.Unix(1633489701, 0),
405 orphans: map[bc.Hash]*orphanTx{
407 expiration: time.Unix(1633489701, 0),
413 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
414 testTxs[0].SpentOutputIDs[0]: {
416 expiration: time.Unix(1633489701, 0),
425 before.ExpireOrphan(time.Unix(1633479701, 0))
426 if !testutil.DeepEqual(before, want) {
427 t.Errorf("got %v want %v", before, want)
431 func TestProcessOrphans(t *testing.T) {
432 dispatcher := event.NewDispatcher()
440 pool: map[bc.Hash]*TxDesc{},
441 utxo: map[bc.Hash]*types.Tx{},
442 eventDispatcher: dispatcher,
443 orphans: map[bc.Hash]*orphanTx{
450 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
451 testTxs[3].SpentOutputIDs[0]: {
461 pool: map[bc.Hash]*TxDesc{
467 utxo: map[bc.Hash]*types.Tx{
468 *testTxs[3].ResultIds[0]: testTxs[3],
469 *testTxs[3].ResultIds[1]: testTxs[3],
471 eventDispatcher: dispatcher,
472 orphans: map[bc.Hash]*orphanTx{},
473 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
475 processTx: &TxDesc{Tx: testTxs[2]},
479 pool: map[bc.Hash]*TxDesc{},
480 utxo: map[bc.Hash]*types.Tx{},
481 eventDispatcher: dispatcher,
482 orphans: map[bc.Hash]*orphanTx{
494 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
495 testTxs[3].SpentOutputIDs[0]: {
502 testTxs[4].SpentOutputIDs[0]: {
512 pool: map[bc.Hash]*TxDesc{
522 utxo: map[bc.Hash]*types.Tx{
523 *testTxs[3].ResultIds[0]: testTxs[3],
524 *testTxs[3].ResultIds[1]: testTxs[3],
525 *testTxs[4].ResultIds[0]: testTxs[4],
526 *testTxs[4].ResultIds[1]: testTxs[4],
528 eventDispatcher: dispatcher,
529 orphans: map[bc.Hash]*orphanTx{},
530 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
532 processTx: &TxDesc{Tx: testTxs[2]},
536 for i, c := range cases {
537 c.before.store = &mockStore{}
538 c.before.addTransaction(c.processTx)
539 c.before.processOrphans(c.processTx)
540 c.before.RemoveTransaction(&c.processTx.Tx.ID)
542 c.before.lastUpdated = 0
543 for _, txD := range c.before.pool {
544 txD.Added = time.Time{}
547 if !testutil.DeepEqual(c.before, c.after) {
548 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
553 func TestRemoveOrphan(t *testing.T) {
557 removeHashes []*bc.Hash
561 orphans: map[bc.Hash]*orphanTx{
563 expiration: time.Unix(1533489701, 0),
569 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
570 testTxs[0].SpentOutputIDs[0]: {
572 expiration: time.Unix(1533489701, 0),
581 orphans: map[bc.Hash]*orphanTx{},
582 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
584 removeHashes: []*bc.Hash{
590 orphans: map[bc.Hash]*orphanTx{
592 expiration: time.Unix(1533489701, 0),
598 expiration: time.Unix(1533489701, 0),
604 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
605 testTxs[0].SpentOutputIDs[0]: {
607 expiration: time.Unix(1533489701, 0),
613 expiration: time.Unix(1533489701, 0),
622 orphans: map[bc.Hash]*orphanTx{
624 expiration: time.Unix(1533489701, 0),
630 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
631 testTxs[0].SpentOutputIDs[0]: {
633 expiration: time.Unix(1533489701, 0),
641 removeHashes: []*bc.Hash{
647 for i, c := range cases {
648 for _, hash := range c.removeHashes {
649 c.before.removeOrphan(hash)
651 if !testutil.DeepEqual(c.before, c.after) {
652 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
657 type mockStore1 struct{}
659 func (s *mockStore1) BlockExist(hash *bc.Hash) bool { return false }
660 func (s *mockStore1) GetBlock(*bc.Hash) (*types.Block, error) { return nil, nil }
661 func (s *mockStore1) GetBlockHeader(*bc.Hash) (*types.BlockHeader, error) { return nil, nil }
662 func (s *mockStore1) GetStoreStatus() *BlockStoreState { return nil }
663 func (s *mockStore1) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
664 func (s *mockStore1) GetTransactionsUtxo(utxoView *state.UtxoViewpoint, tx []*bc.Tx) error {
666 for _, hash := range testTxs[2].SpentOutputIDs {
667 utxoView.Entries[hash] = &storage.UtxoEntry{Type: storage.NormalUTXOType, Spent: false}
671 func (s *mockStore1) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
672 func (s *mockStore1) GetConsensusResult(uint64) (*state.ConsensusResult, error) { return nil, nil }
673 func (s *mockStore1) GetMainChainHash(uint64) (*bc.Hash, error) { return nil, nil }
674 func (s *mockStore1) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil }
675 func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
676 func (s *mockStore1) SaveBlockHeader(*types.BlockHeader) error { return nil }
677 func (s *mockStore1) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
681 func TestProcessTransaction(t *testing.T) {
683 pool: make(map[bc.Hash]*TxDesc),
684 utxo: make(map[bc.Hash]*types.Tx),
685 orphans: make(map[bc.Hash]*orphanTx),
686 orphansByPrev: make(map[bc.Hash]map[bc.Hash]*orphanTx),
687 store: &mockStore1{},
688 eventDispatcher: event.NewDispatcher(),
729 pool: map[bc.Hash]*TxDesc{
736 utxo: map[bc.Hash]*types.Tx{
737 *testTxs[2].ResultIds[0]: testTxs[2],
738 *testTxs[2].ResultIds[1]: testTxs[2],
748 for i, c := range cases {
749 txPool.ProcessTransaction(c.addTx.Tx, c.addTx.StatusFail, 0, 0)
750 for _, txD := range txPool.pool {
751 txD.Added = time.Time{}
753 for _, txD := range txPool.orphans {
754 txD.Added = time.Time{}
755 txD.expiration = time.Time{}
758 if !testutil.DeepEqual(txPool.pool, c.want.pool) {
759 t.Errorf("case %d: test ProcessTransaction pool mismatch got %s want %s", i, spew.Sdump(txPool.pool), spew.Sdump(c.want.pool))
761 if !testutil.DeepEqual(txPool.utxo, c.want.utxo) {
762 t.Errorf("case %d: test ProcessTransaction utxo mismatch got %s want %s", i, spew.Sdump(txPool.utxo), spew.Sdump(c.want.utxo))
764 if !testutil.DeepEqual(txPool.orphans, c.want.orphans) {
765 t.Errorf("case %d: test ProcessTransaction orphans mismatch got %s want %s", i, spew.Sdump(txPool.orphans), spew.Sdump(c.want.orphans))
767 if !testutil.DeepEqual(txPool.orphansByPrev, c.want.orphansByPrev) {
768 t.Errorf("case %d: test ProcessTransaction orphansByPrev mismatch got %s want %s", i, spew.Sdump(txPool.orphansByPrev), spew.Sdump(c.want.orphansByPrev))