Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 710f4ca

Browse files
montekkirphmeier
andauthored
Remove Parent Hash to Session mapping (#928)
* Adds a SigningContext type * Bump spec versions * Fixes requested changes * Bump ParachainHost api_version and guard signing_context call * Improve error message * If there is no signing_context api use default value Co-authored-by: Robert Habermeier <[email protected]>
1 parent 5f37995 commit 710f4ca

File tree

15 files changed

+254
-161
lines changed

15 files changed

+254
-161
lines changed

Cargo.lock

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

network/src/legacy/gossip/attestation.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,13 @@ impl View {
131131
///
132132
/// This will be pruned later on a call to `prune_old_leaves`, when this leaf
133133
/// is not a leaf anymore.
134-
pub(super) fn new_local_leaf(&mut self, relay_chain_leaf: Hash, validation_data: MessageValidationData) {
134+
pub(super) fn new_local_leaf(
135+
&mut self,
136+
validation_data: MessageValidationData,
137+
) {
138+
let relay_chain_leaf = validation_data.signing_context.parent_hash.clone();
135139
self.leaf_work.push((
136-
relay_chain_leaf,
140+
validation_data.signing_context.parent_hash.clone(),
137141
LeafView {
138142
validation_data,
139143
knowledge: Default::default(),
@@ -207,7 +211,6 @@ impl View {
207211

208212
// validate signature.
209213
let res = view.validation_data.check_statement(
210-
&message.relay_chain_leaf,
211214
&message.signed_statement,
212215
);
213216

network/src/legacy/gossip/mod.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use sc_network_gossip::{
6060
use polkadot_validation::{SignedStatement};
6161
use polkadot_primitives::{Block, Hash};
6262
use polkadot_primitives::parachain::{
63-
ParachainHost, ValidatorId, ErasureChunk as PrimitiveChunk
63+
ParachainHost, ValidatorId, ErasureChunk as PrimitiveChunk, SigningContext,
6464
};
6565
use polkadot_erasure_coding::{self as erasure};
6666
use codec::{Decode, Encode};
@@ -377,13 +377,12 @@ impl RegisteredMessageValidator {
377377
/// relevant to this leaf.
378378
pub(crate) fn new_local_leaf(
379379
&self,
380-
relay_chain_leaf: Hash,
381380
validation: MessageValidationData,
382381
) -> NewLeafActions {
383382
// add an entry in attestation_view
384383
// prune any entries from attestation_view which are no longer leaves
385384
let mut inner = self.inner.inner.write();
386-
inner.attestation_view.new_local_leaf(relay_chain_leaf, validation);
385+
inner.attestation_view.new_local_leaf(validation);
387386

388387
let mut actions = Vec::new();
389388

@@ -460,11 +459,13 @@ impl GossipService for RegisteredMessageValidator {
460459
pub(crate) struct MessageValidationData {
461460
/// The authorities' parachain validation keys at a block.
462461
pub(crate) authorities: Vec<ValidatorId>,
462+
/// The signing context.
463+
pub(crate) signing_context: SigningContext,
463464
}
464465

465466
impl MessageValidationData {
466467
// check a statement's signature.
467-
fn check_statement(&self, relay_chain_leaf: &Hash, statement: &SignedStatement) -> Result<(), ()> {
468+
fn check_statement(&self, statement: &SignedStatement) -> Result<(), ()> {
468469
let sender = match self.authorities.get(statement.sender as usize) {
469470
Some(val) => val,
470471
None => return Err(()),
@@ -475,7 +476,7 @@ impl MessageValidationData {
475476
&statement.statement,
476477
&statement.signature,
477478
sender.clone(),
478-
relay_chain_leaf,
479+
&self.signing_context,
479480
);
480481

481482
if good {
@@ -826,7 +827,9 @@ mod tests {
826827
let topic_c = attestation_topic(hash_c);
827828

828829
// topic_a is in all 3 views -> succeed
829-
validator.inner.write().attestation_view.new_local_leaf(hash_a, MessageValidationData::default());
830+
let mut validation_data = MessageValidationData::default();
831+
validation_data.signing_context.parent_hash = hash_a;
832+
validator.inner.write().attestation_view.new_local_leaf(validation_data);
830833
// topic_b is in the neighbor's view but not ours -> fail
831834
// topic_c is not in either -> fail
832835

@@ -937,7 +940,9 @@ mod tests {
937940
}
938941
});
939942
let encoded = statement.encode();
940-
validator.inner.write().attestation_view.new_local_leaf(hash_a, MessageValidationData::default());
943+
let mut validation_data = MessageValidationData::default();
944+
validation_data.signing_context.parent_hash = hash_a;
945+
validator.inner.write().attestation_view.new_local_leaf(validation_data);
941946

942947
{
943948
let mut message_allowed = validator.message_allowed();

network/src/protocol/mod.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ impl NetworkServiceOps for PolkadotNetworkService {
162162
trait GossipOps: Clone + Send + crate::legacy::GossipService + 'static {
163163
fn new_local_leaf(
164164
&self,
165-
relay_parent: Hash,
166165
validation_data: crate::legacy::gossip::MessageValidationData,
167166
) -> crate::legacy::gossip::NewLeafActions;
168167

@@ -177,10 +176,12 @@ trait GossipOps: Clone + Send + crate::legacy::GossipService + 'static {
177176
impl GossipOps for RegisteredMessageValidator {
178177
fn new_local_leaf(
179178
&self,
180-
relay_parent: Hash,
181179
validation_data: crate::legacy::gossip::MessageValidationData,
182180
) -> crate::legacy::gossip::NewLeafActions {
183-
RegisteredMessageValidator::new_local_leaf(self, relay_parent, validation_data)
181+
RegisteredMessageValidator::new_local_leaf(
182+
self,
183+
validation_data,
184+
)
184185
}
185186

186187
fn register_availability_store(
@@ -804,7 +805,6 @@ impl<Api, Sp, Gossip> Worker<Api, Sp, Gossip> where
804805
authorities: Vec<ValidatorId>,
805806
) {
806807
// glue: let gossip know about our new local leaf.
807-
let relay_parent = table.consensus_parent_hash().clone();
808808
let (signal, exit) = exit_future::signal();
809809

810810
let key = table.session_key();
@@ -814,19 +814,20 @@ impl<Api, Sp, Gossip> Worker<Api, Sp, Gossip> where
814814
}
815815
}
816816

817+
let signing_context = table.signing_context().clone();
818+
let relay_parent = signing_context.parent_hash.clone();
817819
let new_leaf_actions = self.gossip_handle.new_local_leaf(
818-
relay_parent,
819-
crate::legacy::gossip::MessageValidationData { authorities },
820+
crate::legacy::gossip::MessageValidationData { authorities, signing_context },
820821
);
821822

822823
new_leaf_actions.perform(&self.gossip_handle);
823824

824825
self.protocol_handler.consensus_instances.insert(
825-
relay_parent,
826+
relay_parent.clone(),
826827
ConsensusNetworkingInstance {
827828
statement_table: table.clone(),
828-
relay_parent,
829-
attestation_topic: crate::legacy::gossip::attestation_topic(relay_parent),
829+
relay_parent: relay_parent.clone(),
830+
attestation_topic: crate::legacy::gossip::attestation_topic(relay_parent.clone()),
830831
_drop_signal: signal,
831832
},
832833
);
@@ -1324,7 +1325,7 @@ impl ParachainNetwork for Service {
13241325
) -> Self::BuildTableRouter {
13251326
let authorities = authorities.to_vec();
13261327
let mut sender = self.sender.clone();
1327-
let relay_parent = table.consensus_parent_hash().clone();
1328+
let relay_parent = table.signing_context().parent_hash.clone();
13281329

13291330
Box::pin(async move {
13301331
sender.send(

network/src/protocol/tests.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use polkadot_primitives::{Block, Header, BlockId};
2020
use polkadot_primitives::parachain::{
2121
Id as ParaId, Chain, DutyRoster, ParachainHost, ValidatorId,
2222
Retriable, CollatorId, AbridgedCandidateReceipt,
23-
GlobalValidationSchedule, LocalValidationData, ErasureChunk,
23+
GlobalValidationSchedule, LocalValidationData, ErasureChunk, SigningContext,
2424
};
2525
use polkadot_validation::SharedTable;
2626

@@ -114,7 +114,6 @@ impl crate::legacy::GossipService for MockGossip {
114114
impl GossipOps for MockGossip {
115115
fn new_local_leaf(
116116
&self,
117-
_relay_parent: Hash,
118117
_validation_data: crate::legacy::gossip::MessageValidationData,
119118
) -> crate::legacy::gossip::NewLeafActions {
120119
crate::legacy::gossip::NewLeafActions::new()
@@ -294,6 +293,22 @@ impl ParachainHost<Block> for RuntimeApi {
294293
) -> ClientResult<NativeOrEncoded<Option<Vec<AbridgedCandidateReceipt>>>> {
295294
Ok(NativeOrEncoded::Native(Some(Vec::new())))
296295
}
296+
297+
fn ParachainHost_signing_context_runtime_api_impl(
298+
&self,
299+
_at: &BlockId,
300+
_: ExecutionContext,
301+
_: Option<()>,
302+
_: Vec<u8>,
303+
) -> ClientResult<NativeOrEncoded<SigningContext>> {
304+
Ok(NativeOrEncoded::Native(
305+
SigningContext {
306+
session_index: Default::default(),
307+
parent_hash: Default::default(),
308+
}
309+
)
310+
)
311+
}
297312
}
298313

299314
impl super::Service {
@@ -389,11 +404,15 @@ fn consensus_instances_cleaned_up() {
389404
let relay_parent = [0; 32].into();
390405
let authorities = Vec::new();
391406

407+
let signing_context = SigningContext {
408+
session_index: Default::default(),
409+
parent_hash: relay_parent,
410+
};
392411
let table = Arc::new(SharedTable::new(
393412
Vec::new(),
394413
HashMap::new(),
395414
None,
396-
relay_parent,
415+
signing_context,
397416
AvailabilityStore::new_in_memory(service.clone()),
398417
None,
399418
));

primitives/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ application-crypto = { package = "sp-application-crypto", git = "https://github.
1313
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
1414
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
1515
sp-std = { package = "sp-std", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
16+
sp-staking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
1617
runtime_primitives = { package = "sp-runtime", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
1718
polkadot-parachain = { path = "../parachain", default-features = false }
1819
trie = { package = "sp-trie", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
@@ -33,6 +34,7 @@ std = [
3334
"sp-api/std",
3435
"sp-std/std",
3536
"sp-version/std",
37+
"sp-staking/std",
3638
"runtime_primitives/std",
3739
"serde",
3840
"polkadot-parachain/std",

primitives/src/parachain.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,15 @@ pub enum ValidityAttestation {
608608
Explicit(ValidatorSignature),
609609
}
610610

611+
/// A type returned by runtime with current session index and a parent hash.
612+
#[derive(Clone, Eq, PartialEq, Default, Decode, Encode, RuntimeDebug)]
613+
pub struct SigningContext {
614+
/// Current session index.
615+
pub session_index: sp_staking::SessionIndex,
616+
/// Hash of the parent.
617+
pub parent_hash: Hash,
618+
}
619+
611620
/// An attested candidate. This is submitted to the relay chain by a block author.
612621
#[derive(Clone, PartialEq, Decode, Encode, RuntimeDebug)]
613622
pub struct AttestedCandidate {
@@ -655,7 +664,7 @@ impl FeeSchedule {
655664

656665
sp_api::decl_runtime_apis! {
657666
/// The API for querying the state of parachains on-chain.
658-
#[api_version(2)]
667+
#[api_version(3)]
659668
pub trait ParachainHost {
660669
/// Get the current validators.
661670
fn validators() -> Vec<ValidatorId>;
@@ -673,6 +682,8 @@ sp_api::decl_runtime_apis! {
673682
/// Extract the abridged head that was set in the extrinsics.
674683
fn get_heads(extrinsics: Vec<<Block as BlockT>::Extrinsic>)
675684
-> Option<Vec<AbridgedCandidateReceipt>>;
685+
/// Get a `SigningContext` with current `SessionIndex` and parent hash.
686+
fn signing_context() -> SigningContext;
676687
}
677688
}
678689

0 commit comments

Comments
 (0)