Skip to content

Commit

Permalink
Merge pull request #114 from agoraxyz/I110-im-online-fix
Browse files Browse the repository at this point in the history
Add pallet-im-online storage migration
  • Loading branch information
OnyxSkyscape authored Mar 23, 2023
2 parents b082aa8 + d511143 commit 3d46290
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 58 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ substrate-frame-rpc-system = { version = "15.0.0", default-features = false }
pallet-aura = { version = "14.0.0", default-features = false }
pallet-balances = { version = "15.0.0", default-features = false }
pallet-grandpa = { version = "15.0.0", default-features = false }
pallet-im-online = { version = "14.0.0", default-features = false }
pallet-randomness-collective-flip = { version = "15.0.0", default-features = false }
pallet-session = { version = "15.0.0", default-features = false }
pallet-sudo = { version = "15.0.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions gn-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ frame-system = { workspace = true, optional = true }
substrate-frame-rpc-system = { workspace = true }

# substrate pallets
pallet-im-online = { workspace = true }
pallet-transaction-payment = { workspace = true, optional = true }
pallet-transaction-payment-rpc = { workspace = true }

Expand Down
9 changes: 7 additions & 2 deletions gn-node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use gn_runtime::{
AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, SessionConfig, Signature,
SudoConfig, SystemConfig, ValidatorManagerConfig, WASM_BINARY,
AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, ImOnlineConfig,
SessionConfig, Signature, SudoConfig, SystemConfig, ValidatorManagerConfig, WASM_BINARY,
};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use sc_service::ChainType;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{sr25519, Pair, Public};
Expand Down Expand Up @@ -35,6 +36,7 @@ struct AuthorityKeys {
account_id: AccountId,
aura_id: AuraId,
grandpa_id: GrandpaId,
im_online_id: ImOnlineId,
}

impl AuthorityKeys {
Expand All @@ -43,13 +45,15 @@ impl AuthorityKeys {
account_id: get_account_id_from_seed::<sr25519::Public>(seed),
aura_id: get_from_seed::<AuraId>(seed),
grandpa_id: get_from_seed::<GrandpaId>(seed),
im_online_id: get_from_seed::<ImOnlineId>(seed),
}
}

pub fn to_session_keys(&self) -> gn_runtime::opaque::SessionKeys {
gn_runtime::opaque::SessionKeys {
aura: self.aura_id.clone(),
grandpa: self.grandpa_id.clone(),
im_online: self.im_online_id.clone(),
}
}
}
Expand Down Expand Up @@ -182,6 +186,7 @@ fn testnet_genesis(
})
.collect(),
},
im_online: ImOnlineConfig { keys: vec![] },
aura: AuraConfig {
authorities: vec![],
},
Expand Down
1 change: 0 additions & 1 deletion gn-pallets/pallet-validator-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#![deny(clippy::dbg_macro)]
#![deny(unused_crate_dependencies)]

pub mod migration;
#[cfg(test)]
mod mock;
#[cfg(test)]
Expand Down
15 changes: 0 additions & 15 deletions gn-pallets/pallet-validator-manager/src/migration.rs

This file was deleted.

9 changes: 7 additions & 2 deletions gn-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ std = [
"frame-support/std",
"frame-system/std",
"frame-system-rpc-runtime-api/std",
"frame-try-runtime/std",
"frame-try-runtime?/std",
"pallet-aura/std",
"pallet-balances/std",
"pallet-grandpa/std",
"pallet-guild/std",
"pallet-im-online/std",
"pallet-oracle/std",
"pallet-randomness-collective-flip/std",
"pallet-session/std",
Expand All @@ -57,12 +58,14 @@ std = [
]
try-runtime = [
"frame-executive/try-runtime",
"frame-support/try-runtime",
"frame-system/try-runtime",
"frame-try-runtime",
"pallet-aura/try-runtime",
"pallet-balances/try-runtime",
"pallet-grandpa/try-runtime",
"pallet-guild/try-runtime",
"pallet-im-online/try-runtime",
"pallet-oracle/try-runtime",
"pallet-randomness-collective-flip/try-runtime",
"pallet-sudo/try-runtime",
Expand All @@ -81,6 +84,7 @@ pallet-validator-manager = { version = "0.0.0-alpha", path = "../gn-pallets/pall
hex-literal = { version = "0.3.4", optional = true }
parity-scale-codec = { workspace = true, features = ["derive"] }
scale-info = { workspace = true, features = ["derive"] }
log = { version = "0.4.17", default-features = false }

# substrate frame
frame-benchmarking = { workspace = true, optional = true }
Expand All @@ -89,12 +93,13 @@ frame-support = { workspace = true, optional = true }
frame-system = { workspace = true, optional = true }
frame-system-benchmarking = { workspace = true, optional = true }
frame-system-rpc-runtime-api = { workspace = true, optional = true }
frame-try-runtime = { workspace = true, optional = true }
frame-try-runtime = { workspace = true, features = ["try-runtime"], optional = true }

# substrate pallets
pallet-aura = { workspace = true }
pallet-balances = { workspace = true }
pallet-grandpa = { workspace = true }
pallet-im-online = { workspace = true }
pallet-randomness-collective-flip = { workspace = true }
pallet-session = { workspace = true }
pallet-sudo = { workspace = true }
Expand Down
154 changes: 116 additions & 38 deletions gn-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub use pallet_balances::Call as BalancesCall;
use pallet_grandpa::{
fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList,
};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use parity_scale_codec::Encode;
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
Expand All @@ -24,15 +26,17 @@ use sp_runtime::{
Verify,
},
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, MultiSignature,
ApplyExtrinsicResult, MultiSignature, SaturatedConversion,
};
use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;

use frame_support::{
construct_runtime, parameter_types,
construct_runtime,
pallet_prelude::TransactionPriority,
parameter_types,
traits::{ConstU32, ConstU64, ConstU8, KeyOwnerProofSystem},
weights::{
constants::{RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND},
Expand Down Expand Up @@ -75,10 +79,20 @@ pub mod opaque {
/// Opaque block identifier type.
pub type BlockId = generic::BlockId<Block>;

// TODO Remove after im_online runtime upgrade is done.
impl_opaque_keys! {
pub struct OldSessionKeys {
pub aura: Aura,
pub grandpa: Grandpa,

}
}

impl_opaque_keys! {
pub struct SessionKeys {
pub aura: Aura,
pub grandpa: Grandpa,
pub im_online: ImOnline,
}
}
}
Expand All @@ -95,7 +109,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 101,
spec_version: 102,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -145,6 +159,10 @@ parameter_types! {
pub const MinAuthorities: u32 = 2;
pub const Period: u32 = 2 * MINUTES;
pub const Offset: u32 = 0;
pub const ImOnlineUnsignedPriority: TransactionPriority = TransactionPriority::max_value();
pub const MaxKeys: u32 = 10_000;
pub const MaxPeerInHeartbeats: u32 = 10_000;
pub const MaxPeerDataEncodingSize: u32 = 1_000;
}

// Configure FRAME pallets to include in runtime.
Expand Down Expand Up @@ -203,6 +221,79 @@ impl frame_system::Config for Runtime {

impl pallet_randomness_collective_flip::Config for Runtime {}

impl<Call> frame_system::offchain::CreateSignedTransaction<Call> for Runtime
where
RuntimeCall: From<Call>,
{
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 sp_runtime::traits::Extrinsic>::SignaturePayload,
)> {
let tip = 0;
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 = generic::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(tip),
);
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<Call> frame_system::offchain::SendTransactionTypes<Call> for Runtime
where
RuntimeCall: From<Call>,
{
type Extrinsic = UncheckedExtrinsic;
type OverarchingCall = RuntimeCall;
}

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

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

impl pallet_aura::Config for Runtime {
type AuthorityId = AuraId;
type DisabledValidators = ();
Expand Down Expand Up @@ -299,6 +390,26 @@ impl pallet_validator_manager::Config for Runtime {
type MinAuthorities = MinAuthorities;
}

// should be removed along with UpgradeSessionKeys
fn transform_session_keys(_v: AccountId, old: opaque::OldSessionKeys) -> opaque::SessionKeys {
let dummy_id = pallet_im_online::sr25519::AuthorityId::try_from(old.aura.as_ref()).unwrap();

opaque::SessionKeys {
grandpa: old.grandpa,
aura: old.aura,
im_online: dummy_id,
}
}

// When this is removed, should also remove `OldSessionKeys`.
pub struct UpgradeSessionKeys;
impl frame_support::traits::OnRuntimeUpgrade for UpgradeSessionKeys {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
Session::upgrade_keys::<opaque::OldSessionKeys, _>(transform_session_keys);
BlockWeights::get().max_block
}
}

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub enum Runtime where
Expand All @@ -315,6 +426,7 @@ construct_runtime!(
Timestamp: pallet_timestamp,
ValidatorManager: pallet_validator_manager,
Session: pallet_session,
ImOnline: pallet_im_online,
Aura: pallet_aura,
Grandpa: pallet_grandpa,

Expand Down Expand Up @@ -354,41 +466,9 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
// TODO remove this after migration
vm_upgrade::Upgrade,
UpgradeSessionKeys,
>;

// TODO remove this after migration (validator manager pallet)
mod vm_upgrade {
use super::*;
use frame_support::traits::OnRuntimeUpgrade;

pub struct Upgrade;
impl OnRuntimeUpgrade for Upgrade {
fn on_runtime_upgrade() -> Weight {
pallet_validator_manager::migration::on_runtime_upgrade::<Runtime>();
BlockWeights::get().max_block
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
assert!(!ValidatorManager::validators().is_empty(), "invalid state");
Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
assert!(!ValidatorManager::validators().is_empty(), "invalid state");
assert_eq!(
ValidatorManager::validators(),
ValidatorManager::approved_validators(),
"migration failed"
);
Ok(())
}
}
}

#[cfg(feature = "runtime-benchmarks")]
#[macro_use]
extern crate frame_benchmarking;
Expand Down Expand Up @@ -590,8 +670,6 @@ impl_runtime_apis! {
}
}



#[cfg(feature = "try-runtime")]
impl frame_try_runtime::TryRuntime<Block> for Runtime {
fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) {
Expand Down

0 comments on commit 3d46290

Please sign in to comment.