OSDN Git Service

feat(remove mining): remove mining code (#1877)
[bytom/bytom.git] / protocol / txpool_test.go
1 package protocol
2
3 import (
4         "testing"
5         "time"
6
7         "github.com/davecgh/go-spew/spew"
8
9         "github.com/bytom/bytom/consensus"
10         "github.com/bytom/bytom/database/storage"
11         "github.com/bytom/bytom/event"
12         "github.com/bytom/bytom/protocol/bc"
13         "github.com/bytom/bytom/protocol/bc/types"
14         "github.com/bytom/bytom/protocol/state"
15         "github.com/bytom/bytom/testutil"
16 )
17
18 var testTxs = []*types.Tx{
19         //tx0
20         types.NewTx(types.TxData{
21                 SerializedSize: 100,
22                 Inputs: []*types.TxInput{
23                         types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 1, 1, []byte{0x51}),
24                 },
25                 Outputs: []*types.TxOutput{
26                         types.NewTxOutput(*consensus.BTMAssetID, 1, []byte{0x6a}),
27                 },
28         }),
29         //tx1
30         types.NewTx(types.TxData{
31                 SerializedSize: 100,
32                 Inputs: []*types.TxInput{
33                         types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 1, 1, []byte{0x51}),
34                 },
35                 Outputs: []*types.TxOutput{
36                         types.NewTxOutput(*consensus.BTMAssetID, 1, []byte{0x6b}),
37                 },
38         }),
39         //tx2
40         types.NewTx(types.TxData{
41                 SerializedSize: 150,
42                 TimeRange:      0,
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}),
46                 },
47                 Outputs: []*types.TxOutput{
48                         types.NewTxOutput(*consensus.BTMAssetID, 1, []byte{0x6b}),
49                         types.NewTxOutput(bc.NewAssetID([32]byte{0xa1}), 4, []byte{0x61}),
50                 },
51         }),
52         //tx3
53         types.NewTx(types.TxData{
54                 SerializedSize: 100,
55                 Inputs: []*types.TxInput{
56                         types.NewSpendInput(nil, testutil.MustDecodeHash("dbea684b5c5153ed7729669a53d6c59574f26015a3e1eb2a0e8a1c645425a764"), bc.NewAssetID([32]byte{0xa1}), 4, 1, []byte{0x61}),
57                 },
58                 Outputs: []*types.TxOutput{
59                         types.NewTxOutput(bc.NewAssetID([32]byte{0xa1}), 3, []byte{0x62}),
60                         types.NewTxOutput(bc.NewAssetID([32]byte{0xa1}), 1, []byte{0x63}),
61                 },
62         }),
63         //tx4
64         types.NewTx(types.TxData{
65                 SerializedSize: 100,
66                 Inputs: []*types.TxInput{
67                         types.NewSpendInput(nil, testutil.MustDecodeHash("d84d0be0fd08e7341f2d127749bb0d0844d4560f53bd54861cee9981fd922cad"), bc.NewAssetID([32]byte{0xa1}), 3, 0, []byte{0x62}),
68                 },
69                 Outputs: []*types.TxOutput{
70                         types.NewTxOutput(bc.NewAssetID([32]byte{0xa1}), 2, []byte{0x64}),
71                         types.NewTxOutput(bc.NewAssetID([32]byte{0xa1}), 1, []byte{0x65}),
72                 },
73         }),
74         //tx5
75         types.NewTx(types.TxData{
76                 SerializedSize: 100,
77                 Inputs: []*types.TxInput{
78                         types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 1, 1, []byte{0x51}),
79                 },
80                 Outputs: []*types.TxOutput{
81                         types.NewTxOutput(*consensus.BTMAssetID, 0, []byte{0x51}),
82                 },
83         }),
84         //tx6
85         types.NewTx(types.TxData{
86                 SerializedSize: 100,
87                 Inputs: []*types.TxInput{
88                         types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 3, 1, []byte{0x51}),
89                         types.NewSpendInput(nil, testutil.MustDecodeHash("d84d0be0fd08e7341f2d127749bb0d0844d4560f53bd54861cee9981fd922cad"), bc.NewAssetID([32]byte{0xa1}), 3, 0, []byte{0x62}),
90                 },
91                 Outputs: []*types.TxOutput{
92                         types.NewTxOutput(*consensus.BTMAssetID, 2, []byte{0x51}),
93                         types.NewTxOutput(bc.NewAssetID([32]byte{0xa1}), 0, []byte{0x65}),
94                 },
95         }),
96 }
97
98 type mockStore struct{}
99
100 func (s *mockStore) SaveChainStatus(*state.BlockNode, *state.UtxoViewpoint, *state.ContractViewpoint) error {
101         return nil
102 }
103 func (s *mockStore) GetBlockHeader(hash *bc.Hash) (*types.BlockHeader, error)     { return nil, nil }
104 func (s *mockStore) GetCheckpoint(hash *bc.Hash) (*state.Checkpoint, error)       { return nil, nil }
105 func (s *mockStore) GetCheckpointsByHeight(u uint64) ([]*state.Checkpoint, error) { return nil, nil }
106 func (s *mockStore) BlockExist(hash *bc.Hash) bool                                { return false }
107 func (s *mockStore) GetBlock(*bc.Hash) (*types.Block, error)                      { return nil, nil }
108 func (s *mockStore) GetStoreStatus() *BlockStoreState                             { return nil }
109 func (s *mockStore) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
110 func (s *mockStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error     { return nil }
111 func (s *mockStore) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error)                 { return nil, nil }
112 func (s *mockStore) LoadBlockIndex(uint64) (*state.BlockIndex, error)             { return nil, nil }
113 func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error          { return nil }
114
115 func TestAddOrphan(t *testing.T) {
116         cases := []struct {
117                 before         *TxPool
118                 after          *TxPool
119                 addOrphan      *TxDesc
120                 requireParents []*bc.Hash
121         }{
122                 {
123                         before: &TxPool{
124                                 orphans:       map[bc.Hash]*orphanTx{},
125                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
126                         },
127                         after: &TxPool{
128                                 orphans: map[bc.Hash]*orphanTx{
129                                         testTxs[0].ID: {
130                                                 TxDesc: &TxDesc{
131                                                         Tx: testTxs[0],
132                                                 },
133                                         },
134                                 },
135                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
136                                         testTxs[0].SpentOutputIDs[0]: {
137                                                 testTxs[0].ID: {
138                                                         TxDesc: &TxDesc{
139                                                                 Tx: testTxs[0],
140                                                         },
141                                                 },
142                                         },
143                                 },
144                         },
145                         addOrphan:      &TxDesc{Tx: testTxs[0]},
146                         requireParents: []*bc.Hash{&testTxs[0].SpentOutputIDs[0]},
147                 },
148                 {
149                         before: &TxPool{
150                                 orphans: map[bc.Hash]*orphanTx{
151                                         testTxs[0].ID: {
152                                                 TxDesc: &TxDesc{
153                                                         Tx: testTxs[0],
154                                                 },
155                                         },
156                                 },
157                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
158                                         testTxs[0].SpentOutputIDs[0]: {
159                                                 testTxs[0].ID: {
160                                                         TxDesc: &TxDesc{
161                                                                 Tx: testTxs[0],
162                                                         },
163                                                 },
164                                         },
165                                 },
166                         },
167                         after: &TxPool{
168                                 orphans: map[bc.Hash]*orphanTx{
169                                         testTxs[0].ID: {
170                                                 TxDesc: &TxDesc{
171                                                         Tx: testTxs[0],
172                                                 },
173                                         },
174                                         testTxs[1].ID: {
175                                                 TxDesc: &TxDesc{
176                                                         Tx: testTxs[1],
177                                                 },
178                                         },
179                                 },
180                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
181                                         testTxs[0].SpentOutputIDs[0]: {
182                                                 testTxs[0].ID: {
183                                                         TxDesc: &TxDesc{
184                                                                 Tx: testTxs[0],
185                                                         },
186                                                 },
187                                                 testTxs[1].ID: {
188                                                         TxDesc: &TxDesc{
189                                                                 Tx: testTxs[1],
190                                                         },
191                                                 },
192                                         },
193                                 },
194                         },
195                         addOrphan:      &TxDesc{Tx: testTxs[1]},
196                         requireParents: []*bc.Hash{&testTxs[1].SpentOutputIDs[0]},
197                 },
198                 {
199                         before: &TxPool{
200                                 orphans:       map[bc.Hash]*orphanTx{},
201                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
202                         },
203                         after: &TxPool{
204                                 orphans: map[bc.Hash]*orphanTx{
205                                         testTxs[2].ID: {
206                                                 TxDesc: &TxDesc{
207                                                         Tx: testTxs[2],
208                                                 },
209                                         },
210                                 },
211                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
212                                         testTxs[2].SpentOutputIDs[1]: {
213                                                 testTxs[2].ID: {
214                                                         TxDesc: &TxDesc{
215                                                                 Tx: testTxs[2],
216                                                         },
217                                                 },
218                                         },
219                                 },
220                         },
221                         addOrphan:      &TxDesc{Tx: testTxs[2]},
222                         requireParents: []*bc.Hash{&testTxs[2].SpentOutputIDs[1]},
223                 },
224         }
225
226         for i, c := range cases {
227                 c.before.addOrphan(c.addOrphan, c.requireParents)
228                 for _, orphan := range c.before.orphans {
229                         orphan.expiration = time.Time{}
230                 }
231                 for _, orphans := range c.before.orphansByPrev {
232                         for _, orphan := range orphans {
233                                 orphan.expiration = time.Time{}
234                         }
235                 }
236                 if !testutil.DeepEqual(c.before, c.after) {
237                         t.Errorf("case %d: got %v want %v", i, c.before, c.after)
238                 }
239         }
240 }
241
242 func TestAddTransaction(t *testing.T) {
243         dispatcher := event.NewDispatcher()
244         cases := []struct {
245                 before *TxPool
246                 after  *TxPool
247                 addTx  *TxDesc
248         }{
249                 {
250                         before: &TxPool{
251                                 pool:            map[bc.Hash]*TxDesc{},
252                                 utxo:            map[bc.Hash]*types.Tx{},
253                                 eventDispatcher: dispatcher,
254                         },
255                         after: &TxPool{
256                                 pool: map[bc.Hash]*TxDesc{
257                                         testTxs[2].ID: {
258                                                 Tx:         testTxs[2],
259                                                 StatusFail: false,
260                                         },
261                                 },
262                                 utxo: map[bc.Hash]*types.Tx{
263                                         *testTxs[2].ResultIds[0]: testTxs[2],
264                                         *testTxs[2].ResultIds[1]: testTxs[2],
265                                 },
266                         },
267                         addTx: &TxDesc{
268                                 Tx:         testTxs[2],
269                                 StatusFail: false,
270                         },
271                 },
272                 {
273                         before: &TxPool{
274                                 pool:            map[bc.Hash]*TxDesc{},
275                                 utxo:            map[bc.Hash]*types.Tx{},
276                                 eventDispatcher: dispatcher,
277                         },
278                         after: &TxPool{
279                                 pool: map[bc.Hash]*TxDesc{
280                                         testTxs[2].ID: {
281                                                 Tx:         testTxs[2],
282                                                 StatusFail: true,
283                                         },
284                                 },
285                                 utxo: map[bc.Hash]*types.Tx{
286                                         *testTxs[2].ResultIds[0]: testTxs[2],
287                                 },
288                         },
289                         addTx: &TxDesc{
290                                 Tx:         testTxs[2],
291                                 StatusFail: true,
292                         },
293                 },
294         }
295
296         for i, c := range cases {
297                 c.before.addTransaction(c.addTx)
298                 for _, txD := range c.before.pool {
299                         txD.Added = time.Time{}
300                 }
301                 if !testutil.DeepEqual(c.before.pool, c.after.pool) {
302                         t.Errorf("case %d: got %v want %v", i, c.before.pool, c.after.pool)
303                 }
304                 if !testutil.DeepEqual(c.before.utxo, c.after.utxo) {
305                         t.Errorf("case %d: got %v want %v", i, c.before.utxo, c.after.utxo)
306                 }
307         }
308 }
309
310 func TestExpireOrphan(t *testing.T) {
311         before := &TxPool{
312                 orphans: map[bc.Hash]*orphanTx{
313                         testTxs[0].ID: {
314                                 expiration: time.Unix(1533489701, 0),
315                                 TxDesc: &TxDesc{
316                                         Tx: testTxs[0],
317                                 },
318                         },
319                         testTxs[1].ID: {
320                                 expiration: time.Unix(1633489701, 0),
321                                 TxDesc: &TxDesc{
322                                         Tx: testTxs[1],
323                                 },
324                         },
325                 },
326                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
327                         testTxs[0].SpentOutputIDs[0]: {
328                                 testTxs[0].ID: {
329                                         expiration: time.Unix(1533489701, 0),
330                                         TxDesc: &TxDesc{
331                                                 Tx: testTxs[0],
332                                         },
333                                 },
334                                 testTxs[1].ID: {
335                                         expiration: time.Unix(1633489701, 0),
336                                         TxDesc: &TxDesc{
337                                                 Tx: testTxs[1],
338                                         },
339                                 },
340                         },
341                 },
342         }
343
344         want := &TxPool{
345                 orphans: map[bc.Hash]*orphanTx{
346                         testTxs[1].ID: {
347                                 expiration: time.Unix(1633489701, 0),
348                                 TxDesc: &TxDesc{
349                                         Tx: testTxs[1],
350                                 },
351                         },
352                 },
353                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
354                         testTxs[0].SpentOutputIDs[0]: {
355                                 testTxs[1].ID: {
356                                         expiration: time.Unix(1633489701, 0),
357                                         TxDesc: &TxDesc{
358                                                 Tx: testTxs[1],
359                                         },
360                                 },
361                         },
362                 },
363         }
364
365         before.ExpireOrphan(time.Unix(1633479701, 0))
366         if !testutil.DeepEqual(before, want) {
367                 t.Errorf("got %v want %v", before, want)
368         }
369 }
370
371 func TestProcessOrphans(t *testing.T) {
372         dispatcher := event.NewDispatcher()
373         cases := []struct {
374                 before    *TxPool
375                 after     *TxPool
376                 processTx *TxDesc
377         }{
378                 {
379                         before: &TxPool{
380                                 pool:            map[bc.Hash]*TxDesc{},
381                                 utxo:            map[bc.Hash]*types.Tx{},
382                                 eventDispatcher: dispatcher,
383                                 orphans: map[bc.Hash]*orphanTx{
384                                         testTxs[3].ID: {
385                                                 TxDesc: &TxDesc{
386                                                         Tx: testTxs[3],
387                                                 },
388                                         },
389                                 },
390                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
391                                         testTxs[3].SpentOutputIDs[0]: {
392                                                 testTxs[3].ID: {
393                                                         TxDesc: &TxDesc{
394                                                                 Tx: testTxs[3],
395                                                         },
396                                                 },
397                                         },
398                                 },
399                         },
400                         after: &TxPool{
401                                 pool: map[bc.Hash]*TxDesc{
402                                         testTxs[3].ID: {
403                                                 Tx:         testTxs[3],
404                                                 StatusFail: false,
405                                         },
406                                 },
407                                 utxo: map[bc.Hash]*types.Tx{
408                                         *testTxs[3].ResultIds[0]: testTxs[3],
409                                         *testTxs[3].ResultIds[1]: testTxs[3],
410                                 },
411                                 eventDispatcher: dispatcher,
412                                 orphans:         map[bc.Hash]*orphanTx{},
413                                 orphansByPrev:   map[bc.Hash]map[bc.Hash]*orphanTx{},
414                         },
415                         processTx: &TxDesc{Tx: testTxs[2]},
416                 },
417                 {
418                         before: &TxPool{
419                                 pool:            map[bc.Hash]*TxDesc{},
420                                 utxo:            map[bc.Hash]*types.Tx{},
421                                 eventDispatcher: dispatcher,
422                                 orphans: map[bc.Hash]*orphanTx{
423                                         testTxs[3].ID: {
424                                                 TxDesc: &TxDesc{
425                                                         Tx: testTxs[3],
426                                                 },
427                                         },
428                                         testTxs[4].ID: {
429                                                 TxDesc: &TxDesc{
430                                                         Tx: testTxs[4],
431                                                 },
432                                         },
433                                 },
434                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
435                                         testTxs[3].SpentOutputIDs[0]: {
436                                                 testTxs[3].ID: {
437                                                         TxDesc: &TxDesc{
438                                                                 Tx: testTxs[3],
439                                                         },
440                                                 },
441                                         },
442                                         testTxs[4].SpentOutputIDs[0]: {
443                                                 testTxs[4].ID: {
444                                                         TxDesc: &TxDesc{
445                                                                 Tx: testTxs[4],
446                                                         },
447                                                 },
448                                         },
449                                 },
450                         },
451                         after: &TxPool{
452                                 pool: map[bc.Hash]*TxDesc{
453                                         testTxs[3].ID: {
454                                                 Tx:         testTxs[3],
455                                                 StatusFail: false,
456                                         },
457                                         testTxs[4].ID: {
458                                                 Tx:         testTxs[4],
459                                                 StatusFail: false,
460                                         },
461                                 },
462                                 utxo: map[bc.Hash]*types.Tx{
463                                         *testTxs[3].ResultIds[0]: testTxs[3],
464                                         *testTxs[3].ResultIds[1]: testTxs[3],
465                                         *testTxs[4].ResultIds[0]: testTxs[4],
466                                         *testTxs[4].ResultIds[1]: testTxs[4],
467                                 },
468                                 eventDispatcher: dispatcher,
469                                 orphans:         map[bc.Hash]*orphanTx{},
470                                 orphansByPrev:   map[bc.Hash]map[bc.Hash]*orphanTx{},
471                         },
472                         processTx: &TxDesc{Tx: testTxs[2]},
473                 },
474         }
475
476         for i, c := range cases {
477                 c.before.store = &mockStore{}
478                 c.before.addTransaction(c.processTx)
479                 c.before.processOrphans(c.processTx)
480                 c.before.RemoveTransaction(&c.processTx.Tx.ID)
481                 c.before.store = nil
482                 c.before.lastUpdated = 0
483                 for _, txD := range c.before.pool {
484                         txD.Added = time.Time{}
485                 }
486
487                 if !testutil.DeepEqual(c.before, c.after) {
488                         t.Errorf("case %d: got %v want %v", i, c.before, c.after)
489                 }
490         }
491 }
492
493 func TestRemoveOrphan(t *testing.T) {
494         cases := []struct {
495                 before       *TxPool
496                 after        *TxPool
497                 removeHashes []*bc.Hash
498         }{
499                 {
500                         before: &TxPool{
501                                 orphans: map[bc.Hash]*orphanTx{
502                                         testTxs[0].ID: {
503                                                 expiration: time.Unix(1533489701, 0),
504                                                 TxDesc: &TxDesc{
505                                                         Tx: testTxs[0],
506                                                 },
507                                         },
508                                 },
509                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
510                                         testTxs[0].SpentOutputIDs[0]: {
511                                                 testTxs[0].ID: {
512                                                         expiration: time.Unix(1533489701, 0),
513                                                         TxDesc: &TxDesc{
514                                                                 Tx: testTxs[0],
515                                                         },
516                                                 },
517                                         },
518                                 },
519                         },
520                         after: &TxPool{
521                                 orphans:       map[bc.Hash]*orphanTx{},
522                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{},
523                         },
524                         removeHashes: []*bc.Hash{
525                                 &testTxs[0].ID,
526                         },
527                 },
528                 {
529                         before: &TxPool{
530                                 orphans: map[bc.Hash]*orphanTx{
531                                         testTxs[0].ID: {
532                                                 expiration: time.Unix(1533489701, 0),
533                                                 TxDesc: &TxDesc{
534                                                         Tx: testTxs[0],
535                                                 },
536                                         },
537                                         testTxs[1].ID: {
538                                                 expiration: time.Unix(1533489701, 0),
539                                                 TxDesc: &TxDesc{
540                                                         Tx: testTxs[1],
541                                                 },
542                                         },
543                                 },
544                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
545                                         testTxs[0].SpentOutputIDs[0]: {
546                                                 testTxs[0].ID: {
547                                                         expiration: time.Unix(1533489701, 0),
548                                                         TxDesc: &TxDesc{
549                                                                 Tx: testTxs[0],
550                                                         },
551                                                 },
552                                                 testTxs[1].ID: {
553                                                         expiration: time.Unix(1533489701, 0),
554                                                         TxDesc: &TxDesc{
555                                                                 Tx: testTxs[1],
556                                                         },
557                                                 },
558                                         },
559                                 },
560                         },
561                         after: &TxPool{
562                                 orphans: map[bc.Hash]*orphanTx{
563                                         testTxs[0].ID: {
564                                                 expiration: time.Unix(1533489701, 0),
565                                                 TxDesc: &TxDesc{
566                                                         Tx: testTxs[0],
567                                                 },
568                                         },
569                                 },
570                                 orphansByPrev: map[bc.Hash]map[bc.Hash]*orphanTx{
571                                         testTxs[0].SpentOutputIDs[0]: {
572                                                 testTxs[0].ID: {
573                                                         expiration: time.Unix(1533489701, 0),
574                                                         TxDesc: &TxDesc{
575                                                                 Tx: testTxs[0],
576                                                         },
577                                                 },
578                                         },
579                                 },
580                         },
581                         removeHashes: []*bc.Hash{
582                                 &testTxs[1].ID,
583                         },
584                 },
585         }
586
587         for i, c := range cases {
588                 for _, hash := range c.removeHashes {
589                         c.before.removeOrphan(hash)
590                 }
591                 if !testutil.DeepEqual(c.before, c.after) {
592                         t.Errorf("case %d: got %v want %v", i, c.before, c.after)
593                 }
594         }
595 }
596
597 type mockStore1 struct{}
598
599 func (s *mockStore1) SaveChainStatus(*state.BlockNode, *state.UtxoViewpoint, *state.ContractViewpoint) error {
600         return nil
601 }
602 func (s *mockStore1) GetBlockHeader(hash *bc.Hash) (*types.BlockHeader, error)     { return nil, nil }
603 func (s *mockStore1) GetCheckpoint(hash *bc.Hash) (*state.Checkpoint, error)       { return nil, nil }
604 func (s *mockStore1) GetCheckpointsByHeight(u uint64) ([]*state.Checkpoint, error) { return nil, nil }
605 func (s *mockStore1) BlockExist(hash *bc.Hash) bool                                { return false }
606 func (s *mockStore1) GetBlock(*bc.Hash) (*types.Block, error)                      { return nil, nil }
607 func (s *mockStore1) GetStoreStatus() *BlockStoreState                             { return nil }
608 func (s *mockStore1) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
609 func (s *mockStore1) GetTransactionsUtxo(utxoView *state.UtxoViewpoint, tx []*bc.Tx) error {
610         for _, hash := range testTxs[2].SpentOutputIDs {
611                 utxoView.Entries[hash] = &storage.UtxoEntry{IsCoinBase: false, Spent: false}
612         }
613         return nil
614 }
615 func (s *mockStore1) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error)        { return nil, nil }
616 func (s *mockStore1) LoadBlockIndex(uint64) (*state.BlockIndex, error)    { return nil, nil }
617 func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
618
619 func TestProcessTransaction(t *testing.T) {
620         txPool := &TxPool{
621                 pool:            make(map[bc.Hash]*TxDesc),
622                 utxo:            make(map[bc.Hash]*types.Tx),
623                 orphans:         make(map[bc.Hash]*orphanTx),
624                 orphansByPrev:   make(map[bc.Hash]map[bc.Hash]*orphanTx),
625                 store:           &mockStore1{},
626                 eventDispatcher: event.NewDispatcher(),
627         }
628         cases := []struct {
629                 want  *TxPool
630                 addTx *TxDesc
631         }{
632                 //Dust tx
633                 {
634                         want: &TxPool{},
635                         addTx: &TxDesc{
636                                 Tx:         testTxs[3],
637                                 StatusFail: false,
638                         },
639                 },
640                 //Dust tx
641                 {
642                         want: &TxPool{},
643                         addTx: &TxDesc{
644                                 Tx:         testTxs[4],
645                                 StatusFail: false,
646                         },
647                 },
648                 //Dust tx
649                 {
650                         want: &TxPool{},
651                         addTx: &TxDesc{
652                                 Tx:         testTxs[5],
653                                 StatusFail: false,
654                         },
655                 },
656                 //Dust tx
657                 {
658                         want: &TxPool{},
659                         addTx: &TxDesc{
660                                 Tx:         testTxs[6],
661                                 StatusFail: false,
662                         },
663                 },
664                 //normal tx
665                 {
666                         want: &TxPool{
667                                 pool: map[bc.Hash]*TxDesc{
668                                         testTxs[2].ID: {
669                                                 Tx:         testTxs[2],
670                                                 StatusFail: false,
671                                                 Weight:     150,
672                                         },
673                                 },
674                                 utxo: map[bc.Hash]*types.Tx{
675                                         *testTxs[2].ResultIds[0]: testTxs[2],
676                                         *testTxs[2].ResultIds[1]: testTxs[2],
677                                 },
678                         },
679                         addTx: &TxDesc{
680                                 Tx:         testTxs[2],
681                                 StatusFail: false,
682                         },
683                 },
684         }
685
686         for i, c := range cases {
687                 txPool.ProcessTransaction(c.addTx.Tx, c.addTx.StatusFail, 0, 0)
688                 for _, txD := range txPool.pool {
689                         txD.Added = time.Time{}
690                 }
691                 for _, txD := range txPool.orphans {
692                         txD.Added = time.Time{}
693                         txD.expiration = time.Time{}
694                 }
695
696                 if !testutil.DeepEqual(txPool.pool, c.want.pool) {
697                         t.Errorf("case %d: test ProcessTransaction pool mismatch got %s want %s", i, spew.Sdump(txPool.pool), spew.Sdump(c.want.pool))
698                 }
699                 if !testutil.DeepEqual(txPool.utxo, c.want.utxo) {
700                         t.Errorf("case %d: test ProcessTransaction utxo mismatch got %s want %s", i, spew.Sdump(txPool.utxo), spew.Sdump(c.want.utxo))
701                 }
702                 if !testutil.DeepEqual(txPool.orphans, c.want.orphans) {
703                         t.Errorf("case %d: test ProcessTransaction orphans mismatch got %s want %s", i, spew.Sdump(txPool.orphans), spew.Sdump(c.want.orphans))
704                 }
705                 if !testutil.DeepEqual(txPool.orphansByPrev, c.want.orphansByPrev) {
706                         t.Errorf("case %d: test ProcessTransaction orphansByPrev mismatch got %s want %s", i, spew.Sdump(txPool.orphansByPrev), spew.Sdump(c.want.orphansByPrev))
707                 }
708         }
709 }