Skip to content

Commit

Permalink
Merge pull request #1163 from lightninglabs/minrelayfee
Browse files Browse the repository at this point in the history
min_relay_fee check during channel funding
  • Loading branch information
gijswijs authored Nov 1, 2024
2 parents 588e58b + e111765 commit e6b78bd
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 84 deletions.
53 changes: 5 additions & 48 deletions chain_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/funding"
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
Expand All @@ -37,8 +36,6 @@ const (
type LndRpcChainBridge struct {
lnd *lndclient.LndServices

getBlockHeaderSupported *bool

blockTimestampCache *lru.Cache[uint32, cacheableTimestamp]

assetStore *tapdb.AssetStore
Expand Down Expand Up @@ -138,31 +135,6 @@ func (l *LndRpcChainBridge) GetBlockHash(ctx context.Context,
return blockHash, nil
}

// GetBlockHeaderSupported returns true if the chain backend supports the
// `GetBlockHeader` RPC call.
func (l *LndRpcChainBridge) GetBlockHeaderSupported(ctx context.Context) bool {
// Check if we've already asserted the compatibility of the chain
// backend.
if l.getBlockHeaderSupported != nil {
return *l.getBlockHeaderSupported
}

// The ChainKit.GetBlockHeader() RPC call was added in lnd v0.17.1.
getBlockHeaderMinimalVersion := &verrpc.Version{
AppMajor: 0,
AppMinor: 17,
AppPatch: 1,
}

getBlockHeaderUnsupported := lndclient.AssertVersionCompatible(
l.lnd.Version, getBlockHeaderMinimalVersion,
)
getBlockHeaderSupported := getBlockHeaderUnsupported == nil

l.getBlockHeaderSupported = &getBlockHeaderSupported
return *l.getBlockHeaderSupported
}

// VerifyBlock returns an error if a block (with given header and height) is not
// present on-chain. It also checks to ensure that block height corresponds to
// the given block header.
Expand Down Expand Up @@ -194,12 +166,7 @@ func (l *LndRpcChainBridge) VerifyBlock(ctx context.Context,
// Ensure that the block header corresponds to a block on-chain. Fetch
// only the corresponding block header and not the entire block if
// supported.
if l.GetBlockHeaderSupported(ctx) {
_, err = l.GetBlockHeader(ctx, header.BlockHash())
return err
}

_, err = l.GetBlock(ctx, header.BlockHash())
_, err = l.GetBlockHeader(ctx, header.BlockHash())
return err
}

Expand Down Expand Up @@ -233,20 +200,10 @@ func (l *LndRpcChainBridge) GetBlockTimestamp(ctx context.Context,
return 0
}

// Let's see if we can get the block header directly.
var header *wire.BlockHeader
if l.GetBlockHeaderSupported(ctx) {
header, err = l.GetBlockHeader(ctx, hash)
if err != nil {
return 0
}
} else {
block, err := l.lnd.ChainKit.GetBlock(ctx, hash)
if err != nil {
return 0
}

header = &block.Header
// Get block header.
header, err := l.GetBlockHeader(ctx, hash)
if err != nil {
return 0
}

ts := uint32(header.Timestamp.Unix())
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/basic-price-oracle/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ require (
github.com/kkdai/bstream v1.0.0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf // indirect
github.com/lightninglabs/lndclient v0.18.4-1 // indirect
github.com/lightninglabs/lndclient v0.18.4-3 // indirect
github.com/lightninglabs/neutrino v0.16.1-0.20240425105051-602843d34ffd // indirect
github.com/lightninglabs/neutrino/cache v1.1.2 // indirect
github.com/lightningnetwork/lightning-onion v1.2.1-0.20240712235311-98bd56499dfb // indirect
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/basic-price-oracle/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf/go.mod h1:vxmQ
github.com/lightninglabs/lightning-node-connect v0.2.5-alpha h1:ZRVChwczFXK0CEbxOCWwUA6TIZvrkE0APd1T3WjFAwg=
github.com/lightninglabs/lightning-node-connect/hashmailrpc v1.0.2 h1:Er1miPZD2XZwcfE4xoS5AILqP1mj7kqnhbBSxW9BDxY=
github.com/lightninglabs/lightning-node-connect/hashmailrpc v1.0.2/go.mod h1:antQGRDRJiuyQF6l+k6NECCSImgCpwaZapATth2Chv4=
github.com/lightninglabs/lndclient v0.18.4-1 h1:k2UnxHGNH243NRe5/dL2sKgrxc8WMHbFQRdnCtfilwc=
github.com/lightninglabs/lndclient v0.18.4-1/go.mod h1:/HLqmZGL9MtP8F1g+laq+L9VrsugBN5tsTct3C5wWCg=
github.com/lightninglabs/lndclient v0.18.4-3 h1:Xk3ZuCQE4ZlF70jaToryL2MvRcryiE0zTfUjJbmzUBY=
github.com/lightninglabs/lndclient v0.18.4-3/go.mod h1:/HLqmZGL9MtP8F1g+laq+L9VrsugBN5tsTct3C5wWCg=
github.com/lightninglabs/neutrino v0.16.1-0.20240425105051-602843d34ffd h1:D8aRocHpoCv43hL8egXEMYyPmyOiefFHZ66338KQB2s=
github.com/lightninglabs/neutrino v0.16.1-0.20240425105051-602843d34ffd/go.mod h1:x3OmY2wsA18+Kc3TSV2QpSUewOCiscw2mKpXgZv2kZk=
github.com/lightninglabs/neutrino/cache v1.1.2 h1:C9DY/DAPaPxbFC+xNNEI/z1SJY9GS3shmlu5hIQ798g=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/lib/pq v1.10.9
github.com/lightninglabs/aperture v0.3.2-beta.0.20241015115230-d59b5514c19a
github.com/lightninglabs/lightning-node-connect/hashmailrpc v1.0.2
github.com/lightninglabs/lndclient v0.18.4-1
github.com/lightninglabs/lndclient v0.18.4-3
github.com/lightninglabs/neutrino/cache v1.1.2
github.com/lightningnetwork/lnd v0.18.3-beta.rc3.0.20241025090009-615f3d633e61
github.com/lightningnetwork/lnd/cert v1.2.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ github.com/lightninglabs/lightning-node-connect v0.2.5-alpha h1:ZRVChwczFXK0CEbx
github.com/lightninglabs/lightning-node-connect v0.2.5-alpha/go.mod h1:A9Pof9fETkH+F67BnOmrBDThPKstqp73wlImWOZvTXQ=
github.com/lightninglabs/lightning-node-connect/hashmailrpc v1.0.2 h1:Er1miPZD2XZwcfE4xoS5AILqP1mj7kqnhbBSxW9BDxY=
github.com/lightninglabs/lightning-node-connect/hashmailrpc v1.0.2/go.mod h1:antQGRDRJiuyQF6l+k6NECCSImgCpwaZapATth2Chv4=
github.com/lightninglabs/lndclient v0.18.4-1 h1:k2UnxHGNH243NRe5/dL2sKgrxc8WMHbFQRdnCtfilwc=
github.com/lightninglabs/lndclient v0.18.4-1/go.mod h1:/HLqmZGL9MtP8F1g+laq+L9VrsugBN5tsTct3C5wWCg=
github.com/lightninglabs/lndclient v0.18.4-3 h1:Xk3ZuCQE4ZlF70jaToryL2MvRcryiE0zTfUjJbmzUBY=
github.com/lightninglabs/lndclient v0.18.4-3/go.mod h1:/HLqmZGL9MtP8F1g+laq+L9VrsugBN5tsTct3C5wWCg=
github.com/lightninglabs/neutrino v0.16.1-0.20240425105051-602843d34ffd h1:D8aRocHpoCv43hL8egXEMYyPmyOiefFHZ66338KQB2s=
github.com/lightninglabs/neutrino v0.16.1-0.20240425105051-602843d34ffd/go.mod h1:x3OmY2wsA18+Kc3TSV2QpSUewOCiscw2mKpXgZv2kZk=
github.com/lightninglabs/neutrino/cache v1.1.2 h1:C9DY/DAPaPxbFC+xNNEI/z1SJY9GS3shmlu5hIQ798g=
Expand Down
28 changes: 0 additions & 28 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import (
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
Expand All @@ -78,17 +77,6 @@ var (
// P2TRChangeType is the type of change address that should be used for
// funding PSBTs, as we'll always want to use P2TR change addresses.
P2TRChangeType = walletrpc.ChangeAddressType_CHANGE_ADDRESS_TYPE_P2TR

// fundPsbtCoinSelectVersion is the version of lnd that enabled better
// coin selection support in the FundPsbt RPC call.
fundPsbtCoinSelectVersion = &verrpc.Version{
AppMajor: 0,
AppMinor: 17,
AppPatch: 99,
BuildTags: []string{
"signrpc", "walletrpc", "chainrpc", "invoicesrpc",
},
}
)

const (
Expand Down Expand Up @@ -2304,22 +2292,6 @@ func (r *rpcServer) CommitVirtualPsbts(ctx context.Context,
req *wrpc.CommitVirtualPsbtsRequest) (*wrpc.CommitVirtualPsbtsResponse,
error) {

// For this call we require `lnd` to be at least v0.17.99-beta (which
// will become v0.18.0-beta eventually) as we need the new coin
// selection mode in the FundPsbt call.
fundPsbtCoinSelectNotSupportedErr := fmt.Errorf("connected lnd "+
"version %v does not support advanced coin selection in the "+
"FundPsbt RPC, need at least v%d.%d.%d for this call",
r.cfg.Lnd.Version.Version, fundPsbtCoinSelectVersion.AppMajor,
fundPsbtCoinSelectVersion.AppMinor,
fundPsbtCoinSelectVersion.AppPatch)
verErr := lndclient.AssertVersionCompatible(
r.cfg.Lnd.Version, fundPsbtCoinSelectVersion,
)
if verErr != nil {
return nil, fundPsbtCoinSelectNotSupportedErr
}

if len(req.VirtualPsbts) == 0 {
return nil, fmt.Errorf("no virtual PSBTs specified")
}
Expand Down
4 changes: 2 additions & 2 deletions tapcfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ var (
// required in lnd to run tapd.
minimalCompatibleVersion = &verrpc.Version{
AppMajor: 0,
AppMinor: 17,
AppPatch: 99,
AppMinor: 18,
AppPatch: 4,

// We don't actually require the invoicesrpc calls. But if we
// try to use lndclient on an lnd that doesn't have it enabled,
Expand Down
12 changes: 12 additions & 0 deletions tapchannel/aux_funding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,18 @@ func (f *FundingController) processFundingReq(fundingFlows fundingFlowIndex,
fundReq.PeerPub.SerializeCompressed())
}

// Before we proceed, we'll make sure the fee rate we're using is above
// the min relay fee.
minRelayFee, err := f.cfg.ChainWallet.MinRelayFee(fundReq.ctx)
if err != nil {
return fmt.Errorf("unable to establish min_relay_fee: %w",
err)
}
if fundReq.FeeRate.FeePerKWeight() < minRelayFee {
return fmt.Errorf("fee rate %v too low, min_relay_fee: %v",
fundReq.FeeRate.FeePerKWeight(), minRelayFee)
}

// To start, we'll make a new pending asset funding desc. This'll be
// our scratch pad during the asset funding process.
tempPID, err := newPendingChanID()
Expand Down
4 changes: 4 additions & 0 deletions tapgarden/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ type WalletAnchor interface {
// relevant to the wallet are sent over.
SubscribeTransactions(context.Context) (<-chan lndclient.Transaction,
<-chan error, error)

// MinRelayFee returns the current minimum relay fee based on
// our chain backend in sat/kw.
MinRelayFee(ctx context.Context) (chainfee.SatPerKWeight, error)
}

// KeyRing is a mirror of the keychain.KeyRing interface, with the addition of
Expand Down
8 changes: 8 additions & 0 deletions tapgarden/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ func (m *MockWalletAnchor) ListTransactions(ctx context.Context, _, _ int32,
return m.Transactions, nil
}

// MinRelayFee estimates the minimum fee rate required for a
// transaction.
func (m *MockWalletAnchor) MinRelayFee(
ctx context.Context) (chainfee.SatPerKWeight, error) {

return chainfee.SatPerKWeight(10), nil
}

type MockChainBridge struct {
FeeEstimateSignal chan struct{}
PublishReq chan *wire.MsgTx
Expand Down
8 changes: 8 additions & 0 deletions wallet_anchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ func (l *LndRpcWalletAnchor) ListChannels(
return l.lnd.Client.ListChannels(ctx, true, false)
}

// MinRelayFee estimates the minimum fee rate required for a
// transaction.
func (l *LndRpcWalletAnchor) MinRelayFee(
ctx context.Context) (chainfee.SatPerKWeight, error) {

return l.lnd.WalletKit.MinRelayFee(ctx)
}

// A compile time assertion to ensure LndRpcWalletAnchor meets the
// tapgarden.WalletAnchor interface.
var _ tapgarden.WalletAnchor = (*LndRpcWalletAnchor)(nil)
Expand Down

0 comments on commit e6b78bd

Please sign in to comment.