Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] #182: 'Only success' call added #196

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ use std::str::FromStr;
use crate::data_model::asset::{PyAsset, PyAssetDefinition, PyAssetDefinitionId, PyAssetId};
use crate::data_model::block::*;
use crate::data_model::crypto::*;
use iroha_crypto::{Hash, HashOf};
use crate::data_model::PyMirror;
use crate::{data_model::account::PyAccountId, isi::PyInstruction};
use iroha_data_model::account::AccountId;
use iroha_data_model::prelude::DomainId;
use iroha_data_model::prelude::*;
use iroha_data_model::ChainId;

use iroha_data_model::events::pipeline::{TransactionEventFilter, BlockEventFilter};

#[allow(unsafe_code)]
const DEFAULT_TRANSACTION_TIME_TO_LIVE_MS: NonZeroU64 =
unsafe { NonZeroU64::new_unchecked(100_000) };
Expand Down Expand Up @@ -82,7 +85,63 @@ impl Client {
.map(|hash| hash.to_string())
.map_err(|e| PyRuntimeError::new_err(format!("Error submitting instruction: {}", e)))
}

fn submit_executable_only_success(&self, py: Python<'_>, isi: PyObject) -> PyResult<String> {
let isi = if let Ok(isi) = isi.extract::<PyInstruction>(py) {
vec![isi.0]
} else if let Ok(isi) = isi.extract::<Vec<PyInstruction>>(py) {
isi.into_iter().map(|isi| isi.0).collect()
} else {
return Err(PyValueError::new_err(""));
};


let transaction = self.client.build_transaction(isi, UnlimitedMetadata::new());
let hash = transaction.hash();
self.client.submit_transaction(&transaction)?;

let filters = vec![
TransactionEventFilter::default().for_hash(hash).into(),
PipelineEventFilterBox::from(
BlockEventFilter::default().for_status(BlockStatus::Applied),
),
];

let mut block_height = 0;
for event in self.client.listen_for_events(filters)? {
let event = event?;
if let EventBox::Pipeline(event) = event {
match event {
PipelineEventBox::Transaction(event) => {
if event.status == TransactionStatus::Approved && event.block_height.is_some() {
block_height = event.block_height.unwrap();
} else {
return Err(PyValueError::new_err("Transaction was not approved."));
}
}
PipelineEventBox::Block(event) => {
if event.header().height == block_height {
return Ok(hash.to_string());
}
}
}
}
}
Err(PyValueError::new_err("No events left."))
}

fn query_transaction_with_hash(&self, hash: [u8; Hash::LENGTH]) -> PyResult<bool> {
let query = iroha_data_model::query::prelude::FindTransactionByHash {
hash: HashOf::from_untyped_unchecked(Hash::prehashed(hash)).into(),
};

let ret = self
.client
.request(query);
println!("{:?}", ret);
Ok(ret.map_err(|e| PyRuntimeError::new_err(format!("{e:?}"))).is_ok())
}

fn query_all_domains(&self) -> PyResult<Vec<String>> {
let query = iroha_data_model::query::prelude::FindAllDomains {};

Expand Down
15 changes: 5 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,32 @@ def GIVEN_registered_asset_definition(GIVEN_new_asset_definition_id):
"""Fixture to provide a registered asset definition."""
with allure.step(
f'GIVEN registered asset definition "{GIVEN_new_asset_definition_id}"'):
(client.submit_executable(
(client.submit_executable_only_success(
[iroha.Instruction
.register_asset_definition(
GIVEN_new_asset_definition_id,
iroha.AssetValueType.numeric_fractional(0))]))
time.sleep(2)
return GIVEN_new_asset_definition_id

@pytest.fixture()
def GIVEN_registered_domain(GIVEN_new_domain_id):
"""Fixture to provide a registered domain in Iroha"""
with allure.step(f'GIVEN registered domain name "{GIVEN_new_domain_id}"'):
(client.submit_executable(
(client.submit_executable_only_success(
[iroha.Instruction
.register_domain(GIVEN_new_domain_id)]))
time.sleep(2)
return GIVEN_new_domain_id

@pytest.fixture()
def GIVEN_registered_account(GIVEN_new_account_id):
"""Fixture to provide a registered account"""
with allure.step(
f'GIVEN client registered the account "{GIVEN_new_account_id}"'):
(client.submit_executable(
(client.submit_executable_only_success(
[iroha.Instruction
.register_account(
GIVEN_new_account_id,
generate_public_key(seed="abcd1122"))]))
time.sleep(2)
return GIVEN_new_account_id

@pytest.fixture()
Expand All @@ -87,12 +84,11 @@ def GIVEN_registered_domain_with_registered_accounts(
"""Fixture to provide a domain with accounts"""
with allure.step(
f'GIVEN client registers the account "{GIVEN_new_account_id}"'):
(client.submit_executable(
(client.submit_executable_only_success(
[iroha.Instruction
.register_account(
GIVEN_new_account_id,
generate_public_key(seed="abcd1122"))]))
time.sleep(2)
return GIVEN_registered_domain

@pytest.fixture()
Expand All @@ -105,11 +101,10 @@ def GIVEN_registered_account_with_minted_assets(
GIVEN_registered_asset_definition + '#' + GIVEN_registered_account)
with allure.step(
f'WHEN client mints an asset "{asset}"'):
(client.submit_executable(
(client.submit_executable_only_success(
[iroha.Instruction
.mint_asset(
5,
asset,
iroha.AssetValueType.numeric_fractional(0))]))
time.sleep(3)
return GIVEN_registered_account
3 changes: 1 addition & 2 deletions tests/transaction/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ def test_register_account(
GIVEN_new_account_id):
with allure.step(
f'WHEN client registers the account "{GIVEN_new_account_id}"'):
(client.submit_executable(
(client.submit_executable_only_success(
[iroha.Instruction
.register_account(
GIVEN_new_account_id,
generate_public_key(seed="abcd1122"))]))
time.sleep(3)
with allure.step(
f'THEN Iroha should have the "{GIVEN_new_account_id}" account'):
assert GIVEN_new_account_id in client.query_all_accounts()
6 changes: 2 additions & 4 deletions tests/transaction/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ def test_register_asset_definition(
GIVEN_new_asset_definition_id):
with allure.step(
f'WHEN client registers a new asset definition id "{GIVEN_new_asset_definition_id}"'):
(client.submit_executable(
(client.submit_executable_only_success(
[iroha.Instruction
.register_asset_definition(
GIVEN_new_asset_definition_id,
iroha.AssetValueType.numeric_fractional(0))]))
time.sleep(3)
with allure.step(
f'THEN Iroha should have the "{GIVEN_new_asset_definition_id}" account'):
assert GIVEN_new_asset_definition_id in client.query_all_asset_definitions()
Expand All @@ -35,12 +34,11 @@ def test_mint_asset(
asset = (lambda s: re.sub(r'(\b\w+\b)(?=.*\1)', '', s))(GIVEN_registered_asset_definition + '#' + GIVEN_registered_account)
with allure.step(
f'WHEN client mints an asset "{asset}"'):
(client.submit_executable(
(client.submit_executable_only_success(
[iroha.Instruction
.mint_asset(
5,
asset)]))
time.sleep(3)
with allure.step(
f'THEN Iroha should have the new asset "{asset}"'):
assert asset in client.query_all_assets()
3 changes: 1 addition & 2 deletions tests/transaction/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ def story_account_registers_domain():
def test_register_domain(
GIVEN_new_domain_id):
with allure.step(f'WHEN client registers the domain name "{GIVEN_new_domain_id}"'):
(client.submit_executable(
(client.submit_executable_only_success(
[iroha.Instruction
.register_domain(GIVEN_new_domain_id)]))
time.sleep(3)
with allure.step(f'THEN Iroha should have the domain name "{GIVEN_new_domain_id}"'):
assert GIVEN_new_domain_id in client.query_all_domains()
Loading