diff --git a/Cargo.lock b/Cargo.lock index 60a28eb32..df3887bf7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6014,6 +6014,7 @@ dependencies = [ "frame-system", "log", "pallet-balances", + "pallet-drand", "pallet-scheduler", "pallet-subtensor", "parity-scale-codec", diff --git a/pallets/admin-utils/Cargo.toml b/pallets/admin-utils/Cargo.toml index c67c00914..c5cf200d1 100644 --- a/pallets/admin-utils/Cargo.toml +++ b/pallets/admin-utils/Cargo.toml @@ -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] diff --git a/pallets/admin-utils/tests/mock.rs b/pallets/admin-utils/tests/mock.rs index dca08ab72..86444933c 100644 --- a/pallets/admin-utils/tests/mock.rs +++ b/pallets/admin-utils/tests/mock.rs @@ -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; @@ -21,13 +22,13 @@ type Block = frame_system::mocking::MockBlock; // 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, Error} = 4, Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event} = 5, + Drand: pallet_drand::{Pallet, Call, Storage, Event} = 6, } ); @@ -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; + parameter_types! { pub const InitialMinAllowedWeights: u16 = 0; pub const InitialEmissionValue: u16 = 0; @@ -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; + 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 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> for Test { + fn create_transaction>( + call: RuntimeCall, + _public: Self::Public, + _account: Self::AccountId, + nonce: Index, + ) -> Option<( + RuntimeCall, + ::SignaturePayload, + )> { + Some((call, (nonce, ()))) + } +} + +impl frame_system::offchain::SendTransactionTypes for Test +where + RuntimeCall: From, +{ + 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(); diff --git a/pallets/subtensor/tests/mock.rs b/pallets/subtensor/tests/mock.rs index 7a2967e8c..d909390fd 100644 --- a/pallets/subtensor/tests/mock.rs +++ b/pallets/subtensor/tests/mock.rs @@ -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}, @@ -34,6 +34,7 @@ frame_support::construct_runtime!( Utility: pallet_utility::{Pallet, Call, Storage, Event} = 8, Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event} = 9, Preimage: pallet_preimage::{Pallet, Call, Storage, Event} = 10, + Drand: pallet_drand::{Pallet, Call, Storage, Event} = 11, } ); @@ -49,10 +50,9 @@ pub type BalanceCall = pallet_balances::Call; #[allow(dead_code)] pub type TestRuntimeCall = frame_system::Call; -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; @@ -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; @@ -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 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 frame_system::offchain::SendTransactionTypes for Test +where + RuntimeCall: From, +{ + type Extrinsic = UncheckedExtrinsic; + type OverarchingCall = RuntimeCall; +} + +impl pallet_drand::Config for Test { + type RuntimeEvent = RuntimeEvent; + type WeightInfo = pallet_drand::weights::SubstrateWeight; + 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; + +impl frame_system::offchain::CreateSignedTransaction> for Test { + fn create_transaction>( + call: RuntimeCall, + _public: Self::Public, + _account: Self::AccountId, + nonce: Index, + ) -> Option<( + RuntimeCall, + ::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 { @@ -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.