Skip to content

Commit

Permalink
bring drand into mock runtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnReedV committed Nov 21, 2024
1 parent 46b512f commit 357fd6a
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions pallets/admin-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ log = { workspace = true }
pallet-subtensor = { version = "4.0.0-dev", default-features = false, path = "../subtensor" }
sp-weights = { workspace = true }
substrate-fixed = { workspace = true }
pallet-drand = { workspace = true, default-features = false }


[dev-dependencies]
Expand Down
79 changes: 76 additions & 3 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::U256;
use sp_core::{ConstU64, H256};
use sp_runtime::{
testing::TestXt,
traits::{BlakeTwo256, ConstU32, IdentityLookup},
BuildStorage, Perbill,
BuildStorage, KeyTypeId, Perbill,
};
use sp_std::cmp::Ordering;
use sp_weights::Weight;
Expand All @@ -21,13 +22,13 @@ type Block = frame_system::mocking::MockBlock<Test>;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test
{
pub enum Test {
System: frame_system = 1,
Balances: pallet_balances = 2,
AdminUtils: pallet_admin_utils = 3,
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
}
);

Expand Down Expand Up @@ -63,6 +64,10 @@ pub type Balance = u64;
#[allow(dead_code)]
pub type BlockNumber = u64;

pub type TestAuthId = test_crypto::TestAuthId;
pub type Index = u64;
pub type UncheckedExtrinsic = TestXt<RuntimeCall, ()>;

parameter_types! {
pub const InitialMinAllowedWeights: u16 = 0;
pub const InitialEmissionValue: u16 = 0;
Expand Down Expand Up @@ -272,6 +277,74 @@ impl pallet_scheduler::Config for Test {
type Preimages = ();
}

impl pallet_drand::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_drand::weights::SubstrateWeight<Test>;
type AuthorityId = TestAuthId;
type Verifier = pallet_drand::verifier::QuicknetVerifier;
type UnsignedPriority = ConstU64<{ 1 << 20 }>;
type HttpFetchTimeout = ConstU64<1_000>;
}

impl frame_system::offchain::SigningTypes for Test {
type Public = test_crypto::Public;
type Signature = test_crypto::Signature;
}

pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"test");

mod test_crypto {
use super::KEY_TYPE;
use sp_core::sr25519::{Public as Sr25519Public, Signature as Sr25519Signature};
use sp_core::U256;
use sp_runtime::{
app_crypto::{app_crypto, sr25519},
traits::IdentifyAccount,
};

app_crypto!(sr25519, KEY_TYPE);

pub struct TestAuthId;

impl frame_system::offchain::AppCrypto<Public, Signature> for TestAuthId {
type RuntimeAppPublic = Public;
type GenericSignature = Sr25519Signature;
type GenericPublic = Sr25519Public;
}

impl IdentifyAccount for Public {
type AccountId = U256;

fn into_account(self) -> U256 {
let mut bytes = [0u8; 32];
bytes.copy_from_slice(self.as_ref());
U256::from_big_endian(&bytes)
}
}
}

impl frame_system::offchain::CreateSignedTransaction<pallet_drand::Call<Test>> for Test {
fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: RuntimeCall,
_public: Self::Public,
_account: Self::AccountId,
nonce: Index,
) -> Option<(
RuntimeCall,
<UncheckedExtrinsic as sp_runtime::traits::Extrinsic>::SignaturePayload,
)> {
Some((call, (nonce, ())))
}
}

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

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
sp_tracing::try_init_simple();
Expand Down
91 changes: 85 additions & 6 deletions pallets/subtensor/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use frame_support::{
use frame_system as system;
use frame_system::{limits, EnsureNever, EnsureRoot, RawOrigin};
use pallet_collective::MemberCount;
use sp_core::{Get, H256, U256};
use sp_core::{offchain::KeyTypeId, ConstU64, Get, H256, U256};
use sp_runtime::Perbill;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
Expand All @@ -34,6 +34,7 @@ frame_support::construct_runtime!(
Utility: pallet_utility::{Pallet, Call, Storage, Event} = 8,
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 9,
Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>} = 10,
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 11,
}
);

Expand All @@ -49,10 +50,9 @@ pub type BalanceCall = pallet_balances::Call<Test>;
#[allow(dead_code)]
pub type TestRuntimeCall = frame_system::Call<Test>;

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}
pub type Index = u64;

pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"test");

#[allow(dead_code)]
pub type AccountId = U256;
Expand Down Expand Up @@ -112,6 +112,11 @@ impl system::Config for Test {
type Block = Block;
}

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}

parameter_types! {
pub const InitialMinAllowedWeights: u16 = 0;
pub const InitialEmissionValue: u16 = 0;
Expand Down Expand Up @@ -452,6 +457,80 @@ impl pallet_preimage::Config for Test {
type Consideration = ();
}

mod test_crypto {
use super::KEY_TYPE;
use sp_core::{
sr25519::{Public as Sr25519Public, Signature as Sr25519Signature},
U256,
};
use sp_runtime::{
app_crypto::{app_crypto, sr25519},
traits::IdentifyAccount,
};

app_crypto!(sr25519, KEY_TYPE);

pub struct TestAuthId;

// Implement AppCrypto for TestAuthId using test_crypto::Public and test_crypto::Signature
impl frame_system::offchain::AppCrypto<Public, Signature> for TestAuthId {
type RuntimeAppPublic = Public;
type GenericSignature = Sr25519Signature;
type GenericPublic = Sr25519Public;
}

// Implement IdentifyAccount for Public
impl IdentifyAccount for Public {
type AccountId = U256;

fn into_account(self) -> U256 {
let mut bytes = [0u8; 32];
bytes.copy_from_slice(self.as_ref());
U256::from_big_endian(&bytes)
}
}
}

pub type TestAuthId = test_crypto::TestAuthId;

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

impl pallet_drand::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_drand::weights::SubstrateWeight<Test>;
type AuthorityId = TestAuthId;
type Verifier = pallet_drand::verifier::QuicknetVerifier;
type UnsignedPriority = ConstU64<{ 1 << 20 }>;
type HttpFetchTimeout = ConstU64<1_000>;
}

impl frame_system::offchain::SigningTypes for Test {
type Public = test_crypto::Public;
type Signature = test_crypto::Signature;
}

pub type UncheckedExtrinsic = sp_runtime::testing::TestXt<RuntimeCall, ()>;

impl frame_system::offchain::CreateSignedTransaction<pallet_drand::Call<Test>> for Test {
fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: RuntimeCall,
_public: Self::Public,
_account: Self::AccountId,
nonce: Index,
) -> Option<(
RuntimeCall,
<UncheckedExtrinsic as sp_runtime::traits::Extrinsic>::SignaturePayload,
)> {
Some((call, (nonce, ())))
}
}

#[allow(dead_code)]
// Build genesis storage according to the mock runtime.
pub fn new_test_ext(block_number: BlockNumber) -> sp_io::TestExternalities {
Expand Down Expand Up @@ -531,7 +610,7 @@ pub(crate) fn step_epochs(count: u16, netuid: u16) {
}
}

/// Increments current block by `1`, running all hooks associated with doing so, and asserts
/// Increments current block by 1, running all hooks associated with doing so, and asserts
/// that the block number was in fact incremented.
///
/// Returns the new block number.
Expand Down

0 comments on commit 357fd6a

Please sign in to comment.