mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 04:39:24 +00:00
Add pallet sessions to runtime, create pallet-tendermint
This commit is contained in:
@@ -24,6 +24,7 @@ sp-core = { git = "https://github.com/serai-dex/substrate" }
|
||||
sp-application-crypto = { git = "https://github.com/serai-dex/substrate" }
|
||||
sp-inherents = { git = "https://github.com/serai-dex/substrate" }
|
||||
sp-timestamp = { git = "https://github.com/serai-dex/substrate" }
|
||||
sp-staking = { git = "https://github.com/serai-dex/substrate" }
|
||||
sp-blockchain = { git = "https://github.com/serai-dex/substrate" }
|
||||
sp-runtime = { git = "https://github.com/serai-dex/substrate" }
|
||||
sp-api = { git = "https://github.com/serai-dex/substrate" }
|
||||
@@ -37,6 +38,8 @@ sc-service = { git = "https://github.com/serai-dex/substrate" }
|
||||
sc-client-api = { git = "https://github.com/serai-dex/substrate" }
|
||||
sc-consensus = { git = "https://github.com/serai-dex/substrate" }
|
||||
|
||||
pallet-session = { git = "https://github.com/serai-dex/substrate" }
|
||||
|
||||
substrate-prometheus-endpoint = { git = "https://github.com/serai-dex/substrate" }
|
||||
|
||||
tendermint-machine = { path = "../tendermint", features = ["substrate"] }
|
||||
|
||||
37
substrate/pallet-tendermint/Cargo.toml
Normal file
37
substrate/pallet-tendermint/Cargo.toml
Normal file
@@ -0,0 +1,37 @@
|
||||
[package]
|
||||
name = "pallet-tendermint"
|
||||
version = "0.1.0"
|
||||
description = "Tendermint pallet for Substrate"
|
||||
license = "AGPL-3.0-only"
|
||||
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/pallet-tendermint"
|
||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||
edition = "2021"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[dependencies]
|
||||
|
||||
parity-scale-codec = { version = "3", default-features = false, features = ["derive"] }
|
||||
scale-info = { version = "2", default-features = false, features = ["derive"] }
|
||||
|
||||
sp-application-crypto = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
frame-system = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
frame-support = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
[features]
|
||||
std = [
|
||||
"sp-application-crypto/std",
|
||||
|
||||
"frame-system/std",
|
||||
"frame-support/std",
|
||||
]
|
||||
|
||||
runtime-benchmarks = [
|
||||
"frame-system/runtime-benchmarks",
|
||||
"frame-support/runtime-benchmarks",
|
||||
]
|
||||
|
||||
default = ["std"]
|
||||
60
substrate/pallet-tendermint/src/lib.rs
Normal file
60
substrate/pallet-tendermint/src/lib.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use frame_support::traits::OneSessionHandler;
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use frame_support::pallet_prelude::*;
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(PhantomData<T>);
|
||||
}
|
||||
|
||||
pub use pallet::*;
|
||||
|
||||
pub mod crypto {
|
||||
use sp_application_crypto::{KeyTypeId, app_crypto, sr25519};
|
||||
app_crypto!(sr25519, KeyTypeId(*b"tend"));
|
||||
|
||||
impl<C> sp_application_crypto::BoundToRuntimeAppPublic for crate::Pallet<C> {
|
||||
type Public = Public;
|
||||
}
|
||||
|
||||
sp_application_crypto::with_pair! {
|
||||
pub type AuthorityPair = Pair;
|
||||
}
|
||||
pub type AuthoritySignature = Signature;
|
||||
pub type AuthorityId = Public;
|
||||
}
|
||||
|
||||
impl<C, V> OneSessionHandler<V> for Pallet<C> {
|
||||
type Key = crypto::Public;
|
||||
|
||||
fn on_genesis_session<'a, I: 'a>(_validators: I)
|
||||
where
|
||||
I: Iterator<Item = (&'a V, Self::Key)>,
|
||||
V: 'a,
|
||||
{
|
||||
}
|
||||
|
||||
fn on_new_session<'a, I: 'a>(_changed: bool, _validators: I, _queued: I)
|
||||
where
|
||||
I: Iterator<Item = (&'a V, Self::Key)>,
|
||||
V: 'a,
|
||||
{
|
||||
/*
|
||||
if !changed {
|
||||
return;
|
||||
}
|
||||
|
||||
for validator in validators {
|
||||
...
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
fn on_disabled(_validator_index: u32) {}
|
||||
}
|
||||
@@ -18,11 +18,11 @@ codec = { package = "parity-scale-codec", version = "3", default-features = fals
|
||||
scale-info = { version = "2", default-features = false, features = ["derive"] }
|
||||
|
||||
sp-core = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-application-crypto = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-std = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-version = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-inherents = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-offchain = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-session = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-transaction-pool = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-block-builder = { git = "https://github.com/serai-dex/substrate", default-features = false}
|
||||
sp-runtime = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
@@ -41,6 +41,9 @@ pallet-transaction-payment = { git = "https://github.com/serai-dex/substrate", d
|
||||
pallet-contracts-primitives = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
pallet-contracts = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
pallet-session = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
pallet-tendermint = { path = "../pallet-tendermint", default-features = false }
|
||||
|
||||
frame-system-rpc-runtime-api = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
@@ -53,11 +56,11 @@ std = [
|
||||
"scale-info/std",
|
||||
|
||||
"sp-core/std",
|
||||
"sp-application-crypto/std",
|
||||
"sp-std/std",
|
||||
"sp-version/std",
|
||||
"sp-inherents/std",
|
||||
"sp-offchain/std",
|
||||
"sp-session/std",
|
||||
"sp-transaction-pool/std",
|
||||
"sp-block-builder/std",
|
||||
"sp-runtime/std",
|
||||
@@ -75,6 +78,9 @@ std = [
|
||||
"pallet-contracts/std",
|
||||
"pallet-contracts-primitives/std",
|
||||
|
||||
"pallet-session/std",
|
||||
"pallet-tendermint/std",
|
||||
|
||||
"frame-system-rpc-runtime-api/std",
|
||||
"pallet-transaction-payment-rpc-runtime-api/std",
|
||||
]
|
||||
@@ -90,6 +96,8 @@ runtime-benchmarks = [
|
||||
|
||||
"pallet-timestamp/runtime-benchmarks",
|
||||
"pallet-balances/runtime-benchmarks",
|
||||
|
||||
"pallet-tendermint/runtime-benchmarks",
|
||||
]
|
||||
|
||||
default = ["std"]
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
#[cfg(feature = "std")]
|
||||
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
|
||||
|
||||
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
|
||||
pub use sp_core::sr25519::Signature;
|
||||
use sp_core::OpaqueMetadata;
|
||||
pub use sp_core::sr25519::{Public, Signature};
|
||||
use sp_runtime::{
|
||||
create_runtime_str, generic, impl_opaque_keys,
|
||||
traits::{IdentityLookup, BlakeTwo256, Block as BlockT},
|
||||
traits::{Convert, OpaqueKeys, IdentityLookup, BlakeTwo256, Block as BlockT},
|
||||
transaction_validity::{TransactionSource, TransactionValidity},
|
||||
ApplyExtrinsicResult, Perbill,
|
||||
};
|
||||
@@ -32,11 +32,13 @@ pub use pallet_timestamp::Call as TimestampCall;
|
||||
pub use pallet_balances::Call as BalancesCall;
|
||||
use pallet_transaction_payment::CurrencyAdapter;
|
||||
|
||||
use pallet_session::PeriodicSessions;
|
||||
|
||||
/// An index to a block.
|
||||
pub type BlockNumber = u32;
|
||||
|
||||
/// Account ID type, equivalent to a public key
|
||||
pub type AccountId = sp_core::sr25519::Public;
|
||||
pub type AccountId = Public;
|
||||
|
||||
/// Balance of an account.
|
||||
pub type Balance = u64;
|
||||
@@ -57,10 +59,14 @@ pub mod opaque {
|
||||
pub type BlockId = generic::BlockId<Block>;
|
||||
|
||||
impl_opaque_keys! {
|
||||
pub struct SessionKeys {}
|
||||
pub struct SessionKeys {
|
||||
pub tendermint: Tendermint,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use opaque::SessionKeys;
|
||||
|
||||
#[sp_version::runtime_version]
|
||||
pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
spec_name: create_runtime_str!("serai"),
|
||||
@@ -206,6 +212,30 @@ impl pallet_contracts::Config for Runtime {
|
||||
type MaxStorageKeyLen = ConstU32<128>;
|
||||
}
|
||||
|
||||
impl pallet_tendermint::Config for Runtime {}
|
||||
|
||||
const SESSION_LENGTH: BlockNumber = 5 * DAYS;
|
||||
type Sessions = PeriodicSessions<ConstU32<{ SESSION_LENGTH }>, ConstU32<{ SESSION_LENGTH }>>;
|
||||
|
||||
pub struct IdentityValidatorIdOf;
|
||||
impl Convert<Public, Option<Public>> for IdentityValidatorIdOf {
|
||||
fn convert(key: Public) -> Option<Public> {
|
||||
Some(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl pallet_session::Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type ValidatorId = AccountId;
|
||||
type ValidatorIdOf = IdentityValidatorIdOf;
|
||||
type ShouldEndSession = Sessions;
|
||||
type NextSessionRotation = Sessions;
|
||||
type SessionManager = ();
|
||||
type SessionHandler = <SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
|
||||
type Keys = SessionKeys;
|
||||
type WeightInfo = pallet_session::weights::SubstrateWeight<Runtime>;
|
||||
}
|
||||
|
||||
pub type Address = AccountId;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
@@ -242,6 +272,8 @@ construct_runtime!(
|
||||
Balances: pallet_balances,
|
||||
TransactionPayment: pallet_transaction_payment,
|
||||
Contracts: pallet_contracts,
|
||||
Session: pallet_session,
|
||||
Tendermint: pallet_tendermint,
|
||||
}
|
||||
);
|
||||
|
||||
@@ -317,18 +349,6 @@ sp_api::impl_runtime_apis! {
|
||||
}
|
||||
}
|
||||
|
||||
impl sp_session::SessionKeys<Block> for Runtime {
|
||||
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
|
||||
opaque::SessionKeys::generate(seed)
|
||||
}
|
||||
|
||||
fn decode_session_keys(
|
||||
encoded: Vec<u8>,
|
||||
) -> Option<Vec<(Vec<u8>, KeyTypeId)>> {
|
||||
opaque::SessionKeys::decode_into_raw_public_keys(&encoded)
|
||||
}
|
||||
}
|
||||
|
||||
impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Index> for Runtime {
|
||||
fn account_nonce(account: AccountId) -> Index {
|
||||
System::account_nonce(account)
|
||||
|
||||
Reference in New Issue
Block a user