Skip to content

Commit 5d76d4f

Browse files
authored
feat: Introduce pallet-rewards for block reward distribution (#131)
* feat: Introduce `np-arithmetic` * feat: Introduce `np-rewards` * feat: Introduce `pallet-rewards` for block reward distribution
1 parent 634813a commit 5d76d4f

File tree

13 files changed

+1161
-0
lines changed

13 files changed

+1161
-0
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@ members = [
2323
"frame/cosmos/x/wasm",
2424
"frame/cosmos/x/wasm/types",
2525
"frame/multimap",
26+
"frame/rewards",
2627
"frame/solana",
2728
"frame/solana/runtime-api",
2829
"frame/wtema",
30+
"primitives/arithmetic",
2931
"primitives/babel",
3032
"primitives/consensus", # dummy
3133
"primitives/consensus/pow",
3234
"primitives/cosmos",
3335
"primitives/ethereum",
3436
"primitives/multimap",
3537
"primitives/nostr",
38+
"primitives/rewards",
3639
"primitives/runtime",
3740
"primitives/solana",
3841
"runtime/common",
@@ -159,6 +162,7 @@ wat = "1.0"
159162
# substrate
160163
sc-client-api = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409" }
161164
sc-consensus = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409" }
165+
frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409", default-features = false }
162166
frame-support = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409", default-features = false }
163167
frame-system = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409", default-features = false }
164168
pallet-assets = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409", default-features = false }
@@ -198,12 +202,14 @@ nc-consensus = { path = "client/consensus" }
198202
nc-consensus-pow = { path = "client/consensus/pow" }
199203
noir-core-primitives = { path = "core-primitives", default-features = false }
200204
noir-runtime-common = { path = "runtime/common", default-features = false }
205+
np-arithmetic = { path = "primitives/arithmetic", default-features = false }
201206
np-babel = { path = "primitives/babel", default-features = false }
202207
np-consensus-pow = { path = "primitives/consensus/pow", default-features = false }
203208
np-cosmos = { path = "primitives/cosmos", default-features = false }
204209
np-ethereum = { path = "primitives/ethereum", default-features = false }
205210
np-multimap = { path = "primitives/multimap", default-features = false }
206211
np-nostr = { path = "primitives/nostr", default-features = false }
212+
np-rewards = { path = "primitives/rewards", default-features = false }
207213
np-runtime = { path = "primitives/runtime", default-features = false }
208214
np-solana = { path = "primitives/solana", default-features = false }
209215
pallet-cosmos = { path = "frame/cosmos", default-features = false }
@@ -216,6 +222,7 @@ pallet-cosmos-x-bank-types = { path = "frame/cosmos/x/bank/types", default-featu
216222
pallet-cosmos-x-wasm = { path = "frame/cosmos/x/wasm", default-features = false }
217223
pallet-cosmos-x-wasm-types = { path = "frame/cosmos/x/wasm/types", default-features = false }
218224
pallet-multimap = { path = "frame/multimap", default-features = false }
225+
pallet-rewards = { path = "frame/rewards", default-features = false }
219226
pallet-solana = { path = "frame/solana", default-features = false }
220227
pallet-wtema = { path = "frame/wtema", default-features = false }
221228

frame/rewards/Cargo.toml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[package]
2+
name = "pallet-rewards"
3+
description = "FRAME rewards for block reward distribution"
4+
license = "GPL-3.0-or-later"
5+
authors = { workspace = true }
6+
version = { workspace = true }
7+
edition = { workspace = true }
8+
repository = { workspace = true }
9+
publish = false
10+
11+
[dependencies]
12+
frame-benchmarking = { workspace = true, optional = true }
13+
frame-support = { workspace = true }
14+
frame-system = { workspace = true }
15+
np-arithmetic = { workspace = true }
16+
np-rewards = { workspace = true }
17+
parity-scale-codec = { workspace = true, features = ["derive"] }
18+
scale-info = { workspace = true, features = ["derive"] }
19+
sp-inherents = { workspace = true }
20+
21+
[dev-dependencies]
22+
pallet-balances = { workspace = true, default-features = true }
23+
sp-io = { workspace = true, default-features = true }
24+
25+
[features]
26+
default = ["std"]
27+
std = [
28+
"frame-benchmarking?/std",
29+
"frame-support/std",
30+
"frame-system/std",
31+
"np-arithmetic/std",
32+
"np-rewards/std",
33+
"parity-scale-codec/std",
34+
"scale-info/std",
35+
"sp-inherents/std",
36+
]
37+
runtime-benchmarks = [
38+
"frame-benchmarking/runtime-benchmarks",
39+
"frame-support/runtime-benchmarks",
40+
"frame-system/runtime-benchmarks",
41+
]
42+
try-runtime = [
43+
"frame-support/try-runtime",
44+
"frame-system/try-runtime",
45+
]

frame/rewards/src/benchmarking.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// This file is part of Noir.
2+
3+
// Copyright (C) Haderech Pte. Ltd.
4+
// SPDX-License-Identifier: GPL-3.0-or-later
5+
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
19+
#![cfg(feature = "runtime-benchmarks")]
20+
21+
use super::*;
22+
use frame_benchmarking::v2::*;
23+
use frame_support::{
24+
sp_runtime::traits::{Get, One},
25+
traits::Hooks,
26+
};
27+
use frame_system::RawOrigin;
28+
29+
const SEED: u32 = 0;
30+
31+
#[benchmarks]
32+
mod benchmarks {
33+
use super::*;
34+
35+
#[benchmark]
36+
fn coinbase(n: Linear<1, { T::MaxRewardSplits::get() }>) -> Result<(), BenchmarkError> {
37+
type System<T> = frame_system::Pallet<T>;
38+
39+
let mut reward = T::EmissionSchedule::get();
40+
let mut rewards: Vec<(T::AccountId, BalanceOf<T>)> = Vec::new();
41+
let payout = T::MinPayout::get();
42+
for index in 1..n {
43+
reward -= payout;
44+
rewards.push((account("miner", index, SEED), payout));
45+
}
46+
let payout = reward;
47+
rewards.push((account("miner", n, SEED), reward));
48+
49+
#[extrinsic_call]
50+
_(RawOrigin::None, rewards.clone());
51+
52+
assert_eq!(Processed::<T>::get(), true);
53+
assert_eq!(Rewards::<T>::get(System::<T>::block_number()), rewards);
54+
assert_eq!(RewardLocks::<T>::get(account::<T::AccountId>("miner", n, SEED)), Some(payout));
55+
56+
Ok(())
57+
}
58+
59+
#[benchmark]
60+
fn on_finalize(n: Linear<1, { T::MaxRewardSplits::get() }>) {
61+
type Rewards<T> = Pallet<T>;
62+
type System<T> = frame_system::Pallet<T>;
63+
64+
let number = System::<T>::block_number() + One::one();
65+
let mut reward = T::EmissionSchedule::get();
66+
let mut rewards: Vec<(T::AccountId, BalanceOf<T>)> = Vec::new();
67+
let payout = T::MinPayout::get();
68+
for index in 1..n {
69+
reward -= payout;
70+
rewards.push((account("miner", index, SEED), payout));
71+
}
72+
rewards.push((account("miner", n, SEED), reward));
73+
Rewards::<T>::insert_coinbase(number, rewards);
74+
75+
#[block]
76+
{
77+
Rewards::<T>::on_finalize(number);
78+
Rewards::<T>::on_initialize(number + T::MaturationTime::get());
79+
}
80+
}
81+
82+
impl_benchmark_test_suite!(Rewards, crate::mock::new_test_ext(), crate::mock::Text,);
83+
}

0 commit comments

Comments
 (0)