• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版abfee0cb744c0bb224eb41515016d1644365d585 (tree)
时间2020-03-11 11:06:41
作者shenao78 <shenao.78@163....>
Commitershenao78

Log Message

opt mov

更改概述

  • delete: application/mov/match/{match.go => engine.go}
  • delete: application/mov/match/{match_fee.go => fee_strategy.go}
  • delete: application/mov/match/{match_test.go => engine_test.go}
  • modified: application/mov/mov_core.go (diff)

差异

--- a/application/mov/match/match.go
+++ b/application/mov/match/engine.go
@@ -216,29 +216,6 @@ func setMatchTxArguments(txInput *types.TxInput, isPartialTrade bool, position i
216216 txInput.SetArguments(arguments)
217217 }
218218
219-func validateTradePairs(tradePairs []*common.TradePair) error {
220- if len(tradePairs) < 2 {
221- return errors.New("size of trade pairs at least 2")
222- }
223-
224- assetMap := make(map[string]bool)
225- for _, tradePair := range tradePairs {
226- assetMap[tradePair.FromAssetID.String()] = true
227- if *tradePair.FromAssetID == *tradePair.ToAssetID {
228- return errors.New("from asset id can't equal to asset id")
229- }
230- }
231-
232- for _, tradePair := range tradePairs {
233- key := tradePair.ToAssetID.String()
234- if _, ok := assetMap[key]; !ok {
235- return errors.New("invalid trade pairs")
236- }
237- delete(assetMap, key)
238- }
239- return nil
240-}
241-
242219 func sortOrders(orders []*common.Order) []*common.Order {
243220 if len(orders) == 0 {
244221 return nil
@@ -262,3 +239,26 @@ func sortOrders(orders []*common.Order) []*common.Order {
262239 }
263240 return sortedOrders
264241 }
242+
243+func validateTradePairs(tradePairs []*common.TradePair) error {
244+ if len(tradePairs) < 2 {
245+ return errors.New("size of trade pairs at least 2")
246+ }
247+
248+ assetMap := make(map[string]bool)
249+ for _, tradePair := range tradePairs {
250+ assetMap[tradePair.FromAssetID.String()] = true
251+ if *tradePair.FromAssetID == *tradePair.ToAssetID {
252+ return errors.New("from asset id can't equal to asset id")
253+ }
254+ }
255+
256+ for _, tradePair := range tradePairs {
257+ key := tradePair.ToAssetID.String()
258+ if _, ok := assetMap[key]; !ok {
259+ return errors.New("invalid trade pairs")
260+ }
261+ delete(assetMap, key)
262+ }
263+ return nil
264+}
--- a/application/mov/match/match_test.go
+++ b/application/mov/match/engine_test.go
@@ -89,7 +89,7 @@ func TestGenerateMatchedTxs(t *testing.T) {
8989
9090 for i, c := range cases {
9191 movStore := mock.NewMovStore([]*common.TradePair{btc2eth, eth2btc}, c.initStoreOrders)
92- matchEngine := NewEngine(NewOrderBook(movStore, nil, nil), NewDefaultFeeStrategy(0.05), mock.RewardProgram)
92+ matchEngine := NewEngine(NewOrderBook(movStore, nil, nil), NewDefaultFeeStrategy(), mock.RewardProgram)
9393 var gotMatchedTxs []*types.Tx
9494 for matchEngine.HasMatchedTx(c.tradePairs...) {
9595 matchedTx, err := matchEngine.NextMatchedTx(c.tradePairs...)
--- a/application/mov/match/match_fee.go
+++ b/application/mov/match/fee_strategy.go
@@ -35,13 +35,11 @@ type FeeStrategy interface {
3535 }
3636
3737 // DefaultFeeStrategy represent the default fee charge strategy
38-type DefaultFeeStrategy struct {
39- maxFeeRate float64
40-}
38+type DefaultFeeStrategy struct {}
4139
4240 // NewDefaultFeeStrategy return a new instance of DefaultFeeStrategy
43-func NewDefaultFeeStrategy(maxFeeRate float64) *DefaultFeeStrategy {
44- return &DefaultFeeStrategy{maxFeeRate: maxFeeRate}
41+func NewDefaultFeeStrategy() *DefaultFeeStrategy {
42+ return &DefaultFeeStrategy{}
4543 }
4644
4745 // 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
5755
5856 for i, receiveAmount := range receiveAmounts {
5957 amount := receiveAmount.Amount
60- minFeeAmount := calcMinFeeAmount(amount)
58+ minFeeAmount := d.calcMinFeeAmount(amount)
6159 receives[i] = &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: amount - minFeeAmount}
6260 feeMap[*receiveAmount.AssetId] += minFeeAmount
6361
64- maxFeeAmount := calcMaxFeeAmount(amount, d.maxFeeRate)
62+ maxFeeAmount := d.calcMaxFeeAmount(amount)
6563 feeAmount, reminder := feeMap[*receiveAmount.AssetId], uint64(0)
6664 if feeAmount > maxFeeAmount {
6765 reminder = feeAmount - maxFeeAmount
6866 feeAmount = maxFeeAmount
6967 }
7068
71- if feeAmount > 0 {
72- fees = append(fees, &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: feeAmount})
73- }
69+ fees = append(fees, &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: feeAmount})
7470
7571 // There is the remaining amount after paying the handling fee, assign it evenly to participants in the transaction
7672 averageAmount := reminder / uint64(len(receiveAmounts))
@@ -94,8 +90,8 @@ func (d *DefaultFeeStrategy) Allocate(receiveAmounts, priceDiffs []*bc.AssetAmou
9490 func (d *DefaultFeeStrategy) Validate(receiveAmounts []*bc.AssetAmount, feeAmounts map[bc.AssetID]uint64) error {
9591 for _, receiveAmount := range receiveAmounts {
9692 feeAmount := feeAmounts[*receiveAmount.AssetId]
97- maxFeeAmount := calcMaxFeeAmount(receiveAmount.Amount, d.maxFeeRate)
98- minFeeAmount := calcMinFeeAmount(receiveAmount.Amount)
93+ maxFeeAmount := d.calcMaxFeeAmount(receiveAmount.Amount)
94+ minFeeAmount := d.calcMinFeeAmount(receiveAmount.Amount)
9995 if feeAmount < minFeeAmount || feeAmount > maxFeeAmount {
10096 return ErrAmountOfFeeOutOfRange
10197 }
@@ -103,10 +99,10 @@ func (d *DefaultFeeStrategy) Validate(receiveAmounts []*bc.AssetAmount, feeAmoun
10399 return nil
104100 }
105101
106-func calcMinFeeAmount(amount uint64) uint64 {
102+func (d *DefaultFeeStrategy) calcMinFeeAmount(amount uint64) uint64 {
107103 return uint64(math.Ceil(float64(amount) / 1000))
108104 }
109105
110-func calcMaxFeeAmount(amount uint64, maxFeeRate float64) uint64 {
111- return uint64(math.Ceil(float64(amount) * maxFeeRate))
106+func (d *DefaultFeeStrategy) calcMaxFeeAmount(amount uint64) uint64 {
107+ return uint64(math.Ceil(float64(amount) * 0.05))
112108 }
--- a/application/mov/mov_core.go
+++ b/application/mov/mov_core.go
@@ -15,8 +15,6 @@ import (
1515 "github.com/bytom/vapor/protocol/bc/types"
1616 )
1717
18-const maxFeeRate = 0.05
19-
2018 var (
2119 errInvalidTradePairs = errors.New("The trade pairs in the tx input is invalid")
2220 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
9189 return nil, errNotConfiguredRewardProgram
9290 }
9391
94- matchEngine := match.NewEngine(orderBook, match.NewDefaultFeeStrategy(maxFeeRate), rewardProgram)
92+ matchEngine := match.NewEngine(orderBook, match.NewDefaultFeeStrategy(), rewardProgram)
9593 tradePairIterator := database.NewTradePairIterator(m.movStore)
9694 matchCollector := newMatchTxCollector(matchEngine, tradePairIterator, gasLeft, isTimeout)
9795 return matchCollector.result()
@@ -301,13 +299,13 @@ func validateMatchedTxFee(tx *types.Tx, blockHeight uint64) error {
301299 return err
302300 }
303301
302+ receivedAmount, _ := match.CalcReceivedAmount(orders)
304303 feeAmounts := make(map[bc.AssetID]uint64)
305304 for assetID, fee := range matchedTxFees {
306305 feeAmounts[assetID] = fee.amount
307306 }
308307
309- receivedAmount, _ := match.CalcReceivedAmount(orders)
310- feeStrategy := match.NewDefaultFeeStrategy(maxFeeRate)
308+ feeStrategy := match.NewDefaultFeeStrategy()
311309 return feeStrategy.Validate(receivedAmount, feeAmounts)
312310 }
313311
Show on old repository browser