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

Remove Parent Hash to Session mapping #928

Merged
merged 9 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

9 changes: 6 additions & 3 deletions network/src/legacy/gossip/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ impl View {
///
/// This will be pruned later on a call to `prune_old_leaves`, when this leaf
/// is not a leaf anymore.
pub(super) fn new_local_leaf(&mut self, relay_chain_leaf: Hash, validation_data: MessageValidationData) {
pub(super) fn new_local_leaf(
&mut self,
validation_data: MessageValidationData,
montekki marked this conversation as resolved.
Show resolved Hide resolved
) {
let relay_chain_leaf = validation_data.signing_context.parent_hash.clone();
self.leaf_work.push((
relay_chain_leaf,
validation_data.signing_context.parent_hash.clone(),
LeafView {
validation_data,
knowledge: Default::default(),
Expand Down Expand Up @@ -207,7 +211,6 @@ impl View {

// validate signature.
let res = view.validation_data.check_statement(
&message.relay_chain_leaf,
&message.signed_statement,
);

Expand Down
19 changes: 12 additions & 7 deletions network/src/legacy/gossip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use sc_network_gossip::{
use polkadot_validation::{SignedStatement};
use polkadot_primitives::{Block, Hash};
use polkadot_primitives::parachain::{
ParachainHost, ValidatorId, ErasureChunk as PrimitiveChunk
ParachainHost, ValidatorId, ErasureChunk as PrimitiveChunk, SigningContext,
};
use polkadot_erasure_coding::{self as erasure};
use codec::{Decode, Encode};
Expand Down Expand Up @@ -377,13 +377,12 @@ impl RegisteredMessageValidator {
/// relevant to this leaf.
pub(crate) fn new_local_leaf(
&self,
relay_chain_leaf: Hash,
validation: MessageValidationData,
) -> NewLeafActions {
// add an entry in attestation_view
// prune any entries from attestation_view which are no longer leaves
let mut inner = self.inner.inner.write();
inner.attestation_view.new_local_leaf(relay_chain_leaf, validation);
inner.attestation_view.new_local_leaf(validation);

let mut actions = Vec::new();

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

impl MessageValidationData {
// check a statement's signature.
fn check_statement(&self, relay_chain_leaf: &Hash, statement: &SignedStatement) -> Result<(), ()> {
fn check_statement(&self, statement: &SignedStatement) -> Result<(), ()> {
let sender = match self.authorities.get(statement.sender as usize) {
Some(val) => val,
None => return Err(()),
Expand All @@ -475,7 +476,7 @@ impl MessageValidationData {
&statement.statement,
&statement.signature,
sender.clone(),
relay_chain_leaf,
&self.signing_context,
);

if good {
Expand Down Expand Up @@ -826,7 +827,9 @@ mod tests {
let topic_c = attestation_topic(hash_c);

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

Expand Down Expand Up @@ -937,7 +940,9 @@ mod tests {
}
});
let encoded = statement.encode();
validator.inner.write().attestation_view.new_local_leaf(hash_a, MessageValidationData::default());
let mut validation_data = MessageValidationData::default();
validation_data.signing_context.parent_hash = hash_a;
validator.inner.write().attestation_view.new_local_leaf(validation_data);

{
let mut message_allowed = validator.message_allowed();
Expand Down
21 changes: 11 additions & 10 deletions network/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ impl NetworkServiceOps for PolkadotNetworkService {
trait GossipOps: Clone + Send + crate::legacy::GossipService + 'static {
fn new_local_leaf(
&self,
relay_parent: Hash,
validation_data: crate::legacy::gossip::MessageValidationData,
) -> crate::legacy::gossip::NewLeafActions;

Expand All @@ -177,10 +176,12 @@ trait GossipOps: Clone + Send + crate::legacy::GossipService + 'static {
impl GossipOps for RegisteredMessageValidator {
fn new_local_leaf(
&self,
relay_parent: Hash,
validation_data: crate::legacy::gossip::MessageValidationData,
) -> crate::legacy::gossip::NewLeafActions {
RegisteredMessageValidator::new_local_leaf(self, relay_parent, validation_data)
RegisteredMessageValidator::new_local_leaf(
self,
validation_data,
)
}

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

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

let signing_context = table.signing_context().clone();
let relay_parent = signing_context.parent_hash.clone();
let new_leaf_actions = self.gossip_handle.new_local_leaf(
relay_parent,
crate::legacy::gossip::MessageValidationData { authorities },
crate::legacy::gossip::MessageValidationData { authorities, signing_context },
);

new_leaf_actions.perform(&self.gossip_handle);

self.protocol_handler.consensus_instances.insert(
relay_parent,
relay_parent.clone(),
ConsensusNetworkingInstance {
statement_table: table.clone(),
relay_parent,
attestation_topic: crate::legacy::gossip::attestation_topic(relay_parent),
relay_parent: relay_parent.clone(),
attestation_topic: crate::legacy::gossip::attestation_topic(relay_parent.clone()),
_drop_signal: signal,
},
);
Expand Down Expand Up @@ -1324,7 +1325,7 @@ impl ParachainNetwork for Service {
) -> Self::BuildTableRouter {
let authorities = authorities.to_vec();
let mut sender = self.sender.clone();
let relay_parent = table.consensus_parent_hash().clone();
let relay_parent = table.signing_context().parent_hash.clone();

Box::pin(async move {
sender.send(
Expand Down
25 changes: 22 additions & 3 deletions network/src/protocol/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use polkadot_primitives::{Block, Header, BlockId};
use polkadot_primitives::parachain::{
Id as ParaId, Chain, DutyRoster, ParachainHost, ValidatorId,
Retriable, CollatorId, AbridgedCandidateReceipt,
GlobalValidationSchedule, LocalValidationData, ErasureChunk,
GlobalValidationSchedule, LocalValidationData, ErasureChunk, SigningContext,
};
use polkadot_validation::SharedTable;

Expand Down Expand Up @@ -114,7 +114,6 @@ impl crate::legacy::GossipService for MockGossip {
impl GossipOps for MockGossip {
fn new_local_leaf(
&self,
_relay_parent: Hash,
_validation_data: crate::legacy::gossip::MessageValidationData,
) -> crate::legacy::gossip::NewLeafActions {
crate::legacy::gossip::NewLeafActions::new()
Expand Down Expand Up @@ -294,6 +293,22 @@ impl ParachainHost<Block> for RuntimeApi {
) -> ClientResult<NativeOrEncoded<Option<Vec<AbridgedCandidateReceipt>>>> {
Ok(NativeOrEncoded::Native(Some(Vec::new())))
}

fn ParachainHost_signing_context_runtime_api_impl(
&self,
_at: &BlockId,
_: ExecutionContext,
_: Option<()>,
_: Vec<u8>,
) -> ClientResult<NativeOrEncoded<SigningContext>> {
Ok(NativeOrEncoded::Native(
SigningContext {
session_index: Default::default(),
parent_hash: Default::default(),
}
)
)
}
}

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

let signing_context = SigningContext {
session_index: Default::default(),
parent_hash: relay_parent,
};
let table = Arc::new(SharedTable::new(
Vec::new(),
HashMap::new(),
None,
relay_parent,
signing_context,
AvailabilityStore::new_in_memory(service.clone()),
None,
));
Expand Down
2 changes: 2 additions & 0 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ application-crypto = { package = "sp-application-crypto", git = "https://github.
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { package = "sp-std", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-staking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
runtime_primitives = { package = "sp-runtime", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
polkadot-parachain = { path = "../parachain", default-features = false }
trie = { package = "sp-trie", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand All @@ -33,6 +34,7 @@ std = [
"sp-api/std",
"sp-std/std",
"sp-version/std",
"sp-staking/std",
"runtime_primitives/std",
"serde",
"polkadot-parachain/std",
Expand Down
13 changes: 12 additions & 1 deletion primitives/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,15 @@ pub enum ValidityAttestation {
Explicit(ValidatorSignature),
}

/// A type returned by runtime with current session index and a parent hash.
#[derive(Clone, Eq, PartialEq, Default, Decode, Encode, RuntimeDebug)]
pub struct SigningContext {
/// Current session index.
pub session_index: sp_staking::SessionIndex,
/// Hash of the parent.
pub parent_hash: Hash,
montekki marked this conversation as resolved.
Show resolved Hide resolved
}

/// An attested candidate. This is submitted to the relay chain by a block author.
#[derive(Clone, PartialEq, Decode, Encode, RuntimeDebug)]
pub struct AttestedCandidate {
Expand Down Expand Up @@ -655,7 +664,7 @@ impl FeeSchedule {

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

Expand Down
Loading