Skip to content

Commit

Permalink
introduce parentchain genesis storage with write-once lock
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Dec 28, 2023
1 parent b4bcf34 commit 0c9e902
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
30 changes: 28 additions & 2 deletions parentchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ pub mod pallet {
AccountInfoForcedFor {
account: T::AccountId,
},
ParentchainGeneisInitialized {
hash: T::Hash,
},
}

#[pallet::error]
pub enum Error<T, I = ()> {
/// Sahrd vault has been previously initialized and can't be overwritten
ShardVaultAlreadyInitialized,
/// Parentchain genesis hash has already been initialized and can^t be overwritten
GenesisAlreadyInitialized,
}

/// The parentchain mirror of full account information for a particular account ID.
Expand All @@ -69,6 +74,11 @@ pub mod pallet {
pub(super) type ShardVault<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::AccountId, OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn parentchain_genesis_hash)]
pub(super) type ParentchainGenesisHash<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::Hash, OptionQuery>;

/// The current block number being processed. Set by `set_block`.
#[pallet::storage]
#[pallet::getter(fn block_number)]
Expand Down Expand Up @@ -112,13 +122,29 @@ pub mod pallet {
pub fn init_shard_vault(origin: OriginFor<T>, account: T::AccountId) -> DispatchResult {
ensure_root(origin)?;
ensure!(Self::shard_vault().is_none(), Error::<T, I>::ShardVaultAlreadyInitialized);
<crate::pallet::ShardVault<T, I>>::put(account.clone());
Self::deposit_event(crate::pallet::Event::ShardVaultInitialized { account });
<ShardVault<T, I>>::put(account.clone());
Self::deposit_event(Event::ShardVaultInitialized { account });
Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::set_block())]
pub fn init_parentchain_genesis_hash(
origin: OriginFor<T>,
genesis: T::Hash,
) -> DispatchResult {
ensure_root(origin)?;
ensure!(
Self::parentchain_genesis_hash().is_none(),
Error::<T, I>::GenesisAlreadyInitialized
);
<ParentchainGenesisHash<T, I>>::put(genesis);
Self::deposit_event(Event::ParentchainGeneisInitialized { hash: genesis });
Ok(())
}

#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::set_block())]
pub fn force_account_info(
origin: OriginFor<T>,
account: T::AccountId,
Expand Down
18 changes: 18 additions & 0 deletions parentchain/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,25 @@ fn init_shard_vault_works() {
);
})
}
#[test]
fn init_parentchain_genesis_hash_works() {
new_test_ext().execute_with(|| {
let genesis = H256::default();
assert_ok!(ParentchainIntegritee::init_parentchain_genesis_hash(
RuntimeOrigin::root(),
genesis
));
assert_eq!(ParentchainIntegritee::parentchain_genesis_hash().unwrap(), genesis);

System::assert_last_event(RuntimeEvent::ParentchainIntegritee(
ParentchainEvent::ParentchainGeneisInitialized { hash: genesis },
));
assert_noop!(
ParentchainIntegritee::init_parentchain_genesis_hash(RuntimeOrigin::root(), genesis),
Error::<Test, ParentchainInstanceIntegritee>::GenesisAlreadyInitialized
);
})
}
#[test]
fn force_account_info_works() {
new_test_ext().execute_with(|| {
Expand Down

0 comments on commit 0c9e902

Please sign in to comment.