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, []*state.VoteResult) error {
127 func TestAddOrphan(t *testing.T) {
132 requireParents []*bc.Hash
136 orphans: map[bc.Hash]*orphanTx{},
137 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
140 orphans: map[bc.Hash]*orphanTx{
147 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
148 testTxs[0].SpentOutputIDs[0]: {
157 addOrphan: &TxDesc{Tx: testTxs[0]},
158 requireParents: []*bc.Hash{&testTxs[0].SpentOutputIDs[0]},
162 orphans: map[bc.Hash]*orphanTx{
169 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
170 testTxs[0].SpentOutputIDs[0]: {
180 orphans: map[bc.Hash]*orphanTx{
192 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
193 testTxs[0].SpentOutputIDs[0]: {
207 addOrphan: &TxDesc{Tx: testTxs[1]},
208 requireParents: []*bc.Hash{&testTxs[1].SpentOutputIDs[0]},
212 orphans: map[bc.Hash]*orphanTx{},
213 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
216 orphans: map[bc.Hash]*orphanTx{
223 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
224 testTxs[2].SpentOutputIDs[1]: {
233 addOrphan: &TxDesc{Tx: testTxs[2]},
234 requireParents: []*bc.Hash{&testTxs[2].SpentOutputIDs[1]},
238 for i, c := range cases {
239 c.before.addOrphan(c.addOrphan, c.requireParents)
240 for _, orphan := range c.before.orphans {
241 orphan.expiration = time.Time{}
243 for _, orphans := range c.before.orphansByPrev {
244 for _, orphan := range orphans {
245 orphan.expiration = time.Time{}
248 if !testutil.DeepEqual(c.before, c.after) {
249 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
254 func TestAddTransaction(t *testing.T) {
255 dispatcher := event.NewDispatcher()
263 pool: map[bc.Hash]*TxDesc{},
264 utxo: map[bc.Hash]*types.Tx{},
265 eventDispatcher: dispatcher,
268 pool: map[bc.Hash]*TxDesc{
274 utxo: map[bc.Hash]*types.Tx{
275 *testTxs[2].ResultIds[0]: testTxs[2],
276 *testTxs[2].ResultIds[1]: testTxs[2],
286 pool: map[bc.Hash]*TxDesc{},
287 utxo: map[bc.Hash]*types.Tx{},
288 eventDispatcher: dispatcher,
291 pool: map[bc.Hash]*TxDesc{
297 utxo: map[bc.Hash]*types.Tx{
298 *testTxs[2].ResultIds[0]: testTxs[2],
308 pool: map[bc.Hash]*TxDesc{},
309 utxo: map[bc.Hash]*types.Tx{},
310 eventDispatcher: dispatcher,
313 pool: map[bc.Hash]*TxDesc{
319 utxo: map[bc.Hash]*types.Tx{
320 *testTxs[7].ResultIds[0]: testTxs[7],
321 *testTxs[7].ResultIds[1]: testTxs[7],
331 pool: map[bc.Hash]*TxDesc{},
332 utxo: map[bc.Hash]*types.Tx{},
333 eventDispatcher: dispatcher,
336 pool: map[bc.Hash]*TxDesc{
342 utxo: map[bc.Hash]*types.Tx{
343 *testTxs[7].ResultIds[0]: testTxs[7],
353 for i, c := range cases {
354 c.before.addTransaction(c.addTx)
355 for _, txD := range c.before.pool {
356 txD.Added = time.Time{}
358 if !testutil.DeepEqual(c.before.pool, c.after.pool) {
359 t.Errorf("case %d: got %v want %v", i, c.before.pool, c.after.pool)
361 if !testutil.DeepEqual(c.before.utxo, c.after.utxo) {
362 t.Errorf("case %d: got %v want %v", i, c.before.utxo, c.after.utxo)
367 func TestExpireOrphan(t *testing.T) {
369 orphans: map[bc.Hash]*orphanTx{
371 expiration: time.Unix(1533489701, 0),
377 expiration: time.Unix(1633489701, 0),
383 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
384 testTxs[0].SpentOutputIDs[0]: {
386 expiration: time.Unix(1533489701, 0),
392 expiration: time.Unix(1633489701, 0),
402 orphans: map[bc.Hash]*orphanTx{
404 expiration: time.Unix(1633489701, 0),
410 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
411 testTxs[0].SpentOutputIDs[0]: {
413 expiration: time.Unix(1633489701, 0),
422 before.ExpireOrphan(time.Unix(1633479701, 0))
423 if !testutil.DeepEqual(before, want) {
424 t.Errorf("got %v want %v", before, want)
428 func TestProcessOrphans(t *testing.T) {
429 dispatcher := event.NewDispatcher()
437 pool: map[bc.Hash]*TxDesc{},
438 utxo: map[bc.Hash]*types.Tx{},
439 eventDispatcher: dispatcher,
440 orphans: map[bc.Hash]*orphanTx{
447 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
448 testTxs[3].SpentOutputIDs[0]: {
458 pool: map[bc.Hash]*TxDesc{
464 utxo: map[bc.Hash]*types.Tx{
465 *testTxs[3].ResultIds[0]: testTxs[3],
466 *testTxs[3].ResultIds[1]: testTxs[3],
468 eventDispatcher: dispatcher,
469 orphans: map[bc.Hash]*orphanTx{},
470 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
472 processTx: &TxDesc{Tx: testTxs[2]},
476 pool: map[bc.Hash]*TxDesc{},
477 utxo: map[bc.Hash]*types.Tx{},
478 eventDispatcher: dispatcher,
479 orphans: map[bc.Hash]*orphanTx{
491 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
492 testTxs[3].SpentOutputIDs[0]: {
499 testTxs[4].SpentOutputIDs[0]: {
509 pool: map[bc.Hash]*TxDesc{
519 utxo: map[bc.Hash]*types.Tx{
520 *testTxs[3].ResultIds[0]: testTxs[3],
521 *testTxs[3].ResultIds[1]: testTxs[3],
522 *testTxs[4].ResultIds[0]: testTxs[4],
523 *testTxs[4].ResultIds[1]: testTxs[4],
525 eventDispatcher: dispatcher,
526 orphans: map[bc.Hash]*orphanTx{},
527 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
529 processTx: &TxDesc{Tx: testTxs[2]},
533 for i, c := range cases {
534 c.before.store = &mockStore{}
535 c.before.addTransaction(c.processTx)
536 c.before.processOrphans(c.processTx)
537 c.before.RemoveTransaction(&c.processTx.Tx.ID)
539 c.before.lastUpdated = 0
540 for _, txD := range c.before.pool {
541 txD.Added = time.Time{}
544 if !testutil.DeepEqual(c.before, c.after) {
545 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
550 func TestRemoveOrphan(t *testing.T) {
554 removeHashes []*bc.Hash
558 orphans: map[bc.Hash]*orphanTx{
560 expiration: time.Unix(1533489701, 0),
566 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
567 testTxs[0].SpentOutputIDs[0]: {
569 expiration: time.Unix(1533489701, 0),
578 orphans: map[bc.Hash]*orphanTx{},
579 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
581 removeHashes: []*bc.Hash{
587 orphans: map[bc.Hash]*orphanTx{
589 expiration: time.Unix(1533489701, 0),
595 expiration: time.Unix(1533489701, 0),
601 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
602 testTxs[0].SpentOutputIDs[0]: {
604 expiration: time.Unix(1533489701, 0),
610 expiration: time.Unix(1533489701, 0),
619 orphans: map[bc.Hash]*orphanTx{
621 expiration: time.Unix(1533489701, 0),
627 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
628 testTxs[0].SpentOutputIDs[0]: {
630 expiration: time.Unix(1533489701, 0),
638 removeHashes: []*bc.Hash{
644 for i, c := range cases {
645 for _, hash := range c.removeHashes {
646 c.before.removeOrphan(hash)
648 if !testutil.DeepEqual(c.before, c.after) {
649 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
654 type mockStore1 struct{}
656 func (s *mockStore1) BlockExist(hash *bc.Hash) bool { return false }
657 func (s *mockStore1) GetBlock(*bc.Hash) (*types.Block, error) { return nil, nil }
658 func (s *mockStore1) GetStoreStatus() *BlockStoreState { return nil }
659 func (s *mockStore1) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
660 func (s *mockStore1) GetTransactionsUtxo(utxoView *state.UtxoViewpoint, tx []*bc.Tx) error {
662 for _, hash := range testTxs[2].SpentOutputIDs {
663 utxoView.Entries[hash] = &storage.UtxoEntry{Type: storage.NormalUTXOType, Spent: false}
667 func (s *mockStore1) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
668 func (s *mockStore1) GetVoteResult(uint64) (*state.VoteResult, error) { return nil, nil }
669 func (s *mockStore1) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
670 func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
671 func (s *mockStore1) SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, []*state.VoteResult) error {
675 func TestProcessTransaction(t *testing.T) {
677 pool: make(map[bc.Hash]*TxDesc),
678 utxo: make(map[bc.Hash]*types.Tx),
679 orphans: make(map[bc.Hash]*orphanTx),
680 orphansByPrev: make(map[bc.Hash]map[bc.Hash]*orphanTx),
681 store: &mockStore1{},
682 eventDispatcher: event.NewDispatcher(),
723 pool: map[bc.Hash]*TxDesc{
730 utxo: map[bc.Hash]*types.Tx{
731 *testTxs[2].ResultIds[0]: testTxs[2],
732 *testTxs[2].ResultIds[1]: testTxs[2],
742 for i, c := range cases {
743 txPool.ProcessTransaction(c.addTx.Tx, c.addTx.StatusFail, 0, 0)
744 for _, txD := range txPool.pool {
745 txD.Added = time.Time{}
747 for _, txD := range txPool.orphans {
748 txD.Added = time.Time{}
749 txD.expiration = time.Time{}
752 if !testutil.DeepEqual(txPool.pool, c.want.pool) {
753 t.Errorf("case %d: test ProcessTransaction pool mismatch got %s want %s", i, spew.Sdump(txPool.pool), spew.Sdump(c.want.pool))
755 if !testutil.DeepEqual(txPool.utxo, c.want.utxo) {
756 t.Errorf("case %d: test ProcessTransaction utxo mismatch got %s want %s", i, spew.Sdump(txPool.utxo), spew.Sdump(c.want.utxo))
758 if !testutil.DeepEqual(txPool.orphans, c.want.orphans) {
759 t.Errorf("case %d: test ProcessTransaction orphans mismatch got %s want %s", i, spew.Sdump(txPool.orphans), spew.Sdump(c.want.orphans))
761 if !testutil.DeepEqual(txPool.orphansByPrev, c.want.orphansByPrev) {
762 t.Errorf("case %d: test ProcessTransaction orphansByPrev mismatch got %s want %s", i, spew.Sdump(txPool.orphansByPrev), spew.Sdump(c.want.orphansByPrev))