OSDN Git Service

Merge pull request #935 from Bytom/dev
[bytom/bytom.git] / protocol / tx.go
index 588b1c0..38a5ab9 100644 (file)
@@ -3,51 +3,49 @@ package protocol
 import (
        "github.com/bytom/errors"
        "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/legacy"
+       "github.com/bytom/protocol/bc/types"
+       "github.com/bytom/protocol/state"
        "github.com/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 *legacy.Tx) error {
-       newTx := tx.Tx
-       if err := c.checkIssuanceWindow(newTx); err != nil {
-               return err
-       }
-       if ok := c.txPool.HaveTransaction(&newTx.ID); ok {
-               return c.txPool.GetErrCache(&newTx.ID)
+func (c *Chain) ValidateTx(tx *types.Tx) (bool, error) {
+       if ok := c.txPool.HaveTransaction(&tx.ID); ok {
+               return false, c.txPool.GetErrCache(&tx.ID)
        }
 
-       oldBlock, err := c.GetBlockByHash(c.state.hash)
-       if err != nil {
-               return err
+       view := c.txPool.GetTransactionUTXO(tx.Tx)
+       if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx.Tx}); err != nil {
+               return true, err
        }
-       block := legacy.MapBlock(oldBlock)
-       fee, err := validation.ValidateTx(newTx, block)
 
-       if err != nil {
-               c.txPool.AddErrCache(&newTx.ID, err)
-               return err
+       bh := c.BestBlockHeader()
+       block := types.MapBlock(&types.Block{BlockHeader: *bh})
+       if err := view.ApplyTransaction(block, tx.Tx, false); err != nil {
+               return true, err
        }
 
-       c.txPool.AddTransaction(tx, block.BlockHeader.Height, fee)
-       return errors.Sub(ErrBadTx, err)
-}
-
-func (c *Chain) checkIssuanceWindow(tx *bc.Tx) error {
-       if c.MaxIssuanceWindow == 0 {
-               return nil
+       gasStatus, err := validation.ValidateTx(tx.Tx, block)
+       if gasStatus.GasValid == false {
+               c.txPool.AddErrCache(&tx.ID, err)
+               return false, err
        }
-       for _, entryID := range tx.InputIDs {
-               if _, err := tx.Issuance(entryID); err == nil {
-                       if tx.MinTimeMs+bc.DurationMillis(c.MaxIssuanceWindow) < tx.MaxTimeMs {
-                               return errors.WithDetailf(ErrBadTx, "issuance input's time window is larger than the network maximum (%s)", c.MaxIssuanceWindow)
-                       }
-               }
-       }
-       return nil
+
+       _, err = c.txPool.AddTransaction(tx, err != nil, block.BlockHeader.Height, gasStatus.BTMValue)
+       return false, err
 }