From 789f58ff63ecdba035f74a01d2b403fc07e33ca2 Mon Sep 17 00:00:00 2001 From: Dmitry Murzin Date: Tue, 24 Sep 2024 19:21:29 +0300 Subject: [PATCH] fix: Fix timestamp of genesis block Signed-off-by: Dmitry Murzin --- crates/iroha_data_model/src/block.rs | 30 ++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/crates/iroha_data_model/src/block.rs b/crates/iroha_data_model/src/block.rs index 41819ce3dda..d7cc1003822 100644 --- a/crates/iroha_data_model/src/block.rs +++ b/crates/iroha_data_model/src/block.rs @@ -260,8 +260,6 @@ impl SignedBlock { genesis_transactions: Vec, genesis_private_key: &iroha_crypto::PrivateKey, ) -> SignedBlock { - use std::time::SystemTime; - use nonzero_ext::nonzero; let transactions_hash = genesis_transactions @@ -270,12 +268,7 @@ impl SignedBlock { .collect::>() .hash() .expect("Tree is not empty"); - let creation_time_ms = SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap() - .as_millis() - .try_into() - .expect("INTERNAL BUG: Unix timestamp exceedes u64::MAX"); + let creation_time_ms = Self::get_genesis_block_creation_time(&genesis_transactions); let header = BlockHeader { height: nonzero!(1_u64), prev_block_hash: None, @@ -304,6 +297,27 @@ impl SignedBlock { } .into() } + + #[cfg(feature = "std")] + fn get_genesis_block_creation_time(genesis_transactions: &[SignedTransaction]) -> u64 { + use std::time::SystemTime; + + let latest_txn_time = genesis_transactions + .iter() + .map(|tx| tx.creation_time()) + .max() + .expect("INTERNAL BUG: Block empty"); + let now = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap(); + now + // We have invariant that "transaction creation time" < "block creation time" + // See `BlockPayloadCandidate::validate_header` + .max(latest_txn_time + Duration::from_millis(1)) + .as_millis() + .try_into() + .expect("INTERNAL BUG: Unix timestamp exceedes u64::MAX") + } } impl BlockSignature {