OSDN Git Service

add free gas (#295)
[bytom/vapor.git] / blockchain / txbuilder / finalize.go
index daa6308..0955876 100644 (file)
@@ -4,9 +4,10 @@ import (
        "bytes"
        "context"
 
+       "github.com/vapor/common/arithmetic"
        cfg "github.com/vapor/config"
-       "github.com/vapor/consensus"
        "github.com/vapor/errors"
+       "github.com/vapor/math/checked"
        "github.com/vapor/protocol"
        "github.com/vapor/protocol/bc/types"
        "github.com/vapor/protocol/vm"
@@ -29,7 +30,9 @@ var (
 // 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
        }
 
@@ -42,15 +45,17 @@ func FinalizeTx(ctx context.Context, c *protocol.Chain, tx *types.Tx) error {
        if err != nil {
                return err
        }
-       tx.TxData.SerializedSize = uint64(len(data))
-       tx.Tx.SerializedSize = uint64(len(data))
+       tx.TxData.SerializedSize = uint64(len(data) / 2)
+       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
        }
@@ -84,10 +89,6 @@ func checkTxSighashCommitment(tx *types.Tx) error {
                switch t := inp.TypedInput.(type) {
                case *types.SpendInput:
                        args = t.Arguments
-               case *types.IssuanceInput:
-                       args = t.Arguments
-               case *types.ClaimInput:
-                       args = t.Arguments
                }
                // Note: These numbers will need to change if more args are added such that the minimum length changes
                switch {
@@ -124,23 +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 && input.AssetID() == *consensus.BTMAssetID {
-                       totalInputBTM += input.Amount()
-               }
-       }
-
-       for _, output := range tx.Outputs {
-               if *output.AssetId == *consensus.BTMAssetID {
-                       totalOutputBTM += output.Amount
-               }
-       }
-       fee = totalInputBTM - totalOutputBTM
-       return
-}