Skip to content

Commit

Permalink
move pallet parentchain into this repo (#1606)
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi authored Sep 27, 2024
1 parent e6761d3 commit b9ebc43
Show file tree
Hide file tree
Showing 9 changed files with 680 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5156,8 +5156,8 @@ dependencies = [
[[package]]
name = "pallet-parentchain"
version = "0.11.0"
source = "git+https://github.com/integritee-network/pallets.git?branch=sdk-v0.13.0-polkadot-v0.9.42#abf29acd41a0fca9cd7025b297b6a9fa272a122f"
dependencies = [
"env_logger 0.9.3",
"frame-support",
"frame-system",
"log 0.4.20",
Expand All @@ -5167,6 +5167,7 @@ dependencies = [
"serde 1.0.193",
"sp-core",
"sp-io 7.0.0 (git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42)",
"sp-keyring",
"sp-runtime",
"sp-std",
]
Expand Down
2 changes: 1 addition & 1 deletion app-libs/sgx-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ scale-info = { version = "2.10.0", default-features = false, features = ["derive

# local dependencies
itp-sgx-runtime-primitives = { path = "../../core-primitives/sgx-runtime-primitives", default-features = false }
pallet-parentchain = { default-features = false, path = "pallets/parentchain" }

# Substrate dependencies
frame-executive = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
Expand All @@ -30,7 +31,6 @@ sp-version = { default-features = false, git = "https://github.com/paritytech/su

# Integritee dependencies
pallet-evm = { default-features = false, optional = true, git = "https://github.com/integritee-network/frontier.git", branch = "bar/polkadot-v0.9.42" }
pallet-parentchain = { default-features = false, git = "https://github.com/integritee-network/pallets.git", branch = "sdk-v0.13.0-polkadot-v0.9.42" }

[features]
default = ["std"]
Expand Down
47 changes: 47 additions & 0 deletions app-libs/sgx-runtime/pallets/parentchain/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[package]
name = "pallet-parentchain"
description = "The remote attestation registry and verification pallet for integritee blockchains and parachains"
version = "0.11.0"
authors = ["Integritee AG <[email protected]>"]
homepage = "https://integritee.network/"
repository = "https://github.com/integritee-network/pallets/"
license = "Apache-2.0"
edition = "2021"

[dependencies]
codec = { version = "3.0.0", default-features = false, features = ["derive"], package = "parity-scale-codec" }
log = { version = "0.4.14", default-features = false }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.13", features = ["derive"], optional = true }

# substrate dependencies
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

[dev-dependencies]
env_logger = "0.9.0"
sp-keyring = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

[features]
default = ["std"]
std = [
"codec/std",
"log/std",
"scale-info/std",
"serde",
# substrate dependencies
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]

try-runtime = ["frame-support/try-runtime"]
231 changes: 231 additions & 0 deletions app-libs/sgx-runtime/pallets/parentchain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

/// Index/Nonce type for parentchain runtime
type ParentchainIndex = u32;
/// Balance type for parentchain runtime
type ParentchainBalance = u128;
/// AccountData type for parentchain runtime
type ParentchainAccountData = pallet_balances::AccountData<ParentchainBalance>;

#[frame_support::pallet]
pub mod pallet {
use crate::{weights::WeightInfo, ParentchainAccountData, ParentchainIndex};
use frame_support::{pallet_prelude::*, sp_runtime::traits::Header};
use frame_system::{pallet_prelude::*, AccountInfo};
use sp_runtime::traits::{AtLeast32Bit, Scale};

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);

/// Configuration trait.
#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config {
type RuntimeEvent: From<Event<Self, I>>
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
type WeightInfo: WeightInfo;

/// Type used for expressing timestamp.
type Moment: Parameter
+ Default
+ AtLeast32Bit
+ Scale<Self::BlockNumber, Output = Self::Moment>
+ Copy
+ MaxEncodedLen
+ scale_info::StaticTypeInfo;
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config<I>, I: 'static = ()> {
/// a parentchain block has been registered
SetBlock {
block_number: T::BlockNumber,
parent_hash: T::Hash,
block_hash: T::Hash,
},
SetCreationBlock {
block_number: T::BlockNumber,
block_hash: T::Hash,
},
ShardVaultInitialized {
account: T::AccountId,
},
AccountInfoForcedFor {
account: T::AccountId,
},
ParentchainGenesisInitialized {
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.
#[pallet::storage]
#[pallet::getter(fn account)]
pub type Account<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Blake2_128Concat,
T::AccountId,
AccountInfo<ParentchainIndex, ParentchainAccountData>,
ValueQuery,
>;

/// The current block number being processed. Set by `set_block`.
#[pallet::storage]
#[pallet::getter(fn shard_vault)]
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)]
pub(super) type Number<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::BlockNumber, OptionQuery>;

/// The current block timestamp. Set by `set_now`.
/// this is not guaranteed by the pallet to be consistent with block_number or hash
#[pallet::storage]
#[pallet::getter(fn now)]
pub(super) type Now<T: Config<I>, I: 'static = ()> = StorageValue<_, T::Moment, OptionQuery>;

/// Hash of the previous block. Set by `set_block`.
#[pallet::storage]
#[pallet::getter(fn parent_hash)]
pub(super) type ParentHash<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::Hash, OptionQuery>;

/// Hash of the last block. Set by `set_block`.
#[pallet::storage]
#[pallet::getter(fn block_hash)]
pub(super) type BlockHash<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::Hash, OptionQuery>;

/// Hash of the shard creation block. Set by `set_creation_block`.
#[pallet::storage]
#[pallet::getter(fn creation_block_hash)]
pub(super) type CreationBlockHash<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::Hash, OptionQuery>;

/// The creation block number. Set by `set_creation_block`.
#[pallet::storage]
#[pallet::getter(fn creation_block_number)]
pub(super) type CreationBlockNumber<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::BlockNumber, OptionQuery>;

/// The creation block timestamp. Set by `set_creation_timestamp`.
#[pallet::storage]
#[pallet::getter(fn creation_timestamp)]
pub(super) type CreationTimestamp<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::Moment, OptionQuery>;

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {}

#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::set_block())]
pub fn set_block(origin: OriginFor<T>, header: T::Header) -> DispatchResult {
ensure_root(origin)?;
<Number<T, I>>::put(header.number());
<ParentHash<T, I>>::put(header.parent_hash());
<BlockHash<T, I>>::put(header.hash());
Self::deposit_event(Event::SetBlock {
block_number: *header.number(),
parent_hash: *header.parent_hash(),
block_hash: header.hash(),
});
Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::init_shard_vault())]
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);
<ShardVault<T, I>>::put(account.clone());
Self::deposit_event(Event::ShardVaultInitialized { account });
Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::init_parentchain_genesis_hash())]
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::ParentchainGenesisInitialized { hash: genesis });
Ok(())
}

#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::force_account_info())]
pub fn force_account_info(
origin: OriginFor<T>,
account: T::AccountId,
account_info: AccountInfo<ParentchainIndex, ParentchainAccountData>,
) -> DispatchResult {
ensure_root(origin)?;
<crate::pallet::Account<T, I>>::insert(&account, account_info);
Self::deposit_event(crate::pallet::Event::AccountInfoForcedFor { account });
Ok(())
}

#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::set_now())]
pub fn set_now(origin: OriginFor<T>, now: T::Moment) -> DispatchResult {
ensure_root(origin)?;
<Now<T, I>>::put(now);
Ok(())
}
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::set_creation_block())]
pub fn set_creation_block(origin: OriginFor<T>, header: T::Header) -> DispatchResult {
ensure_root(origin)?;
<CreationBlockNumber<T, I>>::put(header.number());
<CreationBlockHash<T, I>>::put(header.hash());
Self::deposit_event(Event::SetCreationBlock {
block_number: *header.number(),
block_hash: header.hash(),
});
Ok(())
}

#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::set_creation_timestamp())]
pub fn set_creation_timestamp(origin: OriginFor<T>, creation: T::Moment) -> DispatchResult {
ensure_root(origin)?;
<CreationTimestamp<T, I>>::put(creation);
Ok(())
}
}
}

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod weights;
Loading

0 comments on commit b9ebc43

Please sign in to comment.