Skip to content

Commit

Permalink
Merge pull request #24 from ZeroDAO/dev
Browse files Browse the repository at this point in the history
1. Fixed e2e bugs.
2. Added frame_system_ext pallet unit tests.
  • Loading branch information
DarkingLee authored Sep 29, 2023
2 parents 3d4c25e + 57d76bb commit ffafef0
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 85 deletions.
3 changes: 3 additions & 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 crates/core-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sc-client-api = {optional = true, default-features = false, git = "https://githu
sc-offchain = {optional = true, default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

# For testing
lazy_static = "1.4"
rand = { version = "0.8.5", optional = true }
sp-application-crypto = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

Expand Down
37 changes: 37 additions & 0 deletions crates/core-primitives/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
// limitations under the License.

//! Testing utilities.
use crate::HeaderExtension;
use crate::traits::ExtendedHeader;
use crate::traits::HeaderCommitList;
use crate::Header as HeaderT;
use lazy_static::lazy_static;
use melo_das_primitives::KZGCommitment;

use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer};
Expand All @@ -44,6 +46,17 @@ use std::{
ops::Deref,
};

lazy_static! {
pub static ref TEST_COMMITMENTS: Vec<KZGCommitment> = vec![
KZGCommitment::rand(),
KZGCommitment::rand(),
KZGCommitment::rand(),
KZGCommitment::rand(),
KZGCommitment::rand(),
KZGCommitment::rand(),
];
}

pub struct CommitListTest();

impl HeaderCommitList for CommitListTest {
Expand All @@ -52,6 +65,30 @@ impl HeaderCommitList for CommitListTest {
}
}

pub struct CommitListTestWithData();

impl HeaderCommitList for CommitListTestWithData {
fn last() -> Vec<KZGCommitment> {
TEST_COMMITMENTS.to_vec()
}
}

impl CommitListTestWithData {
pub fn commit_bytes() -> Vec<u8> {
TEST_COMMITMENTS
.iter()
.map(|c| c.to_bytes())
.flatten()
.collect()
}

pub fn header_extension() -> HeaderExtension {
HeaderExtension {
commitments_bytes: Self::commit_bytes(),
}
}
}

/// A dummy type which can be used instead of regular cryptographic primitives.
///
/// 1. Wraps a `u64` `AccountId` and is able to `IdentifyAccount`.
Expand Down
16 changes: 10 additions & 6 deletions crates/frame-executive-ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,8 @@ mod tests {
use pallet_transaction_payment::CurrencyAdapter;

use melo_core_primitives::{
testing::{Block, Digest, CommitListTest},
Header as ExtendedHeader,
testing::{Block, CommitListTestWithData, Digest},
Header as ExtendedHeader, HeaderExtension,
};

const TEST_KEY: &[u8] = b":test:key:";
Expand Down Expand Up @@ -906,7 +906,7 @@ mod tests {
}

impl frame_system_ext::Config for Runtime {
type CommitList = CommitListTest;
type CommitList = CommitListTestWithData;
type ExtendedHeader = Header;
}

Expand Down Expand Up @@ -984,6 +984,10 @@ mod tests {
RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest, value })
}

fn extension_test() -> HeaderExtension {
CommitListTestWithData::header_extension()
}

#[test]
fn balance_transfer_dispatch_works() {
let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
Expand Down Expand Up @@ -1055,7 +1059,7 @@ mod tests {
"03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
),
digest: Digest { logs: vec![] },
extension: Default::default(),
extension: extension_test(),
},
extrinsics: vec![],
});
Expand All @@ -1075,7 +1079,7 @@ mod tests {
"03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
),
digest: Digest { logs: vec![] },
extension: Default::default(),
extension: extension_test(),
},
extrinsics: vec![],
});
Expand All @@ -1095,7 +1099,7 @@ mod tests {
),
extrinsics_root: [0u8; 32].into(),
digest: Digest { logs: vec![] },
extension: Default::default(),
extension: extension_test(),
},
extrinsics: vec![],
});
Expand Down
4 changes: 4 additions & 0 deletions crates/frame-system-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
scale-info = { version = "2.1.1", default-features = false, features = ["derive"]}
serde = { version = "1.0.136", features = ["derive"], optional = true }

[dev-dependencies]
sp-io = { version = "7.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-core = { version = "7.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

[features]
default = ["std"]
std = [
Expand Down
76 changes: 76 additions & 0 deletions crates/frame-system-ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,79 @@ impl<T: Config> Pallet<T> {
ext_header
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate as frame_system_ext;
use frame_support::{
parameter_types,
traits::{ConstU32, ConstU64},
};
use melo_core_primitives::{testing::CommitListTestWithData, Header as ExtendedHeader};
use sp_core::H256;
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};

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

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Runtime>;
type Block = frame_system::mocking::MockBlock<Runtime>;

pub type Header = ExtendedHeader<u64, BlakeTwo256>;

// Mock runtime to test the module.
frame_support::construct_runtime! {
pub struct Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
SystemExt: frame_system_ext::{Pallet},
}
}

impl frame_system::Config for Runtime {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type Index = u64;
type BlockNumber = u64;
type RuntimeCall = RuntimeCall;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
}

impl Config for Runtime {
type ExtendedHeader = Header; // Mocked or a concrete type can be provided
type CommitList = CommitListTestWithData; // Mocked or a concrete type can be provided
}

#[test]
fn finalize_works() {
let mut ext = sp_io::TestExternalities::new_empty();

ext.execute_with(|| {
let header = SystemExt::finalize();
assert_eq!(header.extension, CommitListTestWithData::header_extension());
});
}
}
Loading

0 comments on commit ffafef0

Please sign in to comment.