feat: make ValidityWindow generic with Display and datetime conversion

- ValidityWindow<T = BlockId> now generic over bound type
- Generic Display<T> implementation for all bound types
- to_datetime_window() converts block-id window to UTC datetime window
- Backward compatible: default type param is BlockId

Enables ValidityWindow<chrono::DateTime<Utc>> for human-readable display.

Refs #421
This commit is contained in:
ygd58 2026-04-01 13:34:16 +02:00
parent 9fa541f3d1
commit 42985e5c72
No known key found for this signature in database
GPG Key ID: 82B49AE8D5B28600
2 changed files with 25 additions and 3 deletions

View File

@ -8,6 +8,7 @@ license = { workspace = true }
workspace = true
[dependencies]
chrono = { workspace = true }
nssa_core = { workspace = true, optional = true, features = ["host"] }
nssa = { workspace = true, optional = true }
common = { workspace = true, optional = true }

View File

@ -303,11 +303,12 @@ pub struct Nullifier(
);
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub struct ValidityWindow(pub (Option<BlockId>, Option<BlockId>));
#[serde(transparent)]
pub struct ValidityWindow<T = BlockId>(pub (Option<T>, Option<T>));
impl Display for ValidityWindow {
impl<T: Display> Display for ValidityWindow<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0 {
match &self.0 {
(Some(start), Some(end)) => write!(f, "[{start}, {end})"),
(Some(start), None) => write!(f, "[{start}, \u{221e})"),
(None, Some(end)) => write!(f, "(-\u{221e}, {end})"),
@ -316,6 +317,26 @@ impl Display for ValidityWindow {
}
}
impl ValidityWindow<BlockId> {
/// Convert to a UTC datetime window given a block-to-timestamp mapping.
pub fn to_datetime_window<F>(
&self,
block_to_timestamp_ms: F,
) -> ValidityWindow<chrono::DateTime<chrono::Utc>>
where
F: Fn(BlockId) -> Option<u64>,
{
let convert = |id: &BlockId| {
block_to_timestamp_ms(*id)
.and_then(|ms| chrono::DateTime::from_timestamp_millis(ms as i64))
};
ValidityWindow((
self.0.0.as_ref().and_then(&convert),
self.0.1.as_ref().and_then(&convert),
))
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub struct CommitmentSetDigest(
#[serde(with = "base64::arr")]