• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版64b4b3c98067ae87822c00e3a8e1286945eacf79 (tree)
时间2020-03-25 18:46:14
作者shenao78 <shenao.78@163....>
Commitershenao78

Log Message

fix match engine

更改概述

差异

--- a/application/mov/match/engine.go
+++ b/application/mov/match/engine.go
@@ -62,7 +62,7 @@ func (e *Engine) NextMatchedTx(tradePairs ...*common.TradePair) (*types.Tx, erro
6262 return tx, nil
6363 }
6464
65-func (e *Engine) addMatchTxFeeOutput(txData *types.TxData, refunds []RefundAssets, fees []*bc.AssetAmount) error {
65+func (e *Engine) addMatchTxFeeOutput(txData *types.TxData, refunds RefundAssets, fees []*bc.AssetAmount) error {
6666 for _, feeAmount := range fees {
6767 txData.Outputs = append(txData.Outputs, types.NewIntraChainOutput(*feeAmount.AssetId, feeAmount.Amount, e.rewardProgram))
6868 }
@@ -106,7 +106,7 @@ func (e *Engine) buildMatchTx(orders []*common.Order) (*types.Tx, error) {
106106
107107 receivedAmounts, priceDiffs := CalcReceivedAmount(orders)
108108 allocatedAssets := e.feeStrategy.Allocate(receivedAmounts, priceDiffs)
109- if err := addMatchTxOutput(txData, orders, receivedAmounts, allocatedAssets.Receives); err != nil {
109+ if err := addMatchTxOutput(txData, orders, receivedAmounts, allocatedAssets); err != nil {
110110 return nil, err
111111 }
112112
@@ -123,22 +123,25 @@ func (e *Engine) buildMatchTx(orders []*common.Order) (*types.Tx, error) {
123123 return types.NewTx(*txData), nil
124124 }
125125
126-func addMatchTxOutput(txData *types.TxData, orders []*common.Order, receivedAmounts, deductFeeReceives []*bc.AssetAmount) error {
126+func addMatchTxOutput(txData *types.TxData, orders []*common.Order, receivedAmounts []*bc.AssetAmount, allocatedAssets *AllocatedAssets) error {
127127 for i, order := range orders {
128128 contractArgs, err := segwit.DecodeP2WMCProgram(order.Utxo.ControlProgram)
129129 if err != nil {
130130 return err
131131 }
132132
133- requestAmount := CalcRequestAmount(order.Utxo.Amount, contractArgs.RatioNumerator, contractArgs.RatioDenominator)
134133 receivedAmount := receivedAmounts[i].Amount
135134 shouldPayAmount := calcShouldPayAmount(receivedAmount, contractArgs.RatioNumerator, contractArgs.RatioDenominator)
136- isPartialTrade := requestAmount > receivedAmount
137135
138- setMatchTxArguments(txData.Inputs[i], isPartialTrade, len(txData.Outputs), receivedAmounts[i].Amount)
139- txData.Outputs = append(txData.Outputs, types.NewIntraChainOutput(*order.ToAssetID, deductFeeReceives[i].Amount, contractArgs.SellerProgram))
136+ exchangeAmount := order.Utxo.Amount - shouldPayAmount
137+ isPartialTrade := CalcRequestAmount(exchangeAmount, contractArgs.RatioNumerator, contractArgs.RatioDenominator) >= 1
138+
139+ setMatchTxArguments(txData.Inputs[i], isPartialTrade, len(txData.Outputs), receivedAmount)
140+ txData.Outputs = append(txData.Outputs, types.NewIntraChainOutput(*order.ToAssetID, allocatedAssets.Receives[i].Amount, contractArgs.SellerProgram))
140141 if isPartialTrade {
141- txData.Outputs = append(txData.Outputs, types.NewIntraChainOutput(*order.FromAssetID, order.Utxo.Amount-shouldPayAmount, order.Utxo.ControlProgram))
142+ txData.Outputs = append(txData.Outputs, types.NewIntraChainOutput(*order.FromAssetID, exchangeAmount, order.Utxo.ControlProgram))
143+ } else if exchangeAmount > 0 {
144+ allocatedAssets.Refunds.Add(i, *order.FromAssetID, exchangeAmount)
142145 }
143146 }
144147 return nil
--- a/application/mov/match/fee_strategy.go
+++ b/application/mov/match/fee_strategy.go
@@ -15,12 +15,31 @@ var (
1515 // AllocatedAssets represent reallocated assets after calculating fees
1616 type AllocatedAssets struct {
1717 Receives []*bc.AssetAmount
18- Refunds []RefundAssets
18+ Refunds RefundAssets
1919 Fees []*bc.AssetAmount
2020 }
2121
2222 // RefundAssets represent alias for assetAmount array, because each transaction participant can be refunded multiple assets
23-type RefundAssets []*bc.AssetAmount
23+type RefundAssets [][]*bc.AssetAmount
24+
25+// Add used to add a refund to specify order
26+func (r RefundAssets) Add(index int, asset bc.AssetID, amount uint64) {
27+ if index >= len(r) {
28+ return
29+ }
30+
31+ var found bool
32+ for _, assetAmount := range r[index] {
33+ if *assetAmount.AssetId == asset {
34+ assetAmount.Amount += amount
35+ found = true
36+ break
37+ }
38+ }
39+ if !found {
40+ r[index] = append(r[index], &bc.AssetAmount{AssetId: &asset, Amount: amount})
41+ }
42+}
2443
2544 // FeeStrategy used to indicate how to charge a matching fee
2645 type FeeStrategy interface {
@@ -50,7 +69,7 @@ func (d *DefaultFeeStrategy) Allocate(receiveAmounts, priceDiffs []*bc.AssetAmou
5069 }
5170
5271 var fees []*bc.AssetAmount
53- refunds := make([]RefundAssets, len(receiveAmounts))
72+ refunds := make([][]*bc.AssetAmount, len(receiveAmounts))
5473 receives := make([]*bc.AssetAmount, len(receiveAmounts))
5574
5675 for i, receiveAmount := range receiveAmounts {
--- a/application/mov/mov_core.go
+++ b/application/mov/mov_core.go
@@ -162,11 +162,11 @@ func (m *Core) ValidateTx(tx *types.Tx, verifyResult *bc.TxVerifyResult, blockHe
162162 }
163163
164164 if common.IsMatchedTx(tx) {
165- if err := validateMatchedTx(tx, verifyResult, blockHeight); err != nil {
165+ if err := validateMatchedTx(tx, blockHeight); err != nil {
166166 return err
167167 }
168168 } else if common.IsCancelOrderTx(tx) {
169- if err := validateCancelOrderTx(tx, verifyResult); err != nil {
169+ if err := validateCancelOrderTx(tx); err != nil {
170170 return err
171171 }
172172 }
@@ -220,7 +220,7 @@ func calcFeeAmount(matchedTx *types.Tx) (map[bc.AssetID]*matchedTxFee, error) {
220220 return assetFeeMap, nil
221221 }
222222
223-func validateCancelOrderTx(tx *types.Tx, verifyResult *bc.TxVerifyResult) error {
223+func validateCancelOrderTx(tx *types.Tx) error {
224224 for _, input := range tx.Inputs {
225225 if !segwit.IsP2WMCScript(input.ControlProgram()) {
226226 return errInputProgramMustP2WMCScript
@@ -253,7 +253,7 @@ func validateMagneticContractArgs(fromAssetAmount bc.AssetAmount, program []byte
253253 return nil
254254 }
255255
256-func validateMatchedTx(tx *types.Tx, verifyResult *bc.TxVerifyResult, blockHeight uint64) error {
256+func validateMatchedTx(tx *types.Tx, blockHeight uint64) error {
257257 fromAssetIDMap := make(map[string]bool)
258258 toAssetIDMap := make(map[string]bool)
259259 for i, input := range tx.Inputs {
Show on old repository browser