Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolaos Dymitriadis <[email protected]>
  • Loading branch information
AmbientTea committed Feb 25, 2025
1 parent d0bd897 commit a41fb9a
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 95 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions node/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ main-chain-follower-mock = { workspace = true, features = [
tokio = { workspace = true }
cli-commands = { workspace = true }
sp-stake-distribution = { workspace = true, features = ["std"] }
sp-block-rewards-payouts = { workspace = true, features = ["std"] }

[build-dependencies]
substrate-build-script-utils = { workspace = true }
Expand Down
19 changes: 19 additions & 0 deletions node/node/src/inherent_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use jsonrpsee::core::async_trait;
use sc_consensus_aura::{find_pre_digest, SlotDuration};
use sc_service::Arc;
use sidechain_domain::mainchain_epoch::MainchainEpochConfig;
use sidechain_domain::DelegatorKey;
use sidechain_domain::{McBlockHash, ScEpochNumber};
use sidechain_mc_hash::McHashDataSource;
use sidechain_mc_hash::McHashInherentDataProvider as McHashIDP;
Expand All @@ -19,6 +20,8 @@ use sp_api::ProvideRuntimeApi;
use sp_block_production_log::BlockAuthorInherentProvider;
use sp_block_production_log::BlockProductionLogApi;
use sp_block_rewards::BlockBeneficiaryInherentProvider;
use sp_block_rewards_payouts::inherent_data::BlockRewardsPayoutsInherentDataProvider;
use sp_block_rewards_payouts::BlockRewardsPayoutsApi;
use sp_blockchain::HeaderBackend;
use sp_consensus_aura::{
inherents::InherentDataProvider as AuraIDP, sr25519::AuthorityPair as AuraPair, Slot,
Expand All @@ -32,6 +35,7 @@ use sp_native_token_management::{
use sp_partner_chains_consensus_aura::CurrentSlotProvider;
use sp_runtime::traits::{Block as BlockT, Header, Zero};
use sp_session_validator_management::SessionValidatorManagementApi;
use sp_stake_distribution::StakeDistributionDataSource;
use sp_timestamp::{InherentDataProvider as TimestampIDP, Timestamp};
use std::error::Error;
use time_source::TimeSource;
Expand All @@ -43,6 +47,7 @@ pub struct ProposalCIDP<T> {
mc_hash_data_source: Arc<dyn McHashDataSource + Send + Sync>,
authority_selection_data_source: Arc<dyn AuthoritySelectionDataSource + Send + Sync>,
native_token_data_source: Arc<dyn NativeTokenManagementDataSource + Send + Sync>,
stake_distribution_data_source: Arc<dyn StakeDistributionDataSource + Send + Sync>,
}

#[async_trait]
Expand All @@ -58,6 +63,7 @@ where
>,
T::Api: NativeTokenManagementApi<Block>,
T::Api: BlockProductionLogApi<Block, CommitteeMember<CrossChainPublic, SessionKeys>>,
T::Api: BlockRewardsPayoutsApi<Block, BlockAuthor>,
{
type InherentDataProviders = (
AuraIDP,
Expand All @@ -67,6 +73,7 @@ where
BlockAuthorInherentProvider<BlockAuthor>,
BlockBeneficiaryInherentProvider<BeneficiaryId>,
NativeTokenIDP,
BlockRewardsPayoutsInherentDataProvider<BlockAuthor, DelegatorKey>,
);

async fn create_inherent_data_providers(
Expand All @@ -80,6 +87,7 @@ where
mc_hash_data_source,
authority_selection_data_source,
native_token_data_source,
stake_distribution_data_source,
} = self;
let CreateInherentDataConfig { mc_epoch_config, sc_slot_config, time_source } = config;

Expand Down Expand Up @@ -117,6 +125,16 @@ where
)
.await?;

let payouts = BlockRewardsPayoutsInherentDataProvider::new_cardano_stake_based(
client.as_ref(),
stake_distribution_data_source.as_ref(),
parent_hash,
*slot,
mc_epoch_config,
config.sc_slot_config.slot_duration,
)
.await?;

Ok((
slot,
timestamp,
Expand All @@ -125,6 +143,7 @@ where
block_producer_id_provider,
block_beneficiary_provider,
native_token,
payouts,
))
}
}
Expand Down
1 change: 1 addition & 0 deletions node/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ pub async fn new_full<Network: sc_network::NetworkBackend<Block, <Block as Block
data_sources.mc_hash.clone(),
data_sources.authority_selection.clone(),
data_sources.native_token.clone(),
data_sources.stake_distribution,
),
force_authoring,
backoff_authoring_blocks,
Expand Down
4 changes: 4 additions & 0 deletions node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pallet-native-token-management = { workspace = true }
sp-native-token-management = { workspace = true, features = ["serde"] }
pallet-session-runtime-stub = { workspace = true, default-features = false }
pallet-address-associations = { workspace = true }
pallet-block-rewards-payouts = { workspace = true }
sp-block-rewards-payouts = { workspace = true }

[dev-dependencies]
sp-io = { workspace = true }
Expand Down Expand Up @@ -155,6 +157,8 @@ std = [
"sp-native-token-management/std",
"pallet-session-runtime-stub/std",
"sp-block-production-log/std",
"pallet-block-rewards-payouts/std",
"sp-block-rewards-payouts/std",
]

runtime-benchmarks = [
Expand Down
39 changes: 36 additions & 3 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use session_manager::ValidatorManagementSessionManager;
use sidechain_domain::{
NativeTokenAmount, PermissionedCandidateData, RegistrationData, ScEpochNumber, ScSlotNumber,
StakeDelegation, StakePoolPublicKey, UtxoId,
DelegatorKey, MainchainKeyHash, NativeTokenAmount, PermissionedCandidateData, RegistrationData,
ScEpochNumber, ScSlotNumber, StakeDelegation, StakePoolPublicKey, UtxoId,
};
use sidechain_slots::Slot;
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::hexdisplay::HexDisplay;
Expand Down Expand Up @@ -468,7 +469,9 @@ impl pallet_block_rewards::Config for Runtime {
type GetBlockRewardPoints = sp_block_rewards::SimpleBlockCount;
}

#[derive(MaxEncodedLen, Encode, Decode, Clone, TypeInfo, PartialEq, Eq, Debug)]
#[derive(
MaxEncodedLen, Encode, Decode, Clone, TypeInfo, PartialEq, Eq, Debug, Hash, PartialOrd, Ord,
)]
pub enum BlockAuthor {
Incentivized(CrossChainPublic, StakePoolPublicKey),
ProBono(CrossChainPublic),
Expand All @@ -491,6 +494,14 @@ impl From<CommitteeMember<CrossChainPublic, SessionKeys>> for BlockAuthor {
}
}
}
impl From<BlockAuthor> for Option<MainchainKeyHash> {
fn from(value: BlockAuthor) -> Self {
match value {
BlockAuthor::Incentivized(_, key) => Some(key.hash()),
_ => None,
}
}
}
#[cfg(feature = "runtime-benchmarks")]
impl From<[u8; 32]> for BlockAuthor {
fn from(arr: [u8; 32]) -> Self {
Expand Down Expand Up @@ -519,6 +530,18 @@ impl pallet_address_associations::Config for Runtime {
}
}

impl pallet_block_rewards_payouts::Config for Runtime {
type BeneficiaryId = DelegatorKey;

fn should_pay_out(slot: sidechain_slots::Slot) -> Option<sidechain_slots::Slot> {
if *slot % 3 == 0 {
Some(slot)
} else {
None
}
}
}

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub struct Runtime {
Expand All @@ -536,6 +559,7 @@ construct_runtime!(
AddressAssociations: pallet_address_associations,
BlockRewards: pallet_block_rewards,
BlockProductionLog: pallet_block_production_log,
BlockProductionPayouts: pallet_block_rewards_payouts,
// pallet_grandpa reads pallet_session::pallet::CurrentIndex storage.
// Only stub implementation of pallet_session should be wired.
// Partner Chains session_manager ValidatorManagementSessionManager writes to pallet_session::pallet::CurrentIndex.
Expand Down Expand Up @@ -926,6 +950,15 @@ impl_runtime_apis! {
.clone()
}
}

impl sp_block_rewards_payouts::BlockRewardsPayoutsApi<Block, BlockAuthor> for Runtime {
fn should_pay_out(slot: Slot) -> Option<Slot> {
BlockProductionPayouts::should_pay_out(slot)
}
fn blocks_to_reward(slot: Slot) -> Vec<(Slot, BlockAuthor)> {
BlockProductionPayouts::blocks_to_reward(slot).collect()
}
}
}

#[cfg(test)]
Expand Down
10 changes: 3 additions & 7 deletions toolkit/pallets/block-rewards-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ use frame_support::pallet_prelude::*;
pub use pallet::*;
use sp_block_rewards_payouts::*;

pub trait ShouldProcessPayout {
/// Should return slot up to which rewards should be paid out or None.
fn should_pay_out(slot: Slot) -> Option<Slot>;
}

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand All @@ -23,7 +18,8 @@ pub mod pallet {
/// Type identifying reward receiver on the Partner Chain
type BeneficiaryId: Member + Parameter + MaxEncodedLen;

type ShouldProcessPayout: ShouldProcessPayout;
/// Should return slot up to which rewards should be paid out or None.
fn should_pay_out(slot: Slot) -> Option<Slot>;
}

#[pallet::inherent]
Expand Down Expand Up @@ -97,7 +93,7 @@ pub mod pallet {

impl<T: Config> Pallet<T> {
pub fn should_pay_out(slot: Slot) -> Option<Slot> {
<T as Config>::ShouldProcessPayout::should_pay_out(slot)
<T as Config>::should_pay_out(slot)
}

pub fn blocks_to_reward(
Expand Down
Loading

0 comments on commit a41fb9a

Please sign in to comment.