Skip to content

Commit b39600d

Browse files
Merge pull request #418 from YusukeShimizu/YusukeShimizu/bitcoind-feefloor
multi: adapt fee floor to bitcoind version
2 parents 75201c2 + f130b5f commit b39600d

14 files changed

Lines changed: 418 additions & 137 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
{
44
"name": "peerswap",
55
"image": "ghcr.io/xtruder/nix-devcontainer:v1",
6+
"runArgs": [
7+
"--platform=linux/amd64"
8+
],
69
"onCreateCommand": "nix-env -iA cachix -f https://cachix.org/api/v1/install && cachix use peerswap && nix develop --command echo 'Nix environment ready'",
710
"postCreateCommand": "sudo chmod a+w /usr/local/bin/ && nix develop --command bash -c 'GOBIN=/usr/local/bin go install github.com/bufbuild/buf/cmd/buf@v1.50.0'",
811
"customizations": {
@@ -12,7 +15,8 @@
1215
"jnoortheen.nix-ide",
1316
"mkhl.direnv",
1417
"golang.go",
15-
"bufbuild.vscode-buf"
18+
"bufbuild.vscode-buf",
19+
"openai.chatgpt"
1620
]
1721
}
1822
},

cmd/peerswap-plugin/main.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ func run(ctx context.Context, lightningPlugin *clightning.ClightningClient) erro
244244
return err
245245
}
246246

247+
bitcoinFeeFloor := onchain.LegacyFeeFloorSatPerKw
247248
var bitcoinTxWatcher *txwatcher.BlockchainRpcTxWatcher
248249
var bitcoinOnChainService *onchain.BitcoinOnChain
249250
var bitcoinEnabled bool
@@ -253,6 +254,14 @@ func run(ctx context.Context, lightningPlugin *clightning.ClightningClient) erro
253254
bitcoinEnabled = true
254255
bitcoinTxWatcher = txwatcher.NewBlockchainRpcTxWatcher(ctx, txwatcher.NewBitcoinRpc(bitcoinCli), onchain.BitcoinMinConfs, onchain.BitcoinCsv)
255256

257+
floor, detectedVersion, floorErr := determineBitcoinFeeFloor(bitcoinCli)
258+
if floorErr != nil {
259+
log.Infof("Unable to detect Bitcoin Core version, defaulting fee floor to %d sat/kw: %v", floor, floorErr)
260+
} else {
261+
log.Infof("Detected Bitcoin Core version %s, using fee floor %d sat/kw", detectedVersion, floor)
262+
}
263+
bitcoinFeeFloor = floor
264+
256265
// We set the default Estimator to the static regtest estimator.
257266
var bitcoinEstimator onchain.Estimator
258267
bitcoinEstimator, _ = onchain.NewRegtestFeeEstimator()
@@ -271,6 +280,7 @@ func run(ctx context.Context, lightningPlugin *clightning.ClightningClient) erro
271280
bitcoinCli,
272281
"ECONOMICAL",
273282
fallbackFeeRateSatPerKw,
283+
bitcoinFeeFloor,
274284
)
275285
if err != nil {
276286
return err
@@ -281,14 +291,12 @@ func run(ctx context.Context, lightningPlugin *clightning.ClightningClient) erro
281291
return err
282292
}
283293

284-
// Create the bitcoin onchain service with a fallback fee rate of
285-
// 253 sat/kw. (This should be useless in this case).
286-
// TODO: This fee rate does not matter right now but we might want to
287-
// add a config flag to set this higher than the assumed floor fee rate
288-
// of 275 sat/kw (1.1 sat/vb).
294+
// Create the bitcoin onchain service while keeping the same fee floor for
295+
// estimator and fallback paths.
289296
bitcoinOnChainService = onchain.NewBitcoinOnChain(
290297
bitcoinEstimator,
291-
btcutil.Amount(253),
298+
bitcoinFeeFloor,
299+
bitcoinFeeFloor,
292300
chain,
293301
)
294302
} else {
@@ -508,6 +516,30 @@ func getBitcoinClient(li *glightning.Lightning, pluginConfig *clightning.Config)
508516
return bitcoin, nil
509517
}
510518

519+
type getNetworkInfoRequest struct{}
520+
521+
func (r *getNetworkInfoRequest) Name() string {
522+
return "getnetworkinfo"
523+
}
524+
525+
type bitcoindNetworkInfo struct {
526+
Subversion string `json:"subversion"`
527+
}
528+
529+
func determineBitcoinFeeFloor(bitcoinCli *gbitcoin.Bitcoin) (btcutil.Amount, string, error) {
530+
var networkInfo bitcoindNetworkInfo
531+
if err := bitcoinCli.Request(&getNetworkInfoRequest{}, &networkInfo); err != nil {
532+
return onchain.LegacyFeeFloorSatPerKw, "", err
533+
}
534+
535+
floor, normalized := onchain.DetermineFeeFloor(networkInfo.Subversion)
536+
if normalized == "" {
537+
return floor, "", fmt.Errorf("unable to parse bitcoin core subversion %q", networkInfo.Subversion)
538+
}
539+
540+
return floor, normalized, nil
541+
}
542+
511543
func checkClnVersion(network string, fullVersionString string) (bool, error) {
512544
// skip version check if running signet as it needs a custom build
513545
// ? Can someone explain why we need this here?

cmd/peerswaplnd/peerswapd/main.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/elementsproject/peerswap/version"
2727
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
2828

29-
"github.com/btcsuite/btcd/btcutil"
3029
"github.com/btcsuite/btcd/chaincfg"
3130
"github.com/elementsproject/glightning/gelements"
3231
"github.com/elementsproject/peerswap/cmd/peerswaplnd"
@@ -164,10 +163,12 @@ func run() error {
164163
return err
165164
}
166165

166+
bitcoinFeeFloor := onchain.LegacyFeeFloorSatPerKw
167+
167168
// Start the LndEstimator.
168169
lndEstimator, err := onchain.NewLndEstimator(
169170
walletrpc.NewWalletKitClient(cc),
170-
btcutil.Amount(253),
171+
bitcoinFeeFloor,
171172
10*time.Minute,
172173
)
173174
if err != nil {
@@ -177,14 +178,11 @@ func run() error {
177178
return err
178179
}
179180

180-
// Create the bitcoin onchain service with a fallback fee rate of
181-
// 253 sat/kw.
182-
// TODO: This fee rate does not matter right now but we might want to
183-
// add a config flag to set this higher than the assumed floor fee rate
184-
// of 275 sat/kw (1.1 sat/vb).
181+
// Keep estimator and fallback paths aligned by reusing the same fee floor.
185182
bitcoinOnChainService = onchain.NewBitcoinOnChain(
186183
lndEstimator,
187-
btcutil.Amount(253),
184+
bitcoinFeeFloor,
185+
bitcoinFeeFloor,
188186
chain,
189187
)
190188
log.Infof("Bitcoin swaps enabled on network %s", chain.Name)

flake.lock

Lines changed: 44 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)