Skip to content

Commit

Permalink
add offchain mod
Browse files Browse the repository at this point in the history
  • Loading branch information
FereMouSiopi committed Dec 13, 2024
1 parent b652470 commit d671c0c
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 3 additions & 0 deletions substrate/frame/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ sp-genesis-builder = { optional = true, workspace = true }
sp-inherents = { optional = true, workspace = true }
sp-storage = { optional = true, workspace = true }
sp-keyring = { optional = true, workspace = true }
sp-application-crypto = { optional = true, workspace = true }

frame-executive = { optional = true, workspace = true }
frame-system-rpc-runtime-api = { optional = true, workspace = true }
Expand All @@ -72,6 +73,7 @@ runtime = [
"frame-executive",
"frame-system-rpc-runtime-api",
"sp-api",
"sp-application-crypto",
"sp-block-builder",
"sp-consensus-aura",
"sp-consensus-grandpa",
Expand All @@ -96,6 +98,7 @@ std = [
"log/std",
"scale-info/std",
"sp-api?/std",
"sp-application-crypto?/std",
"sp-arithmetic/std",
"sp-block-builder?/std",
"sp-consensus-aura?/std",
Expand Down
2 changes: 0 additions & 2 deletions substrate/frame/im-online/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ log = { workspace = true }
scale-info = { features = ["derive", "serde"], workspace = true }
frame = { workspace = true, features = ["experimental", "runtime"] }
pallet-authorship = { workspace = true }
sp-application-crypto = { features = ["serde"], workspace = true }
sp-staking = { features = ["serde"], workspace = true }

[dev-dependencies]
Expand All @@ -36,7 +35,6 @@ std = [
"pallet-authorship/std",
"pallet-session/std",
"scale-info/std",
"sp-application-crypto/std",
"sp-staking/std",
]
runtime-benchmarks = [
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/im-online/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#![cfg(feature = "runtime-benchmarks")]

use crate::*;
use frame::{benchmarking::prelude::*, traits::UnfilteredDispatchable};
use frame::benchmarking::prelude::*;

const MAX_KEYS: u32 = 1000;

Expand Down
32 changes: 8 additions & 24 deletions substrate/frame/im-online/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,11 @@ extern crate alloc;
use alloc::{vec, vec::Vec};
use codec::{Decode, Encode, MaxEncodedLen};
use frame::{
arithmetic::{PerThing, Perbill, Permill},
deps::{
frame_support::{BoundedSlice, WeakBoundedVec},
frame_system::offchain::{CreateInherent, SubmitTransaction},
sp_io,
sp_runtime::{
offchain::storage::{MutateStorageError, StorageRetrievalError, StorageValueRef},
print, BoundToRuntimeAppPublic,
},
},
derive::RuntimeDebug,
prelude::*,
traits::{
AtLeast32BitUnsigned, Convert, EstimateNextSessionRotation, OneSessionHandler,
SaturatedConversion, Saturating, TrailingZeroInput, ValidatorSet,
ValidatorSetWithIdentification,
},
traits::{ValidatorSet, ValidatorSetWithIdentification},
};
pub use pallet::*;
use scale_info::TypeInfo;
use sp_application_crypto::RuntimeAppPublic;
use sp_staking::{
offence::{Kind, Offence, ReportOffence},
SessionIndex,
Expand All @@ -115,11 +99,11 @@ pub use weights::WeightInfo;

pub mod sr25519 {
mod app_sr25519 {
use sp_application_crypto::{app_crypto, key_types::IM_ONLINE, sr25519};
use frame::deps::sp_application_crypto::{app_crypto, key_types::IM_ONLINE, sr25519};
app_crypto!(sr25519, IM_ONLINE);
}

sp_application_crypto::with_pair! {
frame::deps::sp_application_crypto::with_pair! {
/// An i'm online keypair using sr25519 as its crypto.
pub type AuthorityPair = app_sr25519::Pair;
}
Expand All @@ -133,11 +117,11 @@ pub mod sr25519 {

pub mod ed25519 {
mod app_ed25519 {
use sp_application_crypto::{app_crypto, ed25519, key_types::IM_ONLINE};
use frame::deps::sp_application_crypto::{app_crypto, ed25519, key_types::IM_ONLINE};
app_crypto!(ed25519, IM_ONLINE);
}

sp_application_crypto::with_pair! {
frame::deps::sp_application_crypto::with_pair! {
/// An i'm online keypair using ed25519 as its crypto.
pub type AuthorityPair = app_ed25519::Pair;
}
Expand Down Expand Up @@ -422,7 +406,7 @@ pub mod pallet {
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn offchain_worker(now: BlockNumberFor<T>) {
// Only send messages if we are a potential validator.
if sp_io::offchain::is_validator() {
if offchain::is_validator() {
for res in Self::send_heartbeats(now).into_iter().flatten() {
if let Err(e) = res {
log::debug!(
Expand Down Expand Up @@ -565,7 +549,7 @@ impl<T: Config> Pallet<T> {
let residual = Permill::from_rational(1u32, session_length.saturated_into());
let threshold: Permill = progress.saturating_pow(6).saturating_add(residual);

let seed = sp_io::offchain::random_seed();
let seed = offchain::random_seed();
let random = <u32>::decode(&mut TrailingZeroInput::new(seed.as_ref()))
.expect("input is padded with zeroes; qed");
let random = Permill::from_parts(random % Permill::ACCURACY);
Expand Down Expand Up @@ -806,7 +790,7 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
let validator_set_count = keys.len() as u32;
let offence = UnresponsivenessOffence { session_index, validator_set_count, offenders };
if let Err(e) = T::ReportUnresponsiveness::report_offence(vec![], offence) {
print(e);
frame::deps::sp_runtime::print(e);
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions substrate/frame/im-online/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
use super::*;
use alloc::vec::Vec;
use frame::{
testing_prelude::storage_alias,
traits::{OnRuntimeUpgrade, WrapperOpaque},
};
use frame::{testing_prelude::*, traits::WrapperOpaque};

#[cfg(feature = "try-runtime")]
use frame::deps::frame_support::ensure;
Expand Down
19 changes: 4 additions & 15 deletions substrate/frame/im-online/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,7 @@
#![cfg(test)]

use frame::{
arithmetic::Permill,
deps::{
frame_system, sp_io,
sp_runtime::{self, testing::UintAuthorityId},
},
runtime::{
prelude::{construct_runtime, derive_impl, parameter_types, weights::Weight},
testing_prelude::BuildStorage,
},
traits::{ConstU32, ConstU64, ConvertInto, EstimateNextSessionRotation},
};
use frame::testing_prelude::*;
use pallet_session::historical as pallet_session_historical;
use sp_staking::{
offence::{OffenceError, ReportOffence},
Expand Down Expand Up @@ -79,7 +68,7 @@ impl pallet_session::historical::SessionManager<u64, u64> for TestSessionManager
}

/// An extrinsic type used for tests.
pub type Extrinsic = sp_runtime::testing::TestXt<RuntimeCall, ()>;
pub type Extrinsic = TestXt<RuntimeCall, ()>;
type IdentificationTuple = (u64, u64);
type Offence = crate::UnresponsivenessOffence<IdentificationTuple>;

Expand All @@ -100,9 +89,9 @@ impl ReportOffence<u64, IdentificationTuple, Offence> for OffenceHandler {
}
}

pub fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext() -> TestExternalities {
let t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
let mut result: sp_io::TestExternalities = t.into();
let mut result: TestExternalities = t.into();
// Set the default keys, otherwise session will discard the validator.
result.execute_with(|| {
for i in 1..=6 {
Expand Down
15 changes: 3 additions & 12 deletions substrate/frame/im-online/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,8 @@

use super::*;
use crate::mock::*;
use frame::{
deps::{
frame_support::dispatch,
sp_core::offchain::{
testing::{TestOffchainExt, TestTransactionPoolExt},
OffchainDbExt, OffchainWorkerExt, TransactionPoolExt,
},
sp_runtime::testing::UintAuthorityId,
},
testing_prelude::*,
};
use frame::testing_prelude::*;

#[test]
fn test_unresponsiveness_slash_fraction() {
let dummy_offence =
Expand Down Expand Up @@ -115,7 +106,7 @@ fn heartbeat(
authority_index: u32,
id: UintAuthorityId,
validators: Vec<u64>,
) -> dispatch::DispatchResult {
) -> DispatchResult {
let heartbeat = Heartbeat {
block_number,
session_index,
Expand Down
54 changes: 46 additions & 8 deletions substrate/frame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@ pub mod prelude {

/// Dispatch types from `frame-support`, other fundamental traits
#[doc(no_inline)]
pub use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};
pub use frame_support::traits::{Contains, IsSubType, OnRuntimeUpgrade};
pub use frame_support::dispatch::{DispatchResult, GetDispatchInfo, PostDispatchInfo};
pub use frame_support::traits::{
Contains, IsSubType, OnRuntimeUpgrade, UnfilteredDispatchable,
};

/// Pallet prelude of `frame-system`.
#[doc(no_inline)]
Expand All @@ -214,15 +216,25 @@ pub mod prelude {
pub use super::derive::*;

/// All hashing related things
pub use super::hashing::*;
pub use super::cryptography::*;

/// All arithmetic types used for safe math.
pub use super::arithmetic::*;

pub use super::offchain::*;

pub use super::session::*;

/// Runtime traits
#[doc(no_inline)]
pub use sp_runtime::traits::{
BlockNumberProvider, Bounded, DispatchInfoOf, Dispatchable, SaturatedConversion,
Saturating, StaticLookup, TrailingZeroInput,
BlockNumberProvider, Bounded, Convert, ConvertInto, DispatchInfoOf, Dispatchable,
SaturatedConversion, Saturating, StaticLookup, TrailingZeroInput,
};

/// Bounded storage related types.
pub use sp_runtime::{BoundedSlice, BoundedVec, WeakBoundedVec};

/// Other error/result types for runtime
#[doc(no_inline)]
pub use sp_runtime::{DispatchErrorWithPostInfo, DispatchResultWithInfo, TokenError};
Expand Down Expand Up @@ -493,7 +505,10 @@ pub mod runtime {
#[cfg(feature = "std")]
pub mod testing_prelude {
pub use sp_core::storage::Storage;
pub use sp_runtime::BuildStorage;
pub use sp_runtime::{
testing::{TestSignature, TestXt, UintAuthorityId},
BuildStorage,
};
}
}

Expand Down Expand Up @@ -527,11 +542,32 @@ pub mod derive {
pub use sp_runtime::RuntimeDebug;
}

pub mod hashing {
pub use sp_core::{hashing::*, H160, H256, H512, U256, U512};
pub mod cryptography {
pub use sp_application_crypto::{BoundToRuntimeAppPublic, RuntimeAppPublic};
pub use sp_core::{
crypto::{VrfPublic, VrfSecret, Wraps},
hashing::*,
Pair, H160, H256, H512, U256, U512,
};
pub use sp_runtime::traits::{BlakeTwo256, Hash, Keccak256};
}

pub mod offchain {
pub use frame_system::offchain::{CreateInherent, SubmitTransaction};
pub use sp_core::offchain::{
testing::{TestOffchainExt, TestTransactionPoolExt},
OffchainDbExt, OffchainWorkerExt, TransactionPoolExt,
};
pub use sp_io::offchain;
pub use sp_runtime::offchain::storage::{
MutateStorageError, StorageRetrievalError, StorageValueRef,
};
}

pub mod session {
pub use frame_support::traits::{EstimateNextSessionRotation, OneSessionHandler};
}

/// Access to all of the dependencies of this crate. In case the prelude re-exports are not enough,
/// this module can be used.
///
Expand All @@ -558,6 +594,8 @@ pub mod deps {
#[cfg(feature = "runtime")]
pub use sp_api;
#[cfg(feature = "runtime")]
pub use sp_application_crypto;
#[cfg(feature = "runtime")]
pub use sp_block_builder;
#[cfg(feature = "runtime")]
pub use sp_consensus_aura;
Expand Down

0 comments on commit d671c0c

Please sign in to comment.