-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a test using a query filter in a smart contract
Signed-off-by: Nikita Strygin <dcnick3@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
168 additions
and
46 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
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,73 @@ | ||
use std::str::FromStr as _; | ||
|
||
use eyre::Result; | ||
use iroha_client::{ | ||
client::ClientQueryError, | ||
data_model::{ | ||
prelude::*, | ||
query::{cursor::ForwardCursor, error::QueryExecutionFail}, | ||
}, | ||
}; | ||
use test_network::*; | ||
|
||
#[test] | ||
fn live_query_is_dropped_after_smart_contract_end() -> Result<()> { | ||
let (_rt, _peer, client) = <PeerBuilder>::new().with_port(11_140).start_with_runtime(); | ||
wait_for_genesis_committed(&[client.clone()], 0); | ||
|
||
let wasm = iroha_wasm_builder::Builder::new( | ||
"tests/integration/smartcontracts/query_assets_and_save_cursor", | ||
) | ||
.show_output() | ||
.build()? | ||
.optimize()? | ||
.into_bytes()?; | ||
|
||
let transaction = client.build_transaction( | ||
WasmSmartContract::from_compiled(wasm), | ||
UnlimitedMetadata::default(), | ||
); | ||
client.submit_transaction_blocking(&transaction)?; | ||
|
||
let metadata_value = client.request(FindAccountKeyValueByIdAndKey::new( | ||
client.account_id.clone(), | ||
Name::from_str("cursor").unwrap(), | ||
))?; | ||
let cursor: String = metadata_value.try_into()?; | ||
let asset_cursor = serde_json::from_str::<ForwardCursor>(&cursor)?; | ||
|
||
let err = client | ||
.request_with_cursor::<Vec<Asset>>(asset_cursor) | ||
.expect_err("Request with cursor from smart contract should fail"); | ||
|
||
assert!(matches!( | ||
err, | ||
ClientQueryError::Validation(ValidationFail::QueryFailed( | ||
QueryExecutionFail::UnknownCursor | ||
)) | ||
)); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn smart_contract_can_filter_queries() -> Result<()> { | ||
let (_rt, _peer, client) = <PeerBuilder>::new().with_port(11_260).start_with_runtime(); | ||
wait_for_genesis_committed(&[client.clone()], 0); | ||
|
||
let wasm = iroha_wasm_builder::Builder::new( | ||
"tests/integration/smartcontracts/smart_contract_can_filter_queries", | ||
) | ||
.show_output() | ||
.build()? | ||
.optimize()? | ||
.into_bytes()?; | ||
|
||
let transaction = client.build_transaction( | ||
WasmSmartContract::from_compiled(wasm), | ||
UnlimitedMetadata::default(), | ||
); | ||
client.submit_transaction_blocking(&transaction)?; | ||
|
||
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
21 changes: 21 additions & 0 deletions
21
client/tests/integration/smartcontracts/smart_contract_can_filter_queries/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,21 @@ | ||
[package] | ||
name = "smart_contract_can_filter_queries" | ||
|
||
edition.workspace = true | ||
version.workspace = true | ||
authors.workspace = true | ||
|
||
license.workspace = true | ||
|
||
[lib] | ||
crate-type = ['cdylib'] | ||
|
||
[dependencies] | ||
iroha_smart_contract.workspace = true | ||
|
||
panic-halt.workspace = true | ||
lol_alloc.workspace = true | ||
getrandom.workspace = true | ||
parity-scale-codec.workspace = true | ||
nonzero_ext.workspace = true | ||
serde_json = { workspace = true, default-features = false } |
69 changes: 69 additions & 0 deletions
69
client/tests/integration/smartcontracts/smart_contract_can_filter_queries/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,69 @@ | ||
//! Smart contract which executes [`FindAllAssets`] and saves cursor to the owner's metadata. | ||
#![no_std] | ||
|
||
#[cfg(not(test))] | ||
extern crate panic_halt; | ||
|
||
extern crate alloc; | ||
|
||
use alloc::{collections::BTreeSet, string::ToString, vec::Vec}; | ||
|
||
use iroha_smart_contract::{ | ||
data_model::query::predicate::{string::StringPredicate, value::QueryOutputPredicate}, | ||
prelude::*, | ||
QueryOutputCursor, | ||
}; | ||
use lol_alloc::{FreeListAllocator, LockedAllocator}; | ||
|
||
#[global_allocator] | ||
static ALLOC: LockedAllocator<FreeListAllocator> = LockedAllocator::new(FreeListAllocator::new()); | ||
|
||
getrandom::register_custom_getrandom!(iroha_smart_contract::stub_getrandom); | ||
|
||
/// Create two asset definitions in the looking_glass domain, query all asset definitions, filter them to only be in the looking_glass domain, check that the results are consistent | ||
#[iroha_smart_contract::main] | ||
fn main(_owner: AccountId) { | ||
// create the "looking_glass" domain | ||
Register::domain(Domain::new("looking_glass".parse().unwrap())) | ||
.execute() | ||
.dbg_unwrap(); | ||
|
||
// create two asset definitions inside the `looking_glass` domain | ||
let time_id: AssetDefinitionId = "time#looking_glass".parse().dbg_unwrap(); | ||
let space_id: AssetDefinitionId = "space#looking_glass".parse().dbg_unwrap(); | ||
|
||
Register::asset_definition(AssetDefinition::new( | ||
time_id.clone(), | ||
AssetValueType::Numeric(NumericSpec::default()), | ||
)) | ||
.execute() | ||
.dbg_unwrap(); | ||
|
||
Register::asset_definition(AssetDefinition::new( | ||
space_id.clone(), | ||
AssetValueType::Numeric(NumericSpec::default()), | ||
)) | ||
.execute() | ||
.dbg_unwrap(); | ||
|
||
// genesis registers some more asset definitions, but we apply a filter to find only the ones from the `looking_glass` domain | ||
let cursor: QueryOutputCursor<Vec<AssetDefinition>> = FindAllAssetsDefinitions | ||
.filter(QueryOutputPredicate::Identifiable( | ||
StringPredicate::EndsWith("#looking_glass".to_string()), | ||
)) | ||
.execute() | ||
.dbg_unwrap(); | ||
|
||
let mut asset_definition_ids = BTreeSet::new(); | ||
|
||
for asset_definition in cursor { | ||
let asset_definition = asset_definition.dbg_unwrap(); | ||
asset_definition_ids.insert(asset_definition.id().clone()); | ||
} | ||
|
||
assert_eq!( | ||
asset_definition_ids, | ||
[time_id, space_id].into_iter().collect() | ||
); | ||
} |