test(wallet): a chosen guardian delay holds at exactly its own number of blocks (Phase 2)

For delays at each edge of the script number's encoding -- 6, 16, 17,
127, 128, 32767, 32768 and 65535 -- an ancestor quorum's script-path spend
passes correctlySpends at nSequence = delay and fails one block short.
GroupSpendBuilder offers a guardian a coin only once it is that deep and
puts that sequence on every input, and GroupSpend.check refuses the
default's lock on a leaf that chose another.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-10-24 07:33:59 +02:00
1 parent b1e3e884cc
commit e6102bc106
2 files changed
+54

No files matched your search

@@ -193,6 +193,30 @@ class GuardianSpendRoundTest {
}
}
/**
* A chosen delay, at every edge of its encoding: the script interpreter takes
* the spend at exactly that many blocks and not one fewer. Where an encoding
* mistake would be caught before a coin could be lost to it.
* docs/guardian-delay.md, Phase 2.
*/
@Test
fun `a chosen delay holds at exactly its own number of blocks`() {
val child = someKey(0xc6)
val childRoom = SharedKeyDerivation.marmotGroupId(child)
val cache = GuardianDerivation.signingCache(ancestorKey.toString(), listOf(childRoom), chain, 0, 0, 0).cache
listOf(6L, 16L, 17L, 127L, 128L, 32_767L, 32_768L, 65_535L).forEach { delay ->
val grant = GuardianDerivation.grant(ancestorKey.toString(), ancestorRoom, emptyList(), childRoom, delay = delay)
val address = address(child, grant)
val funding = funding(address)
assertSpends(guardianSpend(spend(funding, sequence = delay), funding, address, address.leaves[0], cache), funding)
assertFails("one block short of $delay") {
assertSpends(guardianSpend(spend(funding, sequence = delay - 1), funding, address, address.leaves[0], cache), funding)
}
}
}
@Test
fun `the wrong cache signs for a key the leaf does not name`() {
val child = someKey(0xc4)
@@ -530,6 +530,36 @@ class GuardianSigningJvmTest {
assertEquals(built.fee.sat, 2 * ((checked.weight + 3) / 4).toLong())
}
/**
* The builder and the check read each leaf's own delay, at the edges of its
* encoding: a coin one block short of the lock is the owner's alone, at the
* lock it is the guardian's too, and every input carries exactly that lock.
* docs/guardian-delay.md, Phase 2.
*/
@Test
fun `the builder and the check follow a chosen delay, not the default`() {
val address = fr.acinq.bitcoin.Bitcoin.addressFromPublicKeyScript(chain.chainHash, elsewhere.toByteArray()).right!!
listOf(6L, 17L, 128L, 32_768L, 65_535L).forEach { delay ->
val guardians = GuardianDerivation.grant(thresholdPublicKey, roomId, emptyList(), guardedChildRoomId, delay = delay)
val guarded = assertNotNull(GuardianManager.verify(guardedStatement(guardians), roomId, thresholdPublicKey), "a delay of $delay")
val funding = fundingOf(guarded.account)
// Confirmed at height 100, so `delay` confirmations at tip 99 + delay.
val young = fundedState(guarded.account, funding, height = 100, tip = 98 + delay.toInt())
assertEquals(GroupSpendBuilder.Result.NothingToSpend, GroupSpendBuilder.sendAll(young, address, 2, emptySet(), guarded.spender()), "a delay of $delay")
val old = fundedState(guarded.account, funding, height = 100, tip = 99 + delay.toInt())
val built = assertIs<GroupSpendBuilder.Result.Built>(GroupSpendBuilder.sendAll(old, address, 2, emptySet(), guarded.spender()))
assertEquals(listOf(delay), built.psbt.global.tx.txIn.map { it.sequence })
assertIs<GroupSpend.Result.Ok>(GroupSpend.check(built.psbt, guarded.account, guarded.spender(thresholdPublicKey)))
// The default's lock on a leaf that chose another is refused.
val atDefault = built.psbt.global.tx.copy(txIn = built.psbt.global.tx.txIn.map { it.copy(sequence = GuardianDerivation.DEFAULT_DELAY) })
val refused = GroupSpend.check(built.psbt.copy(global = built.psbt.global.copy(tx = atDefault)), guarded.account, guarded.spender(thresholdPublicKey))
if (delay != GuardianDerivation.DEFAULT_DELAY) assertIs<GroupSpend.Result.Refused>(refused, "a delay of $delay")
}
}
@Test
fun `an ancestor's quorum spends a descendant's coin through its leaf, on every device`(): Unit = runBlocking {
val proposer = device(members[0], signerIndex = 0)