Skip to content

Commit a8bdaeb

Browse files
test(tbtc): stabilize coordination layer assertion
1 parent 3c58b76 commit a8bdaeb

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24
44

55
toolchain go1.24.1
66

7+
78
replace (
89
github.com/bnb-chain/tss-lib => github.com/threshold-network/tss-lib v0.0.0-20230901144531-2e712689cfbe
910
// btcd in version v.0.23 extracted `btcd/btcec` to a separate package `btcd/btcec/v2`.

pkg/tbtc/node_test.go

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -344,22 +344,27 @@ func TestNode_RunCoordinationLayer(t *testing.T) {
344344
if signer.wallet.publicKey.Equal(walletPublicKey) {
345345
result, ok := map[uint64]*coordinationResult{
346346
900: {
347+
window: window,
347348
proposal: &mockCoordinationProposal{ActionDepositSweep},
348349
},
349350
// Omit window at block 1800 to make sure the layer doesn't
350351
// crash if no result is produced.
351352
2700: {
353+
window: window,
352354
proposal: &mockCoordinationProposal{ActionRedemption},
353355
},
354356
// Put some trash value to make sure coordination windows
355357
// are distributed correctly.
356358
2705: {
359+
window: window,
357360
proposal: &mockCoordinationProposal{ActionMovingFunds},
358361
},
359362
3600: {
363+
window: window,
360364
proposal: &mockCoordinationProposal{ActionNoop},
361365
},
362366
4500: {
367+
window: window,
363368
proposal: &mockCoordinationProposal{ActionMovedFundsSweep},
364369
},
365370
}[window.coordinationBlock]
@@ -405,6 +410,10 @@ loop:
405410
for {
406411
select {
407412
case result := <-processedResultsChan:
413+
if result == nil {
414+
continue
415+
}
416+
408417
processedResults = append(processedResults, result)
409418

410419
// Once the second-last coordination window is processed, stop the
@@ -425,24 +434,68 @@ loop:
425434
3,
426435
len(processedResults),
427436
)
428-
testutils.AssertStringsEqual(
437+
438+
resultActionsByWindow := make(map[uint64]WalletActionType, len(processedResults))
439+
for _, result := range processedResults {
440+
resultActionsByWindow[result.window.coordinationBlock] =
441+
result.proposal.ActionType()
442+
}
443+
444+
testutils.AssertIntsEqual(
429445
t,
430-
"first result",
431-
ActionDepositSweep.String(),
432-
processedResults[0].proposal.ActionType().String(),
446+
"processed coordination windows count",
447+
3,
448+
len(resultActionsByWindow),
433449
)
450+
451+
firstAction, ok := resultActionsByWindow[900]
452+
if !ok {
453+
t.Fatal("expected coordination result for window at block 900")
454+
}
434455
testutils.AssertStringsEqual(
435456
t,
436-
"second result",
437-
ActionRedemption.String(),
438-
processedResults[1].proposal.ActionType().String(),
457+
"result for block 900",
458+
ActionDepositSweep.String(),
459+
firstAction.String(),
439460
)
461+
462+
secondAction, ok := resultActionsByWindow[2700]
463+
if !ok {
464+
t.Fatal("expected coordination result for window at block 2700")
465+
}
440466
testutils.AssertStringsEqual(
441467
t,
442-
"third result",
443-
ActionNoop.String(),
444-
processedResults[2].proposal.ActionType().String(),
468+
"result for block 2700",
469+
ActionRedemption.String(),
470+
secondAction.String(),
445471
)
472+
473+
if _, ok := resultActionsByWindow[2705]; ok {
474+
t.Fatal("unexpected coordination result for non-window block 2705")
475+
}
476+
477+
// Result processing is asynchronous, so by the time the test cancels the
478+
// coordination layer after the third processed result, either the 3600
479+
// window or the subsequent 4500 window may already be in flight.
480+
if thirdAction, ok := resultActionsByWindow[3600]; ok {
481+
testutils.AssertStringsEqual(
482+
t,
483+
"result for block 3600",
484+
ActionNoop.String(),
485+
thirdAction.String(),
486+
)
487+
} else {
488+
fourthAction, ok := resultActionsByWindow[4500]
489+
if !ok {
490+
t.Fatal("expected coordination result for block 3600 or 4500")
491+
}
492+
testutils.AssertStringsEqual(
493+
t,
494+
"result for block 4500",
495+
ActionMovedFundsSweep.String(),
496+
fourthAction.String(),
497+
)
498+
}
446499
}
447500

448501
type mockCoordinationProposal struct {

0 commit comments

Comments
 (0)