-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbackend.go
More file actions
310 lines (271 loc) · 13.8 KB
/
Copy pathbackend.go
File metadata and controls
310 lines (271 loc) · 13.8 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package chainsource
import (
"context"
"errors"
"github.com/btcsuite/btcd/btcutil/v2"
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btcd/wire/v2"
)
// ErrPackageMempoolAcceptUnsupported is returned by ChainBackend
// implementations whose underlying RPC cannot test a multi-transaction
// package for mempool acceptance. It is distinct from a per-tx
// "rejected" outcome: the backend never evaluated the package at all.
// Callers that treat package preflight as best-effort should downgrade
// this error to a soft-miss; callers that require package validation
// should surface it as a hard failure.
var ErrPackageMempoolAcceptUnsupported = errors.New("package " +
"testmempoolaccept not supported by backend")
// MempoolAcceptResult is the per-transaction outcome of a
// TestMempoolAccept call. One result is returned for each input tx, in
// the same order.
type MempoolAcceptResult struct {
// Txid is the transaction hash the result applies to.
Txid chainhash.Hash
// Accepted reports whether the backend would accept the
// transaction into its mempool.
Accepted bool
// Reason carries the backend's human-readable rejection reason
// when Accepted is false. Empty on acceptance.
Reason string
}
// ChainBackend defines the interface that must be implemented by all
// blockchain backend providers. This abstraction allows the ChainSource actor
// to work with different backends (lnd's chainntnfs, block explorers like
// mempool.space, or custom implementations) without coupling to any specific
// implementation.
//
// All methods must be safe for concurrent use. Implementations should handle
// their own internal synchronization and connection management.
type ChainBackend interface {
// EstimateFee returns the estimated fee rate in satoshis per vbyte for
// a transaction to confirm within the target number of blocks. Returns
// an error if fee estimation fails or is unavailable.
EstimateFee(ctx context.Context,
targetConf uint32) (btcutil.Amount, error)
// BestBlock returns the current best known block height and hash from
// the blockchain. This represents the tip of the longest valid chain
// according to the backend's view.
BestBlock(ctx context.Context) (int32, chainhash.Hash, error)
// TestMempoolAccept tests whether one or more transactions would be
// accepted by the mempool without actually broadcasting them. When
// len(txs) > 1 the backend must evaluate the transactions as a
// package (matching Bitcoin Core's testmempoolaccept JSON array
// form); backends that can only validate individual transactions
// must return ErrPackageMempoolAcceptUnsupported rather than
// silently evaluating the first tx in isolation.
//
// The returned slice has one entry per input tx, in the same
// order. Not all backends may support this operation at all; those
// should return a non-nil error from the single-tx call so callers
// can distinguish "rejected" from "not evaluated".
TestMempoolAccept(ctx context.Context,
txs ...*wire.MsgTx) ([]MempoolAcceptResult, error)
// BroadcastTx broadcasts a transaction to the network. The label
// parameter is optional and may be used for wallet tracking. Returns
// an error if the broadcast fails.
BroadcastTx(ctx context.Context, tx *wire.MsgTx, label string) error
// RegisterConf registers for confirmation notifications of a
// transaction. The registration returns a ConfRegistration that
// provides channels for receiving confirmation events.
//
// Parameters:
// - ctx: Context for cancellation
// - txid: The transaction ID to monitor (can be nil to match by
// script)
// - pkScript: The public key script to watch for
// - numConfs: Target number of confirmations
// - heightHint: Earliest block containing the tx (0 if unknown)
// - includeBlock: If true, include the full block in the confirmation
// event for merkle proof construction
//
// The returned ConfRegistration must have buffered channels and a
// Cancel function for cleanup.
RegisterConf(ctx context.Context, txid *chainhash.Hash, pkScript []byte,
numConfs uint32, heightHint uint32,
includeBlock bool) (*ConfRegistration, error)
// RegisterSpend registers for spend notifications of a transaction
// output. The registration returns a SpendRegistration that provides
// channels for receiving spend events.
//
// Parameters:
// - ctx: Context for cancellation
// - outpoint: The output to monitor (can be nil to match by script)
// - pkScript: The public key script to watch for
// - heightHint: Earliest block containing a spend (0 if unknown)
//
// The returned SpendRegistration must have buffered channels and a
// Cancel function for cleanup.
RegisterSpend(ctx context.Context, outpoint *wire.OutPoint,
pkScript []byte, heightHint uint32) (*SpendRegistration, error)
// RegisterBlocks registers for new block notifications. The
// registration returns a BlockRegistration that provides a channel for
// receiving block events.
//
// The returned BlockRegistration must have a buffered channel and a
// Cancel function for cleanup. The backend may optionally backfill
// missed blocks if the client provides a best known block.
RegisterBlocks(ctx context.Context) (*BlockRegistration, error)
// SubmitPackage atomically submits a parent+child transaction
// package to the network. This is required for V3 package relay
// where the child pays fees for otherwise non-relayable parents.
SubmitPackage(ctx context.Context, parents []*wire.MsgTx,
child *wire.MsgTx) error
// Start initializes the backend and any background processes. This
// must be called before using any other methods.
Start() error
// Stop shuts down the backend and cleans up all resources. All pending
// registrations will be cancelled.
Stop() error
}
// TxRemover is an optional capability a ChainBackend may implement to remove a
// previously broadcast transaction from the wallet, so the wallet stops
// rebroadcasting it. A caller that has abandoned a transaction (e.g. a
// unilateral-exit proof tx whose job terminally failed) uses it to stop a
// full-node wallet from perpetually re-publishing a transaction that can never
// confirm (wavelength#609). Backends without a rebroadcasting wallet (e.g. a
// pure Esplora chain source) do not implement it; the chainsource actor treats
// a RemoveTxRequest as a no-op for those.
type TxRemover interface {
// RemoveTx removes the transaction with the given hash, and any
// transactions that depend on it, from the wallet's store and its
// unconfirmed-rebroadcast set.
RemoveTx(ctx context.Context, txid chainhash.Hash) error
}
// ConfRegistration encapsulates the channels and control functions for a
// confirmation registration. This mirrors lnd's chainntnfs.ConfirmationEvent
// structure but provides a backend-agnostic interface.
//
// The registration is reorg-aware: after a confirmation has been delivered
// on Confirmed, a subsequent reorg that buries the original block can cause
// a fresh send on Reorged. Once the transaction re-confirms on the new
// canonical chain another event arrives on Confirmed. The lifecycle is
// therefore Confirmed -> Reorged -> Confirmed -> ... terminated by either a
// send on Done (the confirmation is past the backend's reorg-safety depth)
// or a caller-driven Cancel. Backends that cannot observe reorgs leave
// Reorged and Done as never-firing channels; callers must not assume either
// will ever fire.
type ConfRegistration struct {
// Confirmed fires every time the transaction reaches the target
// number of confirmations on the canonical chain. After a Reorged
// event it may fire again when the transaction re-confirms. The
// channel is buffered.
Confirmed <-chan *TxConfirmation
// Reorged fires when a previously delivered confirmation is reorged
// out of the canonical chain. The payload is the backend forwarder's
// monotonic sequence number (see TxConfirmation.Seq) rather than
// reorg depth or block identity, which the lndclient gRPC transport
// cannot preserve; the sequence lets the consumer order this signal
// against confirmations sharing the same sequence space even though
// they arrive on a different channel. Backends that cannot observe
// reorgs leave this channel nil-equivalent (allocated but never
// written to); reading from it is always safe.
Reorged <-chan uint64
// Done fires once when the confirmation watch is past the backend's
// reorg-safety depth and will receive no further events. Callers
// must still invoke Cancel to release client-side resources.
// Backends that cannot synthesize a safety-depth signal leave this
// channel nil-equivalent (allocated but never written to).
Done <-chan struct{}
// Cancel is a function that can be called to cancel this registration
// and clean up resources. After calling Cancel, no more events will be
// sent on any channels.
Cancel func()
}
// TxConfirmation contains details about a confirmed transaction. This is sent
// when a monitored transaction reaches its target confirmation count.
type TxConfirmation struct {
// BlockHash is the hash of the block containing the transaction.
BlockHash *chainhash.Hash
// BlockHeight is the height of the block containing the transaction.
BlockHeight uint32
// TxIndex is the position of the transaction within the block.
TxIndex uint32
// Tx is the confirmed transaction itself.
Tx *wire.MsgTx
// Block is the full block containing the transaction. Only populated
// when the confirmation was registered with IncludeBlock=true. This
// matches lnd's chainntnfs behavior.
Block *wire.MsgBlock
// Seq is a per-registration monotonic sequence number stamped by the
// backend forwarder in the order it observed lifecycle events.
// Confirmed and Reorged share one sequence space so the consumer can
// order them even though they arrive on separate channels (a select
// over two ready channels picks at random and cannot recover order).
// The consumer applies an event only when its Seq exceeds the highest
// Seq seen so far, discarding a stale event that lost a cross-channel
// race. Zero means the backend does not stamp sequences (it never
// reorgs, so ordering is moot); such events are always applied.
Seq uint64
}
// SpendRegistration encapsulates the channels and control functions for a
// spend registration. This mirrors lnd's chainntnfs.SpendEvent structure.
//
// The registration is reorg-aware: after a spend has been delivered on
// Spend, a subsequent reorg that buries the spending block can cause a
// fresh send on Reorged. If the outpoint is then re-spent on the new
// canonical chain (by the same or a different transaction) another event
// arrives on Spend. The lifecycle is therefore Spend -> Reorged -> Spend
// -> ... terminated by either a send on Done (the spend is past the
// backend's reorg-safety depth) or a caller-driven Cancel. Backends that
// cannot observe reorgs leave Reorged and Done as never-firing channels.
type SpendRegistration struct {
// Spend fires every time the monitored outpoint is spent on the
// canonical chain. After a Reorged event it may fire again when a
// new spender confirms. The channel is buffered.
Spend <-chan *SpendDetail
// Reorged fires when a previously delivered spend is reorged out of
// the canonical chain. The payload is the backend forwarder's
// monotonic sequence number (see SpendDetail.Seq) rather than reorg
// depth or block identity, which the lndclient gRPC transport cannot
// preserve; the sequence lets the consumer order this signal against
// spends sharing the same sequence space even though they arrive on a
// different channel. Backends that cannot observe spend reorgs leave
// this channel nil-equivalent (allocated but never written to);
// reading from it is always safe.
Reorged <-chan uint64
// Done fires once when the spend watch is past the backend's
// reorg-safety depth and will receive no further events. Callers
// must still invoke Cancel to release client-side resources.
// Backends that cannot synthesize a safety-depth signal leave this
// channel nil-equivalent (allocated but never written to).
Done <-chan struct{}
// Cancel is a function that can be called to cancel this registration
// and clean up resources.
Cancel func()
}
// SpendDetail contains details about a spend of a monitored outpoint. This is
// sent when the outpoint is consumed by a confirmed transaction.
type SpendDetail struct {
// SpentOutPoint is the outpoint that was spent.
SpentOutPoint *wire.OutPoint
// SpenderTxHash is the hash of the spending transaction.
SpenderTxHash *chainhash.Hash
// SpendingTx is the full spending transaction.
SpendingTx *wire.MsgTx
// SpenderInputIndex is the input index in the spending transaction
// that consumed the outpoint.
SpenderInputIndex uint32
// SpendingHeight is the block height where the spending transaction
// was confirmed.
SpendingHeight int32
// Seq is a per-registration monotonic sequence number stamped by the
// backend forwarder in the order it observed lifecycle events. Spend
// and Reorged share one sequence space so the consumer can order them
// even though they arrive on separate channels (a select over two
// ready channels picks at random and cannot recover order). The
// consumer applies an event only when its Seq exceeds the highest Seq
// seen so far, discarding a stale event that lost a cross-channel
// race. Zero means the backend does not stamp sequences (it never
// reorgs, so ordering is moot); such events are always applied.
Seq uint64
}
// BlockRegistration encapsulates the channels and control functions for a
// block subscription.
type BlockRegistration struct {
// Epochs is a channel that sends an event for each new block connected
// to the chain. The channel is buffered to handle bursts of blocks.
Epochs <-chan *BlockEpoch
// Cancel is a function that can be called to cancel this registration
// and clean up resources.
Cancel func()
}