Commit 987da512 authored by asynchronous rob's avatar asynchronous rob Committed by Gavin Wood
Browse files

update substrate reference (#249)

* update substrate reference

* bump wasm
parent 3832fc23
Pipeline #37093 passed with stages
in 13 minutes and 42 seconds
This diff is collapsed.
...@@ -60,7 +60,7 @@ use polkadot_primitives::parachain::{ ...@@ -60,7 +60,7 @@ use polkadot_primitives::parachain::{
Id as ParaId, BlockData, CollatorId, CandidateReceipt, Collation, PoVBlock, Id as ParaId, BlockData, CollatorId, CandidateReceipt, Collation, PoVBlock,
ConsolidatedIngressRoots, ConsolidatedIngressRoots,
}; };
use substrate_network::{PeerId, RequestId, Context, Severity}; use substrate_network::{PeerId, RequestId, Context};
use substrate_network::{message, generic_message}; use substrate_network::{message, generic_message};
use substrate_network::specialization::NetworkSpecialization as Specialization; use substrate_network::specialization::NetworkSpecialization as Specialization;
use substrate_network::StatusMessage as GenericFullStatus; use substrate_network::StatusMessage as GenericFullStatus;
...@@ -73,6 +73,15 @@ use std::collections::{HashMap, HashSet}; ...@@ -73,6 +73,15 @@ use std::collections::{HashMap, HashSet};
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
mod cost {
pub(super) const UNEXPECTED_MESSAGE: i32 = -200;
pub(super) const INVALID_FORMAT: i32 = -200;
pub(super) const UNKNOWN_PEER: i32 = -50;
pub(super) const COLLATOR_ALREADY_KNOWN: i32 = -100;
pub(super) const BAD_COLLATION: i32 = -1000;
}
type FullStatus = GenericFullStatus<Block>; type FullStatus = GenericFullStatus<Block>;
/// Specialization of the network service for the polkadot protocol. /// Specialization of the network service for the polkadot protocol.
...@@ -352,7 +361,7 @@ impl PolkadotProtocol { ...@@ -352,7 +361,7 @@ impl PolkadotProtocol {
Message::PovBlock(req_id, data) => self.on_pov_block(ctx, who, req_id, data), Message::PovBlock(req_id, data) => self.on_pov_block(ctx, who, req_id, data),
Message::BlockData(_req_id, _data) => { Message::BlockData(_req_id, _data) => {
// current block data is never requested bare by the node. // current block data is never requested bare by the node.
ctx.report_peer(who, Severity::Bad("Peer sent un-requested block data".to_string())); ctx.report_peer(who, cost::UNEXPECTED_MESSAGE);
} }
Message::Collation(relay_parent, collation) => self.on_collation(ctx, who, relay_parent, collation), Message::Collation(relay_parent, collation) => self.on_collation(ctx, who, relay_parent, collation),
Message::CollatorRole(role) => self.on_new_role(ctx, who, role), Message::CollatorRole(role) => self.on_new_role(ctx, who, role),
...@@ -370,7 +379,7 @@ impl PolkadotProtocol { ...@@ -370,7 +379,7 @@ impl PolkadotProtocol {
}; };
if !info.claimed_validator { if !info.claimed_validator {
ctx.report_peer(who, Severity::Bad("Session key broadcasted without setting authority role".to_string())); ctx.report_peer(who, cost::UNEXPECTED_MESSAGE);
return; return;
} }
...@@ -419,7 +428,7 @@ impl PolkadotProtocol { ...@@ -419,7 +428,7 @@ impl PolkadotProtocol {
self.pending.push(req); self.pending.push(req);
self.dispatch_pending_requests(ctx); self.dispatch_pending_requests(ctx);
} }
None => ctx.report_peer(who, Severity::Bad("Unexpected block data response".to_string())), None => ctx.report_peer(who, cost::UNEXPECTED_MESSAGE),
} }
} }
...@@ -436,10 +445,7 @@ impl PolkadotProtocol { ...@@ -436,10 +445,7 @@ impl PolkadotProtocol {
debug!(target: "p_net", "New collator role {:?} from {}", role, who); debug!(target: "p_net", "New collator role {:?} from {}", role, who);
if info.validator_keys.as_slice().is_empty() { if info.validator_keys.as_slice().is_empty() {
ctx.report_peer( ctx.report_peer(who, cost::UNEXPECTED_MESSAGE);
who,
Severity::Bad("Sent collator role without registering first as validator".to_string()),
);
} else { } else {
// update role for all saved session keys for this validator. // update role for all saved session keys for this validator.
let local_collations = &mut self.local_collations; let local_collations = &mut self.local_collations;
...@@ -484,7 +490,7 @@ impl Specialization<Block> for PolkadotProtocol { ...@@ -484,7 +490,7 @@ impl Specialization<Block> for PolkadotProtocol {
if let Some((ref acc_id, ref para_id)) = local_status.collating_for { if let Some((ref acc_id, ref para_id)) = local_status.collating_for {
if self.collator_peer(acc_id.clone()).is_some() { if self.collator_peer(acc_id.clone()).is_some() {
ctx.report_peer(who, Severity::Useless("Unknown Polkadot-specific reason".to_string())); ctx.report_peer(who, cost::COLLATOR_ALREADY_KNOWN);
return return
} }
...@@ -563,7 +569,7 @@ impl Specialization<Block> for PolkadotProtocol { ...@@ -563,7 +569,7 @@ impl Specialization<Block> for PolkadotProtocol {
Some(msg) => self.on_polkadot_message(ctx, who, msg), Some(msg) => self.on_polkadot_message(ctx, who, msg),
None => { None => {
trace!(target: "p_net", "Bad message from {}", who); trace!(target: "p_net", "Bad message from {}", who);
ctx.report_peer(who, Severity::Bad("Invalid polkadot protocol message format".to_string())); ctx.report_peer(who, cost::INVALID_FORMAT);
*message = Some(generic_message::Message::ChainSpecific(raw)); *message = Some(generic_message::Message::ChainSpecific(raw));
} }
} }
...@@ -607,16 +613,16 @@ impl PolkadotProtocol { ...@@ -607,16 +613,16 @@ impl PolkadotProtocol {
let collated_acc = collation.receipt.collator.clone(); let collated_acc = collation.receipt.collator.clone();
match self.peers.get(&from) { match self.peers.get(&from) {
None => ctx.report_peer(from, Severity::Useless("Unknown Polkadot specific reason".to_string())), None => ctx.report_peer(from, cost::UNKNOWN_PEER),
Some(peer_info) => match peer_info.collating_for { Some(peer_info) => match peer_info.collating_for {
None => ctx.report_peer(from, Severity::Bad("Sent collation without registering collator intent".to_string())), None => ctx.report_peer(from, cost::UNEXPECTED_MESSAGE),
Some((ref acc_id, ref para_id)) => { Some((ref acc_id, ref para_id)) => {
let structurally_valid = para_id == &collation_para && acc_id == &collated_acc; let structurally_valid = para_id == &collation_para && acc_id == &collated_acc;
if structurally_valid && collation.receipt.check_signature().is_ok() { if structurally_valid && collation.receipt.check_signature().is_ok() {
debug!(target: "p_net", "Received collation for parachain {:?} from peer {}", para_id, from); debug!(target: "p_net", "Received collation for parachain {:?} from peer {}", para_id, from);
self.collators.on_collation(acc_id.clone(), relay_parent, collation) self.collators.on_collation(acc_id.clone(), relay_parent, collation)
} else { } else {
ctx.report_peer(from, Severity::Bad("Sent malformed collation".to_string())) ctx.report_peer(from, cost::INVALID_FORMAT)
}; };
} }
}, },
...@@ -647,7 +653,7 @@ impl PolkadotProtocol { ...@@ -647,7 +653,7 @@ impl PolkadotProtocol {
// disconnect a collator by account-id. // disconnect a collator by account-id.
fn disconnect_bad_collator(&mut self, ctx: &mut Context<Block>, collator_id: CollatorId) { fn disconnect_bad_collator(&mut self, ctx: &mut Context<Block>, collator_id: CollatorId) {
if let Some((who, _)) = self.collator_peer(collator_id) { if let Some((who, _)) = self.collator_peer(collator_id) {
ctx.report_peer(who, Severity::Bad("Consensus layer determined the given collator misbehaved".to_string())) ctx.report_peer(who, cost::BAD_COLLATION)
} }
} }
} }
......
...@@ -28,7 +28,7 @@ use polkadot_primitives::parachain::{ ...@@ -28,7 +28,7 @@ use polkadot_primitives::parachain::{
use substrate_primitives::crypto::UncheckedInto; use substrate_primitives::crypto::UncheckedInto;
use codec::Encode; use codec::Encode;
use substrate_network::{ use substrate_network::{
Severity, PeerId, PeerInfo, ClientHandle, Context, config::Roles, PeerId, PeerInfo, ClientHandle, Context, config::Roles,
message::{BlockRequest, generic::ConsensusMessage}, message::{BlockRequest, generic::ConsensusMessage},
specialization::NetworkSpecialization, generic_message::Message as GenericMessage specialization::NetworkSpecialization, generic_message::Message as GenericMessage
}; };
...@@ -49,10 +49,11 @@ impl Context<Block> for TestContext { ...@@ -49,10 +49,11 @@ impl Context<Block> for TestContext {
unimplemented!() unimplemented!()
} }
fn report_peer(&mut self, peer: PeerId, reason: Severity) { fn report_peer(&mut self, peer: PeerId, reputation: i32) {
match reason { match reputation {
Severity::Bad(_) => self.disabled.push(peer), i if i < -100 => self.disabled.push(peer),
_ => self.disconnected.push(peer), i if i < 0 => self.disconnected.push(peer),
_ => {}
} }
} }
...@@ -68,9 +69,11 @@ impl Context<Block> for TestContext { ...@@ -68,9 +69,11 @@ impl Context<Block> for TestContext {
unimplemented!() unimplemented!()
} }
fn send_chain_specific(&mut self, who: PeerId, message: Vec<u8>){ fn send_chain_specific(&mut self, who: PeerId, message: Vec<u8>) {
self.messages.push((who, message)) self.messages.push((who, message))
} }
fn disconnect_peer(&mut self, _who: PeerId) { }
} }
impl TestContext { impl TestContext {
......
...@@ -47,15 +47,29 @@ decl_module! { ...@@ -47,15 +47,29 @@ decl_module! {
if shuffle_period == 0 { return } if shuffle_period == 0 { return }
if block_number.as_() % shuffle_period == 0 { if block_number.as_() % shuffle_period == 0 {
let mut seed = system::Module::<T>::random_seed().as_ref().to_vec();
seed.extend(b"grandpa_shuffling");
let mut seed = BlakeTwo256::hash(&seed);
let mut voters = grandpa::Module::<T>::grandpa_authorities(); let mut voters = grandpa::Module::<T>::grandpa_authorities();
let voter_count = voters.len(); let voter_count = voters.len();
if voter_count == 0 { return } if voter_count == 0 { return }
let mut seed = {
let phrase = b"grandpa_shuffling";
let seed = system::Module::<T>::random(&phrase[..]);
let seed_len = seed.as_ref().len();
let needed_bytes = voter_count * 4;
// hash only the needed bits of the random seed.
// if earlier bits are influencable, they will not factor into
// the seed used here.
let seed_off = if needed_bytes >= seed_len {
0
} else {
seed_len - needed_bytes
};
BlakeTwo256::hash(&seed.as_ref()[seed_off..])
};
for i in 0..(voter_count - 1) { for i in 0..(voter_count - 1) {
// 4 bytes of entropy used per cycle, 32 bytes entropy per hash // 4 bytes of entropy used per cycle, 32 bytes entropy per hash
let offset = (i * 4 % 32) as usize; let offset = (i * 4 % 32) as usize;
......
...@@ -208,9 +208,24 @@ impl<T: Trait> Module<T> { ...@@ -208,9 +208,24 @@ impl<T: Trait> Module<T> {
_ => Chain::Relay, _ => Chain::Relay,
}).collect::<Vec<_>>(); }).collect::<Vec<_>>();
let mut random_seed = system::Module::<T>::random_seed().as_ref().to_vec();
random_seed.extend(b"validator_role_pairs"); let mut seed = {
let mut seed = BlakeTwo256::hash(&random_seed); let phrase = b"validator_role_pairs";
let seed = system::Module::<T>::random(&phrase[..]);
let seed_len = seed.as_ref().len();
let needed_bytes = validator_count * 4;
// hash only the needed bits of the random seed.
// if earlier bits are influencable, they will not factor into
// the seed used here.
let seed_off = if needed_bytes >= seed_len {
0
} else {
seed_len - needed_bytes
};
BlakeTwo256::hash(&seed.as_ref()[seed_off..])
};
// shuffle // shuffle
for i in 0..(validator_count - 1) { for i in 0..(validator_count - 1) {
...@@ -504,6 +519,7 @@ mod tests { ...@@ -504,6 +519,7 @@ mod tests {
impl Trait for Test {} impl Trait for Test {}
type Parachains = Module<Test>; type Parachains = Module<Test>;
type System = system::Module<Test>;
fn new_test_ext(parachains: Vec<(ParaId, Vec<u8>, Vec<u8>)>) -> TestExternalities<Blake2Hasher> { fn new_test_ext(parachains: Vec<(ParaId, Vec<u8>, Vec<u8>)>) -> TestExternalities<Blake2Hasher> {
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap().0; let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap().0;
...@@ -653,17 +669,16 @@ mod tests { ...@@ -653,17 +669,16 @@ mod tests {
assert_eq!(duty_roster.validator_duty.iter().filter(|&&j| j == Chain::Relay).count(), 2); assert_eq!(duty_roster.validator_duty.iter().filter(|&&j| j == Chain::Relay).count(), 2);
}; };
system::Module::<Test>::set_random_seed([0u8; 32].into());
let duty_roster_0 = Parachains::calculate_duty_roster(); let duty_roster_0 = Parachains::calculate_duty_roster();
check_roster(&duty_roster_0); check_roster(&duty_roster_0);
system::Module::<Test>::set_random_seed([1u8; 32].into()); System::initialize(&1, &H256::from([1; 32]), &Default::default());
let duty_roster_1 = Parachains::calculate_duty_roster(); let duty_roster_1 = Parachains::calculate_duty_roster();
check_roster(&duty_roster_1); check_roster(&duty_roster_1);
assert!(duty_roster_0 != duty_roster_1); assert!(duty_roster_0 != duty_roster_1);
system::Module::<Test>::set_random_seed([2u8; 32].into()); System::initialize(&2, &H256::from([2; 32]), &Default::default());
let duty_roster_2 = Parachains::calculate_duty_roster(); let duty_roster_2 = Parachains::calculate_duty_roster();
check_roster(&duty_roster_2); check_roster(&duty_roster_2);
assert!(duty_roster_0 != duty_roster_2); assert!(duty_roster_0 != duty_roster_2);
...@@ -679,7 +694,6 @@ mod tests { ...@@ -679,7 +694,6 @@ mod tests {
]; ];
with_externalities(&mut new_test_ext(parachains), || { with_externalities(&mut new_test_ext(parachains), || {
system::Module::<Test>::set_random_seed([0u8; 32].into());
let candidate = AttestedCandidate { let candidate = AttestedCandidate {
validity_votes: vec![], validity_votes: vec![],
candidate: CandidateReceipt { candidate: CandidateReceipt {
...@@ -707,7 +721,6 @@ mod tests { ...@@ -707,7 +721,6 @@ mod tests {
]; ];
with_externalities(&mut new_test_ext(parachains), || { with_externalities(&mut new_test_ext(parachains), || {
system::Module::<Test>::set_random_seed([0u8; 32].into());
let mut candidate_a = AttestedCandidate { let mut candidate_a = AttestedCandidate {
validity_votes: vec![], validity_votes: vec![],
candidate: CandidateReceipt { candidate: CandidateReceipt {
...@@ -759,7 +772,6 @@ mod tests { ...@@ -759,7 +772,6 @@ mod tests {
]; ];
with_externalities(&mut new_test_ext(parachains), || { with_externalities(&mut new_test_ext(parachains), || {
system::Module::<Test>::set_random_seed([0u8; 32].into());
let mut candidate = AttestedCandidate { let mut candidate = AttestedCandidate {
validity_votes: vec![], validity_votes: vec![],
candidate: CandidateReceipt { candidate: CandidateReceipt {
...@@ -795,7 +807,6 @@ mod tests { ...@@ -795,7 +807,6 @@ mod tests {
]; ];
with_externalities(&mut new_test_ext(parachains), || { with_externalities(&mut new_test_ext(parachains), || {
system::Module::<Test>::set_random_seed([0u8; 32].into());
let from_a = vec![(1.into(), [1; 32].into())]; let from_a = vec![(1.into(), [1; 32].into())];
let mut candidate_a = AttestedCandidate { let mut candidate_a = AttestedCandidate {
validity_votes: vec![], validity_votes: vec![],
...@@ -865,7 +876,6 @@ mod tests { ...@@ -865,7 +876,6 @@ mod tests {
]; ];
with_externalities(&mut new_test_ext(parachains), || { with_externalities(&mut new_test_ext(parachains), || {
system::Module::<Test>::set_random_seed([0u8; 32].into());
// parachain 99 does not exist // parachain 99 does not exist
let non_existent = vec![(99.into(), [1; 32].into())]; let non_existent = vec![(99.into(), [1; 32].into())];
let mut candidate = new_candidate_with_egress_roots(non_existent); let mut candidate = new_candidate_with_egress_roots(non_existent);
...@@ -890,7 +900,6 @@ mod tests { ...@@ -890,7 +900,6 @@ mod tests {
]; ];
with_externalities(&mut new_test_ext(parachains), || { with_externalities(&mut new_test_ext(parachains), || {
system::Module::<Test>::set_random_seed([0u8; 32].into());
// parachain 0 is self // parachain 0 is self
let to_self = vec![(0.into(), [1; 32].into())]; let to_self = vec![(0.into(), [1; 32].into())];
let mut candidate = new_candidate_with_egress_roots(to_self); let mut candidate = new_candidate_with_egress_roots(to_self);
...@@ -915,7 +924,6 @@ mod tests { ...@@ -915,7 +924,6 @@ mod tests {
]; ];
with_externalities(&mut new_test_ext(parachains), || { with_externalities(&mut new_test_ext(parachains), || {
system::Module::<Test>::set_random_seed([0u8; 32].into());
// parachain 0 is self // parachain 0 is self
let out_of_order = vec![(1.into(), [1; 32].into()), ((0.into(), [1; 32].into()))]; let out_of_order = vec![(1.into(), [1; 32].into()), ((0.into(), [1; 32].into()))];
let mut candidate = new_candidate_with_egress_roots(out_of_order); let mut candidate = new_candidate_with_egress_roots(out_of_order);
...@@ -940,7 +948,6 @@ mod tests { ...@@ -940,7 +948,6 @@ mod tests {
]; ];
with_externalities(&mut new_test_ext(parachains), || { with_externalities(&mut new_test_ext(parachains), || {
system::Module::<Test>::set_random_seed([0u8; 32].into());
// parachain 0 is self // parachain 0 is self
let contains_empty_trie_root = vec![(1.into(), [1; 32].into()), ((2.into(), EMPTY_TRIE_ROOT.into()))]; let contains_empty_trie_root = vec![(1.into(), [1; 32].into()), ((2.into(), EMPTY_TRIE_ROOT.into()))];
let mut candidate = new_candidate_with_egress_roots(contains_empty_trie_root); let mut candidate = new_candidate_with_egress_roots(contains_empty_trie_root);
......
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment