Skip to content

Commit 385e8a0

Browse files
Merge pull request #4 from omadoyeabraham/development
[development --> staging] Completed tests for pallet-multisend
2 parents e9cfc60 + cb672fa commit 385e8a0

File tree

15 files changed

+371
-96
lines changed

15 files changed

+371
-96
lines changed

Cargo.lock

Lines changed: 33 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Since this is a test blockchain project being used to hone my blockchain/substra
88
to accomplish the following test goals:
99

1010
1. Create MultiSend functionality to allow users send tokens to a list of people at once
11+
- [ ] Write tests for this pallet
12+
- [ ] Benchmark this pallet
13+
- [ ] Account for transaction fees when "multisending" tokens
1114
2. Create a skill verification system
1215

1316
## Getting Started

node/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "omniblock-node"
3-
version = "4.0.0-dev"
3+
version = "1.0.0"
44
description = "An OmniBlock Blockchain Node"
55
authors = ["Omadoye Abraham <[email protected]>"]
66
homepage = "https://omniblock.chain/"
@@ -39,6 +39,7 @@ sc-client-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/su
3939
sp-runtime = { version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
4040
sp-timestamp = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
4141

42+
4243
# These dependencies are used for the node template's RPCs
4344
jsonrpc-core = "18.0.0"
4445
sc-rpc = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
@@ -55,7 +56,7 @@ frame-benchmarking = { version = "4.0.0-dev", git = "https://github.com/parityte
5556
frame-benchmarking-cli = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
5657

5758
# Local Dependencies
58-
omniblock-runtime = { version = "4.0.0-dev", path = "../runtime" }
59+
omniblock-runtime = { version = "1.0.0", path = "../runtime" }
5960

6061
[build-dependencies]
6162
substrate-build-script-utils = { version = "3.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }

node/src/chain_spec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn testnet_genesis(
138138
code: wasm_binary.to_vec(),
139139
},
140140
balances: BalancesConfig {
141-
// Configure endowed accounts with initial balance of 1 << 60.
141+
// Configure endowed accounts with initial balance of 1 << 60. i.e 1_152_921_504_606_846_976
142142
balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(),
143143
},
144144
aura: AuraConfig {

node/src/command.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ impl SubstrateCli for Cli {
3636
Ok(match id {
3737
"dev" => Box::new(chain_spec::development_config()?),
3838
"" | "local" => Box::new(chain_spec::local_testnet_config()?),
39-
path =>
40-
Box::new(chain_spec::ChainSpec::from_json_file(std::path::PathBuf::from(path))?),
39+
path => {
40+
Box::new(chain_spec::ChainSpec::from_json_file(std::path::PathBuf::from(path))?)
41+
},
4142
})
4243
}
4344

4445
fn native_runtime_version(_: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
45-
&node_template_runtime::VERSION
46+
&omniblock_runtime::VERSION
4647
}
4748
}
4849

@@ -98,7 +99,7 @@ pub fn run() -> sc_cli::Result<()> {
9899
Ok((cmd.run(client, backend), task_manager))
99100
})
100101
},
101-
Some(Subcommand::Benchmark(cmd)) =>
102+
Some(Subcommand::Benchmark(cmd)) => {
102103
if cfg!(feature = "runtime-benchmarks") {
103104
let runner = cli.create_runner(cmd)?;
104105

@@ -107,7 +108,8 @@ pub fn run() -> sc_cli::Result<()> {
107108
Err("Benchmarking wasn't enabled when building the node. You can enable it with \
108109
`--features runtime-benchmarks`."
109110
.into())
110-
},
111+
}
112+
},
111113
#[cfg(feature = "try-runtime")]
112114
Some(Subcommand::TryRuntime(cmd)) => {
113115
let runner = cli.create_runner(cmd)?;

node/src/service.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
2323
type ExtendHostFunctions = ();
2424

2525
fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
26-
node_template_runtime::api::dispatch(method, data)
26+
omniblock_runtime::api::dispatch(method, data)
2727
}
2828

2929
fn native_version() -> sc_executor::NativeVersion {
30-
node_template_runtime::native_version()
30+
omniblock_runtime::native_version()
3131
}
3232
}
3333

@@ -59,7 +59,7 @@ pub fn new_partial(
5959
ServiceError,
6060
> {
6161
if config.keystore_remote.is_some() {
62-
return Err(ServiceError::Other("Remote Keystores are not supported.".into()))
62+
return Err(ServiceError::Other("Remote Keystores are not supported.".into()));
6363
}
6464

6565
let telemetry = config
@@ -172,11 +172,12 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
172172
if let Some(url) = &config.keystore_remote {
173173
match remote_keystore(url) {
174174
Ok(k) => keystore_container.set_remote_keystore(k),
175-
Err(e) =>
175+
Err(e) => {
176176
return Err(ServiceError::Other(format!(
177177
"Error hooking up remote keystore for {}: {}",
178178
url, e
179-
))),
179+
)))
180+
},
180181
};
181182
}
182183
let grandpa_protocol_name = sc_finality_grandpa::protocol_standard_name(

pallets/multisend/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[package]
22
name = "pallet-multisend"
3-
version = "4.0.0-dev"
3+
version = "1.0.0"
44
description = "FRAME pallet which allows users send tokens to a list of people at once"
55
authors = ["Omadoye Abraham <[email protected]>"]
66
homepage = "https://omadoyeabraham.com"
77
edition = "2021"
88
license = "Unlicense"
99
publish = false
10-
repository = "https://github.com/substrate-developer-hub/substrate-node-template/"
10+
repository = "https://github.com/omadoyeabraham/omniblock/"
1111

1212
[package.metadata.docs.rs]
1313
targets = ["x86_64-unknown-linux-gnu"]
@@ -20,20 +20,26 @@ scale-info = { version = "2.0.1", default-features = false, features = ["derive"
2020
frame-support = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18"}
2121
frame-system = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
2222
frame-benchmarking = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18", optional = true }
23+
pallet-balances = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
24+
sp-std = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
25+
random-string = "1.0"
2326

2427
[dev-dependencies]
2528
sp-core = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
2629
sp-io = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
2730
sp-runtime = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
2831

32+
2933
[features]
3034
default = ["std"]
3135
std = [
3236
"codec/std",
3337
"scale-info/std",
38+
"sp-std/std",
3439
"frame-support/std",
3540
"frame-system/std",
3641
"frame-benchmarking/std",
42+
"pallet-balances/std"
3743
]
3844

3945
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]

pallets/multisend/src/benchmarking.rs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,59 @@
33
use super::*;
44

55
#[allow(unused)]
6-
use crate::Pallet as Template;
7-
use frame_benchmarking::{benchmarks, whitelisted_caller};
86
use frame_system::RawOrigin;
7+
use crate::Pallet as MultiSend;
8+
use frame_benchmarking::{account, benchmarks};
9+
use frame_support::{inherent::Vec, traits::Currency};
10+
11+
const SEED: u32 = 0;
12+
const NUMBER_OF_RECEIVING_ACCOUNTS: u32 = 50;
13+
14+
/// Grab a funded user.
15+
fn create_funded_user<T: Config>(
16+
string: &'static str,
17+
n: u32,
18+
balance_factor: u32,
19+
) -> T::AccountId {
20+
let user = account(string, n, SEED);
21+
let balance = T::Currency::minimum_balance() * balance_factor.into();
22+
let _ = T::Currency::make_free_balance_be(&user, balance);
23+
user
24+
}
25+
26+
// Loop and do the following an arbitrary number of times (e.g n = 100), each time measuring the execution time
27+
// Setup
28+
// -- Create and fund the sending account with (n+1) * 10 tokens, this allows it to send 10 tokens to n accounts and still have some tokens leftover
29+
// -- Create n number of receiving accounts with an existensial deposit
30+
31+
// Action
32+
// -- Call the send_tokens_to_multiple_receivers extrinsic from the pallet
33+
34+
// Verify
35+
// -- Ascertain that the sending account only has 10 tokens left
36+
// -- Ascertain that the receiving accounts all received their 10 tokens
937

1038
benchmarks! {
11-
do_something {
12-
let s in 0 .. 100;
13-
let caller: T::AccountId = whitelisted_caller();
14-
}: _(RawOrigin::Signed(caller), s)
39+
send_tokens_to_multiple_receivers {
40+
let sender = create_funded_user::<T>("user", SEED, (NUMBER_OF_RECEIVING_ACCOUNTS + 1) * 100);
41+
let sender_initial_balance = T::Currency::free_balance(&sender);
42+
let mut token_transfer_requests = Vec::new();
43+
44+
for i in 1 .. NUMBER_OF_RECEIVING_ACCOUNTS {
45+
token_transfer_requests.push(
46+
TokenTransferRequest {
47+
receiver_account: create_funded_user::<T>("receiver", i, 1),
48+
token_amount: T::Currency::minimum_balance() + 10u32.into() } // Send 10 tokens
49+
);
50+
}
51+
52+
}: _(RawOrigin::Signed(sender.clone()), token_transfer_requests.clone())
1553
verify {
16-
assert_eq!(Something::<T>::get(), Some(s));
54+
assert_eq!(true, T::Currency::free_balance(&sender) < sender_initial_balance);
55+
for request in token_transfer_requests {
56+
assert_eq!(T::Currency::minimum_balance() + T::Currency::minimum_balance() + 10u32.into(), T::Currency::free_balance(&request.receiver_account)) // expect 11 tokens = 1 minimum_balance + 10 sent
57+
}
1758
}
1859

19-
impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test);
60+
impl_benchmark_test_suite!(MultiSend, crate::mock::new_test_ext(), crate::mock::Test);
2061
}

0 commit comments

Comments
 (0)