OSDN Git Service

rename (#465)
[bytom/vapor.git] / blockchain / txbuilder / finalize.go
index b9e85bb..3a6ef25 100644 (file)
@@ -4,12 +4,13 @@ import (
        "bytes"
        "context"
 
-       cfg "github.com/vapor/config"
-       "github.com/vapor/consensus"
-       "github.com/vapor/errors"
-       "github.com/vapor/protocol"
-       "github.com/vapor/protocol/bc/types"
-       "github.com/vapor/protocol/vm"
+       "github.com/bytom/vapor/common/arithmetic"
+       cfg "github.com/bytom/vapor/config"
+       "github.com/bytom/vapor/errors"
+       "github.com/bytom/vapor/math/checked"
+       "github.com/bytom/vapor/protocol"
+       "github.com/bytom/vapor/protocol/bc/types"
+       "github.com/bytom/vapor/protocol/vm"
 )
 
 var (
@@ -23,15 +24,15 @@ var (
        ErrOrphanTx = errors.New("finalize can't find transaction input utxo")
        // ErrExtTxFee means transaction fee exceed max limit
        ErrExtTxFee = errors.New("transaction fee exceed max limit")
-       // ErrNoGasInput means transaction has no gas input
-       ErrNoGasInput = errors.New("transaction has no gas input")
 )
 
 // FinalizeTx validates a transaction signature template,
 // assembles a fully signed tx, and stores the effects of
 // its changes on the UTXO set.
 func FinalizeTx(ctx context.Context, c *protocol.Chain, tx *types.Tx) error {
-       if fee := CalculateTxFee(tx); fee > cfg.CommonConfig.Wallet.MaxTxFee {
+       if fee, err := arithmetic.CalculateTxFee(tx); err != nil {
+               return checked.ErrOverflow
+       } else if fee > cfg.CommonConfig.Wallet.MaxTxFee {
                return ErrExtTxFee
        }
 
@@ -39,10 +40,6 @@ func FinalizeTx(ctx context.Context, c *protocol.Chain, tx *types.Tx) error {
                return err
        }
 
-       if len(tx.GasInputIDs) == 0 {
-               return ErrNoGasInput
-       }
-
        // This part is use for prevent tx size  is 0
        data, err := tx.TxData.MarshalText()
        if err != nil {
@@ -52,12 +49,13 @@ func FinalizeTx(ctx context.Context, c *protocol.Chain, tx *types.Tx) error {
        tx.Tx.SerializedSize = uint64(len(data) / 2)
 
        isOrphan, err := c.ValidateTx(tx)
-       if errors.Root(err) == protocol.ErrBadTx {
-               return errors.Sub(ErrRejected, err)
-       }
        if err != nil {
-               return errors.WithDetail(err, "tx rejected: "+err.Error())
+               if errors.Root(err) == err {
+                       return errors.Sub(ErrRejected, err)
+               }
+               return err
        }
+
        if isOrphan {
                return ErrOrphanTx
        }
@@ -127,27 +125,3 @@ func checkTxSighashCommitment(tx *types.Tx) error {
 
        return lastError
 }
-
-// CalculateTxFee calculate transaction fee
-func CalculateTxFee(tx *types.Tx) (fee uint64) {
-       totalInputBTM := uint64(0)
-       totalOutputBTM := uint64(0)
-
-       for _, input := range tx.Inputs {
-               if input.InputType() == types.CoinbaseInputType {
-                       return 0
-               }
-               if input.AssetID() == *consensus.BTMAssetID {
-                       totalInputBTM += input.Amount()
-               }
-       }
-
-       for _, output := range tx.Outputs {
-               if *output.AssetAmount().AssetId == *consensus.BTMAssetID {
-                       totalOutputBTM += output.AssetAmount().Amount
-               }
-       }
-
-       fee = totalInputBTM - totalOutputBTM
-       return
-}