OSDN Git Service

txpool: periodically sweep pool for stale Txs (#337)
[bytom/vapor.git] / protocol / txpool_test.go
index 9db8aa8..a2e63a2 100644 (file)
@@ -94,19 +94,38 @@ var testTxs = []*types.Tx{
                        types.NewIntraChainOutput(bc.NewAssetID([32]byte{0xa1}), 0, []byte{0x65}),
                },
        }),
+       //tx7
+       types.NewTx(types.TxData{
+               SerializedSize: 150,
+               TimeRange:      0,
+               Inputs: []*types.TxInput{
+                       types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 1, 1, []byte{0x51}),
+                       types.NewSpendInput(nil, bc.NewHash([32]byte{0x02}), bc.NewAssetID([32]byte{0xa1}), 4, 1, []byte{0x51}),
+               },
+               Outputs: []*types.TxOutput{
+                       types.NewIntraChainOutput(*consensus.BTMAssetID, 1, []byte{0x6b}),
+                       types.NewVoteOutput(bc.NewAssetID([32]byte{0xa1}), 4, []byte{0x61}, []byte("a8f410b9f7cd9ce352d215ed17c85559c351dc8d18ed89ad403ca28cfc423f612e04a1c9584f945c286c47ec1e5b8405c65ff56e31f44a2627aca4f77e03936f")),
+               },
+       }),
 }
 
 type mockStore struct{}
 
 func (s *mockStore) BlockExist(hash *bc.Hash) bool                                { return false }
 func (s *mockStore) GetBlock(*bc.Hash) (*types.Block, error)                      { return nil, nil }
+func (s *mockStore) GetBlockHeader(*bc.Hash) (*types.BlockHeader, error)          { return nil, nil }
 func (s *mockStore) GetStoreStatus() *BlockStoreState                             { return nil }
 func (s *mockStore) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
 func (s *mockStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error     { return nil }
 func (s *mockStore) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error)                 { return nil, nil }
-func (s *mockStore) LoadBlockIndex(uint64) (*state.BlockIndex, error)             { return nil, nil }
+func (s *mockStore) GetConsensusResult(uint64) (*state.ConsensusResult, error)    { return nil, nil }
+func (s *mockStore) GetMainChainHash(uint64) (*bc.Hash, error)                    { return nil, nil }
+func (s *mockStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error)            { return nil, nil }
 func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error          { return nil }
-func (s *mockStore) SaveChainStatus(*state.BlockNode, *state.UtxoViewpoint) error { return nil }
+func (s *mockStore) SaveBlockHeader(*types.BlockHeader) error                     { return nil }
+func (s *mockStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
+       return nil
+}
 
 func TestAddOrphan(t *testing.T) {
        cases := []struct {
@@ -287,6 +306,51 @@ func TestAddTransaction(t *testing.T) {
                                StatusFail: true,
                        },
                },
+               {
+                       before: &TxPool{
+                               pool:            map[bc.Hash]*TxDesc{},
+                               utxo:            map[bc.Hash]*types.Tx{},
+                               eventDispatcher: dispatcher,
+                       },
+                       after: &TxPool{
+                               pool: map[bc.Hash]*TxDesc{
+                                       testTxs[7].ID: {
+                                               Tx:         testTxs[7],
+                                               StatusFail: false,
+                                       },
+                               },
+                               utxo: map[bc.Hash]*types.Tx{
+                                       *testTxs[7].ResultIds[0]: testTxs[7],
+                                       *testTxs[7].ResultIds[1]: testTxs[7],
+                               },
+                       },
+                       addTx: &TxDesc{
+                               Tx:         testTxs[7],
+                               StatusFail: false,
+                       },
+               },
+               {
+                       before: &TxPool{
+                               pool:            map[bc.Hash]*TxDesc{},
+                               utxo:            map[bc.Hash]*types.Tx{},
+                               eventDispatcher: dispatcher,
+                       },
+                       after: &TxPool{
+                               pool: map[bc.Hash]*TxDesc{
+                                       testTxs[7].ID: {
+                                               Tx:         testTxs[7],
+                                               StatusFail: true,
+                                       },
+                               },
+                               utxo: map[bc.Hash]*types.Tx{
+                                       *testTxs[7].ResultIds[0]: testTxs[7],
+                               },
+                       },
+                       addTx: &TxDesc{
+                               Tx:         testTxs[7],
+                               StatusFail: true,
+                       },
+               },
        }
 
        for i, c := range cases {
@@ -358,7 +422,7 @@ func TestExpireOrphan(t *testing.T) {
                },
        }
 
-       before.ExpireOrphan(time.Unix(1633479701, 0))
+       before.expireOrphan(time.Unix(1633479701, 0))
        if !testutil.DeepEqual(before, want) {
                t.Errorf("got %v want %v", before, want)
        }
@@ -594,18 +658,25 @@ type mockStore1 struct{}
 
 func (s *mockStore1) BlockExist(hash *bc.Hash) bool                                { return false }
 func (s *mockStore1) GetBlock(*bc.Hash) (*types.Block, error)                      { return nil, nil }
+func (s *mockStore1) GetBlockHeader(*bc.Hash) (*types.BlockHeader, error)          { return nil, nil }
 func (s *mockStore1) GetStoreStatus() *BlockStoreState                             { return nil }
 func (s *mockStore1) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
 func (s *mockStore1) GetTransactionsUtxo(utxoView *state.UtxoViewpoint, tx []*bc.Tx) error {
+       // TODO:
        for _, hash := range testTxs[2].SpentOutputIDs {
-               utxoView.Entries[hash] = &storage.UtxoEntry{IsCoinBase: false, Spent: false}
+               utxoView.Entries[hash] = &storage.UtxoEntry{Type: storage.NormalUTXOType, Spent: false}
        }
        return nil
 }
-func (s *mockStore1) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error)                 { return nil, nil }
-func (s *mockStore1) LoadBlockIndex(uint64) (*state.BlockIndex, error)             { return nil, nil }
-func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error          { return nil }
-func (s *mockStore1) SaveChainStatus(*state.BlockNode, *state.UtxoViewpoint) error { return nil }
+func (s *mockStore1) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error)              { return nil, nil }
+func (s *mockStore1) GetConsensusResult(uint64) (*state.ConsensusResult, error) { return nil, nil }
+func (s *mockStore1) GetMainChainHash(uint64) (*bc.Hash, error)                 { return nil, nil }
+func (s *mockStore1) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error)         { return nil, nil }
+func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error       { return nil }
+func (s *mockStore1) SaveBlockHeader(*types.BlockHeader) error                  { return nil }
+func (s *mockStore1) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
+       return nil
+}
 
 func TestProcessTransaction(t *testing.T) {
        txPool := &TxPool{
@@ -624,22 +695,6 @@ func TestProcessTransaction(t *testing.T) {
                {
                        want: &TxPool{},
                        addTx: &TxDesc{
-                               Tx:         testTxs[3],
-                               StatusFail: false,
-                       },
-               },
-               //Dust tx
-               {
-                       want: &TxPool{},
-                       addTx: &TxDesc{
-                               Tx:         testTxs[4],
-                               StatusFail: false,
-                       },
-               },
-               //Dust tx
-               {
-                       want: &TxPool{},
-                       addTx: &TxDesc{
                                Tx:         testTxs[5],
                                StatusFail: false,
                        },