Skip to content

Commit 2a4e635

Browse files
authored
Merge pull request #273 from subspace/block-rewards
Add custom block rewards address
2 parents 0f544ec + 7c30e3c commit 2a4e635

File tree

19 files changed

+117
-19
lines changed

19 files changed

+117
-19
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pallet-rewards/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ codec = { package = "parity-scale-codec", version = "2.3.0", default-features =
2222
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
2323
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
2424
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
25+
subspace-runtime-primitives = { version = "0.1.0", default-features = false, path = "../subspace-runtime-primitives" }
2526

2627
[features]
2728
default = ["std"]

crates/pallet-rewards/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121

2222
mod default_weights;
2323

24-
use frame_support::traits::{Currency, FindAuthor, Get};
24+
use frame_support::traits::{Currency, Get};
2525
use frame_support::weights::Weight;
2626
pub use pallet::*;
27+
use subspace_runtime_primitives::FindBlockRewardAddress;
2728

2829
pub trait WeightInfo {
2930
fn on_initialize() -> Weight;
@@ -33,8 +34,9 @@ pub trait WeightInfo {
3334
mod pallet {
3435
use super::WeightInfo;
3536
use frame_support::pallet_prelude::*;
36-
use frame_support::traits::{Currency, FindAuthor};
37+
use frame_support::traits::Currency;
3738
use frame_system::pallet_prelude::*;
39+
use subspace_runtime_primitives::FindBlockRewardAddress;
3840

3941
type BalanceOf<T> =
4042
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
@@ -50,7 +52,7 @@ mod pallet {
5052
#[pallet::constant]
5153
type BlockReward: Get<BalanceOf<Self>>;
5254

53-
type FindAuthor: FindAuthor<Self::AccountId>;
55+
type FindBlockRewardAddress: FindBlockRewardAddress<Self::AccountId>;
5456

5557
type WeightInfo: WeightInfo;
5658
}
@@ -82,7 +84,7 @@ mod pallet {
8284

8385
impl<T: Config> Pallet<T> {
8486
fn do_initialize(_n: T::BlockNumber) {
85-
let block_author = T::FindAuthor::find_author(
87+
let block_author = T::FindBlockRewardAddress::find_block_reward_address(
8688
frame_system::Pallet::<T>::digest()
8789
.logs
8890
.iter()

crates/pallet-subspace/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ sp-io = { version = "5.0.0", default-features = false, git = "https://github.com
2626
sp-runtime = { version = "5.0.0", default-features = false, git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
2727
sp-std = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
2828
subspace-core-primitives = { version = "0.1.0", default-features = false, path = "../subspace-core-primitives" }
29+
subspace-runtime-primitives = { version = "0.1.0", default-features = false, path = "../subspace-runtime-primitives" }
2930

3031
[dev-dependencies]
3132
pallet-balances = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }

crates/pallet-subspace/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,18 @@ impl<T: Config> frame_support::traits::FindAuthor<T::AccountId> for Pallet<T> {
847847
}
848848
}
849849

850+
impl<T: Config> subspace_runtime_primitives::FindBlockRewardAddress<T::AccountId> for Pallet<T> {
851+
fn find_block_reward_address<'a, I>(digests: I) -> Option<T::AccountId>
852+
where
853+
I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
854+
{
855+
digests
856+
.into_iter()
857+
.find_map(|(id, data)| DigestItemRef::PreRuntime(&id, data).as_subspace_pre_digest())
858+
.map(|pre_digest| pre_digest.solution.reward_address)
859+
}
860+
}
861+
850862
impl<T: Config> frame_support::traits::Randomness<T::Hash, T::BlockNumber> for Pallet<T> {
851863
fn random(subject: &[u8]) -> (T::Hash, T::BlockNumber) {
852864
let mut subject = subject.to_vec();

crates/pallet-subspace/src/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ pub fn go_to_block(keypair: &Keypair, block: u64, slot: u64) {
194194
slot.into(),
195195
Solution {
196196
public_key: FarmerPublicKey::unchecked_from(keypair.public.to_bytes()),
197+
reward_address: FarmerPublicKey::unchecked_from(keypair.public.to_bytes()),
197198
piece_index: 0,
198199
encoding,
199200
signature: keypair.sign(ctx.bytes(&tag)).to_bytes().into(),
@@ -250,6 +251,7 @@ pub fn generate_equivocation_proof(
250251
slot,
251252
Solution {
252253
public_key: public_key.clone(),
254+
reward_address: public_key.clone(),
253255
piece_index,
254256
encoding,
255257
signature: signature.into(),

crates/pallet-transaction-fees/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ codec = { package = "parity-scale-codec", version = "2.3.0", default-features =
2222
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
2323
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
2424
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
25+
subspace-runtime-primitives = { version = "0.1.0", default-features = false, path = "../subspace-runtime-primitives" }
2526

2627
[features]
2728
default = ["std"]

crates/pallet-transaction-fees/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ mod default_weights;
2323

2424
use codec::{Codec, Decode, Encode};
2525
use frame_support::sp_runtime::traits::Zero;
26-
use frame_support::traits::{Currency, FindAuthor, Get};
26+
use frame_support::traits::{Currency, Get};
2727
use frame_support::weights::Weight;
2828
pub use pallet::*;
2929
use scale_info::TypeInfo;
30+
use subspace_runtime_primitives::FindBlockRewardAddress;
3031

3132
type BalanceOf<T> =
3233
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
@@ -47,8 +48,9 @@ struct CollectedFees<Balance: Codec> {
4748
mod pallet {
4849
use super::{BalanceOf, CollectedFees, WeightInfo};
4950
use frame_support::pallet_prelude::*;
50-
use frame_support::traits::{Currency, FindAuthor};
51+
use frame_support::traits::Currency;
5152
use frame_system::pallet_prelude::*;
53+
use subspace_runtime_primitives::FindBlockRewardAddress;
5254

5355
#[pallet::config]
5456
pub trait Config: frame_system::Config {
@@ -85,7 +87,7 @@ mod pallet {
8587

8688
type Currency: Currency<Self::AccountId>;
8789

88-
type FindAuthor: FindAuthor<Self::AccountId>;
90+
type FindBlockRewardAddress: FindBlockRewardAddress<Self::AccountId>;
8991

9092
type WeightInfo: WeightInfo;
9193
}
@@ -172,7 +174,7 @@ where
172174
BalanceOf<T>: From<u64>,
173175
{
174176
fn do_initialize(_n: T::BlockNumber) {
175-
let block_author = T::FindAuthor::find_author(
177+
let block_author = T::FindBlockRewardAddress::find_block_reward_address(
176178
frame_system::Pallet::<T>::digest()
177179
.logs
178180
.iter()

crates/sc-consensus-subspace-rpc/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,29 @@ where
341341
let public_key =
342342
match FarmerPublicKey::from_slice(&solution.public_key) {
343343
Ok(public_key) => public_key,
344-
Err(_) => {
344+
Err(()) => {
345345
warn!(
346346
"Failed to convert public key: {:?}",
347347
solution.public_key
348348
);
349349
return;
350350
}
351351
};
352+
let reward_address =
353+
match FarmerPublicKey::from_slice(&solution.reward_address) {
354+
Ok(public_key) => public_key,
355+
Err(()) => {
356+
warn!(
357+
"Failed to convert reward address: {:?}",
358+
solution.reward_address,
359+
);
360+
return;
361+
}
362+
};
363+
352364
let solution = Solution {
353365
public_key,
366+
reward_address,
354367
piece_index: solution.piece_index,
355368
encoding: solution.encoding,
356369
signature: solution.signature,

crates/sc-consensus-subspace/src/tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,9 @@ fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static
560560
let _ = solution_sender
561561
.send(Solution {
562562
public_key: FarmerPublicKey::unchecked_from(keypair.public.to_bytes()),
563+
reward_address: FarmerPublicKey::unchecked_from(
564+
keypair.public.to_bytes(),
565+
),
563566
piece_index,
564567
encoding,
565568
signature: keypair.sign(ctx.bytes(&tag)).to_bytes().into(),
@@ -666,6 +669,7 @@ pub fn dummy_claim_slot(slot: Slot) -> Option<(PreDigest<FarmerPublicKey>, Farme
666669
PreDigest {
667670
solution: Solution {
668671
public_key: FarmerPublicKey::unchecked_from([0u8; 32]),
672+
reward_address: FarmerPublicKey::unchecked_from([0u8; 32]),
669673
piece_index: 0,
670674
encoding: Piece::default(),
671675
signature: Signature::default(),
@@ -725,6 +729,7 @@ fn propose_and_import_block<Transaction: Send + 'static>(
725729
slot,
726730
solution: Solution {
727731
public_key: FarmerPublicKey::unchecked_from(keypair.public.to_bytes()),
732+
reward_address: FarmerPublicKey::unchecked_from(keypair.public.to_bytes()),
728733
piece_index: 0,
729734
encoding,
730735
signature: signature.into(),

0 commit comments

Comments
 (0)