Skip to content

feat: make ValidityWindow generic with Display and datetime conversion#424

Open
ygd58 wants to merge 2 commits intologos-blockchain:mainfrom
ygd58:feat/validity-window-display
Open

feat: make ValidityWindow generic with Display and datetime conversion#424
ygd58 wants to merge 2 commits intologos-blockchain:mainfrom
ygd58:feat/validity-window-display

Conversation

@ygd58
Copy link
Copy Markdown
Contributor

@ygd58 ygd58 commented Apr 1, 2026

Summary

Refs #421.

Changes

Made ValidityWindow generic over its bound type T:

// Before
pub struct ValidityWindow(pub (Option<BlockId>, Option<BlockId>));

// After
pub struct ValidityWindow<T = BlockId>(pub (Option<T>, Option<T>>));

Generic Display

impl<T: Display> Display for ValidityWindow<T> { ... }

DateTime Conversion

// Convert block-id window to UTC datetime window
let dt_window = window.to_datetime_window(|block_id| get_block_timestamp_ms(block_id));
println!({}, dt_window);
// [2026-03-28 19:49:25 UTC, 2026-03-28 20:49:25 UTC)

Backward Compatible

  • Default type parameter is BlockId — existing code unchanged
  • #[serde(transparent)] ensures JSON serialization unchanged

Refs #421

- 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 logos-blockchain#421
Copy link
Copy Markdown
Collaborator

@schouhy schouhy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Left a comment

Comment on lines +322 to +337
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),
))
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this be used? Blocks have id and a timestamp already.

@ygd58
Copy link
Copy Markdown
Contributor Author

ygd58 commented Apr 24, 2026

Thanks for the question! The use case is displaying validity windows in a human-readable format for tooling (CLIs, explorers, logs).

Currently ValidityWindow can only show raw block IDs like [103311, 103400), which is not meaningful to users. With Display and datetime conversion, a CLI or explorer can show [2026-03-28 19:49 UTC, 2026-03-28 20:49 UTC) instead.

For example, logos-inspector (https://github.com/ygd58/logos-inspector) displays block info to developers. When showing a transaction's validity window, a human-readable time range is much more useful than raw block IDs.

That said, if this is out of scope for the core library, I am happy to implement the conversion in tooling layers instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants