Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC Fuzzing Assets #394

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions evm-template/Cargo.lock

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

4 changes: 4 additions & 0 deletions evm-template/template-fuzzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sp-consensus-aura = { workspace = true }
sp-runtime = { workspace = true }
sp-state-machine = { workspace = true }

pallet-assets = { workspace = true }
pallet-balances = { workspace = true }
pallet-collator-selection = { workspace = true }
pallet-multisig = { workspace = true }
Expand All @@ -43,6 +44,9 @@ cumulus-primitives-core = { workspace = true }
cumulus-primitives-parachain-inherent = { workspace = true }
cumulus-test-relay-sproof-builder = { workspace = true }

clap = { workspace = true }
fp-account = {workspace = true}

[features]
default = [ "std", "try-runtime" ]
std = [
Expand Down
166 changes: 166 additions & 0 deletions evm-template/template-fuzzer/src/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
use evm_runtime_template::{AccountId, RuntimeCall};
use fp_account::AccountId20;
use parity_scale_codec::Compact;

use crate::ExtrinsicData;

pub fn generate_extrinsics_stream(data: &mut [u8]) -> ExtrinsicData {
let mut res = vec![];
let (mut ptr, mut call) = generate_call(data);
while let Some(generated_call) = call {
res.push(generated_call);
(ptr, call) = generate_call(ptr);
}
res
}

fn generate_call(data: &[u8]) -> (&[u8], Option<(u8, u8, RuntimeCall)>) {
// RuntimeCall
if (data.len() < 3) {
return (data, None);
}
let lapse = data[0];
let origin = data[1];
let call_id = data[2];
let mut next_ptr = &data[3..];

let call = match call_id % 7 {
0 => {
if next_ptr.len() < 52 {
return (next_ptr, None);
}
let mut data: [u8; 16] = [0u8; 16];
data.copy_from_slice(&next_ptr[0..16]);
let id = Compact(u128::from_le_bytes(data));
let mut data_20: [u8; 20] = [0u8; 20];
data_20.copy_from_slice(&next_ptr[16..36]);
let beneficiary = AccountId20::from(data_20);
data.copy_from_slice(&next_ptr[36..52]);
let amount = u128::from_le_bytes(data);
next_ptr = &next_ptr[52..];
RuntimeCall::Assets (
pallet_assets::Call::mint {
id,
beneficiary,
amount
}
)
},
1 => {
if next_ptr.len() < 52 {
return (next_ptr, None);
}
let mut data: [u8; 16] = [0u8; 16];
data.copy_from_slice(&next_ptr[0..16]);
let id = Compact(u128::from_le_bytes(data));
let mut data_20: [u8; 20] = [0u8; 20];
data_20.copy_from_slice(&next_ptr[16..36]);
let who = AccountId20::from(data_20);
data.copy_from_slice(&next_ptr[36..52]);
let amount = u128::from_le_bytes(data);
next_ptr = &next_ptr[52..];
RuntimeCall::Assets (
pallet_assets::Call::burn {
id,
who,
amount
}
)
},
2 => {
if next_ptr.len() < 52 {
return (next_ptr, None);
}
let mut data: [u8; 16] = [0u8; 16];
data.copy_from_slice(&next_ptr[0..16]);
let id = Compact(u128::from_le_bytes(data));
let mut data_20: [u8; 20] = [0u8; 20];
data_20.copy_from_slice(&next_ptr[16..36]);
let target = AccountId20::from(data_20);
data.copy_from_slice(&next_ptr[36..52]);
let amount = u128::from_le_bytes(data);
next_ptr = &next_ptr[52..];
RuntimeCall::Assets (
pallet_assets::Call::transfer {
id,
target,
amount
}
)
},
3 => {
if next_ptr.len() < 52 {
return (next_ptr, None);
}
let mut data: [u8; 16] = [0u8; 16];
data.copy_from_slice(&next_ptr[0..16]);
let id = Compact(u128::from_le_bytes(data));
let mut data_20: [u8; 20] = [0u8; 20];
data_20.copy_from_slice(&next_ptr[16..36]);
let target = AccountId20::from(data_20);
data.copy_from_slice(&next_ptr[36..52]);
let amount = u128::from_le_bytes(data);
next_ptr = &next_ptr[52..];
RuntimeCall::Assets (
pallet_assets::Call::transfer_keep_alive {
id,
target,
amount
}
)
},
4 => {
if next_ptr.len() < 52 {
return (next_ptr, None);
}
let mut data: [u8; 16] = [0u8; 16];
data.copy_from_slice(&next_ptr[0..16]);
let id = Compact(u128::from_le_bytes(data));
let mut data_20: [u8; 20] = [0u8; 20];
data_20.copy_from_slice(&next_ptr[16..36]);
let delegate = AccountId20::from(data_20);
data.copy_from_slice(&next_ptr[36..52]);
let amount = u128::from_le_bytes(data);
next_ptr = &next_ptr[52..];
RuntimeCall::Assets (
pallet_assets::Call::approve_transfer { id, delegate, amount }
)
},
5 => {
if next_ptr.len() < 72 {
return (next_ptr, None);
}
let mut data: [u8; 16] = [0u8; 16];
data.copy_from_slice(&next_ptr[0..16]);
let id = Compact(u128::from_le_bytes(data));
let mut data_20: [u8; 20] = [0u8; 20];
data_20.copy_from_slice(&next_ptr[16..36]);
let owner = AccountId20::from(data_20);
data_20.copy_from_slice(&next_ptr[36..56]);
let destination = AccountId20::from(data_20);
data.copy_from_slice(&next_ptr[56..72]);
let amount = u128::from_le_bytes(data);
next_ptr = &next_ptr[72..];
RuntimeCall::Assets (
pallet_assets::Call::transfer_approved { id, owner, destination, amount }
)
},
6 => {
if next_ptr.len() < 32 {
return (next_ptr, None);
}
let mut data: [u8; 16] = [0u8; 16];
data.copy_from_slice(&next_ptr[0..16]);
let id = Compact(u128::from_le_bytes(data));
data.copy_from_slice(&next_ptr[16..32]);
let min_balance = u128::from_le_bytes(data);
next_ptr = &next_ptr[32..];
RuntimeCall::Assets (
pallet_assets::Call::set_min_balance { id, min_balance }
)
}
_ => unreachable!()
};

(&next_ptr, Some((lapse, origin, call)))
}
109 changes: 29 additions & 80 deletions evm-template/template-fuzzer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
time::{Duration, Instant},
};

use clap::Args;
// Local Imports
use evm_runtime_template::{
constants::SLOT_DURATION, AccountId, AllPalletsWithSystem, Balance, Balances, EVMChainIdConfig,
Expand All @@ -26,6 +27,11 @@ use sp_runtime::{
};
use sp_state_machine::BasicExternalities;

mod random;
mod assets;

type ExtrinsicData = Vec<(/* lapse */ u8, /* origin */ u8, RuntimeCall)>;

fn generate_genesis(accounts: &[AccountId]) -> Storage {
use evm_runtime_template::{BalancesConfig, RuntimeGenesisConfig};
#[cfg(not(feature = "tanssi"))]
Expand Down Expand Up @@ -79,13 +85,16 @@ fn generate_genesis(accounts: &[AccountId]) -> Storage {
.unwrap()
}

fn process_input(accounts: &[AccountId], genesis: &Storage, data: &[u8]) {
let mut data = data;
fn process_input(
accounts: &[AccountId],
genesis: &Storage,
data: &mut [u8],
generate_extrinsics: impl Fn(&mut [u8]) -> ExtrinsicData,
call_filter: impl Fn(&RuntimeCall, usize) -> bool,
) {
// We build the list of extrinsics we will execute
let extrinsics: Vec<(/* lapse */ u8, /* origin */ u8, RuntimeCall)> =
iter::from_fn(|| DecodeLimit::decode_with_depth_limit(64, &mut data).ok())
.filter(|(_, _, x)| !matches!(x, RuntimeCall::System(_)))
.collect();
let extrinsics: ExtrinsicData = generate_extrinsics(data);

if extrinsics.is_empty() {
return;
}
Expand All @@ -101,7 +110,7 @@ fn process_input(accounts: &[AccountId], genesis: &Storage, data: &[u8]) {

for (lapse, origin, extrinsic) in extrinsics {
let origin_no = origin as usize % accounts.len();
if !recursive_call_filter(&extrinsic, origin_no) {
if !call_filter(&extrinsic, origin_no) {
continue;
}
if lapse > 0 {
Expand Down Expand Up @@ -209,77 +218,6 @@ fn initialize_block(block: u32) {
// Calls that need to be called before each block starts (init_calls) go here
}

fn recursive_call_filter(call: &RuntimeCall, origin: usize) -> bool {
match call {
//recursion
RuntimeCall::Sudo(
pallet_sudo::Call::sudo { call }
| pallet_sudo::Call::sudo_unchecked_weight { call, weight: _ },
) if origin == 0 => recursive_call_filter(call, origin),
RuntimeCall::Utility(
pallet_utility::Call::with_weight { call, weight: _ }
| pallet_utility::Call::dispatch_as { as_origin: _, call }
| pallet_utility::Call::as_derivative { index: _, call },
) => recursive_call_filter(call, origin),
RuntimeCall::Utility(
pallet_utility::Call::force_batch { calls }
| pallet_utility::Call::batch { calls }
| pallet_utility::Call::batch_all { calls },
) => calls.iter().map(|call| recursive_call_filter(call, origin)).all(|e| e),
RuntimeCall::Scheduler(
pallet_scheduler::Call::schedule_named_after {
id: _,
after: _,
maybe_periodic: _,
priority: _,
call,
}
| pallet_scheduler::Call::schedule { when: _, maybe_periodic: _, priority: _, call }
| pallet_scheduler::Call::schedule_named {
when: _,
id: _,
maybe_periodic: _,
priority: _,
call,
}
| pallet_scheduler::Call::schedule_after {
after: _,
maybe_periodic: _,
priority: _,
call,
},
) => recursive_call_filter(call, origin),
RuntimeCall::Multisig(
pallet_multisig::Call::as_multi_threshold_1 { other_signatories: _, call }
| pallet_multisig::Call::as_multi {
threshold: _,
other_signatories: _,
maybe_timepoint: _,
call,
max_weight: _,
},
) => recursive_call_filter(call, origin),
RuntimeCall::Whitelist(
pallet_whitelist::Call::dispatch_whitelisted_call_with_preimage { call },
) => recursive_call_filter(call, origin),

// restrictions
RuntimeCall::Sudo(_) if origin != 0 => false,
RuntimeCall::System(
frame_system::Call::set_code { .. } | frame_system::Call::kill_prefix { .. },
) => false,
#[cfg(not(feature = "tanssi"))]
RuntimeCall::CollatorSelection(
pallet_collator_selection::Call::set_desired_candidates { max },
) =>
*max < <<Runtime as pallet_collator_selection::Config>::MaxCandidates as Get<u32>>::get(
),
RuntimeCall::Balances(pallet_balances::Call::force_adjust_total_issuance { .. }) => false,

_ => true,
}
}

fn finalize_block(elapsed: Duration) {
println!("\n time spent: {elapsed:?}");
assert!(elapsed.as_secs() <= 2, "block execution took too much time");
Expand Down Expand Up @@ -317,10 +255,21 @@ fn check_invariants(block: u32, initial_total_issuance: Balance) {
}

fn main() {
// let args = cli::Args::parse();
let accounts: Vec<AccountId> = (0..5).map(|i| [i; 32].into()).collect();
let genesis = generate_genesis(&accounts);

// if args.random {
// (random::generate_extrinsic_stream, random::recursive_call_filter)
// } else if args.assets {

// } else {
// println!("Mode was not specified. Halting");
// return;
// };

ziggy::fuzz!(|data: &[u8]| {
process_input(&accounts, &genesis, data);
let mut data = data.to_vec();
process_input(&accounts, &genesis, &mut data, assets::generate_extrinsics_stream, |_, _| true);
});
}
}
Loading
Loading