feat: make ValidityWindow generic with Display and datetime conversion#424
feat: make ValidityWindow generic with Display and datetime conversion#424ygd58 wants to merge 2 commits intologos-blockchain:mainfrom
Conversation
- 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
schouhy
left a comment
There was a problem hiding this comment.
Thanks for the PR. Left a comment
| 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), | ||
| )) | ||
| } |
There was a problem hiding this comment.
How would this be used? Blocks have id and a timestamp already.
|
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. |
Summary
Refs #421.
Changes
Made
ValidityWindowgeneric over its bound typeT:Generic Display
DateTime Conversion
Backward Compatible
BlockId— existing code unchanged#[serde(transparent)]ensures JSON serialization unchangedRefs #421