mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Properly define the core pallet instead of placing it within the runtime
This commit is contained in:
@@ -33,6 +33,7 @@ frame-system = { git = "https://github.com/serai-dex/patch-polkadot-sdk", rev =
|
||||
frame-support = { git = "https://github.com/serai-dex/patch-polkadot-sdk", rev = "d4624c561765c13b38eb566e435131a8c329a543", default-features = false }
|
||||
frame-executive = { git = "https://github.com/serai-dex/patch-polkadot-sdk", rev = "d4624c561765c13b38eb566e435131a8c329a543", default-features = false }
|
||||
|
||||
serai-core-pallet = { path = "../core", default-features = false }
|
||||
serai-coins-pallet = { path = "../coins", default-features = false }
|
||||
serai-validator-sets-pallet = { path = "../validator-sets", default-features = false }
|
||||
serai-signals-pallet = { path = "../signals", default-features = false }
|
||||
@@ -42,6 +43,7 @@ substrate-wasm-builder = { git = "https://github.com/serai-dex/patch-polkadot-sd
|
||||
|
||||
[features]
|
||||
std = [
|
||||
"borsh/std",
|
||||
"scale/std",
|
||||
|
||||
"sp-core/std",
|
||||
@@ -55,6 +57,7 @@ std = [
|
||||
"frame-support/std",
|
||||
"frame-executive/std",
|
||||
|
||||
"serai-core-pallet/std",
|
||||
"serai-coins-pallet/std",
|
||||
"serai-validator-sets-pallet/std",
|
||||
"serai-signals-pallet/std",
|
||||
@@ -69,6 +72,7 @@ try-runtime = [
|
||||
"frame-support/try-runtime",
|
||||
"frame-executive/try-runtime",
|
||||
|
||||
"serai-core-pallet/try-runtime",
|
||||
"serai-coins-pallet/try-runtime",
|
||||
"serai-validator-sets-pallet/try-runtime",
|
||||
"serai-signals-pallet/try-runtime",
|
||||
@@ -80,6 +84,8 @@ runtime-benchmarks = [
|
||||
"frame-system/runtime-benchmarks",
|
||||
"frame-support/runtime-benchmarks",
|
||||
|
||||
"serai-core-pallet/runtime-benchmarks",
|
||||
"serai-coins-pallet/runtime-benchmarks",
|
||||
"serai-validator-sets-pallet/runtime-benchmarks",
|
||||
"serai-signals-pallet/runtime-benchmarks",
|
||||
]
|
||||
|
||||
@@ -1,161 +0,0 @@
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use borsh::BorshSerialize;
|
||||
|
||||
use frame_support::pallet_prelude::*;
|
||||
|
||||
use serai_abi::{
|
||||
primitives::merkle::{UnbalancedMerkleTree, IncrementalUnbalancedMerkleTree as Iumt},
|
||||
*,
|
||||
};
|
||||
|
||||
/// A wrapper around a `StorageValue` which offers a high-level API as an IUMT.
|
||||
struct IncrementalUnbalancedMerkleTree<
|
||||
T: frame_support::StorageValue<Iumt, Query = Option<Iumt>>,
|
||||
const BRANCH_TAG: u8 = 1,
|
||||
const LEAF_TAG: u8 = 0,
|
||||
>(PhantomData<T>);
|
||||
impl<
|
||||
T: frame_support::StorageValue<Iumt, Query = Option<Iumt>>,
|
||||
const BRANCH_TAG: u8,
|
||||
const LEAF_TAG: u8,
|
||||
> IncrementalUnbalancedMerkleTree<T, BRANCH_TAG, LEAF_TAG>
|
||||
{
|
||||
/// Create a new Merkle tree, expecting there to be none already present.
|
||||
///
|
||||
/// Panics if a Merkle tree was already present.
|
||||
fn new_expecting_none() {
|
||||
T::mutate(|value| {
|
||||
assert!(value.is_none());
|
||||
*value = Some(Iumt::new());
|
||||
});
|
||||
}
|
||||
/// Append a leaf to the Merkle tree.
|
||||
///
|
||||
/// Panics if no Merkle tree was present.
|
||||
fn append<L: BorshSerialize>(leaf: &L) {
|
||||
let leaf = sp_core::blake2_256(&borsh::to_vec(&(LEAF_TAG, leaf)).unwrap());
|
||||
|
||||
T::mutate(|value| {
|
||||
let tree = value.as_mut().unwrap();
|
||||
tree.append(BRANCH_TAG, leaf);
|
||||
})
|
||||
}
|
||||
/// Get the unbalanced merkle tree.
|
||||
///
|
||||
/// Panics if no Merkle tree was present.
|
||||
fn get() -> UnbalancedMerkleTree {
|
||||
T::get().unwrap().calculate(BRANCH_TAG)
|
||||
}
|
||||
/// Take the Merkle tree.
|
||||
///
|
||||
/// Panics if no Merkle tree was present.
|
||||
fn take() -> UnbalancedMerkleTree {
|
||||
T::mutate(|value| value.take().unwrap().calculate(BRANCH_TAG))
|
||||
}
|
||||
}
|
||||
|
||||
#[frame_support::pallet]
|
||||
mod pallet {
|
||||
use super::*;
|
||||
|
||||
/// The set of all blocks prior added to the blockchain.
|
||||
#[pallet::storage]
|
||||
pub type Blocks<T: Config> = StorageMap<_, Identity, T::Hash, (), OptionQuery>;
|
||||
/// The Merkle tree of all blocks added to the blockchain.
|
||||
#[pallet::storage]
|
||||
#[pallet::unbounded]
|
||||
pub(super) type BlocksCommitment<T: Config> = StorageValue<_, Iumt, OptionQuery>;
|
||||
pub(super) type BlocksCommitmentMerkle<T> = IncrementalUnbalancedMerkleTree<BlocksCommitment<T>>;
|
||||
|
||||
/// The Merkle tree of all transactions within the current block.
|
||||
#[pallet::storage]
|
||||
#[pallet::unbounded]
|
||||
pub(super) type BlockTransactionsCommitment<T: Config> = StorageValue<_, Iumt, OptionQuery>;
|
||||
pub(super) type BlockTransactionsCommitmentMerkle<T> =
|
||||
IncrementalUnbalancedMerkleTree<BlockTransactionsCommitment<T>>;
|
||||
|
||||
/// The hashes of events caused by the current transaction.
|
||||
#[pallet::storage]
|
||||
#[pallet::unbounded]
|
||||
pub(super) type TransactionEvents<T: Config> = StorageValue<_, Iumt, OptionQuery>;
|
||||
pub(super) type TransactionEventsMerkle<T> = IncrementalUnbalancedMerkleTree<
|
||||
TransactionEvents<T>,
|
||||
TRANSACTION_EVENTS_COMMITMENT_BRANCH_TAG,
|
||||
TRANSACTION_EVENTS_COMMITMENT_LEAF_TAG,
|
||||
>;
|
||||
/// The roots of the Merkle trees of each transaction's events.
|
||||
#[pallet::storage]
|
||||
#[pallet::unbounded]
|
||||
pub(super) type BlockEventsCommitment<T: Config> = StorageValue<_, Iumt, OptionQuery>;
|
||||
pub(super) type BlockEventsCommitmentMerkle<T> = IncrementalUnbalancedMerkleTree<
|
||||
BlockEventsCommitment<T>,
|
||||
EVENTS_COMMITMENT_BRANCH_TAG,
|
||||
EVENTS_COMMITMENT_LEAF_TAG,
|
||||
>;
|
||||
|
||||
/// A mapping from an account to its next nonce.
|
||||
#[pallet::storage]
|
||||
pub type NextNonce<T: Config> =
|
||||
StorageMap<_, Blake2_128Concat, T::AccountId, T::Nonce, ValueQuery>;
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config<Hash: Into<[u8; 32]>> {}
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
impl<T: Config> Pallet<T> {
|
||||
pub fn start_transaction() {
|
||||
TransactionEventsMerkle::<T>::new_expecting_none();
|
||||
}
|
||||
// TODO: Have this called
|
||||
pub fn on_event(event: impl TryInto<serai_abi::Event>) {
|
||||
if let Ok(event) = event.try_into() {
|
||||
TransactionEventsMerkle::<T>::append(&event);
|
||||
}
|
||||
}
|
||||
pub fn end_transaction(transaction_hash: [u8; 32]) {
|
||||
BlockTransactionsCommitmentMerkle::<T>::append(&transaction_hash);
|
||||
|
||||
let transaction_events_root = TransactionEventsMerkle::<T>::take().root;
|
||||
|
||||
// Append the leaf (the transaction's hash and its events' root) to the block's events'
|
||||
// commitment
|
||||
BlockEventsCommitmentMerkle::<T>::append(&(&transaction_hash, &transaction_events_root));
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(super) use pallet::*;
|
||||
|
||||
pub struct StartOfBlock<T: Config>(PhantomData<T>);
|
||||
impl<T: Config> frame_support::traits::PreInherents for StartOfBlock<T> {
|
||||
fn pre_inherents() {
|
||||
if frame_system::Pallet::<T>::block_number().is_zero() {
|
||||
BlocksCommitmentMerkle::<T>::new_expecting_none();
|
||||
} else {
|
||||
let parent_hash = frame_system::Pallet::<T>::parent_hash();
|
||||
Blocks::<T>::set(parent_hash, Some(()));
|
||||
let parent_hash: [u8; 32] = parent_hash.into();
|
||||
BlocksCommitmentMerkle::<T>::append(&parent_hash);
|
||||
}
|
||||
|
||||
BlockTransactionsCommitmentMerkle::<T>::new_expecting_none();
|
||||
BlockEventsCommitmentMerkle::<T>::new_expecting_none();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EndOfBlock<T: Config>(PhantomData<T>);
|
||||
impl<T: Config> frame_support::traits::PostTransactions for EndOfBlock<T> {
|
||||
fn post_transactions() {
|
||||
frame_system::Pallet::<T>::deposit_log(sp_runtime::generic::DigestItem::Consensus(
|
||||
SeraiExecutionDigest::CONSENSUS_ID,
|
||||
borsh::to_vec(&SeraiExecutionDigest {
|
||||
builds_upon: BlocksCommitmentMerkle::<T>::get(),
|
||||
transactions_commitment: BlockTransactionsCommitmentMerkle::<T>::take(),
|
||||
events_commitment: BlockEventsCommitmentMerkle::<T>::take(),
|
||||
})
|
||||
.unwrap(),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ extern crate alloc;
|
||||
use alloc::borrow::Cow;
|
||||
|
||||
use sp_core::sr25519::Public;
|
||||
use sp_runtime::{Perbill, Weight, traits::Header as _};
|
||||
use sp_runtime::{Perbill, Weight};
|
||||
use sp_version::RuntimeVersion;
|
||||
|
||||
#[rustfmt::skip]
|
||||
@@ -19,8 +19,6 @@ use serai_abi::{
|
||||
|
||||
use serai_coins_pallet::{CoinsInstance, LiquidityTokensInstance};
|
||||
|
||||
mod core_pallet;
|
||||
|
||||
type Block = SubstrateBlock;
|
||||
|
||||
/// The lookup for a SeraiAddress -> Public.
|
||||
@@ -75,7 +73,7 @@ mod runtime {
|
||||
pub type System = frame_system::Pallet<Runtime>;
|
||||
|
||||
#[runtime::pallet_index(1)]
|
||||
pub type Core = core_pallet::Pallet<Runtime>;
|
||||
pub type Core = serai_core_pallet::Pallet<Runtime>;
|
||||
|
||||
#[runtime::pallet_index(2)]
|
||||
pub type Coins = serai_coins_pallet::Pallet<Runtime, CoinsInstance>;
|
||||
@@ -125,12 +123,12 @@ impl frame_system::Config for Runtime {
|
||||
type SingleBlockMigrations = ();
|
||||
type MultiBlockMigrator = ();
|
||||
|
||||
type PreInherents = core_pallet::StartOfBlock<Runtime>;
|
||||
type PreInherents = serai_core_pallet::StartOfBlock<Runtime>;
|
||||
type PostInherents = ();
|
||||
type PostTransactions = core_pallet::EndOfBlock<Runtime>;
|
||||
type PostTransactions = serai_core_pallet::EndOfBlock<Runtime>;
|
||||
}
|
||||
|
||||
impl core_pallet::Config for Runtime {}
|
||||
impl serai_core_pallet::Config for Runtime {}
|
||||
|
||||
impl serai_coins_pallet::Config<CoinsInstance> for Runtime {
|
||||
type AllowMint = serai_coins_pallet::AlwaysAllowMint; // TODO
|
||||
@@ -230,7 +228,6 @@ sp_api::impl_runtime_apis! {
|
||||
VERSION
|
||||
}
|
||||
fn initialize_block(header: &Header) -> sp_runtime::ExtrinsicInclusionMode {
|
||||
core_pallet::Blocks::<Runtime>::set(header.parent_hash(), Some(()));
|
||||
Executive::initialize_block(header)
|
||||
}
|
||||
fn execute_block(block: Block) {
|
||||
@@ -257,7 +254,7 @@ impl serai_abi::TransactionContext for Context {
|
||||
|
||||
/// If a block is present in the blockchain.
|
||||
fn block_is_present_in_blockchain(&self, hash: &serai_abi::primitives::BlockHash) -> bool {
|
||||
core_pallet::Blocks::<Runtime>::get(hash).is_some()
|
||||
serai_core_pallet::Pallet::<Runtime>::block_exists(hash)
|
||||
}
|
||||
/// The time embedded into the current block.
|
||||
fn current_time(&self) -> Option<u64> {
|
||||
@@ -265,7 +262,7 @@ impl serai_abi::TransactionContext for Context {
|
||||
}
|
||||
/// Get the next nonce for an account.
|
||||
fn next_nonce(&self, signer: &SeraiAddress) -> u32 {
|
||||
core_pallet::NextNonce::<Runtime>::get(signer)
|
||||
serai_core_pallet::Pallet::<Runtime>::next_nonce(signer)
|
||||
}
|
||||
/// If the signer can pay the SRI fee.
|
||||
fn can_pay_fee(
|
||||
@@ -284,11 +281,10 @@ impl serai_abi::TransactionContext for Context {
|
||||
}
|
||||
|
||||
fn start_transaction(&self) {
|
||||
Core::start_transaction();
|
||||
Core::start_transaction()
|
||||
}
|
||||
/// Consume the next nonce for an account.
|
||||
fn consume_next_nonce(&self, signer: &SeraiAddress) {
|
||||
core_pallet::NextNonce::<Runtime>::mutate(signer, |value| *value += 1);
|
||||
serai_core_pallet::Pallet::<Runtime>::consume_next_nonce(signer)
|
||||
}
|
||||
/// Have the transaction pay its SRI fee.
|
||||
fn pay_fee(
|
||||
|
||||
Reference in New Issue
Block a user