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

omni-executor init #3122

Merged
merged 13 commits into from
Oct 16, 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
4,375 changes: 4,375 additions & 0 deletions common/primitives/core/Cargo.lock

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions common/primitives/core/src/intention.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::H160;
use sp_runtime::{traits::ConstU32, BoundedVec};

pub const CALL_ETHEREUM_INPUT_LEN: u32 = 10 * 1024;

#[derive(Encode, Decode, Debug, Clone, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
pub enum Intention {
#[codec(index = 0)]
TransferEthereum(TransferEthereum),
#[codec(index = 1)]
CallEthereum(CallEthereum),
}

#[derive(Encode, Decode, Debug, Clone, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
pub struct TransferEthereum {
pub to: H160,
pub value: [u8; 32],
}

pub type CallEthereumInputLen = ConstU32<CALL_ETHEREUM_INPUT_LEN>;

#[derive(Encode, Decode, Debug, Clone, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
pub struct CallEthereum {
pub address: H160,
pub input: BoundedVec<u8, CallEthereumInputLen>,
}
3 changes: 3 additions & 0 deletions common/primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub use assertion::Assertion;
pub mod identity;
pub use identity::*;

pub mod intention;
pub use intention::*;

pub mod omni_account;
pub use omni_account::*;

Expand Down
14 changes: 13 additions & 1 deletion parachain/pallets/omni-account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ mod mock;
#[cfg(test)]
mod tests;

pub use core_primitives::{GetAccountStoreHash, Identity, MemberAccount, OmniAccountConverter};
pub use core_primitives::{
GetAccountStoreHash, Identity, Intention, MemberAccount, OmniAccountConverter,
};
pub use frame_system::pallet_prelude::BlockNumberFor;
pub use pallet::*;

Expand Down Expand Up @@ -137,6 +139,8 @@ pub mod pallet {
DispatchedAsOmniAccount { who: T::AccountId, result: DispatchResult },
/// Some call is dispatched as signed origin
DispatchedAsSigned { who: T::AccountId, result: DispatchResult },
/// Intention is requested
IntentionRequested { who: T::AccountId, intention: Intention },
}

#[pallet::error]
Expand Down Expand Up @@ -306,6 +310,14 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(6)]
#[pallet::weight((195_000_000, DispatchClass::Normal))]
pub fn request_intention(origin: OriginFor<T>, intention: Intention) -> DispatchResult {
let who = T::OmniAccountOrigin::ensure_origin(origin)?;
Self::deposit_event(Event::IntentionRequested { who, intention });
Ok(())
}
}
}

Expand Down
57 changes: 56 additions & 1 deletion parachain/pallets/omni-account/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

use crate::{mock::*, AccountStore, MemberAccountHash, *};
use core_primitives::Identity;
use core_primitives::{CallEthereum, Identity};
use frame_support::{assert_noop, assert_ok};
use sp_core::hashing::blake2_256;
use sp_core::H160;
use sp_runtime::{traits::BadOrigin, ModuleError};
use sp_std::vec;

Expand All @@ -37,6 +38,10 @@ fn publicize_account_call(id: Identity) -> Box<RuntimeCall> {
Box::new(call)
}

fn request_intention_call(intention: Intention) -> Box<RuntimeCall> {
RuntimeCall::OmniAccount(crate::Call::request_intention { intention }).into()
}

fn make_balance_transfer_call(dest: AccountId, value: Balance) -> Box<RuntimeCall> {
let call = RuntimeCall::Balances(pallet_balances::Call::transfer { dest, value });
Box::new(call)
Expand Down Expand Up @@ -589,6 +594,56 @@ fn publicize_account_identity_not_found_works() {
});
}

#[test]
fn request_intention_works() {
new_test_ext().execute_with(|| {
let tee_signer = get_tee_signer();
let who = alice();
let who_identity = Identity::from(who.clone());
let who_identity_hash = who_identity.hash();
let who_omni_account = who_identity.to_omni_account();

let private_account =
MemberAccount::Private(vec![1, 2, 3], H256::from(blake2_256(&[1, 2, 3])));

assert_ok!(OmniAccount::create_account_store(
RuntimeOrigin::signed(tee_signer.clone()),
who_identity.clone(),
));

let call = add_account_call(private_account);
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
who_identity.hash(),
call
));

let intention = Intention::CallEthereum(CallEthereum {
address: H160::zero(),
input: BoundedVec::new(),
});

let call = request_intention_call(intention.clone());
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
who_identity_hash,
call
));

System::assert_has_event(
Event::DispatchedAsOmniAccount {
who: who_omni_account.clone(),
result: DispatchResult::Ok(()),
}
.into(),
);

System::assert_has_event(
Event::IntentionRequested { who: who_omni_account, intention }.into(),
);
});
}

#[test]
fn dispatch_as_signed_works() {
new_test_ext().execute_with(|| {
Expand Down
7 changes: 7 additions & 0 deletions tee-worker/omni-executor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
debug/
target/
.idea/
**/*.bin

cache

Loading
Loading