Skip to content

Commit

Permalink
[refactor]: Create separate method for creating new block and reverti…
Browse files Browse the repository at this point in the history
…ng latest

Signed-off-by: Shanin Roman <shanin1000@yandex.ru>
  • Loading branch information
Erigara committed Mar 20, 2024
1 parent cf9d4d2 commit 5508ecd
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ parity-scale-codec = { version = "3.6.5", default-features = false }
json5 = "0.4.1"
toml = "0.8.8"

storage = { git = "https://github.com/Erigara/storage.git", rev = "79689ae68573bfe4c4d80d902c726d84fec22257" }
storage = { git = "https://github.com/Erigara/storage.git", rev = "e0afe4b42810e9b3cf067cfbaa63a6ad7af6e2ba" }

[workspace.lints]
rustdoc.private_doc_tests = "deny"
Expand Down
4 changes: 2 additions & 2 deletions core/benches/blocks/apply_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl StateApplyBlocks {
instructions
.into_iter()
.map(|instructions| -> Result<_> {
let mut state_block = state.block(false);
let mut state_block = state.block();
let block = create_block(
&mut state_block,
instructions,
Expand All @@ -67,7 +67,7 @@ impl StateApplyBlocks {
/// If state height isn't updated after applying block
pub fn measure(Self { state, blocks }: &Self) -> Result<()> {
for (block, i) in blocks.iter().zip(1..) {
let mut state_block = state.block(false);
let mut state_block = state.block();
state_block.apply(block)?;
assert_eq!(state_block.height(), i);
state_block.commit();
Expand Down
2 changes: 1 addition & 1 deletion core/benches/blocks/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub fn build_state(
let state = State::new(World::with([domain], UniqueVec::new()), kura, query_handle);

{
let mut state_block = state.block(false);
let mut state_block = state.block();

state_block.config.transaction_limits = TransactionLimits::new(u64::MAX, u64::MAX);
state_block.config.executor_runtime.fuel_limit = u64::MAX;
Expand Down
2 changes: 1 addition & 1 deletion core/benches/blocks/validate_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl StateValidateBlocks {
}: Self,
) -> Result<()> {
for (instructions, i) in instructions.into_iter().zip(1..) {
let mut state_block = state.block(false);
let mut state_block = state.block();
let block = create_block(
&mut state_block,
instructions,
Expand Down
2 changes: 1 addition & 1 deletion core/benches/kura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn measure_block_size_for_n_executors(n_executors: u32) {
let state = State::new(World::new(), kura, query_handle);
let topology = Topology::new(UniqueVec::new());
let mut block = {
let mut state_block = state.block(false);
let mut state_block = state.block();
BlockBuilder::new(vec![tx], topology, Vec::new())
.chain(0, &mut state_block)
.sign(&KeyPair::random())
Expand Down
6 changes: 3 additions & 3 deletions core/benches/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn build_test_and_transient_state(keys: KeyPair) -> State {
);

{
let mut state_block = state.block(false);
let mut state_block = state.block();
let mut state_transaction = state_block.transaction();
let path_to_executor = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../configs/swarm/executor.wasm");
Expand Down Expand Up @@ -150,7 +150,7 @@ fn validate_transaction(criterion: &mut Criterion) {
let _ = criterion.bench_function("validate", move |b| {
let transaction_executor = TransactionExecutor::new(TRANSACTION_LIMITS);
b.iter(|| {
let mut state_block = state.block(false);
let mut state_block = state.block();
match transaction_executor.validate(transaction.clone(), &mut state_block) {
Ok(_) => success_count += 1,
Err(_) => failure_count += 1,
Expand Down Expand Up @@ -178,7 +178,7 @@ fn sign_blocks(criterion: &mut Criterion) {

let mut count = 0;

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

Expand Down
6 changes: 3 additions & 3 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ mod tests {
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(world, kura, query_handle);
let mut state_block = state.block(false);
let mut state_block = state.block();

// Creating an instruction
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
Expand Down Expand Up @@ -758,7 +758,7 @@ mod tests {
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(world, kura, query_handle);
let mut state_block = state.block(false);
let mut state_block = state.block();

// Creating an instruction
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
Expand Down Expand Up @@ -823,7 +823,7 @@ mod tests {
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(world, kura, query_handle);
let mut state_block = state.block(false);
let mut state_block = state.block();
let transaction_limits = &state_block.transaction_executor().transaction_limits;

let domain_id = DomainId::from_str("domain").expect("Valid");
Expand Down
8 changes: 4 additions & 4 deletions core/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ pub mod tests {
query_handle,
);
let tx = accepted_tx("alice@wonderland", &alice_key);
let mut state_block = state.block(false);
let mut state_block = state.block();
state_block.transactions.insert(tx.as_ref().hash(), 1);
state_block.commit();
let state_view = state.view();
Expand Down Expand Up @@ -615,7 +615,7 @@ pub mod tests {
let tx = accepted_tx("alice@wonderland", &alice_key);
let queue = Queue::from_config(config_factory());
queue.push(tx.clone(), &state.view()).unwrap();
let mut state_block = state.block(false);
let mut state_block = state.block();
state_block.transactions.insert(tx.as_ref().hash(), 1);
state_block.commit();
assert_eq!(
Expand Down Expand Up @@ -800,7 +800,7 @@ pub mod tests {
while start_time.elapsed() < run_for {
for tx in queue.collect_transactions_for_block(&state.view(), max_txs_in_block)
{
let mut state_block = state.block(false);
let mut state_block = state.block();
state_block.transactions.insert(tx.as_ref().hash(), 1);
state_block.commit();
}
Expand Down Expand Up @@ -936,7 +936,7 @@ pub mod tests {

let transactions = queue.collect_transactions_for_block(&state.view(), 10);
assert_eq!(transactions.len(), 2);
let mut state_block = state.block(false);
let mut state_block = state.block();
for transaction in transactions {
// Put transaction hashes into state as if they were in the blockchain
state_block
Expand Down
14 changes: 7 additions & 7 deletions core/src/smartcontracts/isi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ mod tests {
let account_id = AccountId::from_str("alice@wonderland")?;
let (public_key, _) = KeyPair::random().into_parts();
let asset_definition_id = AssetDefinitionId::from_str("rose#wonderland")?;
let mut state_block = state.block(false);
let mut state_block = state.block();
let mut state_transaction = state_block.transaction();
Register::domain(Domain::new(DomainId::from_str("wonderland")?))
.execute(&genesis_account_id, &mut state_transaction)?;
Expand All @@ -289,7 +289,7 @@ mod tests {
async fn asset_store() -> Result<()> {
let kura = Kura::blank_kura_for_testing();
let state = state_with_test_domains(&kura)?;
let mut staet_block = state.block(false);
let mut staet_block = state.block();
let mut state_transaction = staet_block.transaction();
let account_id = AccountId::from_str("alice@wonderland")?;
let asset_definition_id = AssetDefinitionId::from_str("rose#wonderland")?;
Expand Down Expand Up @@ -320,7 +320,7 @@ mod tests {
async fn account_metadata() -> Result<()> {
let kura = Kura::blank_kura_for_testing();
let state = state_with_test_domains(&kura)?;
let mut state_block = state.block(false);
let mut state_block = state.block();
let mut state_transaction = state_block.transaction();
let account_id = AccountId::from_str("alice@wonderland")?;
SetKeyValue::account(
Expand Down Expand Up @@ -352,7 +352,7 @@ mod tests {
async fn asset_definition_metadata() -> Result<()> {
let kura = Kura::blank_kura_for_testing();
let state = state_with_test_domains(&kura)?;
let mut state_block = state.block(false);
let mut state_block = state.block();
let mut state_transaction = state_block.transaction();
let definition_id = AssetDefinitionId::from_str("rose#wonderland")?;
let account_id = AccountId::from_str("alice@wonderland")?;
Expand Down Expand Up @@ -383,7 +383,7 @@ mod tests {
async fn domain_metadata() -> Result<()> {
let kura = Kura::blank_kura_for_testing();
let state = state_with_test_domains(&kura)?;
let mut state_block = state.block(false);
let mut state_block = state.block();
let mut state_transaction = state_block.transaction();
let domain_id = DomainId::from_str("wonderland")?;
let account_id = AccountId::from_str("alice@wonderland")?;
Expand Down Expand Up @@ -414,7 +414,7 @@ mod tests {
async fn executing_unregistered_trigger_should_return_error() -> Result<()> {
let kura = Kura::blank_kura_for_testing();
let state = state_with_test_domains(&kura)?;
let mut state_block = state.block(false);
let mut state_block = state.block();
let mut state_transaction = state_block.transaction();
let account_id = AccountId::from_str("alice@wonderland")?;
let trigger_id = TriggerId::from_str("test_trigger_id")?;
Expand All @@ -433,7 +433,7 @@ mod tests {
async fn unauthorized_trigger_execution_should_return_error() -> Result<()> {
let kura = Kura::blank_kura_for_testing();
let state = state_with_test_domains(&kura)?;
let mut state_block = state.block(false);
let mut state_block = state.block();
let mut state_transaction = state_block.transaction();
let account_id = AccountId::from_str("alice@wonderland")?;
let fake_account_id = AccountId::from_str("fake@wonderland")?;
Expand Down
4 changes: 2 additions & 2 deletions core/src/smartcontracts/isi/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ mod tests {
let query_handle = LiveQueryStore::test().start();
let state = State::new(world_with_test_domains(), kura.clone(), query_handle);
{
let mut state_block = state.block(false);
let mut state_block = state.block();
let limits = TransactionLimits {
max_instruction_number: 1,
max_wasm_size_bytes: 0,
Expand Down Expand Up @@ -453,7 +453,7 @@ mod tests {
let query_handle = LiveQueryStore::test().start();
let state = State::new(world_with_test_domains(), kura.clone(), query_handle);

let mut state_block = state.block(false);
let mut state_block = state.block();
let instructions: [InstructionBox; 0] = [];
let tx = TransactionBuilder::new(chain_id.clone(), ALICE_ID.clone())
.with_instructions(instructions)
Expand Down
12 changes: 6 additions & 6 deletions core/src/smartcontracts/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ mod tests {
);
let mut runtime = RuntimeBuilder::<state::SmartContract>::new().build()?;
runtime
.execute(&mut state.block(false).transaction(), authority, wat)
.execute(&mut state.block().transaction(), authority, wat)
.expect("Execution failed");

Ok(())
Expand Down Expand Up @@ -1852,7 +1852,7 @@ mod tests {

let mut runtime = RuntimeBuilder::<state::SmartContract>::new().build()?;
runtime
.execute(&mut state.block(false).transaction(), authority, wat)
.execute(&mut state.block().transaction(), authority, wat)
.expect("Execution failed");

Ok(())
Expand Down Expand Up @@ -1898,7 +1898,7 @@ mod tests {
);

let mut runtime = RuntimeBuilder::<state::SmartContract>::new().build()?;
let res = runtime.validate(&mut state.block(false).transaction(), authority, wat, 1);
let res = runtime.validate(&mut state.block().transaction(), authority, wat, 1);

if let Error::ExportFnCall(ExportFnCallError::Other(report)) =
res.expect_err("Execution should fail")
Expand Down Expand Up @@ -1950,7 +1950,7 @@ mod tests {
);

let mut runtime = RuntimeBuilder::<state::SmartContract>::new().build()?;
let res = runtime.validate(&mut state.block(false).transaction(), authority, wat, 1);
let res = runtime.validate(&mut state.block().transaction(), authority, wat, 1);

if let Error::ExportFnCall(ExportFnCallError::HostExecution(report)) =
res.expect_err("Execution should fail")
Expand Down Expand Up @@ -1994,7 +1994,7 @@ mod tests {
);

let mut runtime = RuntimeBuilder::<state::SmartContract>::new().build()?;
let res = runtime.validate(&mut state.block(false).transaction(), authority, wat, 1);
let res = runtime.validate(&mut state.block().transaction(), authority, wat, 1);

if let Error::ExportFnCall(ExportFnCallError::HostExecution(report)) =
res.expect_err("Execution should fail")
Expand Down Expand Up @@ -2036,7 +2036,7 @@ mod tests {

let mut runtime = RuntimeBuilder::<state::SmartContract>::new().build()?;
let err = runtime
.execute(&mut state.block(false).transaction(), authority, wat)
.execute(&mut state.block().transaction(), authority, wat)
.expect_err("Execution should fail");

assert!(matches!(
Expand Down
66 changes: 48 additions & 18 deletions core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,33 @@ impl World {
}

/// Create struct to apply block's changes
pub fn block(&self, rollback_latest_block: bool) -> WorldBlock {
pub fn block(&self) -> WorldBlock {
WorldBlock {
parameters: self.parameters.block(rollback_latest_block),
trusted_peers_ids: self.trusted_peers_ids.block(rollback_latest_block),
domains: self.domains.block(rollback_latest_block),
roles: self.roles.block(rollback_latest_block),
account_permission_tokens: self.account_permission_tokens.block(rollback_latest_block),
account_roles: self.account_roles.block(rollback_latest_block),
permission_token_schema: self.permission_token_schema.block(rollback_latest_block),
triggers: self.triggers.block(rollback_latest_block),
executor: self.executor.block(rollback_latest_block),
parameters: self.parameters.block(),
trusted_peers_ids: self.trusted_peers_ids.block(),
domains: self.domains.block(),
roles: self.roles.block(),
account_permission_tokens: self.account_permission_tokens.block(),
account_roles: self.account_roles.block(),
permission_token_schema: self.permission_token_schema.block(),
triggers: self.triggers.block(),
executor: self.executor.block(),
events_buffer: Vec::new(),
}
}

/// Create struct to apply block's changes while reverting changes made in the latest block
pub fn block_and_revert(&self) -> WorldBlock {
WorldBlock {
parameters: self.parameters.block_and_revert(),
trusted_peers_ids: self.trusted_peers_ids.block_and_revert(),
domains: self.domains.block_and_revert(),
roles: self.roles.block_and_revert(),
account_permission_tokens: self.account_permission_tokens.block_and_revert(),
account_roles: self.account_roles.block_and_revert(),
permission_token_schema: self.permission_token_schema.block_and_revert(),
triggers: self.triggers.block_and_revert(),
executor: self.executor.block_and_revert(),
events_buffer: Vec::new(),
}
}
Expand Down Expand Up @@ -931,13 +947,27 @@ impl State {
}
}

/// Create structure to execute block
pub fn block(&self, rollback_latest_block: bool) -> StateBlock<'_> {
/// Create structure to execute a block
pub fn block(&self) -> StateBlock<'_> {
StateBlock {
world: self.world.block(),
config: self.config.block(),
block_hashes: self.block_hashes.block(),
transactions: self.transactions.block(),
engine: &self.engine,
kura: &self.kura,
query_handle: &self.query_handle,
new_tx_amounts: &self.new_tx_amounts,
}
}

/// Create structure to execute a block while reverting changes made in the latest block
pub fn block_and_revert(&self) -> StateBlock<'_> {
StateBlock {
world: self.world.block(rollback_latest_block),
config: self.config.block(rollback_latest_block),
block_hashes: self.block_hashes.block(rollback_latest_block),
transactions: self.transactions.block(rollback_latest_block),
world: self.world.block_and_revert(),
config: self.config.block_and_revert(),
block_hashes: self.block_hashes.block_and_revert(),
transactions: self.transactions.block_and_revert(),
engine: &self.engine,
kura: &self.kura,
query_handle: &self.query_handle,
Expand Down Expand Up @@ -1734,7 +1764,7 @@ mod tests {
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(World::default(), kura, query_handle);
let mut state_block = state.block(false);
let mut state_block = state.block();

let mut block_hashes = vec![];
for i in 1..=BLOCK_CNT {
Expand Down Expand Up @@ -1762,7 +1792,7 @@ mod tests {
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(World::default(), kura.clone(), query_handle);
let mut state_block = state.block(false);
let mut state_block = state.block();

for i in 1..=BLOCK_CNT {
let mut block = block.clone();
Expand Down

0 comments on commit 5508ecd

Please sign in to comment.