Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request paritytech#125 from subspace/block-rewards
Browse files Browse the repository at this point in the history
Simple rewards pallet that rewards block producers with 1 SSC per block
  • Loading branch information
nazar-pc authored Nov 11, 2021
2 parents 11061fa + 9dc763c commit cec59d3
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 3 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

36 changes: 36 additions & 0 deletions crates/pallet-rewards/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "pallet-rewards"
version = "0.1.0"
authors = ["Nazar Mokrynskyi <nazar@mokrynskyi.com>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://subspace.network"
repository = "https://github.com/subspace/subspace"
description = "Subspace pallet for issuing rewards to block producers"
readme = "README.md"
include = [
"/src",
"/Cargo.toml",
"/README.md",
]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "2.3.0", default-features = false, features = ["derive"] }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
sp-consensus-subspace = { version = "0.1.0", default-features = false, path = "../sp-consensus-subspace" }

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-consensus-subspace/std",
]
try-runtime = ["frame-support/try-runtime"]
5 changes: 5 additions & 0 deletions crates/pallet-rewards/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Pallet Rewards

Pallet for issuing rewards to block producers.

License: Apache-2.0
26 changes: 26 additions & 0 deletions crates/pallet-rewards/src/default_weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2021 Subspace Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Default weights for the Rewards Pallet
//! This file was not auto-generated.

use frame_support::weights::Weight;

impl crate::WeightInfo for () {
fn on_initialize() -> Weight {
// TODO: Correct value
1
}
}
103 changes: 103 additions & 0 deletions crates/pallet-rewards/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (C) 2021 Subspace Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Subspace pallet for issuing rewards to block producers.

#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms, missing_debug_implementations)]

mod default_weights;

use codec::{Decode, Encode};
use frame_support::traits::{Currency, Get};
use frame_support::weights::Weight;
pub use pallet::*;
use sp_consensus_subspace::digests::PreDigest;
use sp_consensus_subspace::SUBSPACE_ENGINE_ID;

type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

pub trait WeightInfo {
fn on_initialize() -> Weight;
}

#[frame_support::pallet]
mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[pallet::config]
pub trait Config: frame_system::Config {
/// `pallet-rewards` events
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

type Currency: Currency<Self::AccountId>;

/// Fixed reward for block producer.
#[pallet::constant]
type BlockReward: Get<BalanceOf<Self>>;

type WeightInfo: WeightInfo;
}

/// Pallet rewards for issuing rewards to block producers.
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

/// `pallet-rewards` events
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Issued reward for the block author. \[block_author, reward\]
BlockReward(T::AccountId, BalanceOf<T>),
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(now: BlockNumberFor<T>) -> Weight {
Self::do_initialize(now);
T::WeightInfo::on_initialize()
}
}
}

impl<T: Config> Pallet<T> {
fn do_initialize(_n: T::BlockNumber) {
if let Some(block_author) = frame_system::Pallet::<T>::digest()
.logs
.iter()
.filter_map(|s| s.as_pre_runtime())
.find_map(|(id, mut data)| {
if id == SUBSPACE_ENGINE_ID {
PreDigest::decode(&mut data).ok()
} else {
None
}
})
.and_then(|pre_digest| {
T::AccountId::decode(&mut pre_digest.solution.public_key.encode().as_ref()).ok()
})
{
let reward = T::BlockReward::get();
T::Currency::deposit_creating(&block_author, reward);

Self::deposit_event(Event::BlockReward(block_author, reward));
}
}
}
5 changes: 2 additions & 3 deletions crates/pallet-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,14 +807,13 @@ impl<T: Config> Pallet<T> {
.logs
.iter()
.filter_map(|s| s.as_pre_runtime())
.filter_map(|(id, mut data)| {
.find_map(|(id, mut data)| {
if id == SUBSPACE_ENGINE_ID {
PreDigest::decode(&mut data).ok()
} else {
None
}
})
.next();
});

let maybe_randomness: MaybeRandomness = maybe_pre_digest.map(|digest| {
// on the first non-zero block (i.e. block #1)
Expand Down
2 changes: 2 additions & 0 deletions crates/subspace-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ orml-vesting = { version = "0.4.1-dev", default-features = false, git = "https:/
pallet-balances = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
pallet-feeds = { version = "0.1.0", default-features = false, path = "../pallet-feeds" }
pallet-offences-subspace = { version = "0.1.0", default-features = false, path = "../pallet-offences-subspace" }
pallet-rewards = { version = "0.1.0", default-features = false, path = "../pallet-rewards" }
pallet-subspace = { version = "0.1.0", default-features = false, features = ["no-early-solution-range-updates"], path = "../pallet-subspace" }
pallet-sudo = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
pallet-timestamp = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
Expand Down Expand Up @@ -68,6 +69,7 @@ std = [
"pallet-balances/std",
"pallet-feeds/std",
"pallet-offences-subspace/std",
"pallet-rewards/std",
"pallet-subspace/std",
"pallet-sudo/std",
"pallet-timestamp/std",
Expand Down
12 changes: 12 additions & 0 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,17 @@ impl pallet_offences_subspace::Config for Runtime {
type OnOffenceHandler = Subspace;
}

parameter_types! {
pub const BlockReward: Balance = SSC;
}

impl pallet_rewards::Config for Runtime {
type Event = Event;
type Currency = Balances;
type BlockReward = BlockReward;
type WeightInfo = ();
}

impl pallet_feeds::Config for Runtime {
type Event = Event;
}
Expand Down Expand Up @@ -412,6 +423,7 @@ construct_runtime!(

Subspace: pallet_subspace::{Pallet, Call, Config, Storage, Event, ValidateUnsigned} = 2,
OffencesSubspace: pallet_offences_subspace::{Pallet, Storage, Event} = 3,
Rewards: pallet_rewards::{Pallet, Event<T>} = 9,

Balances: pallet_balances::{Pallet, Call, Config<T>, Storage, Event<T>} = 4,
TransactionPayment: pallet_transaction_payment::{Pallet, Storage} = 5,
Expand Down

0 comments on commit cec59d3

Please sign in to comment.