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 (#5151)

Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic authored Oct 18, 2024
1 parent 4976419 commit 07939d1
Show file tree
Hide file tree
Showing 37 changed files with 804 additions and 394 deletions.
2 changes: 1 addition & 1 deletion crates/iroha/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ iroha_logger = { workspace = true }
iroha_telemetry = { workspace = true }
iroha_torii_const = { workspace = true }
iroha_version = { workspace = true }
iroha_test_samples = { workspace = true }

attohttpc = { version = "0.28.0", default-features = false }
eyre = { workspace = true }
Expand All @@ -83,6 +82,7 @@ toml = { workspace = true }
nonzero_ext = { workspace = true }

[dev-dependencies]
iroha_test_samples = { workspace = true }
iroha_genesis = { workspace = true }
iroha_test_network = { workspace = true }
executor_custom_data_model = { version = "=2.0.0-rc.1.0", path = "../../wasm_samples/executor_custom_data_model" }
Expand Down
3 changes: 1 addition & 2 deletions crates/iroha/examples/tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
//! <https://hyperledger.github.io/iroha-2-docs/guide/rust.html#_2-configuring-iroha-2>
use eyre::{Error, WrapErr};
use iroha::config::Config;
use iroha_primitives::numeric::Numeric;
use iroha::{config::Config, data_model::prelude::Numeric};
// #region rust_config_crates
// #endregion rust_config_crates

Expand Down
25 changes: 12 additions & 13 deletions crates/iroha/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@ use std::{collections::HashMap, fmt::Debug};

use eyre::{eyre, Context, Result};
use http::StatusCode;
use iroha_crypto::KeyPair;
use iroha_data_model::{
account::AccountId,
query::{
builder::{QueryBuilder, QueryExecutor},
parameters::ForwardCursor,
predicate::HasPredicateBox,
QueryOutput, QueryOutputBatchBox, QueryRequest, QueryResponse, QueryWithParams,
SingularQuery, SingularQueryBox, SingularQueryOutputBox,
},
ValidationFail,
};
use iroha_torii_const::uri as torii_uri;
use parity_scale_codec::{DecodeAll, Encode};
use url::Url;

use crate::{
client::{join_torii_url, Client, QueryResult, ResponseReport},
data_model::query::Query,
crypto::KeyPair,
data_model::{
account::AccountId,
query::{
builder::{QueryBuilder, QueryExecutor},
parameters::ForwardCursor,
predicate::HasPredicateBox,
Query, QueryOutput, QueryOutputBatchBox, QueryRequest, QueryResponse, QueryWithParams,
SingularQuery, SingularQueryBox, SingularQueryOutputBox,
},
ValidationFail,
},
http::{Method as HttpMethod, RequestBuilder},
http_default::DefaultRequestBuilder,
};
Expand Down
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 @@ -77,5 +77,5 @@ async fn test_with_instruction_and_status(
#[test]
#[ignore = "TODO: implement with the help of Kura Inspector, "]
fn applied_block_must_be_available_in_kura() {
unimplemented!();
unimplemented!("Take a look at previous implementation and restore this test");
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use eyre::Result;
use futures_util::StreamExt;
use iroha::{
client::{self},
data_model::prelude::*,
};
use iroha_data_model::{
events::pipeline::{BlockEventFilter, TransactionEventFilter},
parameter::BlockParameter,
data_model::{
events::pipeline::{BlockEventFilter, TransactionEventFilter},
parameter::BlockParameter,
prelude::*,
},
};
use iroha_test_network::*;
use iroha_test_samples::gen_account_in;
Expand Down
3 changes: 1 addition & 2 deletions crates/iroha/tests/integration/set_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use eyre::Result;
use iroha::{
client,
data_model::{
parameter::{Parameter, Parameters},
parameter::{BlockParameter, Parameter, Parameters},
prelude::*,
},
};
use iroha_data_model::parameter::BlockParameter;
use iroha_test_network::*;
use nonzero_ext::nonzero;

Expand Down
1 change: 0 additions & 1 deletion crates/iroha/tests/integration/transfer_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use iroha_executor_data_model::permission::{
domain::CanUnregisterDomain,
trigger::CanUnregisterTrigger,
};
use iroha_primitives::json::Json;
use iroha_test_network::*;
use iroha_test_samples::{gen_account_in, ALICE_ID, BOB_ID, SAMPLE_GENESIS_ACCOUNT_ID};

Expand Down
11 changes: 7 additions & 4 deletions crates/iroha/tests/integration/triggers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use assert_matches::assert_matches;
use iroha::{client, client::Client};
use iroha_data_model::{
asset::{AssetId, AssetValue},
prelude::{Numeric, QueryBuilderExt},
use iroha::{
client,
client::Client,
data_model::{
asset::{AssetId, AssetValue},
prelude::{Numeric, QueryBuilderExt},
},
};

mod by_call_trigger;
Expand Down
3 changes: 2 additions & 1 deletion crates/iroha/tests/integration/triggers/time_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ fn curr_time() -> Duration {

#[test]
fn mint_asset_after_3_sec() -> Result<()> {
const GAP: Duration = Duration::from_secs(3);

let (network, _rt) = NetworkBuilder::new()
.with_default_pipeline_time()
.start_blocking()?;
Expand All @@ -43,7 +45,6 @@ fn mint_asset_after_3_sec() -> Result<()> {
})?;

let start_time = curr_time();
const GAP: Duration = Duration::from_secs(3);
assert!(
GAP < network.pipeline_time(),
"Schedule should be in the future but within block estimation"
Expand Down
9 changes: 4 additions & 5 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,10 +87,10 @@ 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();
assert_eq!(state.view().height(), i);
}

Ok(())
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().latest_block().as_deref())
.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().latest_block().as_deref())
.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
Loading

0 comments on commit 07939d1

Please sign in to comment.