4 "github.com/bytom/errors"
5 "github.com/bytom/protocol/bc"
6 "github.com/bytom/protocol/bc/types"
7 "github.com/bytom/protocol/state"
8 "github.com/bytom/protocol/validation"
11 // ErrBadTx is returned for transactions failing validation
12 var ErrBadTx = errors.New("invalid transaction")
14 // GetTransactionStatus return the transaction status of give block
15 func (c *Chain) GetTransactionStatus(hash *bc.Hash) (*bc.TransactionStatus, error) {
16 return c.store.GetTransactionStatus(hash)
19 // GetTransactionsUtxo return all the utxos that related to the txs' inputs
20 func (c *Chain) GetTransactionsUtxo(view *state.UtxoViewpoint, txs []*bc.Tx) error {
21 return c.store.GetTransactionsUtxo(view, txs)
24 // ValidateTx validates the given transaction. A cache holds
25 // per-transaction validation results and is consulted before
26 // performing full validation.
27 func (c *Chain) ValidateTx(tx *types.Tx) (bool, error) {
28 if ok := c.txPool.HaveTransaction(&tx.ID); ok {
29 return false, c.txPool.GetErrCache(&tx.ID)
32 view := c.txPool.GetTransactionUTXO(tx.Tx)
33 if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx.Tx}); err != nil {
37 bh := c.BestBlockHeader()
38 block := types.MapBlock(&types.Block{BlockHeader: *bh})
39 if err := view.ApplyTransaction(block, tx.Tx, false); err != nil {
43 gasStatus, err := validation.ValidateTx(tx.Tx, block)
44 if gasStatus.GasValid == false {
45 c.txPool.AddErrCache(&tx.ID, err)
49 _, err = c.txPool.AddTransaction(tx, err != nil, block.BlockHeader.Height, gasStatus.BTMValue)