Skip to content

Commit

Permalink
AccountStore in-memory state initialization and synchronization (#3133)
Browse files Browse the repository at this point in the history
  • Loading branch information
silva-fj authored Oct 25, 2024
1 parent 3d496aa commit cc87102
Show file tree
Hide file tree
Showing 44 changed files with 596 additions and 152 deletions.
34 changes: 25 additions & 9 deletions parachain/pallets/omni-account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub mod pallet {
/// Some member account is made public
AccountMadePublic { who: T::AccountId, member_account_hash: H256 },
/// An account store is updated
AccountStoreUpdated { who: T::AccountId },
AccountStoreUpdated { who: T::AccountId, account_store: MemberAccounts<T> },
/// Some call is dispatched as omni-account origin
DispatchedAsOmniAccount { who: T::AccountId, result: DispatchResult },
/// Some call is dispatched as signed origin
Expand Down Expand Up @@ -245,7 +245,11 @@ pub mod pallet {
MemberAccountHash::<T>::insert(hash, who.clone());
AccountStore::<T>::insert(who.clone(), member_accounts.clone());

Self::deposit_event(Event::AccountAdded { who, member_account_hash: hash });
Self::deposit_event(Event::AccountAdded {
who: who.clone(),
member_account_hash: hash,
});
Self::deposit_event(Event::AccountStoreUpdated { who, account_store: member_accounts });

Ok(())
}
Expand Down Expand Up @@ -276,10 +280,11 @@ pub mod pallet {
if member_accounts.is_empty() {
AccountStore::<T>::remove(&who);
} else {
AccountStore::<T>::insert(who.clone(), member_accounts);
AccountStore::<T>::insert(who.clone(), member_accounts.clone());
}

Self::deposit_event(Event::AccountRemoved { who, member_account_hashes });
Self::deposit_event(Event::AccountRemoved { who: who.clone(), member_account_hashes });
Self::deposit_event(Event::AccountStoreUpdated { who, account_store: member_accounts });

Ok(())
}
Expand All @@ -300,9 +305,13 @@ pub mod pallet {
.ok_or(Error::<T>::AccountNotFound)?;
*m = member_account.into();

AccountStore::<T>::insert(who.clone(), member_accounts);
AccountStore::<T>::insert(who.clone(), member_accounts.clone());

Self::deposit_event(Event::AccountMadePublic { who, member_account_hash: hash });
Self::deposit_event(Event::AccountMadePublic {
who: who.clone(),
member_account_hash: hash,
});
Self::deposit_event(Event::AccountStoreUpdated { who, account_store: member_accounts });

Ok(())
}
Expand Down Expand Up @@ -339,8 +348,11 @@ pub mod pallet {
}

MemberAccountHash::<T>::insert(member_account.hash(), who_account.clone());
AccountStore::<T>::insert(who_account.clone(), member_accounts);
Self::deposit_event(Event::AccountStoreUpdated { who: who_account });
AccountStore::<T>::insert(who_account.clone(), member_accounts.clone());
Self::deposit_event(Event::AccountStoreUpdated {
who: who_account,
account_store: member_accounts,
});

Ok(Pays::No.into())
}
Expand Down Expand Up @@ -386,7 +398,11 @@ pub mod pallet {
MemberAccountHash::<T>::insert(hash, omni_account.clone());
AccountStore::<T>::insert(omni_account.clone(), member_accounts.clone());

Self::deposit_event(Event::AccountStoreCreated { who: omni_account });
Self::deposit_event(Event::AccountStoreCreated { who: omni_account.clone() });
Self::deposit_event(Event::AccountStoreUpdated {
who: omni_account,
account_store: member_accounts.clone(),
});

Ok(member_accounts)
}
Expand Down
67 changes: 51 additions & 16 deletions parachain/pallets/omni-account/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ fn create_account_store_works() {
alice().identity,
));

System::assert_last_event(Event::AccountStoreCreated { who: alice().omni_account }.into());
let member_accounts: MemberAccounts<TestRuntime> =
vec![public_member_account(alice())].try_into().unwrap();

System::assert_has_event(Event::AccountStoreCreated { who: alice().omni_account }.into());
System::assert_last_event(
Event::AccountStoreUpdated {
who: alice().omni_account,
account_store: member_accounts,
}
.into(),
);

// create it the second time will fail
assert_noop!(
Expand Down Expand Up @@ -114,6 +124,13 @@ fn add_account_works() {
}
.into(),
);
System::assert_has_event(
Event::AccountStoreUpdated {
who: alice().omni_account.clone(),
account_store: expected_member_accounts.clone(),
}
.into(),
);

assert_eq!(
AccountStore::<TestRuntime>::get(alice().omni_account).unwrap(),
Expand All @@ -126,20 +143,23 @@ fn add_account_works() {
alice().identity.hash(),
call,
));
let expected_member_accounts: MemberAccounts<TestRuntime> =
BoundedVec::truncate_from(vec![
public_member_account(alice()),
bob.clone(),
charlie.clone(),
]);

System::assert_has_event(
Event::AccountAdded { who: alice().omni_account, member_account_hash: charlie.hash() }
.into(),
);

let expected_member_accounts: MemberAccounts<TestRuntime> =
vec![public_member_account(alice()), bob.clone(), charlie.clone()]
.try_into()
.unwrap();

assert_eq!(
AccountStore::<TestRuntime>::get(alice().omni_account).unwrap(),
expected_member_accounts
System::assert_has_event(
Event::AccountStoreUpdated {
who: alice().omni_account,
account_store: expected_member_accounts.clone(),
}
.into(),
);

assert!(MemberAccountHash::<TestRuntime>::contains_key(bob.hash()));
Expand Down Expand Up @@ -324,16 +344,23 @@ fn remove_account_works() {
.into(),
);

let expected_member_accounts: MemberAccounts<TestRuntime> =
BoundedVec::truncate_from(vec![public_member_account(alice())]);

System::assert_has_event(
Event::AccountRemoved {
who: alice().omni_account,
member_account_hashes: vec![bob.hash()],
}
.into(),
);

let expected_member_accounts: MemberAccounts<TestRuntime> =
vec![public_member_account(alice())].try_into().unwrap();
System::assert_has_event(
Event::AccountStoreUpdated {
who: alice().omni_account,
account_store: expected_member_accounts.clone(),
}
.into(),
);

assert_eq!(
AccountStore::<TestRuntime>::get(alice().omni_account).unwrap(),
Expand Down Expand Up @@ -432,16 +459,24 @@ fn publicize_account_works() {
.into(),
);

let expected_member_accounts: MemberAccounts<TestRuntime> =
BoundedVec::truncate_from(vec![public_member_account(alice()), public_bob]);

System::assert_has_event(
Event::AccountMadePublic {
who: alice().omni_account,
member_account_hash: public_bob.hash(),
member_account_hash: bob().identity.hash(),
}
.into(),
);
System::assert_has_event(
Event::AccountStoreUpdated {
who: alice().omni_account,
account_store: expected_member_accounts.clone(),
}
.into(),
);

let expected_member_accounts: MemberAccounts<TestRuntime> =
vec![public_member_account(alice()), public_bob].try_into().unwrap();
assert_eq!(
AccountStore::<TestRuntime>::get(alice().omni_account).unwrap(),
expected_member_accounts
Expand Down
2 changes: 1 addition & 1 deletion tee-worker/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ impl FilterEvents for FilterableEvents {
fn get_btc_wallet_generated_events(&self) -> Result<Vec<BtcWalletGenerated>, Self::Error> {
self.filter()
}

fn get_account_store_updated_events(&self) -> Result<Vec<AccountStoreUpdated>, Self::Error> {
self.filter()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use itp_types::{
use litentry_primitives::{Address32, Identity};
use log::*;
use sp_core::{blake2_256, H256};
use sp_runtime::traits::{Block as ParentchainBlock, Header as ParentchainHeader};
use sp_std::vec::Vec;
use std::string::ToString;

Expand Down Expand Up @@ -129,11 +130,15 @@ where
{
type Output = Vec<H256>;

fn handle_events(
fn handle_events<Block>(
&self,
executor: &Executor,
events: impl FilterEvents,
) -> Result<Vec<H256>, Error> {
_block_number: <<Block as ParentchainBlock>::Header as ParentchainHeader>::Number,
) -> Result<Vec<H256>, Error>
where
Block: ParentchainBlock,
{
let mut handled_events: Vec<H256> = Vec::new();

if let Ok(events) = events.get_relayer_added_events() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,8 @@ impl FilterEvents for FilterableEvents {
fn get_btc_wallet_generated_events(&self) -> Result<Vec<BtcWalletGenerated>, Self::Error> {
self.filter()
}

fn get_account_store_updated_events(&self) -> Result<Vec<AccountStoreUpdated>, Self::Error> {
self.filter()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use itp_types::{
H256,
};
use log::*;
use sp_runtime::traits::{Block as ParentchainBlock, Header as ParentchainHeader};
use std::vec::Vec;

pub struct ParentchainEventHandler {}
Expand All @@ -52,11 +53,15 @@ where
{
type Output = Vec<H256>;

fn handle_events(
fn handle_events<Block>(
&self,
_executor: &Executor,
_events: impl FilterEvents,
) -> Result<Vec<H256>, Error> {
_block_number: <<Block as ParentchainBlock>::Header as ParentchainHeader>::Number,
) -> Result<Vec<H256>, Error>
where
Block: ParentchainBlock,
{
debug!("not handling any events for target a");
Ok(Vec::new())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,8 @@ impl FilterEvents for FilterableEvents {
fn get_btc_wallet_generated_events(&self) -> Result<Vec<BtcWalletGenerated>, Self::Error> {
self.filter()
}

fn get_account_store_updated_events(&self) -> Result<Vec<AccountStoreUpdated>, Self::Error> {
self.filter()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use itp_types::{
H256,
};
use log::*;
use sp_runtime::traits::{Block as ParentchainBlock, Header as ParentchainHeader};
use std::vec::Vec;

pub struct ParentchainEventHandler {}
Expand All @@ -52,11 +53,15 @@ where
{
type Output = Vec<H256>;

fn handle_events(
fn handle_events<Block>(
&self,
_executor: &Executor,
_events: impl FilterEvents,
) -> Result<Vec<H256>, Error> {
_block_number: <<Block as ParentchainBlock>::Header as ParentchainHeader>::Number,
) -> Result<Vec<H256>, Error>
where
Block: ParentchainBlock,
{
debug!("not handling any events for target B");
Ok(Vec::new())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ impl<
})?
.ok_or_else(|| Error::Other("Could not create events from metadata".into()))?;

let processed_events = self.parentchain_event_handler.handle_events(self, events)?;
let processed_events = self.parentchain_event_handler.handle_events::<ParentchainBlock>(
self,
events,
block_number,
)?;

if self.parentchain_id == ParentchainId::Litentry {
// Include a processed parentchain block confirmation for each block.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use itp_types::{
Address, RsaRequest, ShardIdentifier, H256,
};
use log::*;
use sp_runtime::traits::{Block as ParentchainBlock, Header as ParentchainHeader};
use std::vec::Vec;

pub struct ExtrinsicParser<SignedExtra> {
Expand Down Expand Up @@ -196,6 +197,10 @@ impl FilterEvents for MockEvents {
) -> Result<Vec<itp_types::parentchain::events::BtcWalletGenerated>, Self::Error> {
Ok(Vec::new())
}

fn get_account_store_updated_events(&self) -> Result<Vec<AccountStoreUpdated>, Self::Error> {
Ok(Vec::new())
}
}

pub struct MockParentchainEventHandler {}
Expand All @@ -220,11 +225,15 @@ where
{
type Output = Vec<H256>;

fn handle_events(
fn handle_events<Block>(
&self,
_: &Executor,
_: impl itp_types::parentchain::FilterEvents,
) -> core::result::Result<Vec<H256>, Error> {
_block_number: <<Block as ParentchainBlock>::Header as ParentchainHeader>::Number,
) -> core::result::Result<Vec<H256>, Error>
where
Block: ParentchainBlock,
{
Ok(Vec::from([H256::default()]))
}
}
Expand Down
Loading

0 comments on commit cc87102

Please sign in to comment.