From 7b46f4607ad700548acfbfa20ecf3ea51ffd7c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Ver=C5=A1i=C4=87?= Date: Mon, 13 Nov 2023 17:15:20 +0300 Subject: [PATCH] [refactor]: update executor source code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marin Veršić --- .../.cargo/config.toml | 0 .../test/{validator => executor}/Cargo.toml | 0 .../src/test/{validator => executor}/build.sh | 0 modules/client/src/test/executor/src/lib.rs | 61 ++++++ modules/client/src/test/validator/src/lib.rs | 188 ------------------ 5 files changed, 61 insertions(+), 188 deletions(-) rename modules/client/src/test/{validator => executor}/.cargo/config.toml (100%) rename modules/client/src/test/{validator => executor}/Cargo.toml (100%) rename modules/client/src/test/{validator => executor}/build.sh (100%) create mode 100644 modules/client/src/test/executor/src/lib.rs delete mode 100644 modules/client/src/test/validator/src/lib.rs diff --git a/modules/client/src/test/validator/.cargo/config.toml b/modules/client/src/test/executor/.cargo/config.toml similarity index 100% rename from modules/client/src/test/validator/.cargo/config.toml rename to modules/client/src/test/executor/.cargo/config.toml diff --git a/modules/client/src/test/validator/Cargo.toml b/modules/client/src/test/executor/Cargo.toml similarity index 100% rename from modules/client/src/test/validator/Cargo.toml rename to modules/client/src/test/executor/Cargo.toml diff --git a/modules/client/src/test/validator/build.sh b/modules/client/src/test/executor/build.sh similarity index 100% rename from modules/client/src/test/validator/build.sh rename to modules/client/src/test/executor/build.sh diff --git a/modules/client/src/test/executor/src/lib.rs b/modules/client/src/test/executor/src/lib.rs new file mode 100644 index 000000000..30999850c --- /dev/null +++ b/modules/client/src/test/executor/src/lib.rs @@ -0,0 +1,61 @@ +//! Runtime Executor for client tests +#![no_std] + +extern crate alloc; +#[cfg(not(test))] +extern crate panic_halt; + +use alloc::borrow::ToOwned as _; + +use iroha_executor::{default::default_permission_token_schema, prelude::*, smart_contract}; +use lol_alloc::{FreeListAllocator, LockedAllocator}; + +#[global_allocator] +static ALLOC: LockedAllocator = LockedAllocator::new(FreeListAllocator::new()); + +/// Executor that replaces some of [`Validate`]'s methods with sensible defaults +/// +/// # Warning +/// +/// The defaults are not guaranteed to be stable. +#[derive(Clone, Constructor, Debug, ValidateEntrypoints, ExpressionEvaluator, Validate, Visit)] +pub struct Executor { + verdict: Result, + block_height: u64, + host: smart_contract::Host, +} + +impl Executor { + fn ensure_genesis(block_height: u64) -> MigrationResult { + if block_height != 0 { + return Err("Default Executor is intended to be used only in genesis. \ + Write your own executor if you need to upgrade executor on existing chain." + .to_owned()); + } + + Ok(()) + } +} + +/// Migrate previous executor to the current version. +/// Called by Iroha once just before upgrading executor. +/// +/// # Errors +/// +/// Concrete errors are specific to the implementation. +/// +/// If `migrate()` entrypoint fails then the whole `Upgrade` instruction +/// will be denied and previous executor will stay unchanged. +#[entrypoint] +pub fn migrate(block_height: u64) -> MigrationResult { + Executor::ensure_genesis(block_height)?; + + let schema = default_permission_token_schema(); + let (token_ids, schema_str) = schema.serialize(); + iroha_executor::set_permission_token_schema( + &iroha_executor::data_model::permission::PermissionTokenSchema::new(token_ids, schema_str), + ); + + Ok(()) +} + diff --git a/modules/client/src/test/validator/src/lib.rs b/modules/client/src/test/validator/src/lib.rs deleted file mode 100644 index 48f69a9a8..000000000 --- a/modules/client/src/test/validator/src/lib.rs +++ /dev/null @@ -1,188 +0,0 @@ -//! Runtime Executor for client tests -#![no_std] - -extern crate alloc; -#[cfg(not(test))] -extern crate panic_halt; - -use alloc::borrow::ToOwned as _; - -use iroha_executor::{ - data_model::evaluate::ExpressionEvaluator, default::default_permission_token_schema, - prelude::*, smart_contract, -}; -use lol_alloc::{FreeListAllocator, LockedAllocator}; - -#[global_allocator] -static ALLOC: LockedAllocator = LockedAllocator::new(FreeListAllocator::new()); - -/// Executor that replaces some of [`Validate`]'s methods with sensible defaults -/// -/// # Warning -/// -/// The defaults are not guaranteed to be stable. -#[derive(Debug, Clone)] -pub struct Executor { - verdict: Result, - block_height: u64, - host: smart_contract::Host, -} - -impl Executor { - /// Construct [`Self`] - pub fn new(block_height: u64) -> Self { - Self { - verdict: Ok(()), - block_height, - host: smart_contract::Host, - } - } -} - -macro_rules! defaults { - ( $($executor:ident $(<$param:ident $(: $bound:path)?>)?($operation:ty)),+ $(,)? ) => { $( - fn $executor $(<$param $(: $bound)?>)?(&mut self, authority: &AccountId, operation: $operation) { - iroha_executor::default::$executor(self, authority, operation) - } )+ - }; -} - -impl Visit for Executor { - defaults! { - visit_unsupported(T), - - visit_transaction(&SignedTransaction), - visit_instruction(&InstructionExpr), - visit_expression(&EvaluatesTo), - visit_sequence(&SequenceExpr), - visit_if(&ConditionalExpr), - visit_pair(&PairExpr), - - // Peer validation - visit_unregister_peer(Unregister), - - // Domain validation - visit_unregister_domain(Unregister), - visit_set_domain_key_value(SetKeyValue), - visit_remove_domain_key_value(RemoveKeyValue), - - // Account validation - visit_unregister_account(Unregister), - visit_mint_account_public_key(Mint), - visit_burn_account_public_key(Burn), - visit_mint_account_signature_check_condition(Mint), - visit_set_account_key_value(SetKeyValue), - visit_remove_account_key_value(RemoveKeyValue), - - // Asset validation - visit_register_asset(Register), - visit_unregister_asset(Unregister), - visit_mint_asset(Mint), - visit_burn_asset(Burn), - visit_transfer_asset(Transfer), - visit_set_asset_key_value(SetKeyValue), - visit_remove_asset_key_value(RemoveKeyValue), - - // AssetDefinition validation - visit_unregister_asset_definition(Unregister), - visit_transfer_asset_definition(Transfer), - visit_set_asset_definition_key_value(SetKeyValue), - visit_remove_asset_definition_key_value(RemoveKeyValue), - - // Permission validation - visit_grant_account_permission(Grant), - visit_revoke_account_permission(Revoke), - - // Role validation - visit_register_role(Register), - visit_unregister_role(Unregister), - visit_grant_account_role(Grant), - visit_revoke_account_role(Revoke), - - // Trigger validation - visit_unregister_trigger(Unregister>), - visit_mint_trigger_repetitions(Mint>), - visit_burn_trigger_repetitions(Burn>), - visit_execute_trigger(ExecuteTrigger), - - // Parameter validation - visit_set_parameter(SetParameter), - visit_new_parameter(NewParameter), - - // Upgrade validation - visit_upgrade_executor(Upgrade), - } -} - -impl Validate for Executor { - fn verdict(&self) -> &Result { - &self.verdict - } - - fn block_height(&self) -> u64 { - self.block_height - } - - fn deny(&mut self, reason: ValidationFail) { - self.verdict = Err(reason); - } -} - -impl ExpressionEvaluator for Executor { - fn evaluate( - &self, - expression: &E, - ) -> core::result::Result { - self.host.evaluate(expression) - } -} - -/// Migrate previous executor to the current version. -/// Called by Iroha once just before upgrading executor. -/// -/// # Errors -/// -/// Concrete errors are specific to the implementation. -/// -/// If `migrate()` entrypoint fails then the whole `Upgrade` instruction -/// will be denied and previous executor will stay unchanged. -#[entrypoint] -pub fn migrate(_: u64) -> MigrationResult { - let schema = default_permission_token_schema(); - let (token_ids, schema_str) = schema.serialize(); - - iroha_executor::set_permission_token_schema( - &iroha_executor::data_model::permission::PermissionTokenSchema::new(token_ids, schema_str), - ); - - Ok(()) -} - -#[entrypoint] -pub fn validate_transaction( - authority: AccountId, - transaction: SignedTransaction, - block_height: u64, -) -> Result { - let mut executor = Executor::new(block_height); - executor.visit_transaction(&authority, &transaction); - executor.verdict -} - -#[entrypoint] -pub fn validate_instruction( - authority: AccountId, - instruction: InstructionExpr, - block_height: u64, -) -> Result { - let mut executor = Executor::new(block_height); - executor.visit_instruction(&authority, &instruction); - executor.verdict -} - -#[entrypoint] -pub fn validate_query(authority: AccountId, query: QueryBox, block_height: u64) -> Result { - let mut executor = Executor::new(block_height); - executor.visit_query(&authority, &query); - executor.verdict -}