Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ab37149
enable target_a and _b syncing for sidechain mode and prepare new dem…
brenzi Nov 23, 2023
2ae982d
add shard vault proxy account on target A/B as well. derivation is de…
brenzi Nov 23, 2023
d62b26d
prepare event listener on target A and fix demoscript
brenzi Nov 23, 2023
d9bff04
remove old enclave_bridge unshield call from trusted_call
brenzi Nov 23, 2023
e9c408c
abstracting parentchain effect opaque_calls. stf can send to any pare…
brenzi Nov 23, 2023
ff686b6
sending stf extrinsics to all parentchains now
brenzi Nov 24, 2023
ec5918e
WIP: triggering block import of target_a and b upon sidechain on_slot
brenzi Nov 24, 2023
42b10e7
refactored aura to trigger target_a and target_b import
brenzi Nov 25, 2023
2f12d4b
shielding from target_a worked
brenzi Nov 25, 2023
a62dc20
demo passes first and second rungit add -u!
brenzi Nov 25, 2023
3c5cafc
log cosmetics
brenzi Nov 26, 2023
72f2849
generic event display (fails). log cosmetics
brenzi Nov 27, 2023
30c1ef5
hack the fees. and more pimps
brenzi Nov 27, 2023
c46c67b
recorded demo with this
brenzi Nov 27, 2023
90cb372
fixed cargo test
brenzi Nov 28, 2023
6d97c2a
clippy
brenzi Nov 28, 2023
c47a9c5
fixed enclave tests
brenzi Nov 28, 2023
6eacfd7
cleanup
brenzi Nov 28, 2023
886af3b
Merge branch 'master' into ab/target-a-shielding-unshielding-sidechai…
brenzi Nov 28, 2023
b16439c
fix CI
brenzi Nov 28, 2023
2ea0472
CI: introduce fee tolerance when assering balances
brenzi Nov 28, 2023
13c20f3
fix evm tests
brenzi Nov 28, 2023
20b7468
fix
brenzi Nov 28, 2023
83ee1aa
clippy with test flag
brenzi Nov 28, 2023
39a3147
fix demo_sidechain with fee tolerance
brenzi Nov 28, 2023
a70685d
CI fixes
brenzi Nov 28, 2023
b75036e
review remarks fixed
brenzi Nov 29, 2023
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 @@ -2839,6 +2839,7 @@ dependencies = [
"parse_duration",
"primitive-types",
"prometheus",
"regex 1.9.5",
"scale-info",
"serde 1.0.188",
"serde_derive 1.0.188",
Expand Down Expand Up @@ -3892,6 +3893,7 @@ dependencies = [
"itp-time-utils",
"itp-top-pool-author",
"itp-types",
"itp-utils",
"its-block-composer",
"its-block-verification",
"its-consensus-common",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ where
.filter(|&event| event.to == *vault_account)
.try_for_each(|event| {
info!("found transfer_event to vault account: {}", event);
//call = IndirectCall::ShieldFunds(ShieldFundsArgs{ })
//debug!("shielding from Integritee suppressed");
Self::shield_funds(executor, &event.from, event.amount)
//Err(ParentchainError::FunctionalityDisabled)
})
.map_err(|_| ParentchainError::ShieldFundsFailure)?;
}
Expand Down
1 change: 1 addition & 0 deletions app-libs/parentchain-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
extern crate sgx_tstd as std;

use codec::Decode;

pub mod indirect_calls;
pub mod integritee;
pub mod target_a;
Expand Down
51 changes: 44 additions & 7 deletions app-libs/parentchain-interface/src/target_a/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,64 @@

*/

use codec::Encode;
pub use ita_sgx_runtime::{Balance, Index};

use ita_stf::TrustedCallSigned;
use ita_stf::{Getter, TrustedCall, TrustedCallSigned};
use itc_parentchain_indirect_calls_executor::error::Error;
use itp_stf_primitives::traits::IndirectExecutor;
use itp_types::parentchain::{AccountId, FilterEvents, HandleParentchainEvents};
use itp_stf_primitives::{traits::IndirectExecutor, types::TrustedOperation};
use itp_types::parentchain::{AccountId, FilterEvents, HandleParentchainEvents, ParentchainError};
use itp_utils::hex::hex_encode;
use log::*;

pub struct ParentchainEventHandler {}

impl ParentchainEventHandler {
fn shield_funds<Executor: IndirectExecutor<TrustedCallSigned, Error>>(
executor: &Executor,
account: &AccountId,
amount: Balance,
) -> Result<(), Error> {
trace!("[TargetA] shielding for {:?} amount {}", account, amount,);
let shard = executor.get_default_shard();
let trusted_call =
TrustedCall::balance_shield(executor.get_enclave_account()?, account.clone(), amount);
let signed_trusted_call = executor.sign_call_with_self(&trusted_call, &shard)?;
let trusted_operation =
TrustedOperation::<TrustedCallSigned, Getter>::indirect_call(signed_trusted_call);

let encrypted_trusted_call = executor.encrypt(&trusted_operation.encode())?;
executor.submit_trusted_call(shard, encrypted_trusted_call);

Ok(())
}
}

impl<Executor> HandleParentchainEvents<Executor, TrustedCallSigned, Error>
for ParentchainEventHandler
where
Executor: IndirectExecutor<TrustedCallSigned, Error>,
{
fn handle_events(
_executor: &Executor,
_events: impl FilterEvents,
_vault_account: &AccountId,
executor: &Executor,
events: impl FilterEvents,
vault_account: &AccountId,
) -> Result<(), Error> {
debug!("not handling any events for target A");
let filter_events = events.get_transfer_events();
trace!(
"[TargetA] filtering transfer events to shard vault account: {}",
hex_encode(vault_account.encode().as_slice())
);
if let Ok(events) = filter_events {
events
.iter()
.filter(|&event| event.to == *vault_account)
.try_for_each(|event| {
std::println!("⣿TargetA⣿ 🛡 found transfer event to shard vault account: {} will shield to {}", event.amount, hex_encode(event.from.encode().as_ref()));
Self::shield_funds(executor, &event.from, event.amount)
})
.map_err(|_| ParentchainError::ShieldFundsFailure)?;
}
Ok(())
}
}
17 changes: 10 additions & 7 deletions app-libs/parentchain-interface/src/target_a/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use itc_parentchain_indirect_calls_executor::{
};
use itp_node_api::metadata::pallet_balances::BalancesCallIndexes;
use itp_stf_primitives::traits::IndirectExecutor;
use log::trace;
use log::{debug, trace};

/// The default indirect call (extrinsic-triggered) of the Target-A-Parachain.
#[derive(Debug, Clone, Encode, Decode, Eq, PartialEq)]
Expand All @@ -48,11 +48,16 @@ pub enum IndirectCall {
impl<Executor: IndirectExecutor<TrustedCallSigned, Error>>
IndirectDispatch<Executor, TrustedCallSigned> for IndirectCall
{
fn dispatch(&self, executor: &Executor) -> Result<()> {
fn dispatch(&self, _executor: &Executor) -> Result<()> {
debug!("shielding from TargetA extrinsic to Alice suppressed");
/*
trace!("dispatching indirect call {:?}", self);
match self {
IndirectCall::TransferToAliceShieldsFunds(args) => args.dispatch(executor),
}

*/
Ok(())
}
}

Expand Down Expand Up @@ -89,19 +94,17 @@ where
};
let index = xt.call_index;
let call_args = &mut &xt.call_args[..];
log::trace!("[TransferToAliceShieldsFundsFilter] attempting to execute indirect call with index {:?}", index);
trace!("[TransferToAliceShieldsFundsFilter] attempting to execute indirect call with index {:?}", index);
if index == metadata.transfer_call_indexes().ok()?
|| index == metadata.transfer_keep_alive_call_indexes().ok()?
|| index == metadata.transfer_allow_death_call_indexes().ok()?
{
log::debug!(
"found `transfer` or `transfer_allow_death` or `transfer_keep_alive` call."
);
debug!("found `transfer` or `transfer_allow_death` or `transfer_keep_alive` call.");
let args = decode_and_log_error::<TransferToAliceShieldsFundsArgs>(call_args)?;
if args.destination == ALICE_ACCOUNT_ID.into() {
Some(IndirectCall::TransferToAliceShieldsFunds(args))
} else {
log::debug!("Parentchain transfer was not for Alice; ignoring...");
debug!("Parentchain transfer extrinsic was not for Alice; ignoring...");
// No need to put it into the top pool if it isn't executed in the first place.
None
}
Expand Down
2 changes: 1 addition & 1 deletion app-libs/sgx-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub use frame_support::{
StorageValue,
};
pub use pallet_balances::Call as BalancesCall;
pub use pallet_parentchain::Call as ParentchainCall;
pub use pallet_parentchain::Call as ParentchainPalletCall;
pub use pallet_timestamp::Call as TimestampCall;
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
Expand Down
2 changes: 1 addition & 1 deletion app-libs/stf/src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl ExecuteGetter for TrustedGetterSigned {
let info = System::account(&who);
debug!("TrustedGetter free_balance");
debug!("AccountInfo for {} is {:?}", account_id_to_string(&who), info);
debug!("Account free balance is {}", info.data.free);
std::println!("⣿STF⣿ 🔍 TrustedGetter query: free balance for ⣿⣿⣿ is ⣿⣿⣿",);
Some(info.data.free.encode())
},
TrustedGetter::reserved_balance(who) => {
Expand Down
3 changes: 3 additions & 0 deletions app-libs/stf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ pub mod test_genesis;
pub mod trusted_call;

pub(crate) const ENCLAVE_ACCOUNT_KEY: &str = "Enclave_Account_Key";

// fixme: this if a temporary hack only
pub const STF_TX_FEE: Balance = 100000000;
7 changes: 2 additions & 5 deletions app-libs/stf/src/stf_sgx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ use itp_stf_interface::{
};
use itp_stf_primitives::{error::StfError, traits::TrustedCallVerification};
use itp_storage::storage_value_key;
use itp_types::{
parentchain::{AccountId, ParentchainId},
OpaqueCall,
};
use itp_types::parentchain::{AccountId, ParentchainCall, ParentchainId};
use itp_utils::stringify::account_id_to_string;
use log::*;
use sp_runtime::traits::StaticLookup;
Expand Down Expand Up @@ -147,7 +144,7 @@ where
fn execute_call(
state: &mut State,
call: TCS,
calls: &mut Vec<OpaqueCall>,
calls: &mut Vec<ParentchainCall>,
node_metadata_repo: Arc<NodeMetadataRepository>,
) -> Result<(), Self::Error> {
state.execute_with(|| call.execute(calls, node_metadata_repo))
Expand Down
6 changes: 3 additions & 3 deletions app-libs/stf/src/test_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ const ENDOWED_SEED: Seed = *b"12345678901234567890123456789012";
const SECOND_ENDOWED_SEED: Seed = *b"22345678901234567890123456789012";
const UNENDOWED_SEED: Seed = *b"92345678901234567890123456789012";

const ALICE_FUNDS: Balance = 1000000000000000;
pub const ENDOWED_ACC_FUNDS: Balance = 2000;
pub const SECOND_ENDOWED_ACC_FUNDS: Balance = 1000;
const ALICE_FUNDS: Balance = 10_000_000_000_000_000;
pub const ENDOWED_ACC_FUNDS: Balance = 2_000_000_000_000;
pub const SECOND_ENDOWED_ACC_FUNDS: Balance = 1_000_000_000_000;

pub fn endowed_account() -> ed25519::Pair {
ed25519::Pair::from_seed(&ENDOWED_SEED)
Expand Down
96 changes: 68 additions & 28 deletions app-libs/stf/src/trusted_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::vec::Vec;
#[cfg(feature = "evm")]
use crate::evm_helpers::{create_code_hash, evm_create2_address, evm_create_address};
use crate::{
helpers::{ensure_enclave_signer_account, get_storage_by_key_hash},
helpers::{enclave_signer_account, ensure_enclave_signer_account, get_storage_by_key_hash},
Getter,
};
use codec::{Compact, Decode, Encode};
Expand All @@ -44,7 +44,10 @@ use itp_stf_primitives::{
traits::{TrustedCallSigning, TrustedCallVerification},
types::{AccountId, KeyPair, ShardIdentifier, Signature, TrustedOperation},
};
use itp_types::{parentchain::ProxyType, Address, OpaqueCall};
use itp_types::{
parentchain::{ParentchainCall, ProxyType},
Address, OpaqueCall,
};
use itp_utils::stringify::account_id_to_string;
use log::*;
use sp_core::{
Expand Down Expand Up @@ -214,7 +217,7 @@ where

fn execute(
self,
calls: &mut Vec<OpaqueCall>,
calls: &mut Vec<ParentchainCall>,
node_metadata_repo: Arc<NodeMetadataRepository>,
) -> Result<(), Self::Error> {
let sender = self.call.sender_account().clone();
Expand Down Expand Up @@ -259,12 +262,26 @@ where
},
TrustedCall::balance_transfer(from, to, value) => {
let origin = ita_sgx_runtime::RuntimeOrigin::signed(from.clone());
debug!(
"balance_transfer({}, {}, {})",
std::println!("⣿STF⣿ 🔄 balance_transfer from ⣿⣿⣿ to ⣿⣿⣿ amount ⣿⣿⣿");
// endow fee to enclave (self)
let fee_recipient: AccountId = enclave_signer_account();
// fixme: apply fees through standard frame process and tune it
let fee = crate::STF_TX_FEE;
info!(
"from {}, to {}, amount {}, fee {}",
account_id_to_string(&from),
account_id_to_string(&to),
value
value,
fee
);
ita_sgx_runtime::BalancesCall::<Runtime>::transfer {
dest: MultiAddress::Id(fee_recipient),
value: fee,
}
.dispatch_bypass_filter(origin.clone())
.map_err(|e| {
Self::Error::Dispatch(format!("Balance Transfer error: {:?}", e.error))
})?;
ita_sgx_runtime::BalancesCall::<Runtime>::transfer {
dest: MultiAddress::Id(to),
value,
Expand All @@ -276,28 +293,36 @@ where
Ok(())
},
TrustedCall::balance_unshield(account_incognito, beneficiary, value, shard) => {
debug!(
"balance_unshield({}, {}, {}, {})",
std::println!(
"⣿STF⣿ 🛡👐 balance_unshield from ⣿⣿⣿ to {}, amount {}",
account_id_to_string(&beneficiary),
value
);
// endow fee to enclave (self)
let fee_recipient: AccountId = enclave_signer_account();
// fixme: apply fees through standard frame process and tune it. has to be at least two L1 transfer's fees
let fee = crate::STF_TX_FEE * 3;

info!(
"balance_unshield(from (L2): {}, to (L1): {}, amount {} (+fee: {}), shard {})",
account_id_to_string(&account_incognito),
account_id_to_string(&beneficiary),
value,
fee,
shard
);
unshield_funds(account_incognito, value)?;

calls.push(OpaqueCall::from_tuple(&(
node_metadata_repo
.get_from_metadata(|m| m.unshield_funds_call_indexes())
.map_err(|_| StfError::InvalidMetadata)?
.map_err(|_| StfError::InvalidMetadata)?,
shard,
beneficiary.clone(),
value,
call_hash,
)));
// todo: the following is a placeholder dummy which will replace the above with #1257.
// the extrinsic will be sent and potentially deplete the vault at the current state which
// is nothing to worry about before we solve mentioned issue.
let origin = ita_sgx_runtime::RuntimeOrigin::signed(account_incognito.clone());
ita_sgx_runtime::BalancesCall::<Runtime>::transfer {
dest: MultiAddress::Id(fee_recipient),
value: fee,
}
.dispatch_bypass_filter(origin)
.map_err(|e| {
Self::Error::Dispatch(format!("Balance Unshielding error: {:?}", e.error))
})?;
burn_funds(account_incognito, value)?;

let vault_pubkey: [u8; 32] = get_storage_by_key_hash(SHARD_VAULT_KEY.into())
.ok_or_else(|| {
StfError::Dispatch("shard vault key hasn't been set".to_string())
Expand All @@ -320,7 +345,7 @@ where
None::<ProxyType>,
vault_transfer_call,
));
calls.push(proxy_call);
calls.push(ParentchainCall::TargetA(proxy_call));
Ok(())
},
TrustedCall::balance_shield(enclave_account, who, value) => {
Expand All @@ -329,15 +354,15 @@ where
shield_funds(who, value)?;

// Send proof of execution on chain.
calls.push(OpaqueCall::from_tuple(&(
calls.push(ParentchainCall::Integritee(OpaqueCall::from_tuple(&(
node_metadata_repo
.get_from_metadata(|m| m.publish_hash_call_indexes())
.map_err(|_| StfError::InvalidMetadata)?
.map_err(|_| StfError::InvalidMetadata)?,
call_hash,
Vec::<itp_types::H256>::new(),
b"shielded some funds!".to_vec(),
)));
))));
Ok(())
},
#[cfg(feature = "evm")]
Expand Down Expand Up @@ -476,7 +501,7 @@ where
}
}

fn unshield_funds(account: AccountId, amount: u128) -> Result<(), StfError> {
fn burn_funds(account: AccountId, amount: u128) -> Result<(), StfError> {
let account_info = System::account(&account);
if account_info.data.free < amount {
return Err(StfError::MissingFunds)
Expand All @@ -487,15 +512,30 @@ fn unshield_funds(account: AccountId, amount: u128) -> Result<(), StfError> {
new_free: account_info.data.free - amount,
}
.dispatch_bypass_filter(ita_sgx_runtime::RuntimeOrigin::root())
.map_err(|e| StfError::Dispatch(format!("Unshield funds error: {:?}", e.error)))?;
.map_err(|e| StfError::Dispatch(format!("Burn funds error: {:?}", e.error)))?;
Ok(())
}

fn shield_funds(account: AccountId, amount: u128) -> Result<(), StfError> {
//fixme: make fee configurable and send fee to vault account on L2
let fee = amount / 571; // approx 0.175%

// endow fee to enclave (self)
let fee_recipient: AccountId = enclave_signer_account();

let account_info = System::account(&fee_recipient);
ita_sgx_runtime::BalancesCall::<Runtime>::force_set_balance {
who: MultiAddress::Id(fee_recipient),
new_free: account_info.data.free + fee,
}
.dispatch_bypass_filter(ita_sgx_runtime::RuntimeOrigin::root())
.map_err(|e| StfError::Dispatch(format!("Shield funds error: {:?}", e.error)))?;

// endow shieding amount - fee to beneficiary
let account_info = System::account(&account);
ita_sgx_runtime::BalancesCall::<Runtime>::force_set_balance {
who: MultiAddress::Id(account),
new_free: account_info.data.free + amount,
new_free: account_info.data.free + amount - fee,
}
.dispatch_bypass_filter(ita_sgx_runtime::RuntimeOrigin::root())
.map_err(|e| StfError::Dispatch(format!("Shield funds error: {:?}", e.error)))?;
Expand Down
Loading