-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy patheager_round_join_test.go
More file actions
552 lines (495 loc) · 15.5 KB
/
Copy patheager_round_join_test.go
File metadata and controls
552 lines (495 loc) · 15.5 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package wallet
import (
"context"
"testing"
"time"
btcaddr "github.com/btcsuite/btcd/address/v2"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil/v2"
"github.com/btcsuite/btcd/chaincfg/v2"
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btcd/txscript/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/btcsuite/btclog/v2"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/lightninglabs/wavelength/baselib/actor"
"github.com/lightninglabs/wavelength/chainsource"
"github.com/lightninglabs/wavelength/ledger"
"github.com/lightninglabs/wavelength/lib/actormsg"
"github.com/lightningnetwork/lnd/clock"
fn "github.com/lightningnetwork/lnd/fn/v2"
"github.com/lightningnetwork/lnd/keychain"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// eagerBoardFixture wires the minimum mocks the eager-board path exercises:
// one boarding address, one fresh UTXO paying to it, one assembling round
// actor that captures the resulting TriggerBoardMsg.
type eagerBoardFixture struct {
backend *MockBoardingBackend
store *MockBoardingStore
roundActor *mockRoundActorBehavior
wallet *Ark
epoch chainsource.BlockEpoch
}
// newEagerBoardFixture sets up a wallet actor wired against a mock backend /
// store / round actor. The eager flag selects whether the wallet is built
// with WithEagerRoundJoin. The store is configured for the full
// processUtxo → handleBoard chain when eager is true; when eager is false,
// only the processUtxo expectations are set so an unwanted Fetch /
// UpsertPendingIntent call would fail the test.
func newEagerBoardFixture(t *testing.T, eager bool) *eagerBoardFixture {
t.Helper()
clientKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
operatorKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
rootHash := []byte{0xaa, 0xbb, 0xcc}
taprootKey := txscript.ComputeTaprootOutputKey(
clientKey.PubKey(), rootHash,
)
address, err := btcaddr.NewAddressTaproot(
taprootKey.SerializeCompressed()[1:],
&chaincfg.RegressionNetParams,
)
require.NoError(t, err)
pkScript, err := txscript.PayToAddrScript(address)
require.NoError(t, err)
boardingAddr := &BoardingAddress{
Address: address,
Tapscript: &waddrmgr.Tapscript{
Type: waddrmgr.TapscriptTypeFullTree,
RootHash: rootHash,
},
KeyDesc: keychain.KeyDescriptor{
PubKey: clientKey.PubKey(),
KeyLocator: keychain.KeyLocator{
Family: keychain.KeyFamily(BoardingKeyFamily),
Index: 0,
},
},
OperatorKey: operatorKey.PubKey(),
ExitDelay: 144,
}
outpoint := wire.OutPoint{Hash: chainhash.Hash{0xaa}, Index: 0}
mockTx := &wire.MsgTx{
TxOut: []*wire.TxOut{{
Value: 100_000,
PkScript: pkScript,
}},
}
epochHash := chainhash.Hash{0xbb}
utxo := &Utxo{
Outpoint: outpoint,
PkScript: pkScript,
Amount: 100_000,
Confirmations: 6,
}
backend := &MockBoardingBackend{}
backend.On(
"ListUnspent", mock.Anything, int32(MinBoardingConfs),
int32(MaxConfsForListUnspent),
).Return([]*Utxo{utxo}, nil)
backend.On(
"GetTransaction", mock.Anything, outpoint.Hash,
).Return(&TxInfo{
Tx: mockTx,
BlockHash: &epochHash,
BlockHeight: 100,
}, nil)
backend.On(
"GetBlock", mock.Anything, epochHash,
).Return(&wire.MsgBlock{
Transactions: []*wire.MsgTx{mockTx},
}, nil)
store := &MockBoardingStore{}
store.On(
"LookupBoardingAddress", mock.Anything, pkScript,
).Return(boardingAddr, nil)
store.On(
"InsertBoardingIntents", mock.Anything, mock.Anything,
).Return(nil)
if eager {
// The eager path runs handleBoard inline, which reads the
// confirmed boarding set and persists one pending row.
// Return the freshly inserted intent so the round Tell is
// not short-circuited by an empty boarding balance.
confirmedIntent := BoardingIntent{
Address: *boardingAddr,
Outpoint: outpoint,
ChainInfo: BoardingChainInfo{
ConfHeight: 100,
ConfHash: epochHash,
ConfTx: mockTx,
OutPoint: outpoint,
Amount: btcutil.Amount(100_000),
},
Status: BoardingStatusConfirmed,
}
store.On(
"FetchBoardingIntentsByStatus", mock.Anything,
BoardingStatusConfirmed,
).Return([]BoardingIntent{confirmedIntent}, nil)
store.On(
"UpsertPendingIntent", mock.Anything,
mock.Anything,
).Return(nil)
}
system := actor.NewActorSystem()
t.Cleanup(func() {
// Use Background ctx because t.Context is cancelled before
// Cleanup runs.
err := system.Shutdown(context.Background())
require.NoError(t, err)
})
roundActor := &mockRoundActorBehavior{}
roundKey := actormsg.RoundActorServiceKey()
actor.RegisterWithSystem(
system, actormsg.RoundActorServiceKeyName, roundKey, roundActor,
)
opts := []ArkOption{WithClock(clock.NewDefaultClock())}
if eager {
opts = append(opts, WithEagerRoundJoin(true))
}
w := NewArk(
backend, store, nil, nil, system, fn.None[ledger.Sink](),
btclog.Disabled, opts...,
)
w.seenUtxos = fn.NewSet[UtxoKey]()
w.notifiers = make(map[string]notifierInfo)
return &eagerBoardFixture{
backend: backend,
store: store,
roundActor: roundActor,
wallet: w,
epoch: chainsource.BlockEpoch{
Height: 100,
Hash: epochHash,
},
}
}
// newMultiUTXOEagerBoardFixture wires three confirmed boarding
// UTXOs paying to the same boarding address, all surfacing in one
// block-epoch. Used by the coalescing test to pin down the "one
// TriggerBoardMsg per block, not per UTXO" invariant.
func newMultiUTXOEagerBoardFixture(t *testing.T) *eagerBoardFixture {
t.Helper()
clientKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
operatorKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
rootHash := []byte{0xaa, 0xbb, 0xcc}
taprootKey := txscript.ComputeTaprootOutputKey(
clientKey.PubKey(), rootHash,
)
address, err := btcaddr.NewAddressTaproot(
taprootKey.SerializeCompressed()[1:],
&chaincfg.RegressionNetParams,
)
require.NoError(t, err)
pkScript, err := txscript.PayToAddrScript(address)
require.NoError(t, err)
boardingAddr := &BoardingAddress{
Address: address,
Tapscript: &waddrmgr.Tapscript{
Type: waddrmgr.TapscriptTypeFullTree,
RootHash: rootHash,
},
KeyDesc: keychain.KeyDescriptor{
PubKey: clientKey.PubKey(),
KeyLocator: keychain.KeyLocator{
Family: keychain.KeyFamily(BoardingKeyFamily),
Index: 0,
},
},
OperatorKey: operatorKey.PubKey(),
ExitDelay: 144,
}
epochHash := chainhash.Hash{0xbb}
const numUTXOs = 3
utxos := make([]*Utxo, 0, numUTXOs)
confirmedIntents := make([]BoardingIntent, 0, numUTXOs)
backend := &MockBoardingBackend{}
for i := byte(0); i < numUTXOs; i++ {
outpoint := wire.OutPoint{
Hash: chainhash.Hash{
0xaa,
i,
},
Index: 0,
}
amount := btcutil.Amount(50_000 + int64(i)*10_000)
mockTx := &wire.MsgTx{
TxOut: []*wire.TxOut{{
Value: int64(amount),
PkScript: pkScript,
}},
}
utxos = append(utxos, &Utxo{
Outpoint: outpoint,
PkScript: pkScript,
Amount: amount,
Confirmations: 6,
})
confirmedIntents = append(confirmedIntents, BoardingIntent{
Address: *boardingAddr,
Outpoint: outpoint,
ChainInfo: BoardingChainInfo{
ConfHeight: 100,
ConfHash: epochHash,
ConfTx: mockTx,
OutPoint: outpoint,
Amount: amount,
},
Status: BoardingStatusConfirmed,
})
backend.On(
"GetTransaction", mock.Anything, outpoint.Hash,
).Return(&TxInfo{
Tx: mockTx,
BlockHash: &epochHash,
BlockHeight: 100,
}, nil)
backend.On(
"GetBlock", mock.Anything, epochHash,
).Return(&wire.MsgBlock{
Transactions: []*wire.MsgTx{mockTx},
}, nil).Once()
}
backend.On(
"ListUnspent", mock.Anything, int32(MinBoardingConfs),
int32(MaxConfsForListUnspent),
).Return(utxos, nil)
store := &MockBoardingStore{}
store.On(
"LookupBoardingAddress", mock.Anything, pkScript,
).Return(boardingAddr, nil)
store.On(
"InsertBoardingIntents", mock.Anything, mock.Anything,
).Return(nil)
store.On(
"FetchBoardingIntentsByStatus", mock.Anything,
BoardingStatusConfirmed,
).Return(confirmedIntents, nil)
store.On(
"UpsertPendingIntent", mock.Anything, mock.Anything,
).Return(nil)
system := actor.NewActorSystem()
t.Cleanup(func() {
err := system.Shutdown(context.Background())
require.NoError(t, err)
})
roundActor := &mockRoundActorBehavior{}
roundKey := actormsg.RoundActorServiceKey()
actor.RegisterWithSystem(
system, actormsg.RoundActorServiceKeyName, roundKey, roundActor,
)
w := NewArk(
backend, store, nil, nil, system, fn.None[ledger.Sink](),
btclog.Disabled,
WithClock(
clock.NewDefaultClock(),
),
WithEagerRoundJoin(true),
)
w.seenUtxos = fn.NewSet[UtxoKey]()
w.notifiers = make(map[string]notifierInfo)
return &eagerBoardFixture{
backend: backend,
store: store,
roundActor: roundActor,
wallet: w,
epoch: chainsource.BlockEpoch{
Height: 100,
Hash: epochHash,
},
}
}
// TestEagerRoundJoinAutoBoardsOnBlockEpoch pins down the eager-board path:
// when WithEagerRoundJoin is on, a block-epoch that surfaces a fresh
// boarding UTXO must drive handleBoard inline and produce one
// TriggerBoardMsg to the round actor without the caller having to issue
// a follow-up Board RPC.
func TestEagerRoundJoinAutoBoardsOnBlockEpoch(t *testing.T) {
t.Parallel()
fix := newEagerBoardFixture(t, true)
// handleBlockEpoch records the tip atomically (no heavy work);
// handleProcessTipTick is what runs ListUnspent + the eager-board
// hook on the next tick. Drive both directly so the test does
// not depend on the runTipTickLoop goroutine being started.
res := fix.wallet.handleBlockEpoch(t.Context(), fix.epoch)
require.True(
t, res.IsOk(),
"block-epoch handler must succeed; got %v", res.Err(),
)
res = fix.wallet.handleProcessTipTick(t.Context())
require.True(
t, res.IsOk(),
"tip-tick handler must succeed; got %v", res.Err(),
)
require.Eventually(t, func() bool {
return fix.roundActor.TriggerBoardCalls() == 1
}, 2*time.Second, 10*time.Millisecond,
"eager round-join must self-issue one TriggerBoardMsg per "+
"block-epoch that produced a new boarding UTXO")
}
// TestEagerRoundJoinCoalescesMultiUTXOBlockEpoch pins down the
// per-block (not per-UTXO) dispatch invariant: even when several
// boarding UTXOs confirm in the same block-epoch, the eager-board
// path produces exactly one TriggerBoardMsg. This is the test that
// would catch a regression where the inline handleBoard call leaks
// into the per-UTXO loop of processUtxo.
func TestEagerRoundJoinCoalescesMultiUTXOBlockEpoch(t *testing.T) {
t.Parallel()
fix := newMultiUTXOEagerBoardFixture(t)
// Drive both handlers directly: the lightweight block-epoch
// handler stores the tip, the tip-tick handler runs ListUnspent
// + the eager-board hook.
res := fix.wallet.handleBlockEpoch(t.Context(), fix.epoch)
require.True(
t, res.IsOk(),
"block-epoch handler must succeed; got %v", res.Err(),
)
res = fix.wallet.handleProcessTipTick(t.Context())
require.True(
t, res.IsOk(),
"tip-tick handler must succeed; got %v", res.Err(),
)
// Allow the actor system to drain the Tell.
require.Eventually(t, func() bool {
return fix.roundActor.TriggerBoardCalls() == 1
}, 2*time.Second, 10*time.Millisecond,
"three UTXOs confirming in one block must produce exactly "+
"one TriggerBoardMsg, not three")
// Beyond the eventual count, also confirm there is no late
// over-trigger: poll for a brief window and assert the counter
// stays pinned at 1.
require.Never(t, func() bool {
return fix.roundActor.TriggerBoardCalls() > 1
}, 250*time.Millisecond, 25*time.Millisecond,
"eager-board must not retrigger after the initial dispatch")
}
// TestEagerRoundJoinDisabledSkipsAutoBoard verifies the off-default: the
// same confirmation that auto-boards under eager mode must NOT touch the
// pending-board store or the round actor when the flag is off. This is
// load-bearing for non-SDK hosts (wavecli, server deployments) whose
// callers expect explicit Board RPCs to be the only path that triggers a
// TriggerBoardMsg.
func TestEagerRoundJoinDisabledSkipsAutoBoard(t *testing.T) {
t.Parallel()
fix := newEagerBoardFixture(t, false)
// Drive both handlers: when eager mode is off, neither the
// block-epoch handler nor the tip-tick handler may dispatch
// a TriggerBoardMsg.
res := fix.wallet.handleBlockEpoch(t.Context(), fix.epoch)
require.True(
t, res.IsOk(),
"block-epoch handler must succeed; got %v", res.Err(),
)
res = fix.wallet.handleProcessTipTick(t.Context())
require.True(
t, res.IsOk(),
"tip-tick handler must succeed; got %v", res.Err(),
)
// processUtxo persists the intent, but no Fetch / Upsert / Tell
// should follow. Give the actor system enough time to misbehave.
require.Never(t, func() bool {
return fix.roundActor.TriggerBoardCalls() > 0
}, 250*time.Millisecond, 25*time.Millisecond,
"non-eager wallet must not Tell the round actor on a "+
"boarding confirmation")
}
// TestEagerRoundJoinSetsTriggerRegistrationOnLeave verifies the second
// half of the eager round-join contract: cooperative-leave intents are
// shipped with TriggerRegistration=true so the round FSM advances out
// of PendingRoundAssembly immediately. The off-default keeps the
// batched leave semantics that operator-driven hosts rely on.
//
// The two cases run as named subtests so a failure on the first leg
// does not short-circuit the second; this is the standard pattern for
// parametric Go tests and keeps the failure surface narrow.
func TestEagerRoundJoinSetsTriggerRegistrationOnLeave(t *testing.T) {
t.Parallel()
t.Run("eager=true", func(t *testing.T) {
t.Parallel()
runLeaveTriggerRegistrationCase(t, true)
})
t.Run("eager=false", func(t *testing.T) {
t.Parallel()
runLeaveTriggerRegistrationCase(t, false)
})
}
// runLeaveTriggerRegistrationCase exercises handleLeaveVTXOs with the
// eager flag set to the supplied value and asserts that the captured
// RegisterIntentMsg's TriggerRegistration field matches.
func runLeaveTriggerRegistrationCase(t *testing.T, eager bool) {
t.Helper()
op := testOutpoint(0)
vtxoDescs := map[wire.OutPoint]*VTXODescriptor{
op: {
Outpoint: op,
Amount: 50_000,
PkScript: []byte{
0x51,
0x20,
0x01,
},
PolicyTemplate: []byte{
0xde, 0xad, 0xbe, 0xef,
},
Expiry: 100,
},
}
mgr := &mockVTXOManagerBehavior{
forfeitReserveResp: &actormsg.ReserveForfeitResponse{},
}
roundActor := &mockRoundActorBehavior{}
system := actor.NewActorSystem()
t.Cleanup(func() {
err := system.Shutdown(context.Background())
require.NoError(t, err)
})
mgrKey := actormsg.VTXOManagerServiceKey()
actor.RegisterWithSystem(
system, actormsg.VTXOManagerServiceKeyName, mgrKey, mgr,
)
roundKey := actormsg.RoundActorServiceKey()
actor.RegisterWithSystem(
system, actormsg.RoundActorServiceKeyName, roundKey, roundActor,
)
opts := []ArkOption{}
if eager {
opts = append(opts, WithEagerRoundJoin(true))
}
w := NewArk(
nil, nil, testVTXOReader(vtxoDescs), nil, system,
fn.None[ledger.Sink](), btclog.Disabled, opts...,
)
req := &LeaveVTXOsRequest{
TargetOutpoints: []wire.OutPoint{
op,
},
DestOutput: &wire.TxOut{
PkScript: []byte{
0x51,
0x20,
0xaa,
},
},
}
result := w.Receive(t.Context(), req)
require.True(
t, result.IsOk(),
"leave handler must succeed; got %v", result.Err(),
)
require.Equal(
t, 1, roundActor.registerCalls,
"round actor must observe exactly one RegisterIntentMsg",
)
require.NotNil(t, roundActor.capturedIntent)
require.Equal(
t, eager, roundActor.capturedIntent.TriggerRegistration, "Tr"+
"iggerRegistration must mirror the eagerRoundJoin "+
"flag (eager=%v)", eager,
)
}