github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/protocol/tx.go (about)

     1  package protocol
     2  
     3  import (
     4  	log "github.com/sirupsen/logrus"
     5  
     6  	"github.com/bytom/bytom/errors"
     7  	"github.com/bytom/bytom/protocol/bc"
     8  	"github.com/bytom/bytom/protocol/bc/types"
     9  	"github.com/bytom/bytom/protocol/state"
    10  	"github.com/bytom/bytom/protocol/validation"
    11  )
    12  
    13  // ErrBadTx is returned for transactions failing validation
    14  var ErrBadTx = errors.New("invalid transaction")
    15  
    16  // GetTransactionStatus return the transaction status of give block
    17  func (c *Chain) GetTransactionStatus(hash *bc.Hash) (*bc.TransactionStatus, error) {
    18  	return c.store.GetTransactionStatus(hash)
    19  }
    20  
    21  // GetTransactionsUtxo return all the utxos that related to the txs' inputs
    22  func (c *Chain) GetTransactionsUtxo(view *state.UtxoViewpoint, txs []*bc.Tx) error {
    23  	return c.store.GetTransactionsUtxo(view, txs)
    24  }
    25  
    26  // ValidateTx validates the given transaction. A cache holds
    27  // per-transaction validation results and is consulted before
    28  // performing full validation.
    29  func (c *Chain) ValidateTx(tx *types.Tx) (bool, error) {
    30  	if ok := c.txPool.HaveTransaction(&tx.ID); ok {
    31  		return false, c.txPool.GetErrCache(&tx.ID)
    32  	}
    33  
    34  	if c.txPool.IsDust(tx) {
    35  		c.txPool.AddErrCache(&tx.ID, ErrDustTx)
    36  		return false, ErrDustTx
    37  	}
    38  
    39  	bh := c.BestBlockHeader()
    40  	gasStatus, err := validation.ValidateTx(tx.Tx, types.MapBlock(&types.Block{BlockHeader: *bh}))
    41  	if !gasStatus.GasValid {
    42  		c.txPool.AddErrCache(&tx.ID, err)
    43  		return false, err
    44  	}
    45  
    46  	if err != nil {
    47  		log.WithFields(log.Fields{"module": logModule, "tx_id": tx.Tx.ID.String(), "error": err}).Info("transaction status fail")
    48  	}
    49  
    50  	return c.txPool.ProcessTransaction(tx, err != nil, bh.Height, gasStatus.BTMValue)
    51  }