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