-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathchain_bridge.go
264 lines (213 loc) · 7.74 KB
/
chain_bridge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package taprootassets
import (
"context"
"fmt"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/taproot-assets/rfq"
"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
)
// LndRpcChainBridge is an implementation of the tapgarden.ChainBridge
// 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
// client.
func NewLndRpcChainBridge(lnd *lndclient.LndServices) *LndRpcChainBridge {
return &LndRpcChainBridge{
lnd: lnd,
}
}
// RegisterConfirmationsNtfn registers an intent to be notified once
// txid reaches numConfs confirmations.
func (l *LndRpcChainBridge) RegisterConfirmationsNtfn(ctx context.Context,
txid *chainhash.Hash, pkScript []byte, numConfs, heightHint uint32,
includeBlock bool,
reOrgChan chan struct{}) (*chainntnfs.ConfirmationEvent, chan error,
error) {
opts := []lndclient.NotifierOption{
lndclient.WithReOrgChan(reOrgChan),
}
if includeBlock {
opts = append(opts, lndclient.WithIncludeBlock())
}
ctx, cancel := context.WithCancel(ctx) // nolint:govet
confChan, errChan, err := l.lnd.ChainNotifier.RegisterConfirmationsNtfn(
ctx, txid, pkScript, int32(numConfs), int32(heightHint),
opts...,
)
if err != nil {
cancel()
return nil, nil, fmt.Errorf("unable to register for conf: %w",
err)
}
return &chainntnfs.ConfirmationEvent{
Confirmed: confChan,
Cancel: cancel,
}, errChan, nil
}
// RegisterBlockEpochNtfn registers an intent to be notified of each new block
// connected to the main chain.
func (l *LndRpcChainBridge) RegisterBlockEpochNtfn(
ctx context.Context) (chan int32, chan error, error) {
return l.lnd.ChainNotifier.RegisterBlockEpochNtfn(ctx)
}
// GetBlock returns a chain block given its hash.
func (l *LndRpcChainBridge) GetBlock(ctx context.Context,
hash chainhash.Hash) (*wire.MsgBlock, error) {
block, err := l.lnd.ChainKit.GetBlock(ctx, hash)
if err != nil {
return nil, fmt.Errorf("unable to retrieve block: %w", err)
}
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,
blockHeight int64) (chainhash.Hash, error) {
blockHash, err := l.lnd.ChainKit.GetBlockHash(ctx, blockHeight)
if err != nil {
return chainhash.Hash{}, fmt.Errorf("unable to retrieve "+
"block hash: %w", err)
}
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.
func (l *LndRpcChainBridge) VerifyBlock(ctx context.Context,
header wire.BlockHeader, height uint32) error {
// TODO(ffranr): Once we've released 0.3.0, every proof should have an
// assigned height. At that point, we should return an error for proofs
// with unset (zero) block heights.
if height == 0 {
_, err := l.GetBlock(ctx, header.BlockHash())
return err
}
// Ensure that the block hash matches the hash of the block
// found at the given height.
hash, err := l.GetBlockHash(ctx, int64(height))
if err != nil {
return err
}
expectedHash := header.BlockHash()
if hash != expectedHash {
return fmt.Errorf("block hash and block height "+
"mismatch; (height: %d, hashAtHeight: %s, "+
"expectedHash: %s)", height, hash, expectedHash)
}
// 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
}
// CurrentHeight return the current height of the main chain.
func (l *LndRpcChainBridge) CurrentHeight(ctx context.Context) (uint32, error) {
info, err := l.lnd.Client.GetInfo(ctx)
if err != nil {
return 0, fmt.Errorf("unable to grab block height: %w", err)
}
return info.BlockHeight, nil
}
// PublishTransaction attempts to publish a new transaction to the
// network.
func (l *LndRpcChainBridge) PublishTransaction(ctx context.Context,
tx *wire.MsgTx) error {
label := "tapd-asset-minting"
return l.lnd.WalletKit.PublishTransaction(ctx, tx, label)
}
// EstimateFee returns a fee estimate for the confirmation target.
func (l *LndRpcChainBridge) EstimateFee(ctx context.Context,
confTarget uint32) (chainfee.SatPerKWeight, error) {
return l.lnd.WalletKit.EstimateFeeRate(ctx, int32(confTarget))
}
// A compile time assertion to ensure LndRpcChainBridge meets the
// tapgarden.ChainBridge interface.
var _ tapgarden.ChainBridge = (*LndRpcChainBridge)(nil)
// LndMsgTransportClient is an LND RPC message transport client.
type LndMsgTransportClient struct {
lnd *lndclient.LndServices
}
// NewLndMsgTransportClient creates a new message transport RPC client for a
// given LND service.
func NewLndMsgTransportClient(
lnd *lndclient.LndServices) *LndMsgTransportClient {
return &LndMsgTransportClient{
lnd: lnd,
}
}
// SubscribeCustomMessages creates a subscription to custom messages received
// from our peers.
func (l *LndMsgTransportClient) SubscribeCustomMessages(
ctx context.Context) (<-chan lndclient.CustomMessage,
<-chan error, error) {
return l.lnd.Client.SubscribeCustomMessages(ctx)
}
// SendCustomMessage sends a custom message to a peer.
func (l *LndMsgTransportClient) SendCustomMessage(ctx context.Context,
msg lndclient.CustomMessage) error {
return l.lnd.Client.SendCustomMessage(ctx, msg)
}
// Ensure LndMsgTransportClient implements the rfq.PeerMessenger interface.
var _ rfq.PeerMessenger = (*LndMsgTransportClient)(nil)
// LndRouterClient is an LND router RPC client.
type LndRouterClient struct {
lnd *lndclient.LndServices
}
// NewLndRouterClient creates a new LND router client for a given LND service.
func NewLndRouterClient(lnd *lndclient.LndServices) *LndRouterClient {
return &LndRouterClient{
lnd: lnd,
}
}
// InterceptHtlcs intercepts all incoming HTLCs and calls the given handler
// function with the HTLC details. The handler function can then decide whether
// to accept or reject the HTLC.
func (l *LndRouterClient) InterceptHtlcs(
ctx context.Context, handler lndclient.HtlcInterceptHandler) error {
return l.lnd.Router.InterceptHtlcs(ctx, handler)
}
// Ensure LndRouterClient implements the rfq.HtlcInterceptor interface.
var _ rfq.HtlcInterceptor = (*LndRouterClient)(nil)