Skip to content

Commit

Permalink
feat: bounded discrete time (#4928)
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic authored Aug 27, 2024
1 parent 078e310 commit 3e9cc6f
Show file tree
Hide file tree
Showing 37 changed files with 570 additions and 472 deletions.
4 changes: 2 additions & 2 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,10 @@ mod tests {

let genesis_key_pair = KeyPair::random();
let genesis = GenesisBuilder::default().build_and_sign(
dummy_executor(),
ChainId::from("00000000-0000-0000-0000-000000000000"),
&genesis_key_pair,
dummy_executor(),
vec![],
&genesis_key_pair,
);

let mut config = config_factory(genesis_key_pair.public_key());
Expand Down
4 changes: 2 additions & 2 deletions client/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn query_requests(criterion: &mut Criterion) {
.domain("wonderland".parse().expect("Valid"))
.account(get_key_pair(test_network::Signatory::Alice).into_parts().0)
.finish_domain()
.build_and_sign(executor, chain_id, &genesis_key_pair, topology);
.build_and_sign(chain_id, executor, topology, &genesis_key_pair);

let builder = PeerBuilder::new()
.with_config(configuration)
Expand Down Expand Up @@ -135,7 +135,7 @@ fn instruction_submits(criterion: &mut Criterion) {
.domain("wonderland".parse().expect("Valid"))
.account(configuration.common.key_pair.public_key().clone())
.finish_domain()
.build_and_sign(executor, chain_id, &genesis_key_pair, topology);
.build_and_sign(chain_id, executor, topology, &genesis_key_pair);
let builder = PeerBuilder::new()
.with_config(configuration)
.with_genesis(genesis);
Expand Down
2 changes: 1 addition & 1 deletion client/examples/million_accounts_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn generate_genesis(

let executor = construct_executor("../wasm_samples/default_executor")
.expect("Failed to construct executor");
builder.build_and_sign(executor, chain_id, genesis_key_pair, topology)
builder.build_and_sign(chain_id, executor, topology, genesis_key_pair)
}

fn main_genesis() {
Expand Down
2 changes: 1 addition & 1 deletion client/examples/register_1000_triggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn generate_genesis(

let executor = construct_executor("../wasm_samples/default_executor")
.expect("Failed to construct executor");
Ok(builder.build_and_sign(executor, chain_id, genesis_key_pair, topology))
Ok(builder.build_and_sign(chain_id, executor, topology, genesis_key_pair))
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/triggers/time_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn curr_time() -> core::time::Duration {

SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Failed to get the current system time")
.unwrap()
}

/// Macro to abort compilation, if `e` isn't `true`
Expand Down
2 changes: 0 additions & 2 deletions config/src/parameters/actual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pub struct Queue {
pub capacity: NonZeroUsize,
pub capacity_per_user: NonZeroUsize,
pub transaction_time_to_live: Duration,
pub future_threshold: Duration,
}

#[allow(missing_docs)]
Expand All @@ -109,7 +108,6 @@ impl Default for Queue {
fn default() -> Self {
Self {
transaction_time_to_live: defaults::queue::TRANSACTION_TIME_TO_LIVE,
future_threshold: defaults::queue::FUTURE_THRESHOLD,
capacity: defaults::queue::CAPACITY,
capacity_per_user: defaults::queue::CAPACITY_PER_USER,
}
Expand Down
5 changes: 2 additions & 3 deletions config/src/parameters/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod queue {
pub const CAPACITY_PER_USER: NonZeroUsize = nonzero!(2_usize.pow(16));
// 24 hours
pub const TRANSACTION_TIME_TO_LIVE: Duration = Duration::from_secs(24 * 60 * 60);
pub const FUTURE_THRESHOLD: Duration = Duration::from_secs(1);
}

pub mod kura {
Expand All @@ -40,8 +39,8 @@ pub mod snapshot {
use super::*;

pub const STORE_DIR: &str = "./storage/snapshot";
// The default frequency of making snapshots is 1 minute, need to be adjusted for larger world state view size
pub const CREATE_EVERY: Duration = Duration::from_secs(60);
// 10 mins
pub const CREATE_EVERY: Duration = Duration::from_secs(10 * 60);
}

pub mod torii {
Expand Down
5 changes: 0 additions & 5 deletions config/src/parameters/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@ pub struct Queue {
/// The transaction will be dropped after this time if it is still in the queue.
#[config(default = "defaults::queue::TRANSACTION_TIME_TO_LIVE.into()")]
pub transaction_time_to_live_ms: DurationMs,
/// The threshold to determine if a transaction has been tampered to have a future timestamp.
#[config(default = "defaults::queue::FUTURE_THRESHOLD.into()")]
pub future_threshold_ms: DurationMs,
}

impl Queue {
Expand All @@ -335,13 +332,11 @@ impl Queue {
capacity,
capacity_per_user,
transaction_time_to_live_ms: transaction_time_to_live,
future_threshold_ms: future_threshold,
} = self;
actual::Queue {
capacity,
capacity_per_user,
transaction_time_to_live: transaction_time_to_live.0,
future_threshold: future_threshold.0,
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions config/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,11 @@ fn minimal_config_snapshot() {
capacity: 65536,
capacity_per_user: 65536,
transaction_time_to_live: 86400s,
future_threshold: 1s,
},
snapshot: Snapshot {
mode: ReadWrite,
create_every_ms: DurationMs(
60s,
600s,
),
store_dir: WithOrigin {
value: "./storage/snapshot",
Expand Down
1 change: 0 additions & 1 deletion config/tests/fixtures/full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ format = "compact"
capacity = 65_536
capacity_per_user = 65_536
transaction_time_to_live_ms = 100
future_threshold_ms = 50

[snapshot]
mode = "read_write"
Expand Down
8 changes: 6 additions & 2 deletions core/benches/blocks/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ pub fn create_block(
let transaction = TransactionBuilder::new(chain_id.clone(), account_id)
.with_instructions(instructions)
.sign(account_private_key);
let limits = state.transaction_executor().limits;
let (max_clock_drift, tx_limits) = {
let params = state.world.parameters();
(params.sumeragi().max_clock_drift(), params.transaction)
};

let block = BlockBuilder::new(vec![AcceptedTransaction::accept(
transaction,
&chain_id,
limits,
max_clock_drift,
tx_limits,
)
.unwrap()])
.chain(0, state)
Expand Down
33 changes: 16 additions & 17 deletions core/benches/kura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@ use iroha_core::{
sumeragi::network_topology::Topology,
};
use iroha_crypto::KeyPair;
use iroha_data_model::{parameter::TransactionParameters, prelude::*};
use nonzero_ext::nonzero;
use iroha_data_model::prelude::*;
use test_samples::gen_account_in;
use tokio::{fs, runtime::Runtime};

async fn measure_block_size_for_n_executors(n_executors: u32) {
let dir = tempfile::tempdir().expect("Could not create tempfile.");
let cfg = Config {
init_mode: iroha_config::kura::InitMode::Strict,
debug_output_new_blocks: false,
store_dir: WithOrigin::inline(dir.path().to_path_buf()),
};
let chain_id = ChainId::from("00000000-0000-0000-0000-000000000000");
let (kura, _) = iroha_core::kura::Kura::new(&cfg).unwrap();
let _thread_handle = iroha_core::kura::Kura::start(kura.clone());
let query_handle = LiveQueryStore::test().start();
let state = State::new(World::new(), kura, query_handle);

let (alice_id, alice_keypair) = gen_account_in("test");
let (bob_id, _bob_keypair) = gen_account_in("test");
Expand All @@ -30,23 +39,13 @@ async fn measure_block_size_for_n_executors(n_executors: u32) {
let tx = TransactionBuilder::new(chain_id.clone(), alice_id.clone())
.with_instructions([transfer])
.sign(alice_keypair.private_key());
let txn_limits = TransactionParameters {
max_instructions: nonzero!(4096_u64),
smart_contract_size: nonzero!(1_u64),
let (max_clock_drift, tx_limits) = {
let state_view = state.world.view();
let params = state_view.parameters();
(params.sumeragi().max_clock_drift(), params.transaction)
};
let tx = AcceptedTransaction::accept(tx, &chain_id, txn_limits)
let tx = AcceptedTransaction::accept(tx, &chain_id, max_clock_drift, tx_limits)
.expect("Failed to accept Transaction.");
let dir = tempfile::tempdir().expect("Could not create tempfile.");
let cfg = Config {
init_mode: iroha_config::kura::InitMode::Strict,
debug_output_new_blocks: false,
store_dir: WithOrigin::inline(dir.path().to_path_buf()),
};
let (kura, _) = iroha_core::kura::Kura::new(&cfg).unwrap();
let _thread_handle = iroha_core::kura::Kura::start(kura.clone());

let query_handle = LiveQueryStore::test().start();
let state = State::new(World::new(), kura, query_handle);
let (peer_public_key, peer_private_key) = KeyPair::random().into_parts();
let peer_id = PeerId::new("127.0.0.1:8080".parse().unwrap(), peer_public_key);
let topology = Topology::new(vec![peer_id]);
Expand Down
48 changes: 32 additions & 16 deletions core/benches/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ use iroha_core::{
query::store::LiveQueryStore,
smartcontracts::{isi::Registrable as _, Execute},
state::{State, World},
tx::TransactionExecutor,
};
use iroha_data_model::{
account::AccountId, isi::InstructionBox, parameter::TransactionParameters, prelude::*,
transaction::TransactionBuilder,
account::AccountId, isi::InstructionBox, prelude::*, transaction::TransactionBuilder,
};
use nonzero_ext::nonzero;
use once_cell::sync::Lazy;
use test_samples::gen_account_in;

Expand All @@ -22,9 +19,6 @@ static STARTER_KEYPAIR: Lazy<KeyPair> = Lazy::new(KeyPair::random);
static STARTER_ID: Lazy<AccountId> =
Lazy::new(|| AccountId::new(STARTER_DOMAIN.clone(), STARTER_KEYPAIR.public_key().clone()));

const TRANSACTION_LIMITS: TransactionParameters =
TransactionParameters::new(nonzero!(4096_u64), nonzero!(1_u64));

fn build_test_transaction(chain_id: ChainId) -> TransactionBuilder {
let domain_id: DomainId = "domain".parse().unwrap();
let create_domain = Register::domain(Domain::new(domain_id.clone()));
Expand Down Expand Up @@ -75,13 +69,24 @@ fn build_test_and_transient_state() -> State {

fn accept_transaction(criterion: &mut Criterion) {
let chain_id = ChainId::from("00000000-0000-0000-0000-000000000000");
let state = build_test_and_transient_state();
let (max_clock_drift, tx_limits) = {
let state_view = state.world.view();
let params = state_view.parameters();
(params.sumeragi().max_clock_drift(), params.transaction)
};

let transaction = build_test_transaction(chain_id.clone()).sign(STARTER_KEYPAIR.private_key());
let mut success_count = 0;
let mut failures_count = 0;
let _ = criterion.bench_function("accept", |b| {
b.iter(|| {
match AcceptedTransaction::accept(transaction.clone(), &chain_id, TRANSACTION_LIMITS) {
match AcceptedTransaction::accept(
transaction.clone(),
&chain_id,
max_clock_drift,
tx_limits,
) {
Ok(_) => success_count += 1,
Err(_) => failures_count += 1,
}
Expand Down Expand Up @@ -111,21 +116,26 @@ fn sign_transaction(criterion: &mut Criterion) {

fn validate_transaction(criterion: &mut Criterion) {
let chain_id = ChainId::from("00000000-0000-0000-0000-000000000000");
let state = build_test_and_transient_state();
let (max_clock_drift, tx_limits) = {
let state_view = state.world.view();
let params = state_view.parameters();
(params.sumeragi().max_clock_drift(), params.transaction)
};

let transaction = AcceptedTransaction::accept(
build_test_transaction(chain_id.clone()).sign(STARTER_KEYPAIR.private_key()),
&chain_id,
TRANSACTION_LIMITS,
max_clock_drift,
tx_limits,
)
.expect("Failed to accept transaction.");
let mut success_count = 0;
let mut failure_count = 0;
let state = build_test_and_transient_state();
let _ = criterion.bench_function("validate", move |b| {
let transaction_executor = TransactionExecutor::new(TRANSACTION_LIMITS);
b.iter(|| {
let mut state_block = state.block();
match transaction_executor.validate(transaction.clone(), &mut state_block) {
match state_block.validate(transaction.clone()) {
Ok(_) => success_count += 1,
Err(_) => failure_count += 1,
}
Expand All @@ -136,16 +146,22 @@ fn validate_transaction(criterion: &mut Criterion) {

fn sign_blocks(criterion: &mut Criterion) {
let chain_id = ChainId::from("00000000-0000-0000-0000-000000000000");
let kura = iroha_core::kura::Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(World::new(), kura, query_handle);
let (max_clock_drift, tx_limits) = {
let state_view = state.world.view();
let params = state_view.parameters();
(params.sumeragi().max_clock_drift(), params.transaction)
};

let transaction = AcceptedTransaction::accept(
build_test_transaction(chain_id.clone()).sign(STARTER_KEYPAIR.private_key()),
&chain_id,
TRANSACTION_LIMITS,
max_clock_drift,
tx_limits,
)
.expect("Failed to accept transaction.");
let kura = iroha_core::kura::Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(World::new(), kura, query_handle);
let (_, peer_private_key) = KeyPair::random().into_parts();

let mut count = 0;
Expand Down
Loading

0 comments on commit 3e9cc6f

Please sign in to comment.