OSDN Git Service

aggregate coinbase rewards (#239)
[bytom/vapor.git] / blockchain / txbuilder / finalize.go
index f701548..577830f 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"
@@ -31,7 +32,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
        }
 
@@ -128,30 +131,6 @@ 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
-}
-
 func checkGasInputIDs(tx *types.Tx) error {
        for _, inp := range tx.Inputs {
                switch inp.InputType() {