feat(wallet): a number of blocks said in time, roughly (Phase 3)

text/BlockDuration.approximate: within 5% of exactly one hour, day, month
or year, that unit singular; otherwise the largest unit that comes to at
least 2, rounded -- 216 blocks is about 36 hours, 65,535 about 15 months.
delayText says it beside the block count, "144 blocks, about 1 day", and
the block count alone on regtest.

The catalogue's first plurals, so nothing reads "about 1 hours";
StringCatalogueJvmTest asserts they resolve. Nothing shows a delay yet.

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:36:18 +02:00
1 parent e6102bc106
commit 2dd1caafdd
5 files changed
+213

No files matched your search

@@ -563,4 +563,31 @@
<string name="the_subgroup_s_wallet_will_not_be_guarded">The subgroup's wallet will not be guarded: only the subgroup can spend from it.</string>
<string name="the_group_above_decided_this_wallet_is_guarded">The group above this one decided this group's wallet is guarded. Each group above it can spend any coin in it once it has %1$d confirmations, without this group agreeing.</string>
<string name="the_group_above_decided_this_wallet_is_not_guarded">The group above this one decided this group's wallet is not guarded. Only this group can spend from it.</string>
<!-- A guardian's delay in blocks and roughly in time: docs/guardian-delay.md, D5 -->
<string name="blocks_and_about">%1$s, %2$s</string>
<plurals name="n_blocks">
<item quantity="one">%1$s block</item>
<item quantity="other">%1$s blocks</item>
</plurals>
<plurals name="about_n_minutes">
<item quantity="one">about %1$d minute</item>
<item quantity="other">about %1$d minutes</item>
</plurals>
<plurals name="about_n_hours">
<item quantity="one">about %1$d hour</item>
<item quantity="other">about %1$d hours</item>
</plurals>
<plurals name="about_n_days">
<item quantity="one">about %1$d day</item>
<item quantity="other">about %1$d days</item>
</plurals>
<plurals name="about_n_months">
<item quantity="one">about %1$d month</item>
<item quantity="other">about %1$d months</item>
</plurals>
<plurals name="about_n_years">
<item quantity="one">about %1$d year</item>
<item quantity="other">about %1$d years</item>
</plurals>
</resources>
@@ -0,0 +1,59 @@
package com.machankura.bennu.compose.text
import kotlin.math.abs
import kotlin.math.roundToInt
/**
* A number of blocks said in time, roughly -- `docs/guardian-delay.md`, D5.
*
* Always beside the block count, never instead of it: blocks are what a
* guardian's leaf counts, and ten minutes is the target, not a promise, so a
* delay of 144 blocks can take twenty hours or thirty. Hence *about*.
*
* The rule:
*
* 1. within 5% of exactly one hour, day, month or year, that unit, singular --
* 6 blocks is *about 1 hour*, 4,320 *about 1 month*;
* 2. otherwise the largest of years, months, days, hours and minutes in which
* the delay comes to at least 2, rounded -- 216 blocks is *about 36 hours*,
* 65,535 *about 15 months*.
*
* Years therefore appear only near exactly one year: the longest delay a leaf
* can carry is 1.25 years, and *about 15 months* says more than *about 1 year*.
*/
object BlockDuration {
/** Bitcoin's target, and the testnets' and signet's. Regtest has none. */
const val MINUTES_PER_BLOCK: Long = 10L
/** The units, each with its length in minutes: a month is a twelfth of a Julian year. */
enum class Unit(val minutes: Double) {
MINUTES(1.0),
HOURS(60.0),
DAYS(1_440.0),
MONTHS(43_830.0),
YEARS(525_960.0),
}
data class Approximate(val count: Int, val unit: Unit)
/** How close to exactly one of a unit a delay has to be to be said as one. */
private const val ONE_TOLERANCE = 0.05
fun approximate(blocks: Long): Approximate {
require(blocks > 0) { "a duration is at least one block, not $blocks" }
val minutes = (blocks * MINUTES_PER_BLOCK).toDouble()
val largestFirst = Unit.entries.reversed()
largestFirst.filter { it != Unit.MINUTES }.forEach { unit ->
if (abs(minutes / unit.minutes - 1.0) <= ONE_TOLERANCE) return Approximate(1, unit)
}
largestFirst.forEach { unit ->
val count = minutes / unit.minutes
if (count >= 2.0) return Approximate(count.roundToInt(), unit)
}
return Approximate(minutes.roundToInt(), Unit.MINUTES)
}
}
/** A count of blocks, its thousands grouped: `65,535`. */
fun Long.groupedBlocks(): String = toString().reversed().chunked(3).joinToString(",").reversed()
@@ -0,0 +1,46 @@
package com.machankura.bennu.compose.ui.composable.widgets
import androidx.compose.runtime.Composable
import bennu.composeapp.generated.resources.Res
import bennu.composeapp.generated.resources.about_n_days
import bennu.composeapp.generated.resources.about_n_hours
import bennu.composeapp.generated.resources.about_n_minutes
import bennu.composeapp.generated.resources.about_n_months
import bennu.composeapp.generated.resources.about_n_years
import bennu.composeapp.generated.resources.blocks_and_about
import bennu.composeapp.generated.resources.n_blocks
import com.machankura.bennu.compose.text.BlockDuration
import com.machankura.bennu.compose.text.groupedBlocks
import fr.acinq.bitcoin.Chain
import org.jetbrains.compose.resources.pluralStringResource
import org.jetbrains.compose.resources.stringResource
/**
* A guardian's delay as a member reads it: *144 blocks, about 1 day*.
* `docs/guardian-delay.md`, D5.
*
* Blocks only on regtest, whose blocks come when somebody mines one, so a time
* there would be wrong. [chain] is null where a delay is for no network in
* particular -- a birth certificate is not -- and then the estimate is
* mainnet's, as every network but regtest shares its target.
*/
@Composable
fun delayText(blocks: Long, chain: Chain? = null): String {
val count = pluralStringResource(Res.plurals.n_blocks, blocks.toInt(), blocks.groupedBlocks())
if (chain == Chain.Regtest) return count
return stringResource(Res.string.blocks_and_about, count, approximateDuration(blocks))
}
/** *about 1 day*: [BlockDuration.approximate] in words. */
@Composable
fun approximateDuration(blocks: Long): String {
val approximate = BlockDuration.approximate(blocks)
val plural = when (approximate.unit) {
BlockDuration.Unit.MINUTES -> Res.plurals.about_n_minutes
BlockDuration.Unit.HOURS -> Res.plurals.about_n_hours
BlockDuration.Unit.DAYS -> Res.plurals.about_n_days
BlockDuration.Unit.MONTHS -> Res.plurals.about_n_months
BlockDuration.Unit.YEARS -> Res.plurals.about_n_years
}
return pluralStringResource(plural, approximate.count, approximate.count)
}