OSDN Git Service

filter out known txs (#280)
[bytom/vapor.git] / protocol / tx.go
1 package protocol
2
3 import (
4         log "github.com/sirupsen/logrus"
5
6         "github.com/vapor/errors"
7         "github.com/vapor/protocol/bc"
8         "github.com/vapor/protocol/bc/types"
9         "github.com/vapor/protocol/state"
10         "github.com/vapor/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 c.hasSeenTx(tx) {
31                 return false, nil
32         }
33
34         c.markTransactions(tx)
35
36         return c.validateTx(tx)
37 }
38
39 // validateTx validates the given transaction without checking duplication.
40 func (c *Chain) validateTx(tx *types.Tx) (bool, error) {
41         if ok := c.txPool.HaveTransaction(&tx.ID); ok {
42                 return false, c.txPool.GetErrCache(&tx.ID)
43         }
44
45         if c.txPool.IsDust(tx) {
46                 c.txPool.AddErrCache(&tx.ID, ErrDustTx)
47                 return false, ErrDustTx
48         }
49
50         bh := c.BestBlockHeader()
51         gasStatus, err := validation.ValidateTx(tx.Tx, types.MapBlock(&types.Block{BlockHeader: *bh}))
52         if !gasStatus.GasValid {
53                 c.txPool.AddErrCache(&tx.ID, err)
54                 return false, err
55         }
56
57         if err != nil {
58                 log.WithFields(log.Fields{"module": logModule, "tx_id": tx.Tx.ID.String(), "error": err}).Info("transaction status fail")
59         }
60
61         return c.txPool.ProcessTransaction(tx, err != nil, bh.Height, gasStatus.BTMValue)
62 }