OSDN Git Service

update doc
[bytom/bytom.git] / protocol / txpool.go
index edb6af0..7bdb1e8 100644 (file)
@@ -9,11 +9,11 @@ import (
        "github.com/golang/groupcache/lru"
        log "github.com/sirupsen/logrus"
 
-       "github.com/bytom/consensus"
-       "github.com/bytom/event"
-       "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/types"
-       "github.com/bytom/protocol/state"
+       "github.com/bytom/bytom/consensus"
+       "github.com/bytom/bytom/event"
+       "github.com/bytom/bytom/protocol/bc"
+       "github.com/bytom/bytom/protocol/bc/types"
+       "github.com/bytom/bytom/protocol/state"
 )
 
 // msg type
@@ -36,6 +36,8 @@ var (
        ErrTransactionNotExist = errors.New("transaction are not existed in the mempool")
        // ErrPoolIsFull indicates the pool is full
        ErrPoolIsFull = errors.New("transaction pool reach the max number")
+       // ErrDustTx indicates transaction is dust tx
+       ErrDustTx = errors.New("transaction is dust tx")
 )
 
 type TxMsgEvent struct{ TxMsg *TxPoolMsg }
@@ -190,8 +192,29 @@ func (tp *TxPool) HaveTransaction(txHash *bc.Hash) bool {
        return tp.IsTransactionInPool(txHash) || tp.IsTransactionInErrCache(txHash)
 }
 
-// ProcessTransaction is the main entry for txpool handle new tx
-func (tp *TxPool) ProcessTransaction(tx *types.Tx, statusFail bool, height, fee uint64) (bool, error) {
+func isTransactionNoBtmInput(tx *types.Tx) bool {
+       for _, input := range tx.TxData.Inputs {
+               if input.AssetID() == *consensus.BTMAssetID {
+                       return false
+               }
+       }
+       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) || isTransactionZeroOutput(tx)
+}
+
+func (tp *TxPool) processTransaction(tx *types.Tx, statusFail bool, height, fee uint64) (bool, error) {
        tp.mtx.Lock()
        defer tp.mtx.Unlock()
 
@@ -219,6 +242,15 @@ func (tp *TxPool) ProcessTransaction(tx *types.Tx, statusFail bool, height, fee
        return false, nil
 }
 
+// ProcessTransaction is the main entry for txpool handle new tx, ignore dust tx.
+func (tp *TxPool) ProcessTransaction(tx *types.Tx, statusFail bool, height, fee uint64) (bool, error) {
+       if tp.IsDust(tx) {
+               log.WithFields(log.Fields{"module": logModule, "tx_id": tx.ID.String()}).Warn("dust tx")
+               return false, nil
+       }
+       return tp.processTransaction(tx, statusFail, height, fee)
+}
+
 func (tp *TxPool) addOrphan(txD *TxDesc, requireParents []*bc.Hash) error {
        if len(tp.orphans) >= maxOrphanNum {
                return ErrPoolIsFull
@@ -277,6 +309,8 @@ func (tp *TxPool) checkOrphanUtxos(tx *types.Tx) ([]*bc.Hash, error) {
 
 func (tp *TxPool) orphanExpireWorker() {
        ticker := time.NewTicker(orphanExpireScanInterval)
+       defer ticker.Stop()
+
        for now := range ticker.C {
                tp.ExpireOrphan(now)
        }