• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版24ac2940427bfebec367d0072559cbcd32b45e32 (tree)
时间2019-11-04 18:07:33
作者shenao78 <shenao.78@163....>
Commitershenao78

Log Message

add calcMatchedTxFee test

更改概述

差异

--- a/application/mov/match/match_test.go
+++ b/application/mov/match/match_test.go
@@ -1,6 +1,8 @@
11 package match
22
33 import (
4+ "github.com/vapor/protocol/bc"
5+ "github.com/vapor/testutil"
46 "testing"
57
68 "github.com/vapor/application/mov/common"
@@ -65,7 +67,7 @@ func TestGenerateMatchedTxs(t *testing.T) {
6567
6668 for i, c := range cases {
6769 movStore := mock.NewMovStore([]*common.TradePair{btc2eth, eth2btc}, c.initStoreOrders)
68- matchEngine := NewEngine(NewOrderTable(movStore, nil, nil), 0.05, []byte{0x51})
70+ matchEngine := NewEngine(NewOrderTable(movStore, nil, nil), 0.05, mock.NodeProgram)
6971 var gotMatchedTxs []*types.Tx
7072 for matchEngine.HasMatchedTx(c.tradePair, c.tradePair.Reverse()) {
7173 matchedTx, err := matchEngine.NextMatchedTx(c.tradePair, c.tradePair.Reverse())
@@ -96,3 +98,42 @@ func TestGenerateMatchedTxs(t *testing.T) {
9698 }
9799 }
98100 }
101+
102+func TestCalcMatchedTxFee(t *testing.T) {
103+ cases := []struct {
104+ desc string
105+ tx *types.TxData
106+ maxFeeRate float64
107+ wantMatchedTxFee map[bc.AssetID]*MatchedTxFee
108+ }{
109+ {
110+ desc: "fee less than max fee",
111+ maxFeeRate: 0.05,
112+ wantMatchedTxFee: map[bc.AssetID]*MatchedTxFee{mock.ETH: {FeeAmount: 10, MaxFeeAmount: 26}},
113+ tx: &mock.MatchedTxs[1].TxData,
114+ },
115+ {
116+ desc: "fee refund in tx",
117+ maxFeeRate: 0.05,
118+ wantMatchedTxFee: map[bc.AssetID]*MatchedTxFee{mock.ETH: {FeeAmount: 27, MaxFeeAmount: 27}},
119+ tx: &mock.MatchedTxs[2].TxData,
120+ },
121+ {
122+ desc: "fee is zero",
123+ maxFeeRate: 0.05,
124+ wantMatchedTxFee: map[bc.AssetID]*MatchedTxFee{},
125+ tx: &mock.MatchedTxs[0].TxData,
126+ },
127+ }
128+
129+ for i, c := range cases {
130+ gotMatchedTxFee, err := CalcMatchedTxFee(c.tx, c.maxFeeRate)
131+ if err != nil {
132+ t.Fatal(err)
133+ }
134+
135+ if !testutil.DeepEqual(gotMatchedTxFee, c.wantMatchedTxFee) {
136+ t.Errorf("#%d(%s):fail to caculate matched tx fee, got (%v), want (%v)", i, c.desc, gotMatchedTxFee, c.wantMatchedTxFee)
137+ }
138+ }
139+}
--- a/application/mov/mock/mock.go
+++ b/application/mov/mock/mock.go
@@ -10,8 +10,9 @@ import (
1010 )
1111
1212 var (
13- BTC = bc.NewAssetID([32]byte{1})
14- ETH = bc.NewAssetID([32]byte{2})
13+ BTC = bc.NewAssetID([32]byte{1})
14+ ETH = bc.NewAssetID([32]byte{2})
15+ NodeProgram = []byte{0x58}
1516
1617 Btc2EthOrders = []*common.Order{
1718 {
@@ -96,7 +97,7 @@ var (
9697 },
9798 }
9899
99- Btc2EthMakerTxs = []*types.Tx {
100+ Btc2EthMakerTxs = []*types.Tx{
100101 // Btc2EthOrders[0]
101102 types.NewTx(types.TxData{
102103 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 (
119120 }),
120121 }
121122
122- Eth2BtcMakerTxs = []*types.Tx {
123+ Eth2BtcMakerTxs = []*types.Tx{
123124 // Eth2Btc[0]
124125 types.NewTx(types.TxData{
125126 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 (
136137 Outputs: []*types.TxOutput{types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, Eth2BtcOrders[2].Utxo.Amount, Eth2BtcOrders[2].Utxo.ControlProgram)},
137138 }),
138139 }
139-
140+
140141 MatchedTxs = []*types.Tx{
141142 // partial matched transaction from Btc2EthOrders[0], Eth2BtcOrders[1]
142143 types.NewTx(types.TxData{
@@ -151,7 +152,7 @@ var (
151152 types.NewIntraChainOutput(*Eth2BtcOrders[1].ToAssetID, 8, testutil.MustDecodeHexString("54")),
152153 },
153154 }),
154-
155+
155156 // full matched transaction from Btc2EthOrders[0], Eth2BtcOrders[0]
156157 types.NewTx(types.TxData{
157158 Inputs: []*types.TxInput{
@@ -161,7 +162,7 @@ var (
161162 Outputs: []*types.TxOutput{
162163 types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 500, testutil.MustDecodeHexString("51")),
163164 types.NewIntraChainOutput(*Eth2BtcOrders[0].ToAssetID, 10, testutil.MustDecodeHexString("53")),
164- types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 10, []byte{0x51}),
165+ types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 10, NodeProgram),
165166 },
166167 }),
167168
@@ -177,7 +178,7 @@ var (
177178 // re-order
178179 types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 270, Eth2BtcOrders[2].Utxo.ControlProgram),
179180 // fee
180- types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 27, []byte{0x51}),
181+ types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 27, NodeProgram),
181182 // refund
182183 types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 6, testutil.MustDecodeHexString("51")),
183184 types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 7, testutil.MustDecodeHexString("55")),
Show on old repository browser