Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix timestamp of genesis block #5098

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions crates/iroha_data_model/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@ impl SignedBlock {
genesis_transactions: Vec<SignedTransaction>,
genesis_private_key: &iroha_crypto::PrivateKey,
) -> SignedBlock {
use std::time::SystemTime;

use nonzero_ext::nonzero;

let transactions_hash = genesis_transactions
Expand All @@ -270,12 +268,7 @@ impl SignedBlock {
.collect::<MerkleTree<_>>()
.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,
Expand Down Expand Up @@ -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 {
Expand Down
Loading