Skip to content

Commit

Permalink
[Update] Feature Added the Im_online pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
MauricinhoMorales committed Feb 21, 2024
1 parent e9d159a commit f498428
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 9 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ try-runtime-cli = { version = "0.10.0-dev", optional = true, git = "https://gith
# The node's runtime
sugarfunge-runtime = { version = "0.1.0", path = "../runtime" }

[dependencies.pallet-im-online]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
branch = 'polkadot-v0.9.43'

[build-dependencies]
substrate-build-script-utils = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }

Expand Down
15 changes: 9 additions & 6 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use sc_service::ChainType;
use serde_json::json;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_consensus_grandpa::AuthorityId as GrandpaId;
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use sp_core::{sr25519, Pair, Public};
use sp_runtime::traits::{IdentifyAccount, Verify};
use sugarfunge_runtime::{
opaque::SessionKeys, AccountId, AuraConfig, Balance, BalancesConfig, CouncilConfig,
GenesisConfig, GrandpaConfig, SessionConfig, Signature, SudoConfig,
SystemConfig, ValidatorSetConfig, DOLLARS, WASM_BINARY,
SystemConfig, ValidatorSetConfig, DOLLARS, WASM_BINARY, ImOnlineConfig,
};

// The URL for the telemetry server.
Expand All @@ -33,16 +34,17 @@ where
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}

fn session_keys(aura: AuraId, grandpa: GrandpaId) -> SessionKeys {
SessionKeys { aura, grandpa }
fn session_keys(aura: AuraId, grandpa: GrandpaId, im_online: ImOnlineId) -> SessionKeys {
SessionKeys { aura, grandpa, im_online }
}

/// Generate an Aura authority key.
pub fn authority_keys_from_seed(s: &str) -> (AccountId, AuraId, GrandpaId) {
pub fn authority_keys_from_seed(s: &str) -> (AccountId, AuraId, GrandpaId, ImOnlineId) {
(
get_account_id_from_seed::<sr25519::Public>(s),
get_from_seed::<AuraId>(s),
get_from_seed::<GrandpaId>(s),
get_from_seed::<ImOnlineId>(s),
)
}

Expand Down Expand Up @@ -156,7 +158,7 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AccountId, AuraId, GrandpaId)>,
initial_authorities: Vec<(AccountId, AuraId, GrandpaId, ImOnlineId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
Expand Down Expand Up @@ -189,7 +191,7 @@ fn testnet_genesis(
(
x.0.clone(),
x.0.clone(),
session_keys(x.1.clone(), x.2.clone()),
session_keys(x.1.clone(), x.2.clone(), x.3.clone()),
)
})
.collect::<Vec<_>>(),
Expand All @@ -209,6 +211,7 @@ fn testnet_genesis(
grandpa: GrandpaConfig {
authorities: vec![],
},
im_online: ImOnlineConfig { keys: vec![] },
sudo: SudoConfig {
// Assign network admin rights.
key: Some(root_key),
Expand Down
6 changes: 6 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ sugarfunge-market = { default-features = false, path = "../pallets/market" }
functionland-fula = { git = "https://github.com/functionland/fula-pallet" , branch = "main" , default-features = false }
fula-pool = { git = "https://github.com/functionland/fula-pool" , branch = "main" , default-features = false }

[dependencies.pallet-im-online]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
branch = 'polkadot-v0.9.43'

[build-dependencies]
substrate-wasm-builder = { version = "5.0.0-dev", git = "https://github.com/paritytech/substrate", optional = true, branch = "polkadot-v0.9.43" }

Expand Down Expand Up @@ -155,6 +160,7 @@ std = [
"functionland-fula/std",
"fula-pool/std",
"sugarfunge-primitives/std",
"pallet-im-online/std",
]

runtime-benchmarks = [
Expand Down
82 changes: 79 additions & 3 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use pallet_grandpa::AuthorityId as GrandpaId;

use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_transaction_payment::{ConstFeeMultiplier, CurrencyAdapter, Multiplier};
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::traits::{
AccountIdLookup, BlakeTwo256, Block as BlockT, NumberFor, One, OpaqueKeys,
AccountIdLookup, BlakeTwo256, Block as BlockT, NumberFor, One, OpaqueKeys, Verify, Extrinsic
};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
Expand All @@ -24,6 +24,7 @@ use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use sp_runtime::generic::Era;
use sugarfunge_validator_set as validator_set;

// A few exports that help ease life for downstream crates.
Expand All @@ -41,7 +42,10 @@ pub use frame_support::{
},
ConsensusEngineId, PalletId, StorageValue,
};
use frame_support::pallet_prelude::TransactionPriority;
use frame_system::EnsureRoot;
use sp_runtime::SaturatedConversion;
use codec::Encode;

#[cfg(any(feature = "std", test))]
pub use frame_system::Call as SystemCall;
Expand Down Expand Up @@ -92,6 +96,7 @@ pub mod opaque {
pub struct SessionKeys {
pub aura: Aura,
pub grandpa: Grandpa,
pub im_online: ImOnline,
}
}
}
Expand Down Expand Up @@ -200,7 +205,7 @@ impl frame_system::Config for Runtime {
}

parameter_types! {
pub const MaxAuthorities: u32 = 32;
pub const MaxAuthorities: u32 = 1000;
}

impl pallet_aura::Config for Runtime {
Expand Down Expand Up @@ -493,6 +498,76 @@ impl fula_pool::Config for Runtime {
type MaxPoolParticipants = MaxPoolParticipants;
}

parameter_types! {
pub const ImOnlineUnsignedPriority: TransactionPriority = TransactionPriority::max_value();
pub const StakingUnsignedPriority: TransactionPriority = TransactionPriority::max_value() / 2;
pub const MaxKeys: u32 = 10_000;
pub const MaxPeerInHeartbeats: u32 = 10_000;
pub const MaxPeerDataEncodingSize: u32 = 1_000;
}

impl<LocalCall> frame_system::offchain::CreateSignedTransaction<LocalCall> for Runtime
where
RuntimeCall: From<LocalCall>,
{
fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: RuntimeCall,
public: <Signature as Verify>::Signer,
account: AccountId,
nonce: Index,
) -> Option<(RuntimeCall, <UncheckedExtrinsic as Extrinsic>::SignaturePayload)> {
let period =
BlockHashCount::get().checked_next_power_of_two().map(|c| c / 2).unwrap_or(2) as u64;
let current_block = System::block_number().saturated_into::<u64>().saturating_sub(1);
let era = Era::mortal(period, current_block);
let extra = (
frame_system::CheckNonZeroSender::<Runtime>::new(),
frame_system::CheckSpecVersion::<Runtime>::new(),
frame_system::CheckTxVersion::<Runtime>::new(),
frame_system::CheckGenesis::<Runtime>::new(),
frame_system::CheckEra::<Runtime>::from(era),
frame_system::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
);
let raw_payload = SignedPayload::new(call, extra)
.map_err(|e| {
log::warn!("Unable to create signed payload: {:?}", e);
})
.ok()?;
let signature = raw_payload.using_encoded(|payload| C::sign(payload, public))?;
let address = account;
let (call, extra, _) = raw_payload.deconstruct();
Some((call, (sp_runtime::MultiAddress::Id(address), signature, extra)))
}
}

impl frame_system::offchain::SigningTypes for Runtime {
type Public = <Signature as Verify>::Signer;
type Signature = Signature;
}

impl<C> frame_system::offchain::SendTransactionTypes<C> for Runtime
where
RuntimeCall: From<C>,
{
type Extrinsic = UncheckedExtrinsic;
type OverarchingCall = RuntimeCall;
}

impl pallet_im_online::Config for Runtime {
type AuthorityId = ImOnlineId;
type RuntimeEvent = RuntimeEvent;
type NextSessionRotation = pallet_session::PeriodicSessions<Period, Offset>;
type ValidatorSet = ValidatorSet;
type ReportUnresponsiveness = ValidatorSet;
type UnsignedPriority = ImOnlineUnsignedPriority;
type WeightInfo = pallet_im_online::weights::SubstrateWeight<Runtime>;
type MaxKeys = MaxKeys;
type MaxPeerInHeartbeats = MaxPeerInHeartbeats;
type MaxPeerDataEncodingSize = MaxPeerDataEncodingSize;
}

construct_runtime!(
pub struct Runtime where
Block = Block,
Expand All @@ -511,6 +586,7 @@ construct_runtime!(
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>},
Council: pallet_collective::<Instance1>,
Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>},
ImOnline: pallet_im_online,

// SugarFunge pallets
Asset: sugarfunge_asset::{Pallet, Call, Storage, Event<T>},
Expand Down

0 comments on commit f498428

Please sign in to comment.