OSDN Git Service

opt mov (#511)
authorPoseidon <shenao.78@163.com>
Wed, 11 Mar 2020 02:49:05 +0000 (10:49 +0800)
committerGitHub <noreply@github.com>
Wed, 11 Mar 2020 02:49:05 +0000 (10:49 +0800)
application/mov/match/engine.go [moved from application/mov/match/match.go with 100% similarity]
application/mov/match/engine_test.go [moved from application/mov/match/match_test.go with 99% similarity]
application/mov/match/fee_strategy.go [moved from application/mov/match/match_fee.go with 83% similarity]
application/mov/mov_core.go

similarity index 100%
rename from application/mov/match/match.go
rename to application/mov/match/engine.go
index da08869..8ccb9ae 100644 (file)
@@ -216,29 +216,6 @@ func setMatchTxArguments(txInput *types.TxInput, isPartialTrade bool, position i
        txInput.SetArguments(arguments)
 }
 
-func validateTradePairs(tradePairs []*common.TradePair) error {
-       if len(tradePairs) < 2 {
-               return errors.New("size of trade pairs at least 2")
-       }
-
-       assetMap := make(map[string]bool)
-       for _, tradePair := range tradePairs {
-               assetMap[tradePair.FromAssetID.String()] = true
-               if *tradePair.FromAssetID == *tradePair.ToAssetID {
-                       return errors.New("from asset id can't equal to asset id")
-               }
-       }
-
-       for _, tradePair := range tradePairs {
-               key := tradePair.ToAssetID.String()
-               if _, ok := assetMap[key]; !ok {
-                       return errors.New("invalid trade pairs")
-               }
-               delete(assetMap, key)
-       }
-       return nil
-}
-
 func sortOrders(orders []*common.Order) []*common.Order {
        if len(orders) == 0 {
                return nil
@@ -262,3 +239,26 @@ func sortOrders(orders []*common.Order) []*common.Order {
        }
        return sortedOrders
 }
+
+func validateTradePairs(tradePairs []*common.TradePair) error {
+       if len(tradePairs) < 2 {
+               return errors.New("size of trade pairs at least 2")
+       }
+
+       assetMap := make(map[string]bool)
+       for _, tradePair := range tradePairs {
+               assetMap[tradePair.FromAssetID.String()] = true
+               if *tradePair.FromAssetID == *tradePair.ToAssetID {
+                       return errors.New("from asset id can't equal to asset id")
+               }
+       }
+
+       for _, tradePair := range tradePairs {
+               key := tradePair.ToAssetID.String()
+               if _, ok := assetMap[key]; !ok {
+                       return errors.New("invalid trade pairs")
+               }
+               delete(assetMap, key)
+       }
+       return nil
+}
similarity index 99%
rename from application/mov/match/match_test.go
rename to application/mov/match/engine_test.go
index ffa5398..1ac993a 100644 (file)
@@ -89,7 +89,7 @@ func TestGenerateMatchedTxs(t *testing.T) {
 
        for i, c := range cases {
                movStore := mock.NewMovStore([]*common.TradePair{btc2eth, eth2btc}, c.initStoreOrders)
-               matchEngine := NewEngine(NewOrderBook(movStore, nil, nil), NewDefaultFeeStrategy(0.05), mock.RewardProgram)
+               matchEngine := NewEngine(NewOrderBook(movStore, nil, nil), NewDefaultFeeStrategy(), mock.RewardProgram)
                var gotMatchedTxs []*types.Tx
                for matchEngine.HasMatchedTx(c.tradePairs...) {
                        matchedTx, err := matchEngine.NextMatchedTx(c.tradePairs...)
similarity index 83%
rename from application/mov/match/match_fee.go
rename to application/mov/match/fee_strategy.go
index 62e2211..f3d1dfc 100644 (file)
@@ -35,13 +35,11 @@ type FeeStrategy interface {
 }
 
 // DefaultFeeStrategy represent the default fee charge strategy
-type DefaultFeeStrategy struct {
-       maxFeeRate float64
-}
+type DefaultFeeStrategy struct {}
 
 // NewDefaultFeeStrategy return a new instance of DefaultFeeStrategy
-func NewDefaultFeeStrategy(maxFeeRate float64) *DefaultFeeStrategy {
-       return &DefaultFeeStrategy{maxFeeRate: maxFeeRate}
+func NewDefaultFeeStrategy() *DefaultFeeStrategy {
+       return &DefaultFeeStrategy{}
 }
 
 // Allocate will allocate the price differential in matching transaction to the participants and the fee
@@ -57,20 +55,18 @@ func (d *DefaultFeeStrategy) Allocate(receiveAmounts, priceDiffs []*bc.AssetAmou
 
        for i, receiveAmount := range receiveAmounts {
                amount := receiveAmount.Amount
-               minFeeAmount := calcMinFeeAmount(amount)
+               minFeeAmount := d.calcMinFeeAmount(amount)
                receives[i] = &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: amount - minFeeAmount}
                feeMap[*receiveAmount.AssetId] += minFeeAmount
 
-               maxFeeAmount := calcMaxFeeAmount(amount, d.maxFeeRate)
+               maxFeeAmount := d.calcMaxFeeAmount(amount)
                feeAmount, reminder := feeMap[*receiveAmount.AssetId], uint64(0)
                if feeAmount > maxFeeAmount {
                        reminder = feeAmount - maxFeeAmount
                        feeAmount = maxFeeAmount
                }
 
-               if feeAmount > 0 {
-                       fees = append(fees, &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: feeAmount})
-               }
+               fees = append(fees, &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: feeAmount})
 
                // There is the remaining amount after paying the handling fee, assign it evenly to participants in the transaction
                averageAmount := reminder / uint64(len(receiveAmounts))
@@ -94,8 +90,8 @@ func (d *DefaultFeeStrategy) Allocate(receiveAmounts, priceDiffs []*bc.AssetAmou
 func (d *DefaultFeeStrategy) Validate(receiveAmounts []*bc.AssetAmount, feeAmounts map[bc.AssetID]uint64) error {
        for _, receiveAmount := range receiveAmounts {
                feeAmount := feeAmounts[*receiveAmount.AssetId]
-               maxFeeAmount := calcMaxFeeAmount(receiveAmount.Amount, d.maxFeeRate)
-               minFeeAmount := calcMinFeeAmount(receiveAmount.Amount)
+               maxFeeAmount := d.calcMaxFeeAmount(receiveAmount.Amount)
+               minFeeAmount := d.calcMinFeeAmount(receiveAmount.Amount)
                if feeAmount < minFeeAmount || feeAmount > maxFeeAmount {
                        return ErrAmountOfFeeOutOfRange
                }
@@ -103,10 +99,10 @@ func (d *DefaultFeeStrategy) Validate(receiveAmounts []*bc.AssetAmount, feeAmoun
        return nil
 }
 
-func calcMinFeeAmount(amount uint64) uint64 {
+func (d *DefaultFeeStrategy) calcMinFeeAmount(amount uint64) uint64 {
        return uint64(math.Ceil(float64(amount) / 1000))
 }
 
-func calcMaxFeeAmount(amount uint64, maxFeeRate float64) uint64 {
-       return uint64(math.Ceil(float64(amount) * maxFeeRate))
+func (d *DefaultFeeStrategy) calcMaxFeeAmount(amount uint64) uint64 {
+       return uint64(math.Ceil(float64(amount) * 0.05))
 }
index 320a75f..c8c1338 100644 (file)
@@ -15,8 +15,6 @@ import (
        "github.com/bytom/vapor/protocol/bc/types"
 )
 
-const maxFeeRate = 0.05
-
 var (
        errInvalidTradePairs            = errors.New("The trade pairs in the tx input is invalid")
        errStatusFailMustFalse          = errors.New("status fail of transaction does not allow to be true")
@@ -91,7 +89,7 @@ func (m *MovCore) BeforeProposalBlock(txs []*types.Tx, blockHeight uint64, gasLe
                return nil, errNotConfiguredRewardProgram
        }
 
-       matchEngine := match.NewEngine(orderBook, match.NewDefaultFeeStrategy(maxFeeRate), rewardProgram)
+       matchEngine := match.NewEngine(orderBook, match.NewDefaultFeeStrategy(), rewardProgram)
        tradePairIterator := database.NewTradePairIterator(m.movStore)
        matchCollector := newMatchTxCollector(matchEngine, tradePairIterator, gasLeft, isTimeout)
        return matchCollector.result()
@@ -301,13 +299,13 @@ func validateMatchedTxFee(tx *types.Tx, blockHeight uint64) error {
                return err
        }
 
+       receivedAmount, _ := match.CalcReceivedAmount(orders)
        feeAmounts := make(map[bc.AssetID]uint64)
        for assetID, fee := range matchedTxFees {
                feeAmounts[assetID] = fee.amount
        }
 
-       receivedAmount, _ := match.CalcReceivedAmount(orders)
-       feeStrategy := match.NewDefaultFeeStrategy(maxFeeRate)
+       feeStrategy := match.NewDefaultFeeStrategy()
        return feeStrategy.Validate(receivedAmount, feeAmounts)
 }