Skip to content

Commit

Permalink
Merge pull request #190 from subspace/transaction-fees
Browse files Browse the repository at this point in the history
Split fees into storage/compute/tip, add storage fee escrow
  • Loading branch information
nazar-pc authored Dec 14, 2021
2 parents 11620f9 + f8ce741 commit b8d6f86
Show file tree
Hide file tree
Showing 14 changed files with 579 additions and 64 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

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

4 changes: 1 addition & 3 deletions crates/pallet-rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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"
description = "Pallet for issuing rewards to block producers"
readme = "README.md"
include = [
"/src",
Expand All @@ -22,7 +22,6 @@ codec = { package = "parity-scale-codec", version = "2.3.0", default-features =
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "b6c1c1bcfa5d831bfd1f278064d7af757f9b38f5" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "b6c1c1bcfa5d831bfd1f278064d7af757f9b38f5" }
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"]
Expand All @@ -31,6 +30,5 @@ std = [
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-consensus-subspace/std",
]
try-runtime = ["frame-support/try-runtime"]
38 changes: 20 additions & 18 deletions crates/pallet-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Subspace pallet for issuing rewards to block producers.
//! 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 frame_support::traits::{Currency, Get};
use frame_support::traits::{Currency, FindAuthor, Get};
use frame_support::weights::Weight;
pub use pallet::*;
use sp_consensus_subspace::digests::CompatibleDigestItem;

pub trait WeightInfo {
fn on_initialize() -> Weight;
Expand All @@ -34,7 +33,7 @@ pub trait WeightInfo {
mod pallet {
use super::WeightInfo;
use frame_support::pallet_prelude::*;
use frame_support::traits::Currency;
use frame_support::traits::{Currency, FindAuthor};
use frame_system::pallet_prelude::*;

type BalanceOf<T> =
Expand All @@ -51,6 +50,8 @@ mod pallet {
#[pallet::constant]
type BlockReward: Get<BalanceOf<Self>>;

type FindAuthor: FindAuthor<Self::AccountId>;

type WeightInfo: WeightInfo;
}

Expand Down Expand Up @@ -81,19 +82,20 @@ mod pallet {

impl<T: Config> Pallet<T> {
fn do_initialize(_n: T::BlockNumber) {
if let Some(block_author) = frame_system::Pallet::<T>::digest()
.logs
.iter()
.find_map(|s| s.as_subspace_pre_digest())
.map(|pre_digest| pre_digest.solution.public_key)
{
let reward = T::BlockReward::get();
T::Currency::deposit_creating(&block_author, reward);

Self::deposit_event(Event::BlockReward {
block_author,
reward,
});
}
let block_author = T::FindAuthor::find_author(
frame_system::Pallet::<T>::digest()
.logs
.iter()
.filter_map(|d| d.as_pre_runtime()),
)
.expect("Block author must always be present; qed");

let reward = T::BlockReward::get();
T::Currency::deposit_creating(&block_author, reward);

Self::deposit_event(Event::BlockReward {
block_author,
reward,
});
}
}
34 changes: 24 additions & 10 deletions crates/pallet-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use sp_runtime::transaction_validity::{
use sp_runtime::{
generic::DigestItem,
traits::{One, SaturatedConversion, Saturating, Zero},
ConsensusEngineId,
};
use sp_std::prelude::*;
use subspace_core_primitives::{crypto, RootBlock, PIECE_SIZE, RANDOMNESS_LENGTH, SALT_SIZE};
Expand Down Expand Up @@ -200,11 +201,6 @@ mod pallet {
#[pallet::constant]
type RecordedHistorySegmentSize: Get<u32>;

/// Replication factor, defines minimum desired number of replicas of the blockchain to be
/// stored by the network.
#[pallet::constant]
type ReplicationFactor: Get<u16>;

/// Subspace requires some logic to be triggered on every block to query for whether an epoch
/// has ended and to perform the transition to the next epoch.
///
Expand Down Expand Up @@ -698,14 +694,15 @@ impl<T: Config> Pallet<T> {

// TODO: Temporary testnet hack, we don't update solution range for the first 15_000 blocks
// in order to seed the blockchain with data quickly
#[cfg(all(feature = "no-early-solution-range-updates", not(test)))]
let solution_range = if block_number < 15_000_u32.into() {
previous_solution_range
let solution_range = if cfg!(all(feature = "no-early-solution-range-updates", not(test))) {
if block_number < 15_000_u32.into() {
previous_solution_range
} else {
(previous_solution_range as f64 * adjustment_factor).round() as u64
}
} else {
(previous_solution_range as f64 * adjustment_factor).round() as u64
};
#[cfg(not(all(feature = "no-early-solution-range-updates", not(test))))]
let solution_range = (previous_solution_range as f64 * adjustment_factor).round() as u64;

SolutionRange::<T>::put(solution_range);
EraStartSlot::<T>::put(current_slot);
Expand Down Expand Up @@ -1110,6 +1107,23 @@ impl<T: Config> OnTimestampSet<T::Moment> for Pallet<T> {
}
}

impl<T: Config> frame_support::traits::FindAuthor<T::AccountId> for Pallet<T> {
fn find_author<'a, I>(digests: I) -> Option<T::AccountId>
where
I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
{
digests.into_iter().find_map(|(id, mut data)| {
if id == SUBSPACE_ENGINE_ID {
PreDigest::decode(&mut data)
.map(|pre_digest| pre_digest.solution.public_key)
.ok()
} else {
None
}
})
}
}

impl<T: Config> frame_support::traits::Lateness<T::BlockNumber> for Pallet<T> {
fn lateness(&self) -> T::BlockNumber {
Self::lateness()
Expand Down
1 change: 0 additions & 1 deletion crates/pallet-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ impl Config for Test {
type ConfirmationDepthK = ConfirmationDepthK;
type RecordSize = RecordSize;
type RecordedHistorySegmentSize = RecordedHistorySegmentSize;
type ReplicationFactor = ReplicationFactor;
type EpochChangeTrigger = NormalEpochChange;
type EraChangeTrigger = NormalEraChange;
type EonChangeTrigger = NormalEonChange;
Expand Down
34 changes: 34 additions & 0 deletions crates/pallet-transaction-fees/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "pallet-transaction-fees"
version = "0.1.0"
authors = ["Nazar Mokrynskyi <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://subspace.network"
repository = "https://github.com/subspace/subspace"
description = "Pallet for charging and re-distributing transaction fees"
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 = "b6c1c1bcfa5d831bfd1f278064d7af757f9b38f5" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "b6c1c1bcfa5d831bfd1f278064d7af757f9b38f5" }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }

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

Pallet for charging and re-distributing transaction fees.

License: Apache-2.0
26 changes: 26 additions & 0 deletions crates/pallet-transaction-fees/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
}
}
Loading

0 comments on commit b8d6f86

Please sign in to comment.