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

node-data: add timestamp to Ratification payload #1246

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion consensus/src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use node_data::ledger::*;
use node_data::message::payload::QuorumType;
use std::fmt;
use std::fmt::Display;
use std::time::Duration;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use node_data::message::Payload;

Expand Down Expand Up @@ -176,3 +176,10 @@ impl QuorumMsgSender {
false
}
}

pub fn get_current_timestamp() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|n| n.as_secs())
.expect("This is heavy.")
}
16 changes: 4 additions & 12 deletions consensus/src/proposal/block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::commons::RoundUpdate;
use crate::operations::CallParams;
use crate::commons::{get_current_timestamp, RoundUpdate};
use crate::operations::{CallParams, Operations};
use node_data::ledger::{to_str, Block, Certificate, IterationsInfo, Seed};

use crate::config;
use crate::merkle::merkle_root;
use crate::operations::Operations;

use dusk_bytes::Serializable;
use node_data::ledger;
use node_data::message::payload::Candidate;
use node_data::message::{Header, Message, Topics};
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use tracing::{debug, info};

Expand Down Expand Up @@ -107,7 +106,7 @@ impl<T: Operations> Generator<T> {
let blk_header = ledger::Header {
version: 0,
height: ru.round,
timestamp: self.get_timestamp(),
timestamp: get_current_timestamp(),
gas_limit: config::DEFAULT_BLOCK_GAS_LIMIT,
prev_block_hash,
seed,
Expand All @@ -133,11 +132,4 @@ impl<T: Operations> Generator<T> {

Ok(Block::new(blk_header, txs).expect("block should be valid"))
}

fn get_timestamp(&self) -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|n| n.as_secs())
.expect("This is heavy.")
}
}
5 changes: 4 additions & 1 deletion consensus/src/ratification/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::commons::{ConsensusError, Database, RoundUpdate};
use crate::commons::{
get_current_timestamp, ConsensusError, Database, RoundUpdate,
};
use crate::execution_ctx::ExecutionCtx;
use crate::operations::Operations;
use std::marker::PhantomData;
Expand Down Expand Up @@ -51,6 +53,7 @@ impl<T: Operations + 'static, DB: Database> RatificationStep<T, DB> {
Ratification {
signature,
validation_result: result.clone(),
timestamp: get_current_timestamp(),
},
);

Expand Down
4 changes: 3 additions & 1 deletion node-data/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ impl Serializable for Label {
impl Serializable for Ratification {
fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
w.write_all(&self.signature)?;
w.write_all(&self.timestamp.to_le_bytes())?;
self.validation_result.write(w)?;

Ok(())
Expand All @@ -274,11 +275,12 @@ impl Serializable for Ratification {
Self: Sized,
{
let signature = Self::read_bytes(r)?;

let timestamp = Self::read_u64_le(r)?;
let validation_result = ValidationResult::read(r)?;

Ok(Ratification {
signature,
timestamp,
validation_result,
})
}
Expand Down
1 change: 1 addition & 0 deletions node-data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ pub mod payload {
)]
pub struct Ratification {
pub signature: [u8; 48],
pub timestamp: u64,
pub validation_result: ValidationResult,
}

Expand Down