This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
forked from paritytech/substrate
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request paritytech#125 from subspace/block-rewards
Simple rewards pallet that rewards block producers with 1 SSC per block
- Loading branch information
Showing
8 changed files
with
198 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters