Skip to content

Commit f950443

Browse files
committed
client/{asset,core}: purge dex.Asset from client/asset methods
dex.Asset was a vehicle to pass various asset specific info, but very few fields were used or relevant. Instead of using this big struct to as an input argument to numerous client/asset methods, change the method signatures and structs to only include what is really required for the Wallets to do their work. This makes it much clearer what the dex.Asset configs were really needed for, and much easier to confidently provide the needed data to the wallet methods. The msgjson.Asset.SwapSize and SwapSizeBase fields are now deprecated. These data are fully specified by the asset version, and they are in no way server configuration as implied by the dex.Asset and msgjson.Asset struct docs. Neither the wallets nor Core should trust thest transaction sizes for any purpose. The wallets know their own init tx sizes, and as expected, their were no good reasons to give these externally sourced values to the wallets. The one and only problematic use the foreign SwapSize values was related to order accelaration, where a core method used the value to estimate how much funding is required for an existing partially-settled trade. This was resolved with a new (and trivial) asset.Accelerator method to compute the transaction fee from a remaining swap count and worst case fee rate. Supporting this involved adding the InitTxSize and InitTxSizeBase as fields of the wallet implementations, in the same manner as the server node implementations. This also adds the new client/asset.WalletInfo.SupportedVersions field to support wallet backends that support multiple asset versions. Core methods now check this slice against the version reported by the server in the config response. dex/msgjson: mark Asset.{SwapSize,SwapSizeBase} as deprecated/V0PURGE.
1 parent ee9c630 commit f950443

24 files changed

Lines changed: 772 additions & 556 deletions

client/asset/bch/bch.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ var (
7575

7676
// WalletInfo defines some general information about a Bitcoin Cash wallet.
7777
WalletInfo = &asset.WalletInfo{
78-
Name: "Bitcoin Cash",
79-
Version: version,
78+
Name: "Bitcoin Cash",
79+
Version: version,
80+
SupportedVersions: []uint32{version},
8081
// Same as bitcoin. That's dumb.
8182
UnitInfo: dexbch.UnitInfo,
8283
AvailableWallets: []*asset.WalletDefinition{
@@ -192,6 +193,8 @@ func NewWallet(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network)
192193
Ports: netPorts,
193194
DefaultFallbackFee: defaultFee,
194195
Segwit: false,
196+
InitTxSizeBase: dexbtc.InitTxSizeBase,
197+
InitTxSize: dexbtc.InitTxSize,
195198
LegacyBalance: cfg.Type != walletTypeSPV,
196199
LegacySendToAddr: true,
197200
// Bitcoin Cash uses the Cash Address encoding, which is Bech32, but not

client/asset/btc/btc.go

Lines changed: 87 additions & 39 deletions
Large diffs are not rendered by default.

client/asset/btc/btc_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,10 @@ func testAvailableFund(t *testing.T, segwit bool, walletType string) {
863863
}
864864

865865
ord := &asset.Order{
866+
Version: version,
866867
Value: 0,
867868
MaxSwapCount: 1,
868-
DEXConfig: tBTC,
869+
MaxFeeRate: tBTC.MaxFeeRate,
869870
FeeSuggestion: feeSuggestion,
870871
}
871872

@@ -1298,7 +1299,8 @@ func checkMaxOrder(t *testing.T, wallet asset.Wallet, lots, swapVal, maxFees, es
12981299
maxOrder, err := wallet.MaxOrder(&asset.MaxOrderForm{
12991300
LotSize: tLotSize,
13001301
FeeSuggestion: feeSuggestion,
1301-
AssetConfig: tBTC,
1302+
AssetVersion: version,
1303+
MaxFeeRate: tBTC.MaxFeeRate,
13021304
})
13031305
if err != nil {
13041306
t.Fatalf("MaxOrder error: %v", err)
@@ -1379,9 +1381,10 @@ func TestFundEdges(t *testing.T) {
13791381
unspents := []*ListUnspentResult{p2pkhUnspent}
13801382
node.listUnspent = unspents
13811383
ord := &asset.Order{
1384+
Version: version,
13821385
Value: swapVal,
13831386
MaxSwapCount: lots,
1384-
DEXConfig: tBTC,
1387+
MaxFeeRate: tBTC.MaxFeeRate,
13851388
FeeSuggestion: feeSuggestion,
13861389
}
13871390

@@ -1602,9 +1605,10 @@ func TestFundEdgesSegwit(t *testing.T) {
16021605
unspents := []*ListUnspentResult{p2wpkhUnspent}
16031606
node.listUnspent = unspents
16041607
ord := &asset.Order{
1608+
Version: version,
16051609
Value: swapVal,
16061610
MaxSwapCount: lots,
1607-
DEXConfig: tBTC,
1611+
MaxFeeRate: tBTC.MaxFeeRate,
16081612
FeeSuggestion: feeSuggestion,
16091613
}
16101614

@@ -2869,11 +2873,13 @@ func testPreSwap(t *testing.T, segwit bool, walletType string) {
28692873
}
28702874

28712875
form := &asset.PreSwapForm{
2876+
Version: version,
28722877
LotSize: tLotSize,
28732878
Lots: lots,
2874-
AssetConfig: tBTC,
2879+
MaxFeeRate: tBTC.MaxFeeRate,
28752880
Immediate: false,
28762881
FeeSuggestion: feeSuggestion,
2882+
// Redeem fields unneeded
28772883
}
28782884

28792885
setFunds(minReq)
@@ -2913,8 +2919,8 @@ func testPreRedeem(t *testing.T, segwit bool, walletType string) {
29132919
defer shutdown()
29142920

29152921
preRedeem, err := wallet.PreRedeem(&asset.PreRedeemForm{
2916-
Lots: 5,
2917-
AssetConfig: tBTC,
2922+
Version: version,
2923+
Lots: 5,
29182924
})
29192925
// Shouldn't actually be any path to error.
29202926
if err != nil {

client/asset/btc/livetest/livetest.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ type testRig struct {
9898
}
9999

100100
func (rig *testRig) close() {
101-
close := func(cm *dex.ConnectionMaster) {
101+
closeConn := func(cm *dex.ConnectionMaster) {
102102
closed := make(chan struct{})
103103
go func() {
104104
cm.Disconnect()
@@ -110,8 +110,8 @@ func (rig *testRig) close() {
110110
rig.t.Fatalf("failed to disconnect")
111111
}
112112
}
113-
close(rig.firstWallet.cxn)
114-
close(rig.secondWallet.cxn)
113+
closeConn(rig.firstWallet.cxn)
114+
closeConn(rig.secondWallet.cxn)
115115
}
116116

117117
func (rig *testRig) mineAlpha() error {
@@ -242,9 +242,11 @@ func Run(t *testing.T, cfg *Config) {
242242
checkAmt("second", rig.secondWallet)
243243

244244
ord := &asset.Order{
245+
Version: 0,
245246
Value: contractValue * 3,
246247
MaxSwapCount: lots * 3,
247-
DEXConfig: cfg.Asset,
248+
MaxFeeRate: cfg.Asset.MaxFeeRate,
249+
// Redeem vars omitted.
248250
}
249251
setOrderValue := func(v uint64) {
250252
ord.Value = v
@@ -525,6 +527,7 @@ func Run(t *testing.T, cfg *Config) {
525527
mine()
526528
}
527529

530+
const defaultFee = 100
528531
coinID, err := rig.secondWallet.Refund(swapReceipt.Coin().ID(), swapReceipt.Contract(), 100)
529532
if err != nil {
530533
t.Fatalf("refund error: %v", err)
@@ -533,9 +536,8 @@ func Run(t *testing.T, cfg *Config) {
533536
tLogger.Infof("Refunded with %s", c)
534537

535538
// Test Send.
536-
const defaultFee = 100
537539
tLogger.Info("Testing Send")
538-
coin, err := rig.secondWallet.Send(address, cfg.LotSize, 100)
540+
coin, err := rig.secondWallet.Send(address, cfg.LotSize, defaultFee)
539541
if err != nil {
540542
t.Fatalf("error sending: %v", err)
541543
}
@@ -547,7 +549,7 @@ func Run(t *testing.T, cfg *Config) {
547549
// Test Withdraw.
548550
withdrawer, _ := rig.secondWallet.Wallet.(asset.Withdrawer)
549551
tLogger.Info("Testing Withdraw")
550-
coin, err = withdrawer.Withdraw(address, cfg.LotSize, 100)
552+
coin, err = withdrawer.Withdraw(address, cfg.LotSize, defaultFee)
551553
if err != nil {
552554
t.Fatalf("error withdrawing: %v", err)
553555
}

0 commit comments

Comments
 (0)