diff --git a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/managers/GuardianSpendRoundTest.kt b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/managers/GuardianSpendRoundTest.kt index 33ae869c..0cd2fac7 100644 --- a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/managers/GuardianSpendRoundTest.kt +++ b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/managers/GuardianSpendRoundTest.kt @@ -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) diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt index 45e22bf8..5a96044d 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GuardianSigningJvmTest.kt @@ -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.sendAll(old, address, 2, emptySet(), guarded.spender())) + assertEquals(listOf(delay), built.psbt.global.tx.txIn.map { it.sequence }) + assertIs(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(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)