Skip to content

Commit

Permalink
feat!: introduce block header into every smart contract execution con…
Browse files Browse the repository at this point in the history
…text

Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Oct 16, 2024
1 parent f36f81d commit 851d729
Show file tree
Hide file tree
Showing 27 changed files with 412 additions and 320 deletions.
2 changes: 1 addition & 1 deletion crates/iroha/tests/integration/events/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@ fn applied_block_must_be_available_in_kura() {
.as_ref()
.expect("Must be some")
.kura()
.get_block_by_height(event.header().height().try_into().unwrap())
.get_block(event.header().height().try_into().unwrap())
.expect("Block applied event was received earlier");
}
7 changes: 3 additions & 4 deletions crates/iroha_core/benches/blocks/apply_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ impl StateApplyBlocks {
instructions
.into_iter()
.map(|instructions| {
let mut state_block = state.block();
let block = create_block(
&mut state_block,
let (block, mut state_block) = create_block(
&state,
instructions,
alice_id.clone(),
alice_keypair.private_key(),
Expand Down Expand Up @@ -88,7 +87,7 @@ impl StateApplyBlocks {
}: &Self,
) -> Result<()> {
for (block, i) in blocks.iter().zip(1..) {
let mut state_block = state.block();
let mut state_block = state.block(block.as_ref().header());
let _events = state_block.apply(block, topology.as_ref().to_owned())?;
assert_eq!(state_block.height(), i);
state_block.commit();
Expand Down
52 changes: 38 additions & 14 deletions crates/iroha_core/benches/blocks/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,50 @@ use iroha_executor_data_model::permission::{
};

/// Create block
pub fn create_block(
state: &mut StateBlock<'_>,
pub fn create_block<'a>(
state: &'a State,
instructions: Vec<InstructionBox>,
account_id: AccountId,
account_private_key: &PrivateKey,
topology: &Topology,
peer_private_key: &PrivateKey,
) -> CommittedBlock {
) -> (CommittedBlock, StateBlock<'a>) {
let chain_id = ChainId::from("00000000-0000-0000-0000-000000000000");

let transaction = TransactionBuilder::new(chain_id.clone(), account_id)
.with_instructions(instructions)
.sign(account_private_key);
let (max_clock_drift, tx_limits) = {
let params = state.world.parameters();
let state_view = state.view();
let params = state_view.world.parameters();
(params.sumeragi().max_clock_drift(), params.transaction)
};

let block = BlockBuilder::new(vec![AcceptedTransaction::accept(
let unverified_block = BlockBuilder::new(vec![AcceptedTransaction::accept(
transaction,
&chain_id,
max_clock_drift,
tx_limits,
)
.unwrap()])
.chain(0, state)
.chain(0, &state.view())
.sign(peer_private_key)
.unpack(|_| {})
.categorize(state)
.unpack(|_| {})
.commit(topology)
.unpack(|_| {})
.unwrap();
.unpack(|_| {});

let mut state_block = state.block(unverified_block.header());
let block = unverified_block
.categorize(&mut state_block)
.unpack(|_| {})
.commit(topology)
.unpack(|_| {})
.unwrap();

// Verify that transactions are valid
for tx in block.as_ref().transactions() {
assert_eq!(tx.error, None);
}

block
(block, state_block)
}

pub fn populate_state(
Expand Down Expand Up @@ -201,7 +205,27 @@ pub fn build_state(rt: &tokio::runtime::Handle, account_id: &AccountId) -> State
);

{
let mut state_block = state.block();
let private_key = KeyPair::random().into_parts().1;
let chain_id = ChainId::from("00000000-0000-0000-0000-000000000000");
let transaction = TransactionBuilder::new(chain_id.clone(), account_id.clone())
.with_instructions(Vec::<InstructionBox>::new())
.sign(&private_key);
let (max_clock_drift, tx_limits) = {
let state_view = state.view();
let params = state_view.world.parameters();
(params.sumeragi().max_clock_drift(), params.transaction)
};
let unverified_block = BlockBuilder::new(vec![AcceptedTransaction::accept(
transaction,
&chain_id,
max_clock_drift,
tx_limits,
)
.unwrap()])
.chain(0, &state.view())
.sign(&private_key)
.unpack(|_| {});
let mut state_block = state.block(unverified_block.header());

state_block.world.parameters.transaction =
TransactionParameters::new(NonZeroU64::MAX, NonZeroU64::MAX);
Expand Down
5 changes: 2 additions & 3 deletions crates/iroha_core/benches/blocks/validate_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ impl StateValidateBlocks {
}: Self,
) {
for (instructions, i) in instructions.into_iter().zip(1..) {
let mut state_block = state.block();
let block = create_block(
&mut state_block,
let (block, mut state_block) = create_block(
&state,
instructions,
account_id.clone(),
&account_private_key,
Expand Down
12 changes: 6 additions & 6 deletions crates/iroha_core/benches/kura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ async fn measure_block_size_for_n_executors(n_executors: u32) {
let peer_id = PeerId::new("127.0.0.1:8080".parse().unwrap(), peer_public_key);
let topology = Topology::new(vec![peer_id]);
let mut block = {
let mut state_block = state.block();
BlockBuilder::new(vec![tx])
.chain(0, &mut state_block)
let unverified_block = BlockBuilder::new(vec![tx])
.chain(0, &state.view())
.sign(&peer_private_key)
.unpack(|_| {})
.categorize(&mut state_block)
.unpack(|_| {})
.unpack(|_| {});

let mut state_block = state.block(unverified_block.header());
unverified_block.categorize(&mut state_block).unpack(|_| {})
};

let key_pair = KeyPair::random();
Expand Down
56 changes: 43 additions & 13 deletions crates/iroha_core/benches/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ fn build_test_transaction(chain_id: ChainId) -> TransactionBuilder {
fn build_test_and_transient_state() -> State {
let kura = iroha_core::kura::Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::start_test();
let (account_id, key_pair) = gen_account_in(&*STARTER_DOMAIN);

let state = State::new(
{
let (account_id, _account_keypair) = gen_account_in(&*STARTER_DOMAIN);
let domain = Domain::new(STARTER_DOMAIN.clone()).build(&account_id);
let account = Account::new(account_id.clone()).build(&account_id);
World::with([domain], [account], [])
Expand All @@ -49,7 +49,26 @@ fn build_test_and_transient_state() -> State {
);

{
let mut state_block = state.block();
let chain_id = ChainId::from("00000000-0000-0000-0000-000000000000");
let transaction = TransactionBuilder::new(chain_id.clone(), account_id.clone())
.with_instructions(Vec::<InstructionBox>::new())
.sign(key_pair.private_key());
let (max_clock_drift, tx_limits) = {
let state_view = state.view();
let params = state_view.world.parameters();
(params.sumeragi().max_clock_drift(), params.transaction)
};
let unverified_block = BlockBuilder::new(vec![AcceptedTransaction::accept(
transaction,
&chain_id,
max_clock_drift,
tx_limits,
)
.unwrap()])
.chain(0, &state.view())
.sign(key_pair.private_key())
.unpack(|_| {});
let mut state_block = state.block(unverified_block.header());
let mut state_transaction = state_block.transaction();
let path_to_executor = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../defaults/executor.wasm");
Expand Down Expand Up @@ -117,12 +136,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 (account_id, key_pair) = gen_account_in(&*STARTER_DOMAIN);
let transaction = TransactionBuilder::new(chain_id.clone(), account_id.clone())
.with_instructions(Vec::<InstructionBox>::new())
.sign(key_pair.private_key());
let (max_clock_drift, tx_limits) = {
let state_view = state.world.view();
let params = state_view.parameters();
let state_view = state.view();
let params = state_view.world.parameters();
(params.sumeragi().max_clock_drift(), params.transaction)
};

let unverified_block = BlockBuilder::new(vec![AcceptedTransaction::accept(
transaction,
&chain_id,
max_clock_drift,
tx_limits,
)
.unwrap()])
.chain(0, &state.view())
.sign(key_pair.private_key())
.unpack(|_| {});
let transaction = AcceptedTransaction::accept(
build_test_transaction(chain_id.clone()).sign(STARTER_KEYPAIR.private_key()),
&chain_id,
Expand All @@ -132,13 +165,11 @@ fn validate_transaction(criterion: &mut Criterion) {
.expect("Failed to accept transaction.");
let mut success_count = 0;
let mut failure_count = 0;
let mut state_block = state.block(unverified_block.header());
let _ = criterion.bench_function("validate", move |b| {
b.iter(|| {
let mut state_block = state.block();
match state_block.validate(transaction.clone()) {
Ok(_) => success_count += 1,
Err(_) => failure_count += 1,
}
b.iter(|| match state_block.validate(transaction.clone()) {
Ok(_) => success_count += 1,
Err(_) => failure_count += 1,
});
});
println!("Success count: {success_count}, Failure count: {failure_count}");
Expand Down Expand Up @@ -166,8 +197,7 @@ fn sign_blocks(criterion: &mut Criterion) {

let mut count = 0;

let mut state_block = state.block();
let block = BlockBuilder::new(vec![transaction]).chain(0, &mut state_block);
let block = BlockBuilder::new(vec![transaction]).chain(0, &state.view());

let _ = criterion.bench_function("sign_block", |b| {
b.iter_batched(
Expand Down
Loading

0 comments on commit 851d729

Please sign in to comment.