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, height uint64) bool { return false }
115 func (s *mockStore) GetBlock(*bc.Hash, uint64) (*types.Block, error) { return nil, nil }
116 func (s *mockStore) GetBlockHeader(*bc.Hash, uint64) (*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) GetVoteResult(uint64) (*state.VoteResult, error) { return nil, nil }
122 func (s *mockStore) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
123 func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
124 func (s *mockStore) SaveBlockHeader(*types.BlockHeader) error { return nil }
125 func (s *mockStore) SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, []*state.VoteResult) error {
129 func TestAddOrphan(t *testing.T) {
134 requireParents []*bc.Hash
138 orphans: map[bc.Hash]*orphanTx{},
139 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
142 orphans: map[bc.Hash]*orphanTx{
149 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
150 testTxs[0].SpentOutputIDs[0]: {
159 addOrphan: &TxDesc{Tx: testTxs[0]},
160 requireParents: []*bc.Hash{&testTxs[0].SpentOutputIDs[0]},
164 orphans: map[bc.Hash]*orphanTx{
171 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
172 testTxs[0].SpentOutputIDs[0]: {
182 orphans: map[bc.Hash]*orphanTx{
194 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
195 testTxs[0].SpentOutputIDs[0]: {
209 addOrphan: &TxDesc{Tx: testTxs[1]},
210 requireParents: []*bc.Hash{&testTxs[1].SpentOutputIDs[0]},
214 orphans: map[bc.Hash]*orphanTx{},
215 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
218 orphans: map[bc.Hash]*orphanTx{
225 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
226 testTxs[2].SpentOutputIDs[1]: {
235 addOrphan: &TxDesc{Tx: testTxs[2]},
236 requireParents: []*bc.Hash{&testTxs[2].SpentOutputIDs[1]},
240 for i, c := range cases {
241 c.before.addOrphan(c.addOrphan, c.requireParents)
242 for _, orphan := range c.before.orphans {
243 orphan.expiration = time.Time{}
245 for _, orphans := range c.before.orphansByPrev {
246 for _, orphan := range orphans {
247 orphan.expiration = time.Time{}
250 if !testutil.DeepEqual(c.before, c.after) {
251 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
256 func TestAddTransaction(t *testing.T) {
257 dispatcher := event.NewDispatcher()
265 pool: map[bc.Hash]*TxDesc{},
266 utxo: map[bc.Hash]*types.Tx{},
267 eventDispatcher: dispatcher,
270 pool: map[bc.Hash]*TxDesc{
276 utxo: map[bc.Hash]*types.Tx{
277 *testTxs[2].ResultIds[0]: testTxs[2],
278 *testTxs[2].ResultIds[1]: testTxs[2],
288 pool: map[bc.Hash]*TxDesc{},
289 utxo: map[bc.Hash]*types.Tx{},
290 eventDispatcher: dispatcher,
293 pool: map[bc.Hash]*TxDesc{
299 utxo: map[bc.Hash]*types.Tx{
300 *testTxs[2].ResultIds[0]: testTxs[2],
310 pool: map[bc.Hash]*TxDesc{},
311 utxo: map[bc.Hash]*types.Tx{},
312 eventDispatcher: dispatcher,
315 pool: map[bc.Hash]*TxDesc{
321 utxo: map[bc.Hash]*types.Tx{
322 *testTxs[7].ResultIds[0]: testTxs[7],
323 *testTxs[7].ResultIds[1]: testTxs[7],
333 pool: map[bc.Hash]*TxDesc{},
334 utxo: map[bc.Hash]*types.Tx{},
335 eventDispatcher: dispatcher,
338 pool: map[bc.Hash]*TxDesc{
344 utxo: map[bc.Hash]*types.Tx{
345 *testTxs[7].ResultIds[0]: testTxs[7],
355 for i, c := range cases {
356 c.before.addTransaction(c.addTx)
357 for _, txD := range c.before.pool {
358 txD.Added = time.Time{}
360 if !testutil.DeepEqual(c.before.pool, c.after.pool) {
361 t.Errorf("case %d: got %v want %v", i, c.before.pool, c.after.pool)
363 if !testutil.DeepEqual(c.before.utxo, c.after.utxo) {
364 t.Errorf("case %d: got %v want %v", i, c.before.utxo, c.after.utxo)
369 func TestExpireOrphan(t *testing.T) {
371 orphans: map[bc.Hash]*orphanTx{
373 expiration: time.Unix(1533489701, 0),
379 expiration: time.Unix(1633489701, 0),
385 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
386 testTxs[0].SpentOutputIDs[0]: {
388 expiration: time.Unix(1533489701, 0),
394 expiration: time.Unix(1633489701, 0),
404 orphans: map[bc.Hash]*orphanTx{
406 expiration: time.Unix(1633489701, 0),
412 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
413 testTxs[0].SpentOutputIDs[0]: {
415 expiration: time.Unix(1633489701, 0),
424 before.ExpireOrphan(time.Unix(1633479701, 0))
425 if !testutil.DeepEqual(before, want) {
426 t.Errorf("got %v want %v", before, want)
430 func TestProcessOrphans(t *testing.T) {
431 dispatcher := event.NewDispatcher()
439 pool: map[bc.Hash]*TxDesc{},
440 utxo: map[bc.Hash]*types.Tx{},
441 eventDispatcher: dispatcher,
442 orphans: map[bc.Hash]*orphanTx{
449 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
450 testTxs[3].SpentOutputIDs[0]: {
460 pool: map[bc.Hash]*TxDesc{
466 utxo: map[bc.Hash]*types.Tx{
467 *testTxs[3].ResultIds[0]: testTxs[3],
468 *testTxs[3].ResultIds[1]: testTxs[3],
470 eventDispatcher: dispatcher,
471 orphans: map[bc.Hash]*orphanTx{},
472 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
474 processTx: &TxDesc{Tx: testTxs[2]},
478 pool: map[bc.Hash]*TxDesc{},
479 utxo: map[bc.Hash]*types.Tx{},
480 eventDispatcher: dispatcher,
481 orphans: map[bc.Hash]*orphanTx{
493 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
494 testTxs[3].SpentOutputIDs[0]: {
501 testTxs[4].SpentOutputIDs[0]: {
511 pool: map[bc.Hash]*TxDesc{
521 utxo: map[bc.Hash]*types.Tx{
522 *testTxs[3].ResultIds[0]: testTxs[3],
523 *testTxs[3].ResultIds[1]: testTxs[3],
524 *testTxs[4].ResultIds[0]: testTxs[4],
525 *testTxs[4].ResultIds[1]: testTxs[4],
527 eventDispatcher: dispatcher,
528 orphans: map[bc.Hash]*orphanTx{},
529 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
531 processTx: &TxDesc{Tx: testTxs[2]},
535 for i, c := range cases {
536 c.before.store = &mockStore{}
537 c.before.addTransaction(c.processTx)
538 c.before.processOrphans(c.processTx)
539 c.before.RemoveTransaction(&c.processTx.Tx.ID)
541 c.before.lastUpdated = 0
542 for _, txD := range c.before.pool {
543 txD.Added = time.Time{}
546 if !testutil.DeepEqual(c.before, c.after) {
547 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
552 func TestRemoveOrphan(t *testing.T) {
556 removeHashes []*bc.Hash
560 orphans: map[bc.Hash]*orphanTx{
562 expiration: time.Unix(1533489701, 0),
568 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
569 testTxs[0].SpentOutputIDs[0]: {
571 expiration: time.Unix(1533489701, 0),
580 orphans: map[bc.Hash]*orphanTx{},
581 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
583 removeHashes: []*bc.Hash{
589 orphans: map[bc.Hash]*orphanTx{
591 expiration: time.Unix(1533489701, 0),
597 expiration: time.Unix(1533489701, 0),
603 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
604 testTxs[0].SpentOutputIDs[0]: {
606 expiration: time.Unix(1533489701, 0),
612 expiration: time.Unix(1533489701, 0),
621 orphans: map[bc.Hash]*orphanTx{
623 expiration: time.Unix(1533489701, 0),
629 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
630 testTxs[0].SpentOutputIDs[0]: {
632 expiration: time.Unix(1533489701, 0),
640 removeHashes: []*bc.Hash{
646 for i, c := range cases {
647 for _, hash := range c.removeHashes {
648 c.before.removeOrphan(hash)
650 if !testutil.DeepEqual(c.before, c.after) {
651 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
656 type mockStore1 struct{}
658 func (s *mockStore1) BlockExist(hash *bc.Hash, height uint64) bool { return false }
659 func (s *mockStore1) GetBlock(*bc.Hash, uint64) (*types.Block, error) { return nil, nil }
660 func (s *mockStore1) GetBlockHeader(*bc.Hash, uint64) (*types.BlockHeader, error) { return nil, nil }
661 func (s *mockStore1) GetStoreStatus() *BlockStoreState { return nil }
662 func (s *mockStore1) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
663 func (s *mockStore1) GetTransactionsUtxo(utxoView *state.UtxoViewpoint, tx []*bc.Tx) error {
665 for _, hash := range testTxs[2].SpentOutputIDs {
666 utxoView.Entries[hash] = &storage.UtxoEntry{Type: storage.NormalUTXOType, Spent: false}
670 func (s *mockStore1) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
671 func (s *mockStore1) GetVoteResult(uint64) (*state.VoteResult, error) { return nil, nil }
672 func (s *mockStore1) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
673 func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
674 func (s *mockStore1) SaveBlockHeader(*types.BlockHeader) error { return nil }
675 func (s *mockStore1) SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, []*state.VoteResult) error {
679 func TestProcessTransaction(t *testing.T) {
681 pool: make(map[bc.Hash]*TxDesc),
682 utxo: make(map[bc.Hash]*types.Tx),
683 orphans: make(map[bc.Hash]*orphanTx),
684 orphansByPrev: make(map[bc.Hash]map[bc.Hash]*orphanTx),
685 store: &mockStore1{},
686 eventDispatcher: event.NewDispatcher(),
727 pool: map[bc.Hash]*TxDesc{
734 utxo: map[bc.Hash]*types.Tx{
735 *testTxs[2].ResultIds[0]: testTxs[2],
736 *testTxs[2].ResultIds[1]: testTxs[2],
746 for i, c := range cases {
747 txPool.ProcessTransaction(c.addTx.Tx, c.addTx.StatusFail, 0, 0)
748 for _, txD := range txPool.pool {
749 txD.Added = time.Time{}
751 for _, txD := range txPool.orphans {
752 txD.Added = time.Time{}
753 txD.expiration = time.Time{}
756 if !testutil.DeepEqual(txPool.pool, c.want.pool) {
757 t.Errorf("case %d: test ProcessTransaction pool mismatch got %s want %s", i, spew.Sdump(txPool.pool), spew.Sdump(c.want.pool))
759 if !testutil.DeepEqual(txPool.utxo, c.want.utxo) {
760 t.Errorf("case %d: test ProcessTransaction utxo mismatch got %s want %s", i, spew.Sdump(txPool.utxo), spew.Sdump(c.want.utxo))
762 if !testutil.DeepEqual(txPool.orphans, c.want.orphans) {
763 t.Errorf("case %d: test ProcessTransaction orphans mismatch got %s want %s", i, spew.Sdump(txPool.orphans), spew.Sdump(c.want.orphans))
765 if !testutil.DeepEqual(txPool.orphansByPrev, c.want.orphansByPrev) {
766 t.Errorf("case %d: test ProcessTransaction orphansByPrev mismatch got %s want %s", i, spew.Sdump(txPool.orphansByPrev), spew.Sdump(c.want.orphansByPrev))