diff --git a/chain_bridge.go b/chain_bridge.go index 9e2ca253c..244ee04c1 100644 --- a/chain_bridge.go +++ b/chain_bridge.go @@ -9,6 +9,7 @@ import ( "github.com/lightninglabs/lndclient" "github.com/lightninglabs/taproot-assets/tapgarden" "github.com/lightningnetwork/lnd/chainntnfs" + "github.com/lightningnetwork/lnd/lnrpc/verrpc" "github.com/lightningnetwork/lnd/lnwallet/chainfee" ) @@ -16,6 +17,8 @@ import ( // interface backed by an active remote lnd node. type LndRpcChainBridge struct { lnd *lndclient.LndServices + + getBlockHeaderSupported *bool } // NewLndRpcChainBridge creates a new chain bridge from an active lnd services @@ -79,6 +82,19 @@ func (l *LndRpcChainBridge) GetBlock(ctx context.Context, return block, nil } +// GetBlockHeader returns a block header given its hash. +func (l *LndRpcChainBridge) GetBlockHeader(ctx context.Context, + hash chainhash.Hash) (*wire.BlockHeader, error) { + + header, err := l.lnd.ChainKit.GetBlockHeader(ctx, hash) + if err != nil { + return nil, fmt.Errorf("unable to retrieve block header: %w", + err) + } + + return header, nil +} + // GetBlockHash returns the hash of the block in the best blockchain at the // given height. func (l *LndRpcChainBridge) GetBlockHash(ctx context.Context, @@ -93,6 +109,31 @@ 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. @@ -121,7 +162,14 @@ func (l *LndRpcChainBridge) VerifyBlock(ctx context.Context, "expectedHash: %s)", height, hash, expectedHash) } - // Ensure that the block header corresponds to a block on-chain. + // 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()) return err } diff --git a/go.mod b/go.mod index 997a1db28..d4a64de56 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/btcsuite/btcd/btcutil/psbt v1.1.8 github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f - github.com/btcsuite/btcwallet v0.16.10-0.20230804184612-07be54bc22cf + github.com/btcsuite/btcwallet v0.16.10-0.20231017144732-e3ff37491e9c github.com/caddyserver/certmagic v0.17.2 github.com/davecgh/go-spew v1.1.1 github.com/go-errors/errors v1.0.1 @@ -23,9 +23,9 @@ require ( github.com/lib/pq v1.10.7 github.com/lightninglabs/aperture v0.1.21-beta.0.20230705004936-87bb996a4030 github.com/lightninglabs/lightning-node-connect/hashmailrpc v1.0.2 - github.com/lightninglabs/lndclient v0.17.0-1 + github.com/lightninglabs/lndclient v0.17.0-4 github.com/lightninglabs/neutrino/cache v1.1.1 - github.com/lightningnetwork/lnd v0.17.0-beta + github.com/lightningnetwork/lnd v0.17.1-beta github.com/lightningnetwork/lnd/cert v1.2.2 github.com/lightningnetwork/lnd/clock v1.1.1 github.com/lightningnetwork/lnd/ticker v1.1.1 diff --git a/go.sum b/go.sum index a02060f06..1083c8010 100644 --- a/go.sum +++ b/go.sum @@ -99,8 +99,8 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2/go.mod h1:7SFka0XMvUgj3hfZtyd github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9VhRV3jjAVU7DJVjMaK+IsvSeZvFo= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= -github.com/btcsuite/btcwallet v0.16.10-0.20230804184612-07be54bc22cf h1:iZrvu/dynDPUcLJFkKiN9wnS4EdjwZSJS1H33Rx/a1Y= -github.com/btcsuite/btcwallet v0.16.10-0.20230804184612-07be54bc22cf/go.mod h1:qUPTONX2GVX7ERHvgh352/WySsfYlrkL4729qX9o9cA= +github.com/btcsuite/btcwallet v0.16.10-0.20231017144732-e3ff37491e9c h1:+7tbYEUj0TYYIvuvE9YP+x5dU3FT/8J6Qh8d5YvQwrE= +github.com/btcsuite/btcwallet v0.16.10-0.20231017144732-e3ff37491e9c/go.mod h1:WSKhOJWUmUOHKCKEzdt+jWAHFAE/t4RqVbCwL2pEdiU= github.com/btcsuite/btcwallet/wallet/txauthor v1.3.2 h1:etuLgGEojecsDOYTII8rYiGHjGyV5xTqsXi+ZQ715UU= github.com/btcsuite/btcwallet/wallet/txauthor v1.3.2/go.mod h1:Zpk/LOb2sKqwP2lmHjaZT9AdaKsHPSbNLm2Uql5IQ/0= github.com/btcsuite/btcwallet/wallet/txrules v1.2.0 h1:BtEN5Empw62/RVnZ0VcJaVtVlBijnLlJY+dwjAye2Bg= @@ -521,8 +521,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.17.0-1 h1:r/25L1P86Wjdj0UmqfMNF3Ph+SJOTeGKlOIjkX3iWiw= -github.com/lightninglabs/lndclient v0.17.0-1/go.mod h1:J2RD7ZcIVreQKy6NTOGJMM7AeGvsMLHO+Aoegkzpijs= +github.com/lightninglabs/lndclient v0.17.0-4 h1:2nU7kMYctGmx6NCpbWAwRt3mmlsSzb6HI41WODReYzA= +github.com/lightninglabs/lndclient v0.17.0-4/go.mod h1:oVQUAYEeuYUaoxCWjUBaa/uF0kiqEJJjEaiMhEXHH7Y= github.com/lightninglabs/neutrino v0.16.0 h1:YNTQG32fPR/Zg0vvJVI65OBH8l3U18LSXXtX91hx0q0= github.com/lightninglabs/neutrino v0.16.0/go.mod h1:x3OmY2wsA18+Kc3TSV2QpSUewOCiscw2mKpXgZv2kZk= github.com/lightninglabs/neutrino/cache v1.1.1 h1:TllWOSlkABhpgbWJfzsrdUaDH2fBy/54VSIB4vVqV8M= @@ -531,8 +531,8 @@ github.com/lightninglabs/protobuf-go-hex-display v1.30.0-hex-display h1:pRdza2wl github.com/lightninglabs/protobuf-go-hex-display v1.30.0-hex-display/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= github.com/lightningnetwork/lightning-onion v1.2.1-0.20230823005744-06182b1d7d2f h1:Pua7+5TcFEJXIIZ1I2YAUapmbcttmLj4TTi786bIi3s= github.com/lightningnetwork/lightning-onion v1.2.1-0.20230823005744-06182b1d7d2f/go.mod h1:c0kvRShutpj3l6B9WtTsNTBUtjSmjZXbJd9ZBRQOSKI= -github.com/lightningnetwork/lnd v0.17.0-beta h1:Vzl2W3ClIDdffuQD5IaZwyP7CKR/CbXxWqws72Cq51g= -github.com/lightningnetwork/lnd v0.17.0-beta/go.mod h1:8w27nArqZ1P7U6FP9U78GlaTLNm7u9GhV5Edv1C1yRU= +github.com/lightningnetwork/lnd v0.17.1-beta h1:ya5pXahwrEJ9Sj9TQu0rIsOr1dMK8myuFMslZlmVmhA= +github.com/lightningnetwork/lnd v0.17.1-beta/go.mod h1:wgnCM0tlwxUDZ9y7CeBkXtBJkaY45+R8A3XAgsFS0MA= github.com/lightningnetwork/lnd/cert v1.2.2 h1:71YK6hogeJtxSxw2teq3eGeuy4rHGKcFf0d0Uy4qBjI= github.com/lightningnetwork/lnd/cert v1.2.2/go.mod h1:jQmFn/Ez4zhDgq2hnYSw8r35bqGVxViXhX6Cd7HXM6U= github.com/lightningnetwork/lnd/clock v1.0.1/go.mod h1:KnQudQ6w0IAMZi1SgvecLZQZ43ra2vpDNj7H/aasemg=