Skip to content

Commit

Permalink
fix: replace Duration with u64 in schema
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Jul 12, 2024
1 parent 137d766 commit e8260e3
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use core::str::FromStr as _;
use executor_custom_data_model::mint_rose_args::MintRoseArgs;
use iroha_trigger::{debug::dbg_panic, prelude::*};
use lol_alloc::{FreeListAllocator, LockedAllocator};
use serde::{Deserialize, Serialize};

#[global_allocator]
static ALLOC: LockedAllocator<FreeListAllocator> = LockedAllocator::new(FreeListAllocator::new());
Expand Down
Binary file modified configs/swarm/executor.wasm
Binary file not shown.
33 changes: 18 additions & 15 deletions core/src/smartcontracts/isi/triggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub mod specialized;
/// - TODO: authorities.
/// - TODO: authority permissions.
pub mod isi {
use std::time::Duration;

use iroha_data_model::{
events::EventFilter,
isi::error::{InvalidParameterError, RepetitionError},
Expand Down Expand Up @@ -42,11 +40,15 @@ pub mod isi {
}

let last_block_estimation = state_transaction.latest_block().map(|block| {
block.header().creation_time()
+ Duration::from_millis(block.header().consensus_estimation_ms)
block.header().creation_time() + block.header().consensus_estimation()
});

let engine = state_transaction.engine.clone(); // Cloning engine is cheap
let genesis_creation_time_ms = state_transaction
.world()
.genesis_creation_time_ms()
.expect("INTERNAL BUG: genesis creation time not set");

let triggers = &mut state_transaction.world.triggers;
let trigger_id = new_trigger.id().clone();
let success = match &new_trigger.action.filter {
Expand All @@ -65,20 +67,21 @@ pub mod isi {
TriggeringEventFilterBox::Time(time_filter) => {
if let ExecutionTime::Schedule(schedule) = time_filter.0 {
match last_block_estimation {
// We're in genesis
// Gensis block
None => {
return Err(Error::InvalidParameter(
InvalidParameterError::TimeTriggerInThePast,
));
if schedule.start_ms < genesis_creation_time_ms {
return Err(Error::InvalidParameter(
InvalidParameterError::TimeTriggerInThePast,
));
}
}
Some(latest_block_estimation)
if schedule.start < latest_block_estimation =>
{
return Err(Error::InvalidParameter(
InvalidParameterError::TimeTriggerInThePast,
));
Some(latest_block_estimation) => {
if schedule.start() < latest_block_estimation {
return Err(Error::InvalidParameter(
InvalidParameterError::TimeTriggerInThePast,
));
}
}
Some(_) => (),
}
}
triggers.add_time_trigger(
Expand Down
33 changes: 25 additions & 8 deletions core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub struct WorldBlock<'world> {
pub(crate) executor_data_model: CellBlock<'world, ExecutorDataModel>,
/// Events produced during execution of block
events_buffer: Vec<EventBox>,

pub(crate) genesis_creation_time_ms: Option<u64>,
}

/// Struct for single transaction's aggregated changes
Expand Down Expand Up @@ -155,6 +157,8 @@ pub struct WorldTransaction<'block, 'world> {
pub(crate) executor_data_model: CellTransaction<'block, 'world, ExecutorDataModel>,
/// Events produced during execution of a transaction
events_buffer: TransactionEventBuffer<'block>,

pub(crate) genesis_creation_time_ms: Option<u64>,
}

/// Wrapper for event's buffer to apply transaction rollback
Expand Down Expand Up @@ -193,6 +197,8 @@ pub struct WorldView<'world> {
pub(crate) executor: CellView<'world, Executor>,
/// Executor-defined data model
pub(crate) executor_data_model: CellView<'world, ExecutorDataModel>,

pub(crate) genesis_creation_time_ms: Option<u64>,
}

/// Current state of the blockchain
Expand Down Expand Up @@ -356,6 +362,8 @@ impl World {
executor: self.executor.block(),
executor_data_model: self.executor_data_model.block(),
events_buffer: Vec::new(),

genesis_creation_time_ms: None,
}
}

Expand All @@ -376,6 +384,8 @@ impl World {
executor: self.executor.block_and_revert(),
executor_data_model: self.executor_data_model.block_and_revert(),
events_buffer: Vec::new(),

genesis_creation_time_ms: None,
}
}

Expand All @@ -395,6 +405,8 @@ impl World {
triggers: self.triggers.view(),
executor: self.executor.view(),
executor_data_model: self.executor_data_model.view(),

genesis_creation_time_ms: None,
}
}
}
Expand All @@ -416,6 +428,8 @@ pub trait WorldReadOnly {
fn executor(&self) -> &Executor;
fn executor_data_model(&self) -> &ExecutorDataModel;

fn genesis_creation_time_ms(&self) -> Option<u64>;

// Domain-related methods

/// Get `Domain` without an ability to modify it.
Expand Down Expand Up @@ -698,6 +712,10 @@ macro_rules! impl_world_ro {
fn executor_data_model(&self) -> &ExecutorDataModel {
&self.executor_data_model
}

fn genesis_creation_time_ms(&self) -> Option<u64> {
self.genesis_creation_time_ms
}
}
)*};
}
Expand All @@ -710,6 +728,8 @@ impl<'world> WorldBlock<'world> {
/// Create struct to apply transaction's changes
pub fn trasaction(&mut self) -> WorldTransaction<'_, 'world> {
WorldTransaction {
genesis_creation_time_ms: self.genesis_creation_time_ms(),

parameters: self.parameters.transaction(),
trusted_peers_ids: self.trusted_peers_ids.transaction(),
domains: self.domains.transaction(),
Expand Down Expand Up @@ -1367,16 +1387,13 @@ impl<'state> StateBlock<'state> {
let prev_interval = self.latest_block().map(|latest_block| {
let header = &latest_block.as_ref().header();

TimeInterval {
since: header.creation_time(),
length: header.consensus_estimation(),
}
TimeInterval::new(header.creation_time(), header.consensus_estimation())
});

let interval = TimeInterval {
since: block.as_ref().header().creation_time(),
length: block.as_ref().header().consensus_estimation(),
};
let interval = TimeInterval::new(
block.as_ref().header().creation_time(),
block.as_ref().header().consensus_estimation(),
);

TimeEvent {
prev_interval,
Expand Down
3 changes: 3 additions & 0 deletions core/src/sumeragi/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ impl Sumeragi {
};

let mut state_block = state.block();
state_block.world.genesis_creation_time_ms =
Some(block.header().creation_time_ms);
let block = match ValidBlock::validate(
block,
&self.topology,
Expand Down Expand Up @@ -289,6 +291,7 @@ impl Sumeragi {
}

let mut state_block = state.block();
state_block.world.genesis_creation_time_ms = Some(genesis.0.header().creation_time_ms);
let genesis = ValidBlock::validate(
genesis.0,
&self.topology,
Expand Down
4 changes: 4 additions & 0 deletions core/src/sumeragi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ impl SumeragiHandle {
// NOTE: topology need to be updated up to block's view_change_index
topology.nth_rotation(block.header().view_change_index as usize);

if block.header().is_genesis() {
state_block.world.genesis_creation_time_ms = Some(block.header().creation_time_ms);
}

let block = ValidBlock::validate(
block.clone(),
topology,
Expand Down
2 changes: 1 addition & 1 deletion data_model/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl SignedBlock {
.expect("Tree is not empty");
let first_transaction = &genesis_transactions[0];
let creation_time_ms = u64::try_from(first_transaction.creation_time().as_millis())
.expect("Must fit since Duration was created from u64 in creation_time()");
.expect("INTERNAL BUG: Unix timestamp exceedes u64::MAX");
let header = BlockHeader {
height: nonzero!(1_u64),
prev_block_hash: None,
Expand Down
Loading

0 comments on commit e8260e3

Please sign in to comment.