OSDN Git Service

fix request amount zero (#448)
authorPoseidon <shenao.78@163.com>
Mon, 25 Nov 2019 03:09:02 +0000 (11:09 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 25 Nov 2019 03:09:02 +0000 (11:09 +0800)
application/mov/match/match.go
application/mov/mov_core.go

index 0394393..f3103f4 100644 (file)
@@ -185,7 +185,7 @@ func CalcMatchedTxFee(txData *types.TxData, maxFeeRate float64) (map[bc.AssetID]
                }
 
                oppositeAmount := uint64(assetFeeMap[contractArgs.RequestedAsset].FeeAmount)
-               receiveAmount := vprMath.MinUint64(calcRequestAmount(input.Amount(), contractArgs), oppositeAmount)
+               receiveAmount := vprMath.MinUint64(CalcRequestAmount(input.Amount(), contractArgs), oppositeAmount)
                assetFeeMap[input.AssetID()].MaxFeeAmount = calcMaxFeeAmount(calcShouldPayAmount(receiveAmount, contractArgs), maxFeeRate)
        }
 
@@ -207,7 +207,7 @@ func addMatchTxOutput(txData *types.TxData, txInput *types.TxInput, order *commo
                return err
        }
 
-       requestAmount := calcRequestAmount(order.Utxo.Amount, contractArgs)
+       requestAmount := CalcRequestAmount(order.Utxo.Amount, contractArgs)
        receiveAmount := vprMath.MinUint64(requestAmount, oppositeAmount)
        shouldPayAmount := calcShouldPayAmount(receiveAmount, contractArgs)
        isPartialTrade := requestAmount > receiveAmount
@@ -220,7 +220,7 @@ func addMatchTxOutput(txData *types.TxData, txInput *types.TxInput, order *commo
        return nil
 }
 
-func calcRequestAmount(fromAmount uint64, contractArg *vmutil.MagneticContractArgs) uint64 {
+func CalcRequestAmount(fromAmount uint64, contractArg *vmutil.MagneticContractArgs) uint64 {
        return uint64(int64(fromAmount) * contractArg.RatioNumerator / contractArg.RatioDenominator)
 }
 
index 9eb5b99..1bc9c2f 100644 (file)
@@ -27,6 +27,7 @@ var (
        errNumeratorOfRatioIsOverflow    = errors.New("ratio numerator of contract args product input amount is overflow")
        errLengthOfInputIsIncorrect      = errors.New("length of matched tx input is not equals to actual matched tx input")
        errSpendOutputIDIsIncorrect      = errors.New("spend output id of matched tx is not equals to actual matched tx")
+       errRequestAmountLessThenOne      = errors.New("request amount of order less than one")
 )
 
 // MovCore represent the core logic of the match module, which include generate match transactions before packing the block,
@@ -222,7 +223,7 @@ func validateCancelOrderTx(tx *types.Tx, verifyResult *bc.TxVerifyResult) error
        return nil
 }
 
-func validateMagneticContractArgs(inputAmount uint64, program []byte) error {
+func validateMagneticContractArgs(fromAmount uint64, program []byte) error {
        contractArgs, err := segwit.DecodeP2WMCProgram(program)
        if err != nil {
                return err
@@ -232,9 +233,13 @@ func validateMagneticContractArgs(inputAmount uint64, program []byte) error {
                return errRatioOfTradeLessThanZero
        }
 
-       if _, ok := checked.MulInt64(int64(inputAmount), contractArgs.RatioNumerator); !ok {
+       if _, ok := checked.MulInt64(int64(fromAmount), contractArgs.RatioNumerator); !ok {
                return errNumeratorOfRatioIsOverflow
        }
+
+       if match.CalcRequestAmount(fromAmount, contractArgs) < 1 {
+               return errRequestAmountLessThenOne
+       }
        return nil
 }