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) GetVoteResult(uint64) (*state.VoteResult, error) { return nil, nil }
121 func (s *mockStore) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
122 func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
123 func (s *mockStore) SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, map[uint64]*state.VoteResult) error { return nil }
124 func (s *mockStore) SaveChainNodeStatus(*state.BlockNode, *state.BlockNode) error { return nil }
126 func TestAddOrphan(t *testing.T) {
131 requireParents []*bc.Hash
135 orphans: map[bc.Hash]*orphanTx{},
136 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
139 orphans: map[bc.Hash]*orphanTx{
146 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
147 testTxs[0].SpentOutputIDs[0]: {
156 addOrphan: &TxDesc{Tx: testTxs[0]},
157 requireParents: []*bc.Hash{&testTxs[0].SpentOutputIDs[0]},
161 orphans: map[bc.Hash]*orphanTx{
168 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
169 testTxs[0].SpentOutputIDs[0]: {
179 orphans: map[bc.Hash]*orphanTx{
191 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
192 testTxs[0].SpentOutputIDs[0]: {
206 addOrphan: &TxDesc{Tx: testTxs[1]},
207 requireParents: []*bc.Hash{&testTxs[1].SpentOutputIDs[0]},
211 orphans: map[bc.Hash]*orphanTx{},
212 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
215 orphans: map[bc.Hash]*orphanTx{
222 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
223 testTxs[2].SpentOutputIDs[1]: {
232 addOrphan: &TxDesc{Tx: testTxs[2]},
233 requireParents: []*bc.Hash{&testTxs[2].SpentOutputIDs[1]},
237 for i, c := range cases {
238 c.before.addOrphan(c.addOrphan, c.requireParents)
239 for _, orphan := range c.before.orphans {
240 orphan.expiration = time.Time{}
242 for _, orphans := range c.before.orphansByPrev {
243 for _, orphan := range orphans {
244 orphan.expiration = time.Time{}
247 if !testutil.DeepEqual(c.before, c.after) {
248 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
253 func TestAddTransaction(t *testing.T) {
254 dispatcher := event.NewDispatcher()
262 pool: map[bc.Hash]*TxDesc{},
263 utxo: map[bc.Hash]*types.Tx{},
264 eventDispatcher: dispatcher,
267 pool: map[bc.Hash]*TxDesc{
273 utxo: map[bc.Hash]*types.Tx{
274 *testTxs[2].ResultIds[0]: testTxs[2],
275 *testTxs[2].ResultIds[1]: testTxs[2],
285 pool: map[bc.Hash]*TxDesc{},
286 utxo: map[bc.Hash]*types.Tx{},
287 eventDispatcher: dispatcher,
290 pool: map[bc.Hash]*TxDesc{
296 utxo: map[bc.Hash]*types.Tx{
297 *testTxs[2].ResultIds[0]: testTxs[2],
307 pool: map[bc.Hash]*TxDesc{},
308 utxo: map[bc.Hash]*types.Tx{},
309 eventDispatcher: dispatcher,
312 pool: map[bc.Hash]*TxDesc{
318 utxo: map[bc.Hash]*types.Tx{
319 *testTxs[7].ResultIds[0]: testTxs[7],
320 *testTxs[7].ResultIds[1]: testTxs[7],
330 pool: map[bc.Hash]*TxDesc{},
331 utxo: map[bc.Hash]*types.Tx{},
332 eventDispatcher: dispatcher,
335 pool: map[bc.Hash]*TxDesc{
341 utxo: map[bc.Hash]*types.Tx{
342 *testTxs[7].ResultIds[0]: testTxs[7],
352 for i, c := range cases {
353 c.before.addTransaction(c.addTx)
354 for _, txD := range c.before.pool {
355 txD.Added = time.Time{}
357 if !testutil.DeepEqual(c.before.pool, c.after.pool) {
358 t.Errorf("case %d: got %v want %v", i, c.before.pool, c.after.pool)
360 if !testutil.DeepEqual(c.before.utxo, c.after.utxo) {
361 t.Errorf("case %d: got %v want %v", i, c.before.utxo, c.after.utxo)
366 func TestExpireOrphan(t *testing.T) {
368 orphans: map[bc.Hash]*orphanTx{
370 expiration: time.Unix(1533489701, 0),
376 expiration: time.Unix(1633489701, 0),
382 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
383 testTxs[0].SpentOutputIDs[0]: {
385 expiration: time.Unix(1533489701, 0),
391 expiration: time.Unix(1633489701, 0),
401 orphans: map[bc.Hash]*orphanTx{
403 expiration: time.Unix(1633489701, 0),
409 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
410 testTxs[0].SpentOutputIDs[0]: {
412 expiration: time.Unix(1633489701, 0),
421 before.ExpireOrphan(time.Unix(1633479701, 0))
422 if !testutil.DeepEqual(before, want) {
423 t.Errorf("got %v want %v", before, want)
427 func TestProcessOrphans(t *testing.T) {
428 dispatcher := event.NewDispatcher()
436 pool: map[bc.Hash]*TxDesc{},
437 utxo: map[bc.Hash]*types.Tx{},
438 eventDispatcher: dispatcher,
439 orphans: map[bc.Hash]*orphanTx{
446 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
447 testTxs[3].SpentOutputIDs[0]: {
457 pool: map[bc.Hash]*TxDesc{
463 utxo: map[bc.Hash]*types.Tx{
464 *testTxs[3].ResultIds[0]: testTxs[3],
465 *testTxs[3].ResultIds[1]: testTxs[3],
467 eventDispatcher: dispatcher,
468 orphans: map[bc.Hash]*orphanTx{},
469 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
471 processTx: &TxDesc{Tx: testTxs[2]},
475 pool: map[bc.Hash]*TxDesc{},
476 utxo: map[bc.Hash]*types.Tx{},
477 eventDispatcher: dispatcher,
478 orphans: map[bc.Hash]*orphanTx{
490 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
491 testTxs[3].SpentOutputIDs[0]: {
498 testTxs[4].SpentOutputIDs[0]: {
508 pool: map[bc.Hash]*TxDesc{
518 utxo: map[bc.Hash]*types.Tx{
519 *testTxs[3].ResultIds[0]: testTxs[3],
520 *testTxs[3].ResultIds[1]: testTxs[3],
521 *testTxs[4].ResultIds[0]: testTxs[4],
522 *testTxs[4].ResultIds[1]: testTxs[4],
524 eventDispatcher: dispatcher,
525 orphans: map[bc.Hash]*orphanTx{},
526 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
528 processTx: &TxDesc{Tx: testTxs[2]},
532 for i, c := range cases {
533 c.before.store = &mockStore{}
534 c.before.addTransaction(c.processTx)
535 c.before.processOrphans(c.processTx)
536 c.before.RemoveTransaction(&c.processTx.Tx.ID)
538 c.before.lastUpdated = 0
539 for _, txD := range c.before.pool {
540 txD.Added = time.Time{}
543 if !testutil.DeepEqual(c.before, c.after) {
544 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
549 func TestRemoveOrphan(t *testing.T) {
553 removeHashes []*bc.Hash
557 orphans: map[bc.Hash]*orphanTx{
559 expiration: time.Unix(1533489701, 0),
565 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
566 testTxs[0].SpentOutputIDs[0]: {
568 expiration: time.Unix(1533489701, 0),
577 orphans: map[bc.Hash]*orphanTx{},
578 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
580 removeHashes: []*bc.Hash{
586 orphans: map[bc.Hash]*orphanTx{
588 expiration: time.Unix(1533489701, 0),
594 expiration: time.Unix(1533489701, 0),
600 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
601 testTxs[0].SpentOutputIDs[0]: {
603 expiration: time.Unix(1533489701, 0),
609 expiration: time.Unix(1533489701, 0),
618 orphans: map[bc.Hash]*orphanTx{
620 expiration: time.Unix(1533489701, 0),
626 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
627 testTxs[0].SpentOutputIDs[0]: {
629 expiration: time.Unix(1533489701, 0),
637 removeHashes: []*bc.Hash{
643 for i, c := range cases {
644 for _, hash := range c.removeHashes {
645 c.before.removeOrphan(hash)
647 if !testutil.DeepEqual(c.before, c.after) {
648 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
653 type mockStore1 struct{}
655 func (s *mockStore1) BlockExist(hash *bc.Hash) bool { return false }
656 func (s *mockStore1) GetBlock(*bc.Hash) (*types.Block, error) { return nil, nil }
657 func (s *mockStore1) GetStoreStatus() *BlockStoreState { return nil }
658 func (s *mockStore1) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
659 func (s *mockStore1) GetTransactionsUtxo(utxoView *state.UtxoViewpoint, tx []*bc.Tx) error {
660 for _, hash := range testTxs[2].SpentOutputIDs {
661 utxoView.Entries[hash] = &storage.UtxoEntry{IsCoinBase: false, Spent: false}
665 func (s *mockStore1) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
666 func (s *mockStore1) GetVoteResult(uint64) (*state.VoteResult, error) { return nil, nil }
667 func (s *mockStore1) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
668 func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
669 func (s *mockStore1) SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, map[uint64]*state.VoteResult) error { return nil }
670 func (s *mockStore1) SaveChainNodeStatus(*state.BlockNode, *state.BlockNode) error { return nil }
672 func TestProcessTransaction(t *testing.T) {
674 pool: make(map[bc.Hash]*TxDesc),
675 utxo: make(map[bc.Hash]*types.Tx),
676 orphans: make(map[bc.Hash]*orphanTx),
677 orphansByPrev: make(map[bc.Hash]map[bc.Hash]*orphanTx),
678 store: &mockStore1{},
679 eventDispatcher: event.NewDispatcher(),
720 pool: map[bc.Hash]*TxDesc{
727 utxo: map[bc.Hash]*types.Tx{
728 *testTxs[2].ResultIds[0]: testTxs[2],
729 *testTxs[2].ResultIds[1]: testTxs[2],
739 for i, c := range cases {
740 txPool.ProcessTransaction(c.addTx.Tx, c.addTx.StatusFail, 0, 0)
741 for _, txD := range txPool.pool {
742 txD.Added = time.Time{}
744 for _, txD := range txPool.orphans {
745 txD.Added = time.Time{}
746 txD.expiration = time.Time{}
749 if !testutil.DeepEqual(txPool.pool, c.want.pool) {
750 t.Errorf("case %d: test ProcessTransaction pool mismatch got %s want %s", i, spew.Sdump(txPool.pool), spew.Sdump(c.want.pool))
752 if !testutil.DeepEqual(txPool.utxo, c.want.utxo) {
753 t.Errorf("case %d: test ProcessTransaction utxo mismatch got %s want %s", i, spew.Sdump(txPool.utxo), spew.Sdump(c.want.utxo))
755 if !testutil.DeepEqual(txPool.orphans, c.want.orphans) {
756 t.Errorf("case %d: test ProcessTransaction orphans mismatch got %s want %s", i, spew.Sdump(txPool.orphans), spew.Sdump(c.want.orphans))
758 if !testutil.DeepEqual(txPool.orphansByPrev, c.want.orphansByPrev) {
759 t.Errorf("case %d: test ProcessTransaction orphansByPrev mismatch got %s want %s", i, spew.Sdump(txPool.orphansByPrev), spew.Sdump(c.want.orphansByPrev))