OSDN Git Service

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