OSDN Git Service

add calcMatchedTxFee test mov_core_test
authorshenao78 <shenao.78@163.com>
Mon, 4 Nov 2019 09:07:33 +0000 (17:07 +0800)
committershenao78 <shenao.78@163.com>
Mon, 4 Nov 2019 09:07:33 +0000 (17:07 +0800)
application/mov/match/match_test.go
application/mov/mock/mock.go

index 4e16cb2..7b248f5 100644 (file)
@@ -1,6 +1,8 @@
 package match
 
 import (
+       "github.com/vapor/protocol/bc"
+       "github.com/vapor/testutil"
        "testing"
 
        "github.com/vapor/application/mov/common"
@@ -65,7 +67,7 @@ func TestGenerateMatchedTxs(t *testing.T) {
 
        for i, c := range cases {
                movStore := mock.NewMovStore([]*common.TradePair{btc2eth, eth2btc}, c.initStoreOrders)
-               matchEngine := NewEngine(NewOrderTable(movStore, nil, nil), 0.05, []byte{0x51})
+               matchEngine := NewEngine(NewOrderTable(movStore, nil, nil), 0.05, mock.NodeProgram)
                var gotMatchedTxs []*types.Tx
                for matchEngine.HasMatchedTx(c.tradePair, c.tradePair.Reverse()) {
                        matchedTx, err := matchEngine.NextMatchedTx(c.tradePair, c.tradePair.Reverse())
@@ -96,3 +98,42 @@ func TestGenerateMatchedTxs(t *testing.T) {
                }
        }
 }
+
+func TestCalcMatchedTxFee(t *testing.T) {
+       cases := []struct {
+               desc             string
+               tx               *types.TxData
+               maxFeeRate       float64
+               wantMatchedTxFee map[bc.AssetID]*MatchedTxFee
+       }{
+               {
+                       desc: "fee less than max fee",
+                       maxFeeRate: 0.05,
+                       wantMatchedTxFee: map[bc.AssetID]*MatchedTxFee{mock.ETH: {FeeAmount: 10, MaxFeeAmount: 26}},
+                       tx: &mock.MatchedTxs[1].TxData,
+               },
+               {
+                       desc: "fee refund in tx",
+                       maxFeeRate: 0.05,
+                       wantMatchedTxFee: map[bc.AssetID]*MatchedTxFee{mock.ETH: {FeeAmount: 27, MaxFeeAmount: 27}},
+                       tx: &mock.MatchedTxs[2].TxData,
+               },
+               {
+                       desc: "fee is zero",
+                       maxFeeRate: 0.05,
+                       wantMatchedTxFee: map[bc.AssetID]*MatchedTxFee{},
+                       tx: &mock.MatchedTxs[0].TxData,
+               },
+       }
+
+       for i, c := range cases {
+               gotMatchedTxFee, err := CalcMatchedTxFee(c.tx, c.maxFeeRate)
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               if !testutil.DeepEqual(gotMatchedTxFee, c.wantMatchedTxFee) {
+                       t.Errorf("#%d(%s):fail to caculate matched tx fee, got (%v), want (%v)", i, c.desc, gotMatchedTxFee, c.wantMatchedTxFee)
+               }
+       }
+}
index a23c004..1a0c171 100644 (file)
@@ -10,8 +10,9 @@ import (
 )
 
 var (
-       BTC = bc.NewAssetID([32]byte{1})
-       ETH = bc.NewAssetID([32]byte{2})
+       BTC         = bc.NewAssetID([32]byte{1})
+       ETH         = bc.NewAssetID([32]byte{2})
+       NodeProgram = []byte{0x58}
 
        Btc2EthOrders = []*common.Order{
                {
@@ -96,7 +97,7 @@ var (
                },
        }
 
-       Btc2EthMakerTxs = []*types.Tx {
+       Btc2EthMakerTxs = []*types.Tx{
                // Btc2EthOrders[0]
                types.NewTx(types.TxData{
                        Inputs:  []*types.TxInput{types.NewSpendInput(nil, *Btc2EthOrders[0].Utxo.SourceID, *Btc2EthOrders[0].FromAssetID, Btc2EthOrders[0].Utxo.Amount, Btc2EthOrders[0].Utxo.SourcePos, []byte{0x51})},
@@ -119,7 +120,7 @@ var (
                }),
        }
 
-       Eth2BtcMakerTxs = []*types.Tx {
+       Eth2BtcMakerTxs = []*types.Tx{
                // Eth2Btc[0]
                types.NewTx(types.TxData{
                        Inputs:  []*types.TxInput{types.NewSpendInput(nil, *Eth2BtcOrders[0].Utxo.SourceID, *Eth2BtcOrders[0].FromAssetID, Eth2BtcOrders[0].Utxo.Amount, Eth2BtcOrders[0].Utxo.SourcePos, []byte{0x51})},
@@ -136,7 +137,7 @@ var (
                        Outputs: []*types.TxOutput{types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, Eth2BtcOrders[2].Utxo.Amount, Eth2BtcOrders[2].Utxo.ControlProgram)},
                }),
        }
-       
+
        MatchedTxs = []*types.Tx{
                // partial matched transaction from Btc2EthOrders[0], Eth2BtcOrders[1]
                types.NewTx(types.TxData{
@@ -151,7 +152,7 @@ var (
                                types.NewIntraChainOutput(*Eth2BtcOrders[1].ToAssetID, 8, testutil.MustDecodeHexString("54")),
                        },
                }),
-               
+
                // full matched transaction from Btc2EthOrders[0], Eth2BtcOrders[0]
                types.NewTx(types.TxData{
                        Inputs: []*types.TxInput{
@@ -161,7 +162,7 @@ var (
                        Outputs: []*types.TxOutput{
                                types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 500, testutil.MustDecodeHexString("51")),
                                types.NewIntraChainOutput(*Eth2BtcOrders[0].ToAssetID, 10, testutil.MustDecodeHexString("53")),
-                               types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 10, []byte{0x51}),
+                               types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 10, NodeProgram),
                        },
                }),
 
@@ -177,7 +178,7 @@ var (
                                // re-order
                                types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 270, Eth2BtcOrders[2].Utxo.ControlProgram),
                                // fee
-                               types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 27, []byte{0x51}),
+                               types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 27, NodeProgram),
                                // refund
                                types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 6, testutil.MustDecodeHexString("51")),
                                types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 7, testutil.MustDecodeHexString("55")),