OSDN Git Service

check the dust transaction with output amount is zero (#1659)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Fri, 29 Mar 2019 03:20:45 +0000 (11:20 +0800)
committerPaladz <yzhu101@uottawa.ca>
Fri, 29 Mar 2019 03:20:45 +0000 (11:20 +0800)
* check the dust transaction with output amount is zero

* optimise

* refactor

protocol/txpool.go
protocol/txpool_test.go

index 8578028..f24710c 100644 (file)
@@ -201,8 +201,17 @@ func isTransactionNoBtmInput(tx *types.Tx) bool {
        return true
 }
 
+func isTransactionZeroOutput(tx *types.Tx) bool {
+       for _, output := range tx.TxData.Outputs {
+               if output.Amount == uint64(0) {
+                       return true
+               }
+       }
+       return false
+}
+
 func (tp *TxPool) IsDust(tx *types.Tx) bool {
-       return isTransactionNoBtmInput(tx)
+       return isTransactionNoBtmInput(tx) || isTransactionZeroOutput(tx)
 }
 
 func (tp *TxPool) processTransaction(tx *types.Tx, statusFail bool, height, fee uint64) (bool, error) {
index 7d87648..243b39f 100644 (file)
@@ -71,6 +71,28 @@ var testTxs = []*types.Tx{
                        types.NewTxOutput(bc.NewAssetID([32]byte{0xa1}), 1, []byte{0x65}),
                },
        }),
+       //tx5
+       types.NewTx(types.TxData{
+               SerializedSize: 100,
+               Inputs: []*types.TxInput{
+                       types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 1, 1, []byte{0x51}),
+               },
+               Outputs: []*types.TxOutput{
+                       types.NewTxOutput(*consensus.BTMAssetID, 0, []byte{0x51}),
+               },
+       }),
+       //tx6
+       types.NewTx(types.TxData{
+               SerializedSize: 100,
+               Inputs: []*types.TxInput{
+                       types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, 3, 1, []byte{0x51}),
+                       types.NewSpendInput(nil, testutil.MustDecodeHash("d84d0be0fd08e7341f2d127749bb0d0844d4560f53bd54861cee9981fd922cad"), bc.NewAssetID([32]byte{0xa1}), 3, 0, []byte{0x62}),
+               },
+               Outputs: []*types.TxOutput{
+                       types.NewTxOutput(*consensus.BTMAssetID, 2, []byte{0x51}),
+                       types.NewTxOutput(bc.NewAssetID([32]byte{0xa1}), 0, []byte{0x65}),
+               },
+       }),
 }
 
 type mockStore struct{}
@@ -605,6 +627,30 @@ func TestProcessTransaction(t *testing.T) {
                                StatusFail: false,
                        },
                },
+               //Dust tx
+               {
+                       want: &TxPool{},
+                       addTx: &TxDesc{
+                               Tx:         testTxs[4],
+                               StatusFail: false,
+                       },
+               },
+               //Dust tx
+               {
+                       want: &TxPool{},
+                       addTx: &TxDesc{
+                               Tx:         testTxs[5],
+                               StatusFail: false,
+                       },
+               },
+               //Dust tx
+               {
+                       want: &TxPool{},
+                       addTx: &TxDesc{
+                               Tx:         testTxs[6],
+                               StatusFail: false,
+                       },
+               },
                //normal tx
                {
                        want: &TxPool{