Skip to content

Commit 3e177ef

Browse files
authored
Merge pull request #206 from starius/bump-fee
bumpfee: add more flags as options
2 parents c5f2f9a + 41eb830 commit 3e177ef

File tree

2 files changed

+83
-13
lines changed

2 files changed

+83
-13
lines changed

lnd_services.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ func NewLndServices(cfg *LndServicesConfig) (*GrpcLndServices, error) {
372372
)
373373
walletKitClient := newWalletKitClient(
374374
conn, macaroons[WalletKitServiceMac], timeout, chainParams,
375+
version,
375376
)
376377
invoicesClient := newInvoicesClient(
377378
conn, macaroons[InvoiceServiceMac], timeout,

walletkit_client.go

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/lightningnetwork/lnd/keychain"
2121
"github.com/lightningnetwork/lnd/lnrpc"
2222
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
23+
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
2324
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
2425
"github.com/lightningnetwork/lnd/lnwallet"
2526
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -134,7 +135,8 @@ type WalletKitClient interface {
134135
// child-pays-for-parent (CPFP) scenario. If the given output has been
135136
// used in a previous BumpFee call, then a transaction replacing the
136137
// previous is broadcast, resulting in a replace-by-fee (RBF) scenario.
137-
BumpFee(context.Context, wire.OutPoint, chainfee.SatPerKWeight) error
138+
BumpFee(context.Context, wire.OutPoint, chainfee.SatPerKWeight,
139+
...BumpFeeOption) error
138140

139141
// ListAccounts retrieves all accounts belonging to the wallet by default.
140142
// Optional name and addressType can be provided to filter through all the
@@ -218,6 +220,7 @@ type walletKitClient struct {
218220
walletKitMac serializedMacaroon
219221
timeout time.Duration
220222
params *chaincfg.Params
223+
version *verrpc.Version
221224
}
222225

223226
// A compile time check to ensure that walletKitClient implements the
@@ -226,13 +229,15 @@ var _ WalletKitClient = (*walletKitClient)(nil)
226229

227230
func newWalletKitClient(conn grpc.ClientConnInterface,
228231
walletKitMac serializedMacaroon, timeout time.Duration,
229-
chainParams *chaincfg.Params) *walletKitClient {
232+
chainParams *chaincfg.Params,
233+
version *verrpc.Version) *walletKitClient {
230234

231235
return &walletKitClient{
232236
client: walletrpc.NewWalletKitClient(conn),
233237
walletKitMac: walletKitMac,
234238
timeout: timeout,
235239
params: chainParams,
240+
version: version,
236241
}
237242
}
238243

@@ -737,28 +742,92 @@ func (m *walletKitClient) ListSweepsVerbose(ctx context.Context,
737742
return result, nil
738743
}
739744

745+
// BumpFeeOption customizes a BumpFee call.
746+
type BumpFeeOption func(*walletrpc.BumpFeeRequest)
747+
748+
// WithImmediate is an option for enabling the immediate mode of BumpFee. The
749+
// sweeper will sweep this input without waiting for the next block.
750+
func WithImmediate() BumpFeeOption {
751+
return func(r *walletrpc.BumpFeeRequest) {
752+
r.Immediate = true
753+
}
754+
}
755+
756+
// WithTargetConf is an option for setting the target_conf of BumpFee. If set,
757+
// the underlying fee estimator will use the target_conf to estimate the
758+
// starting fee rate for the fee function. Pass feeRate=0 to BumpFee if you
759+
// add this option.
760+
func WithTargetConf(targetConf uint32) BumpFeeOption {
761+
return func(r *walletrpc.BumpFeeRequest) {
762+
r.TargetConf = targetConf
763+
}
764+
}
765+
766+
// WithBudget is an option for setting the budget of BumpFee. It is the max
767+
// amount in sats that can be used as the fees. Setting this value greater than
768+
// the input's value may result in CPFP - one or more wallet utxos will be used
769+
// to pay the fees specified by the budget. If not set, for new inputs, by
770+
// default 50% of the input's value will be treated as the budget for fee
771+
// bumping; for existing inputs, their current budgets will be retained.
772+
func WithBudget(budget btcutil.Amount) BumpFeeOption {
773+
return func(r *walletrpc.BumpFeeRequest) {
774+
r.Budget = uint64(budget)
775+
}
776+
}
777+
778+
// targetConfFixed is the minimum version in which bug #9470 is merged and new
779+
// meaning of TargetConf is enabled. In versions prior to this version the field
780+
// had a different meaning.
781+
var targetConfFixed = &verrpc.Version{
782+
AppMajor: 0,
783+
AppMinor: 18,
784+
AppPatch: 5,
785+
}
786+
740787
// BumpFee attempts to bump the fee of a transaction by spending one of its
741788
// outputs at the given fee rate. This essentially results in a
742789
// child-pays-for-parent (CPFP) scenario. If the given output has been used in a
743790
// previous BumpFee call, then a transaction replacing the previous is
744791
// broadcast, resulting in a replace-by-fee (RBF) scenario.
745792
func (m *walletKitClient) BumpFee(ctx context.Context, op wire.OutPoint,
746-
feeRate chainfee.SatPerKWeight) error {
793+
feeRate chainfee.SatPerKWeight, opts ...BumpFeeOption) error {
747794

748795
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout)
749796
defer cancel()
750797

751-
_, err := m.client.BumpFee(
752-
m.walletKitMac.WithMacaroonAuth(rpcCtx),
753-
&walletrpc.BumpFeeRequest{
754-
Outpoint: &lnrpc.OutPoint{
755-
TxidBytes: op.Hash[:],
756-
OutputIndex: op.Index,
757-
},
758-
SatPerByte: uint32(feeRate.FeePerKVByte() / 1000),
759-
Force: false,
798+
req := &walletrpc.BumpFeeRequest{
799+
Outpoint: &lnrpc.OutPoint{
800+
TxidBytes: op.Hash[:],
801+
OutputIndex: op.Index,
760802
},
761-
)
803+
SatPerVbyte: uint64(feeRate.FeePerVByte()),
804+
Immediate: false,
805+
}
806+
807+
for _, opt := range opts {
808+
opt(req)
809+
}
810+
811+
// Make sure that feeRate and WithTargetConf are not used together.
812+
if feeRate != 0 && req.TargetConf != 0 {
813+
return fmt.Errorf("can't use target_conf if feeRate != 0")
814+
}
815+
816+
// Make sure that the version of LND is at least targetConfFixed,
817+
// because before it the meaning of TargetConf was different. See
818+
// https://github.com/lightningnetwork/lnd/pull/9470
819+
// TODO(Boris): remove this check when minimalCompatibleVersion
820+
// is bumped to targetConfFixed.
821+
if req.TargetConf != 0 {
822+
err := AssertVersionCompatible(m.version, targetConfFixed)
823+
if err != nil {
824+
return fmt.Errorf("can't use target_conf before " +
825+
"version 0.18.5, see #9470")
826+
}
827+
}
828+
829+
_, err := m.client.BumpFee(m.walletKitMac.WithMacaroonAuth(rpcCtx), req)
830+
762831
return err
763832
}
764833

0 commit comments

Comments
 (0)