Skip to content

Commit

Permalink
feat: Introduce pallet-rewards for block reward distribution (#131)
Browse files Browse the repository at this point in the history
* feat: Introduce `np-arithmetic`

* feat: Introduce `np-rewards`

* feat: Introduce `pallet-rewards` for block reward distribution
  • Loading branch information
conr2d authored Feb 3, 2025
1 parent 634813a commit 5d76d4f
Show file tree
Hide file tree
Showing 13 changed files with 1,161 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ members = [
"frame/cosmos/x/wasm",
"frame/cosmos/x/wasm/types",
"frame/multimap",
"frame/rewards",
"frame/solana",
"frame/solana/runtime-api",
"frame/wtema",
"primitives/arithmetic",
"primitives/babel",
"primitives/consensus", # dummy
"primitives/consensus/pow",
"primitives/cosmos",
"primitives/ethereum",
"primitives/multimap",
"primitives/nostr",
"primitives/rewards",
"primitives/runtime",
"primitives/solana",
"runtime/common",
Expand Down Expand Up @@ -159,6 +162,7 @@ wat = "1.0"
# substrate
sc-client-api = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409" }
sc-consensus = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409" }
frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409", default-features = false }
frame-support = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409", default-features = false }
frame-system = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409", default-features = false }
pallet-assets = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2409", default-features = false }
Expand Down Expand Up @@ -198,12 +202,14 @@ nc-consensus = { path = "client/consensus" }
nc-consensus-pow = { path = "client/consensus/pow" }
noir-core-primitives = { path = "core-primitives", default-features = false }
noir-runtime-common = { path = "runtime/common", default-features = false }
np-arithmetic = { path = "primitives/arithmetic", default-features = false }
np-babel = { path = "primitives/babel", default-features = false }
np-consensus-pow = { path = "primitives/consensus/pow", default-features = false }
np-cosmos = { path = "primitives/cosmos", default-features = false }
np-ethereum = { path = "primitives/ethereum", default-features = false }
np-multimap = { path = "primitives/multimap", default-features = false }
np-nostr = { path = "primitives/nostr", default-features = false }
np-rewards = { path = "primitives/rewards", default-features = false }
np-runtime = { path = "primitives/runtime", default-features = false }
np-solana = { path = "primitives/solana", default-features = false }
pallet-cosmos = { path = "frame/cosmos", default-features = false }
Expand All @@ -216,6 +222,7 @@ pallet-cosmos-x-bank-types = { path = "frame/cosmos/x/bank/types", default-featu
pallet-cosmos-x-wasm = { path = "frame/cosmos/x/wasm", default-features = false }
pallet-cosmos-x-wasm-types = { path = "frame/cosmos/x/wasm/types", default-features = false }
pallet-multimap = { path = "frame/multimap", default-features = false }
pallet-rewards = { path = "frame/rewards", default-features = false }
pallet-solana = { path = "frame/solana", default-features = false }
pallet-wtema = { path = "frame/wtema", default-features = false }

Expand Down
45 changes: 45 additions & 0 deletions frame/rewards/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "pallet-rewards"
description = "FRAME rewards for block reward distribution"
license = "GPL-3.0-or-later"
authors = { workspace = true }
version = { workspace = true }
edition = { workspace = true }
repository = { workspace = true }
publish = false

[dependencies]
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
np-arithmetic = { workspace = true }
np-rewards = { workspace = true }
parity-scale-codec = { workspace = true, features = ["derive"] }
scale-info = { workspace = true, features = ["derive"] }
sp-inherents = { workspace = true }

[dev-dependencies]
pallet-balances = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }

[features]
default = ["std"]
std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"np-arithmetic/std",
"np-rewards/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-inherents/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
]
83 changes: 83 additions & 0 deletions frame/rewards/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// This file is part of Noir.

// Copyright (C) Haderech Pte. Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#![cfg(feature = "runtime-benchmarks")]

use super::*;
use frame_benchmarking::v2::*;
use frame_support::{
sp_runtime::traits::{Get, One},
traits::Hooks,
};
use frame_system::RawOrigin;

const SEED: u32 = 0;

#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn coinbase(n: Linear<1, { T::MaxRewardSplits::get() }>) -> Result<(), BenchmarkError> {
type System<T> = frame_system::Pallet<T>;

let mut reward = T::EmissionSchedule::get();
let mut rewards: Vec<(T::AccountId, BalanceOf<T>)> = Vec::new();
let payout = T::MinPayout::get();
for index in 1..n {
reward -= payout;
rewards.push((account("miner", index, SEED), payout));
}
let payout = reward;
rewards.push((account("miner", n, SEED), reward));

#[extrinsic_call]
_(RawOrigin::None, rewards.clone());

assert_eq!(Processed::<T>::get(), true);
assert_eq!(Rewards::<T>::get(System::<T>::block_number()), rewards);
assert_eq!(RewardLocks::<T>::get(account::<T::AccountId>("miner", n, SEED)), Some(payout));

Ok(())
}

#[benchmark]
fn on_finalize(n: Linear<1, { T::MaxRewardSplits::get() }>) {
type Rewards<T> = Pallet<T>;
type System<T> = frame_system::Pallet<T>;

let number = System::<T>::block_number() + One::one();
let mut reward = T::EmissionSchedule::get();
let mut rewards: Vec<(T::AccountId, BalanceOf<T>)> = Vec::new();
let payout = T::MinPayout::get();
for index in 1..n {
reward -= payout;
rewards.push((account("miner", index, SEED), payout));
}
rewards.push((account("miner", n, SEED), reward));
Rewards::<T>::insert_coinbase(number, rewards);

#[block]
{
Rewards::<T>::on_finalize(number);
Rewards::<T>::on_initialize(number + T::MaturationTime::get());
}
}

impl_benchmark_test_suite!(Rewards, crate::mock::new_test_ext(), crate::mock::Text,);
}
Loading

0 comments on commit 5d76d4f

Please sign in to comment.