Skip to content

Commit dd162d7

Browse files
committed
remove slot dependency from pallet-block-production-log
1 parent 2ea3364 commit dd162d7

File tree

18 files changed

+228
-220
lines changed

18 files changed

+228
-220
lines changed

Cargo.lock

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

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ and epoch duration to calculate current epoch.
1717
Since the block timestamp is not passed to the inherent data provider creation logic by Aura, the exact timestamp is not
1818
available during block verification, but it can be approximated based on the slot number. See the example impelementation
1919
in the demo node crate.
20+
* `pallet-block-production-log` no longer uses slots and is instead parametrized by a `Moment` type.
21+
To match this change, `BlockProductionLogApi::get_author` now accepts a `Moment` value and `BlockAuthorInherentProvider`
22+
also takes `Moment` as type and constructor parameter.
2023

2124
## Removed
2225

demo/node/src/inherent_data.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ where
6060
AuthoritySelectionInputs,
6161
ScEpochNumber,
6262
>,
63-
T::Api: BlockProductionLogApi<Block, CommitteeMember<CrossChainPublic, SessionKeys>>,
63+
T::Api: BlockProductionLogApi<Block, BlockAuthor, Slot>,
6464
T::Api: BlockParticipationApi<Block, BlockAuthor>,
6565
T::Api: GovernedMapIDPApi<Block>,
6666
T::Api: TokenBridgeIDPRuntimeApi<Block>,
@@ -70,7 +70,7 @@ where
7070
TimestampIDP,
7171
McHashIDP,
7272
AriadneIDP,
73-
BlockAuthorInherentProvider<BlockAuthor>,
73+
BlockAuthorInherentProvider<Slot, BlockAuthor>,
7474
BlockParticipationInherentDataProvider<BlockAuthor, DelegatorKey>,
7575
GovernedMapInherentDataProvider,
7676
TokenBridgeInherentDataProvider<AccountId>,
@@ -183,15 +183,15 @@ where
183183
AuthoritySelectionInputs,
184184
ScEpochNumber,
185185
>,
186-
T::Api: BlockProductionLogApi<Block, CommitteeMember<CrossChainPublic, SessionKeys>>,
186+
T::Api: BlockProductionLogApi<Block, BlockAuthor, Slot>,
187187
T::Api: BlockParticipationApi<Block, BlockAuthor>,
188188
T::Api: GovernedMapIDPApi<Block>,
189189
T::Api: TokenBridgeIDPRuntimeApi<Block>,
190190
{
191191
type InherentDataProviders = (
192192
TimestampIDP,
193193
AriadneIDP,
194-
BlockAuthorInherentProvider<BlockAuthor>,
194+
BlockAuthorInherentProvider<Slot, BlockAuthor>,
195195
BlockParticipationInherentDataProvider<BlockAuthor, DelegatorKey>,
196196
GovernedMapInherentDataProvider,
197197
TokenBridgeInherentDataProvider<AccountId>,

demo/node/src/tests/inherent_data_tests.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use sidechain_domain::{
1515
};
1616
use sidechain_mc_hash::mock::MockMcHashDataSource;
1717
use sp_block_participation::BlockProductionData;
18+
use sp_block_production_log::BlockProductionInherentDataV1;
1819
use sp_consensus_aura::Slot;
1920
use sp_core::ecdsa;
2021
use sp_inherents::CreateInherentDataProviders;
@@ -87,14 +88,19 @@ async fn block_proposal_cidp_should_be_created_correctly() {
8788
);
8889
assert_eq!(
8990
inherent_data
90-
.get_data::<BlockAuthor>(&sp_block_production_log::INHERENT_IDENTIFIER)
91+
.get_data::<BlockProductionInherentDataV1<u64, BlockAuthor>>(
92+
&sp_block_production_log::INHERENT_IDENTIFIER
93+
)
9194
.unwrap(),
92-
Some(BlockAuthor::ProBono(
93-
ecdsa::Public::from_raw(hex!(
94-
"000000000000000000000000000000000000000000000000000000000000000001"
95-
))
96-
.into()
97-
))
95+
Some(BlockProductionInherentDataV1 {
96+
moment: 30,
97+
block_producer_id: BlockAuthor::ProBono(
98+
ecdsa::Public::from_raw(hex!(
99+
"000000000000000000000000000000000000000000000000000000000000000001"
100+
))
101+
.into()
102+
)
103+
})
98104
);
99105
assert_eq!(
100106
inherent_data
@@ -163,14 +169,19 @@ async fn block_verification_cidp_should_be_created_correctly() {
163169
);
164170
assert_eq!(
165171
inherent_data
166-
.get_data::<BlockAuthor>(&sp_block_production_log::INHERENT_IDENTIFIER)
172+
.get_data::<BlockProductionInherentDataV1<u64, BlockAuthor>>(
173+
&sp_block_production_log::INHERENT_IDENTIFIER
174+
)
167175
.unwrap(),
168-
Some(BlockAuthor::ProBono(
169-
ecdsa::Public::from_raw(hex!(
170-
"000000000000000000000000000000000000000000000000000000000000000001"
171-
))
172-
.into()
173-
))
176+
Some(BlockProductionInherentDataV1 {
177+
moment: 30,
178+
block_producer_id: BlockAuthor::ProBono(
179+
ecdsa::Public::from_raw(hex!(
180+
"000000000000000000000000000000000000000000000000000000000000000001"
181+
))
182+
.into()
183+
)
184+
})
174185
);
175186
assert_eq!(
176187
inherent_data

demo/node/src/tests/runtime_api_mock.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use sidechain_mc_hash::McHashInherentDigest;
88
use sidechain_slots::Slot;
99
use sp_api::{ApiRef, ProvideRuntimeApi};
1010
use sp_blockchain::HeaderBackend;
11-
use sp_core::{ecdsa, ed25519, sr25519};
11+
use sp_core::ecdsa;
1212
use sp_governed_map::MainChainScriptsV1;
1313
use sp_inherents::InherentIdentifier;
1414
use sp_runtime::Digest;
@@ -97,14 +97,10 @@ sp_api::mock_impl_runtime_apis! {
9797
}
9898
}
9999

100-
impl sp_block_production_log::BlockProductionLogApi<Block, CommitteeMember<CrossChainPublic, SessionKeys>> for TestApi {
101-
fn get_author(_slot: Slot) -> Option<CommitteeMember<CrossChainPublic, SessionKeys>> {
102-
Some(CommitteeMember::permissioned(
103-
ecdsa::Public::from_raw(hex!("000000000000000000000000000000000000000000000000000000000000000001")).into(),
104-
SessionKeys {
105-
aura: sr25519::Public::default().into(),
106-
grandpa: ed25519::Public::default().into()
107-
}
100+
impl sp_block_production_log::BlockProductionLogApi<Block, BlockAuthor, Slot> for TestApi {
101+
fn get_author(_slot: &Slot) -> Option<BlockAuthor> {
102+
Some(BlockAuthor::ProBono(
103+
ecdsa::Public::from_raw(hex!("000000000000000000000000000000000000000000000000000000000000000001")).into()
108104
))
109105
}
110106
}

demo/runtime/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,7 @@ impl pallet_block_production_log::Config for Runtime {
514514
type BlockProducerId = BlockAuthor;
515515
type WeightInfo = pallet_block_production_log::weights::SubstrateWeight<Runtime>;
516516

517-
fn current_slot() -> sp_consensus_slots::Slot {
518-
let slot: u64 = pallet_aura::CurrentSlot::<Runtime>::get().into();
519-
sp_consensus_slots::Slot::from(slot)
520-
}
517+
type Moment = Slot;
521518

522519
#[cfg(feature = "runtime-benchmarks")]
523520
type BenchmarkHelper = PalletBlockProductionLogBenchmarkHelper;
@@ -1135,9 +1132,10 @@ impl_runtime_apis! {
11351132
}
11361133
}
11371134

1138-
impl sp_block_production_log::BlockProductionLogApi<Block, CommitteeMember<CrossChainPublic, SessionKeys>> for Runtime {
1139-
fn get_author(slot: Slot) -> Option<CommitteeMember<CrossChainPublic, SessionKeys>> {
1140-
SessionCommitteeManagement::get_current_authority_round_robin(*slot as usize)
1135+
impl sp_block_production_log::BlockProductionLogApi<Block, BlockAuthor, Slot> for Runtime {
1136+
fn get_author(slot: &Slot) -> Option<BlockAuthor> {
1137+
SessionCommitteeManagement::get_current_authority_round_robin(u64::from(*slot) as usize)
1138+
.map(Into::into)
11411139
}
11421140
}
11431141

e2e-tests/src/substrate_api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,12 @@ def get_validator_set(self, block):
597597
result.append(memberParams)
598598
return result
599599

600+
def get_block_slot(self, block):
601+
return self.substrate.query("Aura", "CurrentSlot", block_hash=block["header"]["hash"]).value
602+
603+
def get_block_timestamp(self, block):
604+
return self.substrate.query("Timestamp", "Now", block_hash=block["header"]["hash"]).value
605+
600606
def get_block_author_and_slot(self, block, validator_set):
601607
"""Custom implementation of substrate.get_block(include_author=True) to get block author, and block slot.
602608
py-substrate-interface does not work because it calls "Validators" function from "Session" pallet,

e2e-tests/tests/delegator_rewards/test_block_production_log.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def test_block_production_log_pallet(api: BlockchainApi, config: ApiConfig):
1717
block_production_log.reverse()
1818
for slot, block_producer_id in block_production_log:
1919
committee = api.get_validator_set(block)
20+
2021
author_aura_key = committee[slot % len(committee)]["keys"]["aura"]
2122
expected_node = next(x for x in config.nodes_config.nodes.values() if x.aura_public_key == author_aura_key)
2223

toolkit/block-production-log/pallet/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "pallet-block-production-log"
33
version.workspace = true
4-
description = "Keeps log of slot to block producer id"
4+
description = "Keeps log of timestamp to block producer id"
55
license = "Apache-2.0"
66
readme = "README.md"
77
authors.workspace = true
@@ -21,7 +21,6 @@ parity-scale-codec = { workspace = true }
2121
scale-info = { workspace = true }
2222
sidechain-domain = { workspace = true }
2323
sp-block-production-log = { workspace = true }
24-
sp-consensus-slots = { workspace = true, features = ["serde"] }
2524
sp-runtime = { workspace = true }
2625
sp-std = { workspace = true }
2726

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Block Production Log pallet
22

3-
This pallet keeps a sorted log of Slot to BlockProducerId.
3+
This pallet keeps a sorted log of timestamp to BlockProducerId.
44
It is a user decision what is the concrete type of BlockProducerId.
55
Inherent data provider that provides BlockProducerId should be wired into the node to make this pallet useful.
66
User should periodically call `take_prefix` to shrink used storage and consume the log according to their needs.
7-
Pallet supports handling many blocks per slot.

0 commit comments

Comments
 (0)