Skip to content

Commit

Permalink
Write trivial test to show mock setup works
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Aug 14, 2024
1 parent 8cc3457 commit 0a7ee3c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 36 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/attestation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pallet-timestamp ={ version="28.0.0", default-features=false }
sp-npos-elections ={ version="27.0.0", default-features=false }
frame-election-provider-support={ version="29.0.0", default-features=false }
pallet-staking-reward-curve ={ version="11.0.0" }
pallet-authorship ={ version="29.0.0", default-features=false }

[features]
default=['std']
Expand Down
2 changes: 1 addition & 1 deletion pallets/attestation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(100)]
#[pallet::weight({100})]
pub fn attest(origin: OriginFor<T>, quote: Vec<u8>) -> DispatchResult {
let who = ensure_signed(origin)?;
// Check that we were expecting a quote from this validator by getting the associated
Expand Down
62 changes: 28 additions & 34 deletions pallets/attestation/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use frame_election_provider_support::{
};
use frame_support::{
derive_impl, parameter_types,
traits::{ConstU32, OneSessionHandler, Randomness},
traits::{ConstU32, FindAuthor, OneSessionHandler, Randomness},
};
use frame_system as system;
use frame_system::EnsureRoot;
Expand All @@ -36,7 +36,6 @@ use std::cell::RefCell;

use crate as pallet_attestation;

const NULL_ARR: [u8; 32] = [0; 32];
type Block = frame_system::mocking::MockBlock<Test>;
type BlockNumber = u64;
type AccountId = u64;
Expand All @@ -49,7 +48,7 @@ frame_support::construct_runtime!(
Attestation: pallet_attestation,
System: frame_system,
Balances: pallet_balances,
// Authorship: pallet_authorship,
Authorship: pallet_authorship,
Timestamp: pallet_timestamp,
Staking: pallet_staking_extension,
FrameStaking: pallet_staking,
Expand All @@ -60,6 +59,10 @@ frame_support::construct_runtime!(
}
);

impl pallet_attestation::Config for Test {
type RuntimeEvent = RuntimeEvent;
}

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
Expand Down Expand Up @@ -310,49 +313,33 @@ parameter_types! {
pub const UncleGenerations: u64 = 0;
}

// Author of block is always 11
// pub struct Author11;
// impl FindAuthor<u64> for Author11 {
// fn find_author<'a, I>(_digests: I) -> Option<u64>
// where
// I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,
// {
// Some(11)
// }
// }
/// Author of block is always 11
pub struct Author11;
impl FindAuthor<u64> for Author11 {
fn find_author<'a, I>(_digests: I) -> Option<u64>
where
I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,
{
Some(11)
}
}

// impl pallet_authorship::Config for Test {
// type EventHandler = ();
// type FindAuthor = Author11;
// }
impl pallet_authorship::Config for Test {
type EventHandler = ();
type FindAuthor = Author11;
}

parameter_types! {
pub const MaxProgramHashes: u32 = 5u32;
pub const KeyVersionNumber: u8 = 1;
}

// impl pallet_registry::Config for Test {
// type RuntimeEvent = RuntimeEvent;
// type MaxProgramHashes = MaxProgramHashes;
// type KeyVersionNumber = KeyVersionNumber;
// type WeightInfo = ();
// }

parameter_types! {
pub const MaxBytecodeLength: u32 = 3;
pub const ProgramDepositPerByte: u32 = 5;
pub const MaxOwnedPrograms: u32 = 5;
}

// impl pallet_programs::Config for Test {
// type Currency = Balances;
// type MaxBytecodeLength = MaxBytecodeLength;
// type ProgramDepositPerByte = ProgramDepositPerByte;
// type MaxOwnedPrograms = MaxOwnedPrograms;
// type RuntimeEvent = RuntimeEvent;
// type WeightInfo = ();
// }

impl pallet_parameters::Config for Test {
type RuntimeEvent = RuntimeEvent;
type UpdateOrigin = EnsureRoot<Self::AccountId>;
Expand All @@ -361,5 +348,12 @@ impl pallet_parameters::Config for Test {

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap();

let pallet_attestation = pallet_attestation::GenesisConfig::<Test> {
initial_pending_attestations: vec![(0, [0; 32])],
};

pallet_attestation.assimilate_storage(&mut t).unwrap();
t.into()
}
5 changes: 4 additions & 1 deletion pallets/attestation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ use crate::mock::*;

#[test]
fn attest() {
new_test_ext().execute_with(|| {})
new_test_ext().execute_with(|| {
let nonce = Attestation::pending_attestations(0).unwrap();
assert_eq!(nonce, [0; 32]);
})
}

0 comments on commit 0a7ee3c

Please sign in to comment.