Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolaos Dymitriadis <nikolaos.dymitriadis@iohk.io>
  • Loading branch information
AmbientTea committed Feb 26, 2025
1 parent 8e4cd4e commit 91f62c4
Show file tree
Hide file tree
Showing 14 changed files with 559 additions and 33 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ members = [
"toolkit/primitives/plutus-data",
"toolkit/pallets/address-associations",
"toolkit/primitives/stake-distribution",
"toolkit/pallets/block-rewards-payouts",
"toolkit/primitives/block-rewards-payouts",
]
resolver = "2"

Expand Down Expand Up @@ -241,3 +243,5 @@ partner-chains-cli = { path = "toolkit/partner-chains-cli", default-features = f
partner-chains-plutus-data = { path = "toolkit/primitives/plutus-data", default-features = false }
partner-chains-smart-contracts-commands = { path = "toolkit/cli/smart-contracts-commands", default-features = false }
pallet-address-associations = { path = "toolkit/pallets/address-associations", default-features = false }
pallet-block-rewards-payouts = { path = "toolkit/pallets/block-rewards-payouts", default-features = false }
sp-block-rewards-payouts = { path = "toolkit/primitives/block-rewards-payouts", default-features = false }
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
41 changes: 39 additions & 2 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 All @@ -136,6 +155,7 @@ pub struct VerifierCIDP<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>,
}

impl<T: Send + Sync> CurrentSlotProvider for VerifierCIDP<T> {
Expand All @@ -155,8 +175,14 @@ where
ScEpochNumber,
>,
T::Api: NativeTokenManagementApi<Block>,
T::Api: BlockRewardsPayoutsApi<Block, BlockAuthor>,
{
type InherentDataProviders = (TimestampIDP, AriadneIDP, NativeTokenIDP);
type InherentDataProviders = (
TimestampIDP,
AriadneIDP,
NativeTokenIDP,
BlockRewardsPayoutsInherentDataProvider<BlockAuthor, DelegatorKey>,
);

async fn create_inherent_data_providers(
&self,
Expand All @@ -169,6 +195,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 @@ -204,7 +231,17 @@ where
)
.await?;

Ok((timestamp, ariadne_data_provider, native_token))
let payouts = BlockRewardsPayoutsInherentDataProvider::new_cardano_stake_based(
client.as_ref(),
stake_distribution_data_source.as_ref(),
parent_hash,
verified_block_slot,
mc_epoch_config,
config.sc_slot_config.slot_duration,
)
.await?;

Ok((timestamp, ariadne_data_provider, native_token, payouts))
}
}

Expand Down
2 changes: 2 additions & 0 deletions node/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub fn new_partial(
data_sources.mc_hash.clone(),
data_sources.authority_selection.clone(),
data_sources.native_token.clone(),
data_sources.stake_distribution.clone(),
),
spawner: &task_manager.spawn_essential_handle(),
registry: config.prometheus_registry(),
Expand Down Expand Up @@ -315,6 +316,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
39 changes: 17 additions & 22 deletions node/node/src/tests/inherent_data_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use authority_selection_inherents::{
authority_selection_inputs::AuthoritySelectionInputs, mock::MockAuthoritySelectionDataSource,
};
use hex_literal::hex;
use main_chain_follower_mock::stake_distribution::StakeDistributionDataSourceMock;
use sidechain_domain::{
MainchainBlock, McBlockHash, McBlockNumber, McEpochNumber, McSlotNumber, NativeTokenAmount,
ScEpochNumber,
DelegatorKey, MainchainBlock, McBlockHash, McBlockNumber, McEpochNumber, McSlotNumber,
NativeTokenAmount, ScEpochNumber,
};
use sidechain_mc_hash::mock::MockMcHashDataSource;
use sidechain_runtime::BlockAuthor;
use sp_block_rewards_payouts::BlockProductionPayoutInfo;
use sp_consensus_aura::Slot;
use sp_core::{ecdsa, H256};
use sp_inherents::CreateInherentDataProviders;
Expand Down Expand Up @@ -47,28 +49,15 @@ async fn block_proposal_cidp_should_be_created_correctly() {
Arc::new(mc_hash_data_source),
Arc::new(MockAuthoritySelectionDataSource::default()),
Arc::new(native_token_data_source),
Arc::new(StakeDistributionDataSourceMock::new()),
)
.create_inherent_data_providers(H256::zero(), ())
.await
.unwrap();

let (
slot,
timestamp,
mc_hash,
ariadne_data,
block_producer_id,
block_beneficiary,
native_token,
) = inherent_data_providers;
let mut inherent_data = InherentData::new();
slot.provide_inherent_data(&mut inherent_data).await.unwrap();
timestamp.provide_inherent_data(&mut inherent_data).await.unwrap();
mc_hash.provide_inherent_data(&mut inherent_data).await.unwrap();
ariadne_data.provide_inherent_data(&mut inherent_data).await.unwrap();
block_producer_id.provide_inherent_data(&mut inherent_data).await.unwrap();
block_beneficiary.provide_inherent_data(&mut inherent_data).await.unwrap();
native_token.provide_inherent_data(&mut inherent_data).await.unwrap();
inherent_data_providers.provide_inherent_data(&mut inherent_data).await.unwrap();

assert_eq!(
inherent_data
.get_data::<Slot>(&sp_consensus_aura::inherents::INHERENT_IDENTIFIER)
Expand Down Expand Up @@ -106,6 +95,14 @@ async fn block_proposal_cidp_should_be_created_correctly() {
.into()
))
);
assert_eq!(
inherent_data
.get_data::<BlockProductionPayoutInfo<BlockAuthor, DelegatorKey>>(
&sp_block_rewards_payouts::INHERENT_IDENTIFIER
)
.unwrap(),
Some(BlockProductionPayoutInfo { up_to_slot: Slot::from(30), production_rundowns: vec![] })
)
}

#[tokio::test]
Expand Down Expand Up @@ -137,17 +134,15 @@ async fn block_verification_cidp_should_be_created_correctly() {
Arc::new(mc_hash_data_source),
Arc::new(MockAuthoritySelectionDataSource::default()),
Arc::new(native_token_data_source),
Arc::new(StakeDistributionDataSourceMock::new()),
);

let inherent_data_providers = verifier_cidp
.create_inherent_data_providers(mock_header().hash(), (30.into(), mc_block_hash))
.await
.unwrap();
let (timestamp, ariadne_data_provider, native_token_provider) = inherent_data_providers;
let mut inherent_data = InherentData::new();
timestamp.provide_inherent_data(&mut inherent_data).await.unwrap();
ariadne_data_provider.provide_inherent_data(&mut inherent_data).await.unwrap();
native_token_provider.provide_inherent_data(&mut inherent_data).await.unwrap();
inherent_data_providers.provide_inherent_data(&mut inherent_data).await.unwrap();

assert_eq!(
inherent_data.get_data::<Timestamp>(&sp_timestamp::INHERENT_IDENTIFIER).unwrap(),
Expand Down
15 changes: 12 additions & 3 deletions node/node/src/tests/runtime_api_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use hex_literal::hex;
use sidechain_domain::*;
use sidechain_mc_hash::McHashInherentDigest;
use sidechain_runtime::opaque::SessionKeys;
use sidechain_runtime::CrossChainPublic;
use sidechain_runtime::{BlockAuthor, CrossChainPublic};
use sidechain_slots::Slot;
use sp_api::{ApiRef, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_core::ecdsa;
use sp_core::{ed25519, sr25519};
use sp_core::{ecdsa, ed25519, sr25519};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero};
use sp_runtime::Digest;
use sp_sidechain::GetGenesisUtxo;
Expand Down Expand Up @@ -121,6 +121,15 @@ sp_api::mock_impl_runtime_apis! {
)
}
}

impl sp_block_rewards_payouts::BlockRewardsPayoutsApi<Block, BlockAuthor> for TestApi {
fn should_pay_out(slot: Slot) -> Option<Slot> {
Some(slot)
}
fn blocks_to_reward(_slot: Slot) -> Vec<(Slot, BlockAuthor)> {
vec![]
}
}
}

impl HeaderBackend<Block> for TestApi {
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
Loading

0 comments on commit 91f62c4

Please sign in to comment.