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 {
126 func (s *mockStore) SaveChainNodeStatus(*state.BlockNode, *state.BlockNode) error { return nil }
128 func TestAddOrphan(t *testing.T) {
133 requireParents []*bc.Hash
137 orphans: map[bc.Hash]*orphanTx{},
138 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
141 orphans: map[bc.Hash]*orphanTx{
148 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
149 testTxs[0].SpentOutputIDs[0]: {
158 addOrphan: &TxDesc{Tx: testTxs[0]},
159 requireParents: []*bc.Hash{&testTxs[0].SpentOutputIDs[0]},
163 orphans: map[bc.Hash]*orphanTx{
170 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
171 testTxs[0].SpentOutputIDs[0]: {
181 orphans: map[bc.Hash]*orphanTx{
193 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
194 testTxs[0].SpentOutputIDs[0]: {
208 addOrphan: &TxDesc{Tx: testTxs[1]},
209 requireParents: []*bc.Hash{&testTxs[1].SpentOutputIDs[0]},
213 orphans: map[bc.Hash]*orphanTx{},
214 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
217 orphans: map[bc.Hash]*orphanTx{
224 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
225 testTxs[2].SpentOutputIDs[1]: {
234 addOrphan: &TxDesc{Tx: testTxs[2]},
235 requireParents: []*bc.Hash{&testTxs[2].SpentOutputIDs[1]},
239 for i, c := range cases {
240 c.before.addOrphan(c.addOrphan, c.requireParents)
241 for _, orphan := range c.before.orphans {
242 orphan.expiration = time.Time{}
244 for _, orphans := range c.before.orphansByPrev {
245 for _, orphan := range orphans {
246 orphan.expiration = time.Time{}
249 if !testutil.DeepEqual(c.before, c.after) {
250 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
255 func TestAddTransaction(t *testing.T) {
256 dispatcher := event.NewDispatcher()
264 pool: map[bc.Hash]*TxDesc{},
265 utxo: map[bc.Hash]*types.Tx{},
266 eventDispatcher: dispatcher,
269 pool: map[bc.Hash]*TxDesc{
275 utxo: map[bc.Hash]*types.Tx{
276 *testTxs[2].ResultIds[0]: testTxs[2],
277 *testTxs[2].ResultIds[1]: testTxs[2],
287 pool: map[bc.Hash]*TxDesc{},
288 utxo: map[bc.Hash]*types.Tx{},
289 eventDispatcher: dispatcher,
292 pool: map[bc.Hash]*TxDesc{
298 utxo: map[bc.Hash]*types.Tx{
299 *testTxs[2].ResultIds[0]: testTxs[2],
309 pool: map[bc.Hash]*TxDesc{},
310 utxo: map[bc.Hash]*types.Tx{},
311 eventDispatcher: dispatcher,
314 pool: map[bc.Hash]*TxDesc{
320 utxo: map[bc.Hash]*types.Tx{
321 *testTxs[7].ResultIds[0]: testTxs[7],
322 *testTxs[7].ResultIds[1]: testTxs[7],
332 pool: map[bc.Hash]*TxDesc{},
333 utxo: map[bc.Hash]*types.Tx{},
334 eventDispatcher: dispatcher,
337 pool: map[bc.Hash]*TxDesc{
343 utxo: map[bc.Hash]*types.Tx{
344 *testTxs[7].ResultIds[0]: testTxs[7],
354 for i, c := range cases {
355 c.before.addTransaction(c.addTx)
356 for _, txD := range c.before.pool {
357 txD.Added = time.Time{}
359 if !testutil.DeepEqual(c.before.pool, c.after.pool) {
360 t.Errorf("case %d: got %v want %v", i, c.before.pool, c.after.pool)
362 if !testutil.DeepEqual(c.before.utxo, c.after.utxo) {
363 t.Errorf("case %d: got %v want %v", i, c.before.utxo, c.after.utxo)
368 func TestExpireOrphan(t *testing.T) {
370 orphans: map[bc.Hash]*orphanTx{
372 expiration: time.Unix(1533489701, 0),
378 expiration: time.Unix(1633489701, 0),
384 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
385 testTxs[0].SpentOutputIDs[0]: {
387 expiration: time.Unix(1533489701, 0),
393 expiration: time.Unix(1633489701, 0),
403 orphans: map[bc.Hash]*orphanTx{
405 expiration: time.Unix(1633489701, 0),
411 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
412 testTxs[0].SpentOutputIDs[0]: {
414 expiration: time.Unix(1633489701, 0),
423 before.ExpireOrphan(time.Unix(1633479701, 0))
424 if !testutil.DeepEqual(before, want) {
425 t.Errorf("got %v want %v", before, want)
429 func TestProcessOrphans(t *testing.T) {
430 dispatcher := event.NewDispatcher()
438 pool: map[bc.Hash]*TxDesc{},
439 utxo: map[bc.Hash]*types.Tx{},
440 eventDispatcher: dispatcher,
441 orphans: map[bc.Hash]*orphanTx{
448 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
449 testTxs[3].SpentOutputIDs[0]: {
459 pool: map[bc.Hash]*TxDesc{
465 utxo: map[bc.Hash]*types.Tx{
466 *testTxs[3].ResultIds[0]: testTxs[3],
467 *testTxs[3].ResultIds[1]: testTxs[3],
469 eventDispatcher: dispatcher,
470 orphans: map[bc.Hash]*orphanTx{},
471 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
473 processTx: &TxDesc{Tx: testTxs[2]},
477 pool: map[bc.Hash]*TxDesc{},
478 utxo: map[bc.Hash]*types.Tx{},
479 eventDispatcher: dispatcher,
480 orphans: map[bc.Hash]*orphanTx{
492 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
493 testTxs[3].SpentOutputIDs[0]: {
500 testTxs[4].SpentOutputIDs[0]: {
510 pool: map[bc.Hash]*TxDesc{
520 utxo: map[bc.Hash]*types.Tx{
521 *testTxs[3].ResultIds[0]: testTxs[3],
522 *testTxs[3].ResultIds[1]: testTxs[3],
523 *testTxs[4].ResultIds[0]: testTxs[4],
524 *testTxs[4].ResultIds[1]: testTxs[4],
526 eventDispatcher: dispatcher,
527 orphans: map[bc.Hash]*orphanTx{},
528 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
530 processTx: &TxDesc{Tx: testTxs[2]},
534 for i, c := range cases {
535 c.before.store = &mockStore{}
536 c.before.addTransaction(c.processTx)
537 c.before.processOrphans(c.processTx)
538 c.before.RemoveTransaction(&c.processTx.Tx.ID)
540 c.before.lastUpdated = 0
541 for _, txD := range c.before.pool {
542 txD.Added = time.Time{}
545 if !testutil.DeepEqual(c.before, c.after) {
546 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
551 func TestRemoveOrphan(t *testing.T) {
555 removeHashes []*bc.Hash
559 orphans: map[bc.Hash]*orphanTx{
561 expiration: time.Unix(1533489701, 0),
567 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
568 testTxs[0].SpentOutputIDs[0]: {
570 expiration: time.Unix(1533489701, 0),
579 orphans: map[bc.Hash]*orphanTx{},
580 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
582 removeHashes: []*bc.Hash{
588 orphans: map[bc.Hash]*orphanTx{
590 expiration: time.Unix(1533489701, 0),
596 expiration: time.Unix(1533489701, 0),
602 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
603 testTxs[0].SpentOutputIDs[0]: {
605 expiration: time.Unix(1533489701, 0),
611 expiration: time.Unix(1533489701, 0),
620 orphans: map[bc.Hash]*orphanTx{
622 expiration: time.Unix(1533489701, 0),
628 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
629 testTxs[0].SpentOutputIDs[0]: {
631 expiration: time.Unix(1533489701, 0),
639 removeHashes: []*bc.Hash{
645 for i, c := range cases {
646 for _, hash := range c.removeHashes {
647 c.before.removeOrphan(hash)
649 if !testutil.DeepEqual(c.before, c.after) {
650 t.Errorf("case %d: got %v want %v", i, c.before, c.after)
655 type mockStore1 struct{}
657 func (s *mockStore1) BlockExist(hash *bc.Hash) bool { return false }
658 func (s *mockStore1) GetBlock(*bc.Hash) (*types.Block, error) { return nil, nil }
659 func (s *mockStore1) GetStoreStatus() *BlockStoreState { return nil }
660 func (s *mockStore1) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
661 func (s *mockStore1) GetTransactionsUtxo(utxoView *state.UtxoViewpoint, tx []*bc.Tx) error {
663 for _, hash := range testTxs[2].SpentOutputIDs {
664 utxoView.Entries[hash] = &storage.UtxoEntry{Type: storage.NormalUTXOType, Spent: false}
668 func (s *mockStore1) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
669 func (s *mockStore1) GetVoteResult(uint64) (*state.VoteResult, error) { return nil, nil }
670 func (s *mockStore1) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
671 func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
672 func (s *mockStore1) SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, []*state.VoteResult) error {
675 func (s *mockStore1) SaveChainNodeStatus(*state.BlockNode, *state.BlockNode) error { return nil }
677 func TestProcessTransaction(t *testing.T) {
679 pool: make(map[bc.Hash]*TxDesc),
680 utxo: make(map[bc.Hash]*types.Tx),
681 orphans: make(map[bc.Hash]*orphanTx),
682 orphansByPrev: make(map[bc.Hash]map[bc.Hash]*orphanTx),
683 store: &mockStore1{},
684 eventDispatcher: event.NewDispatcher(),
725 pool: map[bc.Hash]*TxDesc{
732 utxo: map[bc.Hash]*types.Tx{
733 *testTxs[2].ResultIds[0]: testTxs[2],
734 *testTxs[2].ResultIds[1]: testTxs[2],
744 for i, c := range cases {
745 txPool.ProcessTransaction(c.addTx.Tx, c.addTx.StatusFail, 0, 0)
746 for _, txD := range txPool.pool {
747 txD.Added = time.Time{}
749 for _, txD := range txPool.orphans {
750 txD.Added = time.Time{}
751 txD.expiration = time.Time{}
754 if !testutil.DeepEqual(txPool.pool, c.want.pool) {
755 t.Errorf("case %d: test ProcessTransaction pool mismatch got %s want %s", i, spew.Sdump(txPool.pool), spew.Sdump(c.want.pool))
757 if !testutil.DeepEqual(txPool.utxo, c.want.utxo) {
758 t.Errorf("case %d: test ProcessTransaction utxo mismatch got %s want %s", i, spew.Sdump(txPool.utxo), spew.Sdump(c.want.utxo))
760 if !testutil.DeepEqual(txPool.orphans, c.want.orphans) {
761 t.Errorf("case %d: test ProcessTransaction orphans mismatch got %s want %s", i, spew.Sdump(txPool.orphans), spew.Sdump(c.want.orphans))
763 if !testutil.DeepEqual(txPool.orphansByPrev, c.want.orphansByPrev) {
764 t.Errorf("case %d: test ProcessTransaction orphansByPrev mismatch got %s want %s", i, spew.Sdump(txPool.orphansByPrev), spew.Sdump(c.want.orphansByPrev))