forked from hyperledger/iroha
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dmitry Murzin <diralik@yandex.ru>
- Loading branch information
Showing
7 changed files
with
200 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
client/tests/integration/smartcontracts/executor_add_parameter/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "executor_add_parameter" | ||
|
||
edition.workspace = true | ||
version.workspace = true | ||
authors.workspace = true | ||
|
||
license.workspace = true | ||
|
||
[lib] | ||
crate-type = ['cdylib'] | ||
|
||
[dependencies] | ||
iroha_executor.workspace = true | ||
iroha_schema.workspace = true | ||
|
||
parity-scale-codec.workspace = true | ||
anyhow.workspace = true | ||
serde_json.workspace = true | ||
serde.workspace = true | ||
|
||
panic-halt.workspace = true | ||
lol_alloc.workspace = true | ||
getrandom.workspace = true |
83 changes: 83 additions & 0 deletions
83
client/tests/integration/smartcontracts/executor_add_parameter/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! Runtime Executor which adds new parameter `max_accounts_per_domain`. | ||
//! And checks that parameter when registering new account. | ||
|
||
#![no_std] | ||
|
||
extern crate alloc; | ||
#[cfg(not(test))] | ||
extern crate panic_halt; | ||
|
||
use lol_alloc::{FreeListAllocator, LockedAllocator}; | ||
|
||
use iroha_executor::data_model::parameter::ParameterValueBox; | ||
use iroha_executor::debug::{dbg_panic, DebugExpectExt}; | ||
use iroha_executor::prelude::*; | ||
|
||
#[global_allocator] | ||
static ALLOC: LockedAllocator<FreeListAllocator> = LockedAllocator::new(FreeListAllocator::new()); | ||
|
||
getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom); | ||
|
||
#[derive(Constructor, ValidateEntrypoints, Validate, Visit)] | ||
#[visit(custom(visit_register_account))] | ||
struct Executor { | ||
verdict: Result, | ||
block_height: u64, | ||
} | ||
|
||
const MAX_ACCOUNTS_PER_DOMAIN: &str = "max_accounts_per_domain"; | ||
|
||
fn visit_register_account(executor: &mut Executor, _authority: &AccountId, isi: &Register<Account>) { | ||
if executor.block_height() == 0 { | ||
execute!(executor, isi); | ||
} | ||
|
||
let max_accounts_per_domain = get_max_accounts_per_domain(); | ||
|
||
let domain_id = isi.object().id().domain_id(); | ||
let number_accounts = FindAccountsByDomainId::new(domain_id.clone()) | ||
.execute() | ||
.dbg_expect("Failed to execute FindAccountsByDomainId") | ||
.into_iter() | ||
.count(); | ||
|
||
if number_accounts >= max_accounts_per_domain { | ||
deny!(executor, "Exceed max accounts per domain limit"); | ||
} | ||
|
||
execute!(executor, isi); | ||
} | ||
|
||
fn get_max_accounts_per_domain() -> usize { | ||
let parameter_id: ParameterId = MAX_ACCOUNTS_PER_DOMAIN | ||
.parse() | ||
.dbg_expect("Valid parameter name"); | ||
let parameters = FindAllParameters::new() | ||
.execute() | ||
.dbg_expect("Failed to execute FindAllParameters") | ||
.into_iter() | ||
.collect::<Result<Vec<_>, _>>() | ||
.dbg_expect("Failed to execute FindAllParameters"); | ||
let parameter = parameters | ||
.into_iter() | ||
.find(|parameter| parameter.id() == ¶meter_id) | ||
.dbg_expect("Can't find parameter"); | ||
let ParameterValueBox::Numeric(value) = parameter.val() else { | ||
dbg_panic("Unexpected parameter value"); | ||
}; | ||
let value: u64 = value | ||
.clone() | ||
.try_into() | ||
.dbg_expect("Can't cast parameter value to usize"); | ||
value as usize | ||
} | ||
|
||
#[entrypoint] | ||
pub fn migrate(_block_height: u64) -> MigrationResult { | ||
let parameter_id: ParameterId = MAX_ACCOUNTS_PER_DOMAIN | ||
.parse() | ||
.dbg_expect("Valid parameter name"); | ||
let parameter = Parameter::new(parameter_id, Numeric::new(1, 0).into()); | ||
iroha_executor::add_parameter(¶meter)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters