Skip to content

Commit 2707597

Browse files
committed
decouple block participation from slots
1 parent dd162d7 commit 2707597

File tree

19 files changed

+290
-299
lines changed

19 files changed

+290
-299
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/node/src/inherent_data.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ where
6161
ScEpochNumber,
6262
>,
6363
T::Api: BlockProductionLogApi<Block, BlockAuthor, Slot>,
64-
T::Api: BlockParticipationApi<Block, BlockAuthor>,
64+
T::Api: BlockParticipationApi<Block, BlockAuthor, Slot>,
6565
T::Api: GovernedMapIDPApi<Block>,
6666
T::Api: TokenBridgeIDPRuntimeApi<Block>,
6767
{
@@ -71,7 +71,7 @@ where
7171
McHashIDP,
7272
AriadneIDP,
7373
BlockAuthorInherentProvider<Slot, BlockAuthor>,
74-
BlockParticipationInherentDataProvider<BlockAuthor, DelegatorKey>,
74+
BlockParticipationInherentDataProvider<BlockAuthor, DelegatorKey, Slot>,
7575
GovernedMapInherentDataProvider,
7676
TokenBridgeInherentDataProvider<AccountId>,
7777
);
@@ -122,7 +122,6 @@ where
122122
parent_hash,
123123
*slot,
124124
mc_epoch_config,
125-
config.sc_slot_config.slot_duration,
126125
)
127126
.await?;
128127

@@ -184,15 +183,15 @@ where
184183
ScEpochNumber,
185184
>,
186185
T::Api: BlockProductionLogApi<Block, BlockAuthor, Slot>,
187-
T::Api: BlockParticipationApi<Block, BlockAuthor>,
186+
T::Api: BlockParticipationApi<Block, BlockAuthor, Slot>,
188187
T::Api: GovernedMapIDPApi<Block>,
189188
T::Api: TokenBridgeIDPRuntimeApi<Block>,
190189
{
191190
type InherentDataProviders = (
192191
TimestampIDP,
193192
AriadneIDP,
194193
BlockAuthorInherentProvider<Slot, BlockAuthor>,
195-
BlockParticipationInherentDataProvider<BlockAuthor, DelegatorKey>,
194+
BlockParticipationInherentDataProvider<BlockAuthor, DelegatorKey, Slot>,
196195
GovernedMapInherentDataProvider,
197196
TokenBridgeInherentDataProvider<AccountId>,
198197
);
@@ -248,7 +247,6 @@ where
248247
parent_hash,
249248
verified_block_slot,
250249
mc_epoch_config,
251-
config.sc_slot_config.slot_duration,
252250
)
253251
.await?;
254252

demo/node/src/tests/inherent_data_tests.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::inherent_data::{ProposalCIDP, VerifierCIDP};
2-
use crate::tests::mock::{test_client, test_create_inherent_data_config};
2+
use crate::tests::mock::{
3+
block_participation_data, past_block_author, past_block_slot, test_create_inherent_data_config,
4+
};
35
use crate::tests::runtime_api_mock;
46
use crate::tests::runtime_api_mock::{TestApi, mock_header};
57
use authority_selection_inherents::{
@@ -46,6 +48,7 @@ async fn block_proposal_cidp_should_be_created_correctly() {
4648
test_create_inherent_data_config(),
4749
TestApi::new(ScEpochNumber(2))
4850
.with_headers([(mock_header().hash(), mock_header())])
51+
.with_pariticipation_data(vec![(past_block_slot(), past_block_author())])
4952
.into(),
5053
Arc::new(mc_hash_data_source),
5154
Arc::new(MockAuthoritySelectionDataSource::default()),
@@ -114,7 +117,7 @@ async fn block_proposal_cidp_should_be_created_correctly() {
114117
&runtime_api_mock::TEST_TARGET_INHERENT_ID
115118
)
116119
.unwrap(),
117-
Some(BlockProductionData::new(Slot::from(30), vec![]))
120+
Some(block_participation_data())
118121
);
119122
}
120123

@@ -140,7 +143,9 @@ async fn block_verification_cidp_should_be_created_correctly() {
140143

141144
let verifier_cidp = VerifierCIDP::new(
142145
create_inherent_data_config.clone(),
143-
test_client(),
146+
TestApi::new(ScEpochNumber(2))
147+
.with_pariticipation_data(vec![(past_block_slot(), past_block_author())])
148+
.into(),
144149
Arc::new(mc_hash_data_source),
145150
Arc::new(MockAuthoritySelectionDataSource::default()),
146151
Arc::new(StakeDistributionDataSourceMock::new()),
@@ -195,6 +200,6 @@ async fn block_verification_cidp_should_be_created_correctly() {
195200
&runtime_api_mock::TEST_TARGET_INHERENT_ID
196201
)
197202
.unwrap(),
198-
Some(BlockProductionData::new(Slot::from(30), vec![]))
203+
Some(block_participation_data())
199204
);
200205
}

demo/node/src/tests/mock.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#![allow(clippy::type_complexity)]
22

33
use crate::inherent_data::CreateInherentDataConfig;
4-
use crate::tests::runtime_api_mock::TestApi;
54
use hex_literal::hex;
5+
use partner_chains_demo_runtime::{BlockAuthor, CrossChainPublic};
66
use sc_consensus_aura::SlotDuration;
77
use sidechain_domain::mainchain_epoch::MainchainEpochConfig;
88
use sidechain_domain::*;
9-
use sidechain_slots::{ScSlotConfig, SlotsPerEpoch};
10-
use sp_core::offchain::{Duration, Timestamp};
9+
use sidechain_slots::{ScSlotConfig, Slot, SlotsPerEpoch};
10+
use sp_block_participation::{BlockProducerParticipationData, BlockProductionData};
11+
use sp_core::{
12+
ecdsa,
13+
offchain::{Duration, Timestamp},
14+
};
1115
use std::sync::Arc;
1216

1317
pub fn mock_genesis_utxo() -> UtxoId {
@@ -39,8 +43,20 @@ pub fn test_epoch_config() -> MainchainEpochConfig {
3943
}
4044
}
4145

42-
pub fn test_client() -> Arc<TestApi> {
43-
Arc::new(TestApi::new(ScEpochNumber(2)))
46+
pub fn past_block_author() -> BlockAuthor {
47+
BlockAuthor::ProBono(CrossChainPublic::from(ecdsa::Public::default()))
48+
}
49+
pub fn past_block_slot() -> Slot {
50+
Slot::from(420)
51+
}
52+
53+
pub fn block_participation_data() -> BlockProductionData<BlockAuthor, DelegatorKey> {
54+
BlockProductionData::new(vec![BlockProducerParticipationData {
55+
block_producer: past_block_author(),
56+
block_count: 1,
57+
delegator_total_shares: 0,
58+
delegators: vec![],
59+
}])
4460
}
4561

4662
pub fn test_create_inherent_data_config() -> CreateInherentDataConfig {

demo/node/src/tests/runtime_api_mock.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,24 @@ type Header = <Block as BlockT>::Header;
2424
pub struct TestApi {
2525
pub next_unset_epoch_number: ScEpochNumber,
2626
pub headers: HashMap<Hash, Header>,
27+
pub participation_data: Vec<(Slot, BlockAuthor)>,
2728
}
2829

2930
impl TestApi {
3031
pub fn new(next_unset_epoch_number: ScEpochNumber) -> Self {
3132
let header = mock_header();
3233
let mut headers = HashMap::new();
3334
headers.insert(header.hash(), header);
34-
Self { next_unset_epoch_number, headers }
35+
Self { next_unset_epoch_number, headers, participation_data: Default::default() }
3536
}
3637

3738
pub fn with_headers<Hs: Into<HashMap<Hash, Header>>>(self, headers: Hs) -> Self {
3839
Self { headers: headers.into(), ..self }
3940
}
41+
42+
pub fn with_pariticipation_data(self, participation_data: Vec<(Slot, BlockAuthor)>) -> Self {
43+
Self { participation_data, ..self }
44+
}
4045
}
4146

4247
impl Default for TestApi {
@@ -105,16 +110,16 @@ sp_api::mock_impl_runtime_apis! {
105110
}
106111
}
107112

108-
impl sp_block_participation::BlockParticipationApi<Block, BlockAuthor> for TestApi {
109-
fn should_release_data(slot: Slot) -> Option<Slot> {
110-
Some(slot)
111-
}
112-
fn blocks_produced_up_to_slot(_slot: Slot) -> Vec<(Slot, BlockAuthor)> {
113-
vec![]
113+
impl sp_block_participation::BlockParticipationApi<Block, BlockAuthor, Slot> for TestApi {
114+
fn blocks_to_process(_slot: &Slot) -> Vec<(Slot, BlockAuthor)> {
115+
self.participation_data.clone()
114116
}
115117
fn target_inherent_id() -> InherentIdentifier {
116118
TEST_TARGET_INHERENT_ID
117119
}
120+
fn moment_to_timestamp_millis(slot: Slot) -> u64 {
121+
1000 * (*slot)
122+
}
118123
}
119124

120125
impl sp_governed_map::GovernedMapIDPApi<Block> for TestApi {

demo/runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ frame-system-benchmarking = { workspace = true, optional = true }
7373

7474
# Local Dependencies
7575
sp-block-production-log = { workspace = true }
76-
pallet-block-production-log = { workspace = true }
76+
pallet-block-production-log = { workspace = true, features = ["block-participation"] }
7777
sp-sidechain = { workspace = true }
7878
pallet-sidechain = { workspace = true }
7979
pallet-session-validator-management = { workspace = true }

demo/runtime/src/lib.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -593,20 +593,12 @@ impl pallet_block_producer_metadata::Config for Runtime {
593593

594594
impl pallet_block_participation::Config for Runtime {
595595
type WeightInfo = pallet_block_participation::weights::SubstrateWeight<Runtime>;
596-
type BlockAuthor = BlockAuthor;
597596
type DelegatorId = DelegatorKey;
597+
type BlockAuthor = BlockAuthor;
598598

599-
fn should_release_data(slot: sidechain_slots::Slot) -> Option<sidechain_slots::Slot> {
600-
TestHelperPallet::should_release_participation_data(slot)
601-
}
602-
603-
fn blocks_produced_up_to_slot(slot: Slot) -> impl Iterator<Item = (Slot, BlockAuthor)> {
604-
BlockProductionLog::peek_prefix(slot)
605-
}
599+
type Moment = Slot;
606600

607-
fn discard_blocks_produced_up_to_slot(slot: Slot) {
608-
BlockProductionLog::drop_prefix(&slot)
609-
}
601+
type BlockParticipationProvider = BlockProductionLog;
610602

611603
const TARGET_INHERENT_ID: InherentIdentifier = TestHelperPallet::INHERENT_IDENTIFIER;
612604
}
@@ -1139,15 +1131,16 @@ impl_runtime_apis! {
11391131
}
11401132
}
11411133

1142-
impl sp_block_participation::BlockParticipationApi<Block, BlockAuthor> for Runtime {
1143-
fn should_release_data(slot: Slot) -> Option<Slot> {
1144-
BlockParticipation::should_release_data(slot)
1145-
}
1146-
fn blocks_produced_up_to_slot(slot: Slot) -> Vec<(Slot, BlockAuthor)> {
1147-
<Runtime as pallet_block_participation::Config>::blocks_produced_up_to_slot(slot).collect()
1134+
impl sp_block_participation::BlockParticipationApi<Block, BlockAuthor, Slot> for Runtime {
1135+
fn blocks_to_process(slot: &Slot) -> Vec<(Slot, BlockAuthor)> {
1136+
BlockParticipation::blocks_to_process(slot)
11481137
}
11491138
fn target_inherent_id() -> InherentIdentifier {
1150-
<Runtime as pallet_block_participation::Config>::TARGET_INHERENT_ID
1139+
BlockParticipation::target_inherent_id()
1140+
}
1141+
fn moment_to_timestamp_millis(moment: Slot) -> u64 {
1142+
let slot_duration_millis = <Self as sp_consensus_aura::runtime_decl_for_aura_api::AuraApi<Block, AuraId>>::slot_duration().as_millis();
1143+
*moment * slot_duration_millis
11511144
}
11521145
}
11531146

demo/runtime/src/test_helper_pallet.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,6 @@ pub mod pallet {
6464
}
6565
}
6666

67-
impl<T: Config> Pallet<T> {
68-
pub fn should_release_participation_data(
69-
slot: sidechain_slots::Slot,
70-
) -> Option<sidechain_slots::Slot> {
71-
if (*slot).is_multiple_of(ParticipationDataReleasePeriod::<T>::get()) {
72-
Some(slot)
73-
} else {
74-
None
75-
}
76-
}
77-
}
78-
7967
impl<T: Config> sp_sidechain::OnNewEpoch for Pallet<T> {
8068
fn on_new_epoch(
8169
_old_epoch: ScEpochNumber,

e2e-tests/tests/delegator_rewards/test_spo_journey.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ def block_production_log(block_to_query_storage, api: BlockchainApi):
4949

5050
@fixture(scope="module")
5151
def pc_epochs(block_participation, config: ApiConfig, initial_pc_epoch):
52-
up_to_slot = block_participation["up_to_slot"]
53-
logging.info(f"Participation data up to slot: {up_to_slot}")
54-
start_pc_epoch = (up_to_slot - PARTICIPATION_DATA_SLOT_RANGE) // config.nodes_config.slots_in_epoch
55-
stop_pc_epoch = up_to_slot // config.nodes_config.slots_in_epoch
52+
up_to_moment = block_participation["up_to_moment"]
53+
logging.info(f"Participation data up to slot: {up_to_moment}")
54+
start_pc_epoch = (up_to_moment - PARTICIPATION_DATA_SLOT_RANGE) // config.nodes_config.slots_in_epoch
55+
stop_pc_epoch = up_to_moment // config.nodes_config.slots_in_epoch
5656
epochs = range(start_pc_epoch, stop_pc_epoch)
5757
logging.info(f"Participation data spans PC epochs: {epochs}")
5858
if initial_pc_epoch in epochs:
@@ -114,7 +114,7 @@ def _count_blocks(mc_epoch, producer):
114114
@mark.xdist_group("block_participation")
115115
def test_block_participation_data_is_not_empty(block_participation):
116116
assert block_participation
117-
assert block_participation["up_to_slot"]
117+
assert block_participation["up_to_moment"]
118118
assert block_participation["producer_participation"]
119119

120120

toolkit/block-participation/pallet/src/benchmarking.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use crate::Pallet as BlockParticipation;
55
use frame_benchmarking::v2::*;
66
use frame_system::RawOrigin;
77

8-
#[benchmarks]
8+
#[benchmarks(where <T as crate::Config>::Moment: From<u64>)]
99
mod benchmarks {
1010
use super::*;
1111

1212
#[benchmark]
1313
fn note_processing() -> Result<(), BenchmarkError> {
1414
#[block]
1515
{
16-
BlockParticipation::<T>::note_processing(RawOrigin::None.into(), Slot::from(42))?;
16+
BlockParticipation::<T>::note_processing(RawOrigin::None.into(), 42u64.into())?;
1717
}
1818
Ok(())
1919
}

0 commit comments

Comments
 (0)