OSDN Git Service

core upgrade (#310)
[bytom/bytom.git] / protocol / tx.go
1 package protocol
2
3 import (
4         "github.com/bytom/errors"
5         "github.com/bytom/protocol/bc/legacy"
6         "github.com/bytom/protocol/validation"
7 )
8
9 // ErrBadTx is returned for transactions failing validation
10 var ErrBadTx = errors.New("invalid transaction")
11
12 // ValidateTx validates the given transaction. A cache holds
13 // per-transaction validation results and is consulted before
14 // performing full validation.
15 func (c *Chain) ValidateTx(tx *legacy.Tx) error {
16         newTx := tx.Tx
17         if ok := c.txPool.HaveTransaction(&newTx.ID); ok {
18                 return c.txPool.GetErrCache(&newTx.ID)
19         }
20
21         oldBlock, err := c.GetBlockByHash(c.state.hash)
22         if err != nil {
23                 return err
24         }
25         block := legacy.MapBlock(oldBlock)
26         fee, gasVaild, err := validation.ValidateTx(newTx, block)
27
28         if !gasVaild && err != nil {
29                 c.txPool.AddErrCache(&newTx.ID, err)
30                 return err
31         }
32
33         c.txPool.AddTransaction(tx, block.BlockHeader.Height, fee)
34         return errors.Sub(ErrBadTx, err)
35 }