2025-09-19 19:05:47 -04:00
|
|
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
|
|
|
|
#![doc = include_str!("../README.md")]
|
|
|
|
|
#![deny(missing_docs)]
|
|
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
2025-03-04 06:00:06 -05:00
|
|
|
|
2025-09-19 19:05:47 -04:00
|
|
|
use core::marker::PhantomData;
|
2025-03-04 06:00:06 -05:00
|
|
|
|
2025-09-19 22:12:45 -04:00
|
|
|
extern crate alloc;
|
2025-03-04 06:00:06 -05:00
|
|
|
|
2025-09-19 19:05:47 -04:00
|
|
|
mod iumt;
|
|
|
|
|
pub use iumt::*;
|
2025-03-04 06:00:06 -05:00
|
|
|
|
2025-09-19 22:12:45 -04:00
|
|
|
#[expect(clippy::cast_possible_truncation)]
|
2025-02-26 14:16:04 -05:00
|
|
|
#[frame_support::pallet]
|
2025-09-19 22:12:45 -04:00
|
|
|
pub mod pallet {
|
|
|
|
|
use alloc::vec::Vec;
|
|
|
|
|
|
|
|
|
|
use frame_support::pallet_prelude::*;
|
|
|
|
|
|
|
|
|
|
use serai_abi::primitives::{prelude::*, merkle::IncrementalUnbalancedMerkleTree as Iumt};
|
|
|
|
|
|
2025-03-04 06:00:06 -05:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
/// The set of all blocks prior added to the blockchain.
|
|
|
|
|
#[pallet::storage]
|
2025-09-19 19:05:47 -04:00
|
|
|
pub(super) type Blocks<T: Config> = StorageMap<_, Identity, T::Hash, (), OptionQuery>;
|
2025-03-04 06:00:06 -05:00
|
|
|
/// The Merkle tree of all blocks added to the blockchain.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pallet::unbounded]
|
2025-03-06 03:19:29 -05:00
|
|
|
pub(super) type BlocksCommitment<T: Config> = StorageValue<_, Iumt, OptionQuery>;
|
2025-03-04 06:00:06 -05:00
|
|
|
pub(super) type BlocksCommitmentMerkle<T> = IncrementalUnbalancedMerkleTree<BlocksCommitment<T>>;
|
|
|
|
|
|
|
|
|
|
/// The Merkle tree of all transactions within the current block.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pallet::unbounded]
|
2025-03-06 03:19:29 -05:00
|
|
|
pub(super) type BlockTransactionsCommitment<T: Config> = StorageValue<_, Iumt, OptionQuery>;
|
2025-03-04 06:00:06 -05:00
|
|
|
pub(super) type BlockTransactionsCommitmentMerkle<T> =
|
|
|
|
|
IncrementalUnbalancedMerkleTree<BlockTransactionsCommitment<T>>;
|
2025-02-26 14:16:04 -05:00
|
|
|
|
2025-03-04 06:00:06 -05:00
|
|
|
/// The hashes of events caused by the current transaction.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pallet::unbounded]
|
2025-03-06 03:19:29 -05:00
|
|
|
pub(super) type TransactionEvents<T: Config> = StorageValue<_, Iumt, OptionQuery>;
|
2025-03-04 06:00:06 -05:00
|
|
|
pub(super) type TransactionEventsMerkle<T> = IncrementalUnbalancedMerkleTree<
|
|
|
|
|
TransactionEvents<T>,
|
2025-09-19 22:12:45 -04:00
|
|
|
{ serai_abi::TRANSACTION_EVENTS_COMMITMENT_BRANCH_TAG },
|
|
|
|
|
{ serai_abi::TRANSACTION_EVENTS_COMMITMENT_LEAF_TAG },
|
2025-03-04 06:00:06 -05:00
|
|
|
>;
|
|
|
|
|
/// The roots of the Merkle trees of each transaction's events.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pallet::unbounded]
|
2025-03-06 03:19:29 -05:00
|
|
|
pub(super) type BlockEventsCommitment<T: Config> = StorageValue<_, Iumt, OptionQuery>;
|
2025-03-04 06:00:06 -05:00
|
|
|
pub(super) type BlockEventsCommitmentMerkle<T> = IncrementalUnbalancedMerkleTree<
|
|
|
|
|
BlockEventsCommitment<T>,
|
2025-09-19 22:12:45 -04:00
|
|
|
{ serai_abi::EVENTS_COMMITMENT_BRANCH_TAG },
|
|
|
|
|
{ serai_abi::EVENTS_COMMITMENT_LEAF_TAG },
|
2025-03-04 06:00:06 -05:00
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
/// A mapping from an account to its next nonce.
|
2025-02-26 14:16:04 -05:00
|
|
|
#[pallet::storage]
|
2025-09-19 19:05:47 -04:00
|
|
|
type NextNonce<T: Config> = StorageMap<_, Blake2_128Concat, SeraiAddress, T::Nonce, ValueQuery>;
|
2025-02-26 14:16:04 -05:00
|
|
|
|
2025-09-19 22:12:45 -04:00
|
|
|
/// Mapping from Serai's events to Substrate's.
|
|
|
|
|
#[pallet::event]
|
|
|
|
|
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
|
|
|
|
pub enum Event<T: Config> {
|
|
|
|
|
/// An event from Serai.
|
|
|
|
|
Event(Vec<u8>),
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 14:16:04 -05:00
|
|
|
#[pallet::config]
|
2025-03-06 03:19:29 -05:00
|
|
|
pub trait Config: frame_system::Config<Hash: Into<[u8; 32]>> {}
|
2025-02-26 14:16:04 -05:00
|
|
|
|
|
|
|
|
#[pallet::pallet]
|
|
|
|
|
pub struct Pallet<T>(_);
|
2025-03-04 06:00:06 -05:00
|
|
|
|
|
|
|
|
impl<T: Config> Pallet<T> {
|
2025-09-19 19:05:47 -04:00
|
|
|
/// If a block exists on the current blockchain.
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn block_exists(hash: impl scale::EncodeLike<T::Hash>) -> bool {
|
|
|
|
|
Blocks::<T>::contains_key(hash)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The next nonce for an account.
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn next_nonce(account: &SeraiAddress) -> T::Nonce {
|
|
|
|
|
NextNonce::<T>::get(account)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Consume the next nonce for an account.
|
|
|
|
|
///
|
|
|
|
|
/// Panics if the current nonce is `<_>::MAX`.
|
|
|
|
|
pub fn consume_next_nonce(signer: &SeraiAddress) {
|
|
|
|
|
NextNonce::<T>::mutate(signer, |value| {
|
|
|
|
|
*value = value
|
|
|
|
|
.checked_add(&T::Nonce::one())
|
|
|
|
|
.expect("`consume_next_nonce` called when current nonce is <_>::MAX")
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The code to run when beginning execution of a transaction.
|
|
|
|
|
///
|
|
|
|
|
/// The caller MUST ensure two transactions aren't simultaneously started.
|
2025-03-04 06:00:06 -05:00
|
|
|
pub fn start_transaction() {
|
|
|
|
|
TransactionEventsMerkle::<T>::new_expecting_none();
|
|
|
|
|
}
|
2025-09-19 22:12:45 -04:00
|
|
|
|
2025-09-19 19:05:47 -04:00
|
|
|
/// Emit an event.
|
2025-09-19 22:12:45 -04:00
|
|
|
pub fn emit_event(event: impl Into<serai_abi::Event>) {
|
|
|
|
|
let event = event.into();
|
|
|
|
|
TransactionEventsMerkle::<T>::append(&event);
|
|
|
|
|
Self::deposit_event(Event::Event(borsh::to_vec(&event).unwrap()));
|
2025-03-04 06:00:06 -05:00
|
|
|
}
|
2025-09-19 22:12:45 -04:00
|
|
|
|
2025-09-19 19:05:47 -04:00
|
|
|
/// End execution of a transaction.
|
2025-03-04 06:00:06 -05:00
|
|
|
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));
|
|
|
|
|
}
|
2025-09-19 22:12:45 -04:00
|
|
|
|
|
|
|
|
/// Fetch all of Serai's events.
|
|
|
|
|
///
|
|
|
|
|
/// This MUST only be used for testing purposes.
|
|
|
|
|
pub fn events() -> Vec<serai_abi::Event>
|
|
|
|
|
where
|
|
|
|
|
serai_abi::Event: TryFrom<T::RuntimeEvent>,
|
|
|
|
|
{
|
|
|
|
|
frame_system::Pallet::<T>::events()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(|e| serai_abi::Event::try_from(e.event).ok())
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
2025-03-04 06:00:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-19 19:05:47 -04:00
|
|
|
pub use pallet::*;
|
2025-03-04 06:00:06 -05:00
|
|
|
|
2025-09-19 19:05:47 -04:00
|
|
|
/// The code to run at the start of a block for this pallet.
|
2025-03-04 06:00:06 -05:00
|
|
|
pub struct StartOfBlock<T: Config>(PhantomData<T>);
|
|
|
|
|
impl<T: Config> frame_support::traits::PreInherents for StartOfBlock<T> {
|
|
|
|
|
fn pre_inherents() {
|
2025-09-19 22:12:45 -04:00
|
|
|
use frame_support::pallet_prelude::Zero;
|
|
|
|
|
|
2025-03-06 03:19:29 -05:00
|
|
|
if frame_system::Pallet::<T>::block_number().is_zero() {
|
2025-03-04 06:00:06 -05:00
|
|
|
BlocksCommitmentMerkle::<T>::new_expecting_none();
|
|
|
|
|
} else {
|
2025-03-06 03:19:29 -05:00
|
|
|
let parent_hash = frame_system::Pallet::<T>::parent_hash();
|
|
|
|
|
Blocks::<T>::set(parent_hash, Some(()));
|
2025-03-04 06:00:06 -05:00
|
|
|
let parent_hash: [u8; 32] = parent_hash.into();
|
|
|
|
|
BlocksCommitmentMerkle::<T>::append(&parent_hash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BlockTransactionsCommitmentMerkle::<T>::new_expecting_none();
|
|
|
|
|
BlockEventsCommitmentMerkle::<T>::new_expecting_none();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 19:05:47 -04:00
|
|
|
/// The code to run at the end of a block for this pallet.
|
2025-03-04 06:00:06 -05:00
|
|
|
pub struct EndOfBlock<T: Config>(PhantomData<T>);
|
|
|
|
|
impl<T: Config> frame_support::traits::PostTransactions for EndOfBlock<T> {
|
|
|
|
|
fn post_transactions() {
|
2025-09-19 22:12:45 -04:00
|
|
|
use serai_abi::SeraiExecutionDigest;
|
2025-09-19 19:05:47 -04:00
|
|
|
frame_system::Pallet::<T>::deposit_log(
|
|
|
|
|
frame_support::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(),
|
|
|
|
|
),
|
|
|
|
|
);
|
2025-03-04 06:00:06 -05:00
|
|
|
}
|
2025-02-26 14:16:04 -05:00
|
|
|
}
|