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

Filtering and Identifying Transfer Events in Enclave #1355

Merged
merged 13 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3170,11 +3170,13 @@ dependencies = [
"itp-node-api",
"itp-ocall-api",
"itp-sgx-crypto",
"itp-sgx-runtime-primitives",
"itp-stf-executor",
"itp-stf-primitives",
"itp-test",
"itp-top-pool-author",
"itp-types",
"itp-utils",
"log 0.4.19",
"parity-scale-codec",
"sgx_tstd",
Expand Down
4 changes: 4 additions & 0 deletions core/parentchain/indirect-calls-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ itp-api-client-types = { path = "../../../core-primitives/node-api/api-client-ty
itp-node-api = { path = "../../../core-primitives/node-api", default-features = false }
itp-ocall-api = { path = "../../../core-primitives/ocall-api", default-features = false }
itp-sgx-crypto = { path = "../../../core-primitives/sgx/crypto", default-features = false }
itp-sgx-runtime-primitives = { path = "../../../core-primitives/sgx-runtime-primitives", default-features = false }
itp-stf-executor = { path = "../../../core-primitives/stf-executor", default-features = false }
itp-stf-primitives = { path = "../../../core-primitives/stf-primitives", default-features = false }
itp-top-pool-author = { path = "../../../core-primitives/top-pool-author", default-features = false }
itp-types = { path = "../../../core-primitives/types", default-features = false }
itp-utils = { path = "../../../core-primitives/utils", default-features = false }

# sgx enabled external libraries
futures_sgx = { package = "futures", git = "https://github.com/mesalock-linux/futures-rs-sgx", optional = true }
Expand Down Expand Up @@ -61,6 +63,8 @@ std = [
"itp-top-pool-author/std",
"itp-api-client-types/std",
"itp-types/std",
"itp-sgx-runtime-primitives/std",
"itp-utils/std",
"log/std",
#substrate
"binary-merkle-tree/std",
Expand Down
53 changes: 52 additions & 1 deletion core/parentchain/indirect-calls-executor/src/event_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use crate::error::Result;
use codec::{Decode, Encode};
use itp_api_client_types::{Events, StaticEvent};
use itp_sgx_runtime_primitives::types::{AccountId, Balance};
use itp_types::H256;
use std::vec::Vec;
use itp_utils::stringify::account_id_to_string;
use std::{format, string::String, vec::Vec};

#[derive(Encode, Decode, Debug)]
pub struct ExtrinsicSuccess;
Expand All @@ -43,8 +45,34 @@ pub enum ExtrinsicStatus {
Success,
Failed,
}

#[derive(Encode, Decode, Debug)]
pub struct BalanceTransfer {
pub from: AccountId,
pub to: AccountId,
pub amount: Balance,
}

impl StaticEvent for BalanceTransfer {
const PALLET: &'static str = "Balances";
const EVENT: &'static str = "Transfer";
}

impl BalanceTransfer {
pub fn print_string(&self) -> String {
format!(
"BalanceTransfer :: from: {}, to: {}, amount: {}",
account_id_to_string::<AccountId>(&self.from),
account_id_to_string::<AccountId>(&self.to),
self.amount
)
}
}
clangenb marked this conversation as resolved.
Show resolved Hide resolved

pub trait FilterEvents {
fn get_extrinsic_statuses(&self) -> Result<Vec<ExtrinsicStatus>>;

fn get_transfer_events(&self) -> Result<Vec<BalanceTransfer>>;
}

impl FilterEvents for Events<H256> {
Expand All @@ -68,6 +96,20 @@ impl FilterEvents for Events<H256> {
})
.collect())
}

fn get_transfer_events(&self) -> Result<Vec<BalanceTransfer>> {
Ok(self
.iter()
.flatten() // flatten filters out the nones
.filter_map(|ev| match ev.as_event::<BalanceTransfer>() {
Ok(maybe_event) => maybe_event,
Err(e) => {
log::error!("Could not decode event: {:?}", e);
None
},
})
.collect())
}
}

pub struct MockEvents;
Expand All @@ -76,4 +118,13 @@ impl FilterEvents for MockEvents {
fn get_extrinsic_statuses(&self) -> Result<Vec<ExtrinsicStatus>> {
Ok(Vec::from([ExtrinsicStatus::Success]))
}

fn get_transfer_events(&self) -> Result<Vec<BalanceTransfer>> {
let xsfer = BalanceTransfer {
clangenb marked this conversation as resolved.
Show resolved Hide resolved
to: [0u8; 32].into(),
from: [0u8; 32].into(),
amount: Balance::default(),
};
Ok(Vec::from([xsfer]))
}
}
8 changes: 8 additions & 0 deletions core/parentchain/indirect-calls-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ impl<
let xt_statuses = events.get_extrinsic_statuses()?;
trace!("xt_statuses:: {:?}", xt_statuses);

let filter_events = events.get_transfer_events();

if let Ok(events) = filter_events {
events
.iter()
.for_each(|event| info!("transfer_event :: {}", event.print_string()))
}

// This would be catastrophic but should never happen
if xt_statuses.len() != block.extrinsics().len() {
return Err(Error::Other("Extrinsic Status and Extrinsic count not equal".into()))
Expand Down
47 changes: 10 additions & 37 deletions enclave-runtime/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -487,20 +487,7 @@ dependencies = [
[[package]]
name = "common-primitives"
version = "0.1.0"
source = "git+https://github.com/integritee-network/pallets.git?branch=polkadot-v0.9.42#5c52182eb3a5156e8d9f69c10ca1441214ee6662"
dependencies = [
"derive_more",
"parity-scale-codec",
"scale-info",
"sp-core",
"sp-runtime",
"sp-std",
]

[[package]]
name = "common-primitives"
version = "0.1.0"
source = "git+https://github.com/integritee-network/pallets#5c52182eb3a5156e8d9f69c10ca1441214ee6662"
source = "git+https://github.com/integritee-network/pallets?branch=polkadot-v0.9.42#5c52182eb3a5156e8d9f69c10ca1441214ee6662"
dependencies = [
"derive_more",
"parity-scale-codec",
Expand Down Expand Up @@ -721,9 +708,9 @@ dependencies = [
[[package]]
name = "enclave-bridge-primitives"
version = "0.1.0"
source = "git+https://github.com/integritee-network/pallets.git?branch=polkadot-v0.9.42#5c52182eb3a5156e8d9f69c10ca1441214ee6662"
source = "git+https://github.com/integritee-network/pallets?branch=polkadot-v0.9.42#5c52182eb3a5156e8d9f69c10ca1441214ee6662"
dependencies = [
"common-primitives 0.1.0 (git+https://github.com/integritee-network/pallets.git?branch=polkadot-v0.9.42)",
"common-primitives",
"log",
"parity-scale-codec",
"scale-info",
Expand Down Expand Up @@ -812,7 +799,7 @@ dependencies = [
"sp-core",
"sp-runtime",
"sp-std",
"teerex-primitives 0.1.0 (git+https://github.com/integritee-network/pallets)",
"teerex-primitives",
"webpki",
]

Expand Down Expand Up @@ -1783,10 +1770,12 @@ dependencies = [
"itp-node-api",
"itp-ocall-api",
"itp-sgx-crypto",
"itp-sgx-runtime-primitives",
"itp-stf-executor",
"itp-stf-primitives",
"itp-top-pool-author",
"itp-types",
"itp-utils",
"log",
"parity-scale-codec",
"sgx_tstd",
Expand Down Expand Up @@ -2350,7 +2339,7 @@ dependencies = [
"sp-core",
"sp-runtime",
"sp-std",
"teerex-primitives 0.1.0 (git+https://github.com/integritee-network/pallets.git?branch=polkadot-v0.9.42)",
"teerex-primitives",
]

[[package]]
Expand Down Expand Up @@ -3012,7 +3001,7 @@ dependencies = [
[[package]]
name = "pallet-parentchain"
version = "0.9.0"
source = "git+https://github.com/integritee-network/pallets.git?branch=polkadot-v0.9.42#5c52182eb3a5156e8d9f69c10ca1441214ee6662"
source = "git+https://github.com/integritee-network/pallets?branch=polkadot-v0.9.42#5c52182eb3a5156e8d9f69c10ca1441214ee6662"
dependencies = [
"frame-support",
"frame-system",
Expand Down Expand Up @@ -4584,25 +4573,9 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "teerex-primitives"
version = "0.1.0"
source = "git+https://github.com/integritee-network/pallets.git?branch=polkadot-v0.9.42#5c52182eb3a5156e8d9f69c10ca1441214ee6662"
dependencies = [
"common-primitives 0.1.0 (git+https://github.com/integritee-network/pallets.git?branch=polkadot-v0.9.42)",
"derive_more",
"log",
"parity-scale-codec",
"scale-info",
"serde 1.0.164",
"sp-core",
"sp-runtime",
"sp-std",
]

[[package]]
name = "teerex-primitives"
version = "0.1.0"
source = "git+https://github.com/integritee-network/pallets#5c52182eb3a5156e8d9f69c10ca1441214ee6662"
source = "git+https://github.com/integritee-network/pallets?branch=polkadot-v0.9.42#5c52182eb3a5156e8d9f69c10ca1441214ee6662"
dependencies = [
"common-primitives 0.1.0 (git+https://github.com/integritee-network/pallets)",
"common-primitives",
"derive_more",
"log",
"parity-scale-codec",
Expand Down
6 changes: 6 additions & 0 deletions scripts/test_transfer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Test transfer from Alice to random account
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an intermediate solution, I shall accept the js-stuff, but I would like to stay consistent and only use our shell-rust integration tests. Also because what you implemented here is already possible with our current rust-cli.

I do think, however, that when we want to extend our integration testing suite, that a switch to js/ts is reasonable, but then with a holistic approach. :)


## Install
```bash
npm install
```
Loading
Loading