client/finality-grandpa: Add regression test for observer polling network (was #4778) (#4795)

* client/finality-grandpa: Add regression test observer polling network

Ensure `Future` implementation of `ObserverWork` is polling its
`NetworkBridge`. Regression test for bug introduced in d9837d7dd and
fixed in 504b4e89e.

When polled, `NetworkBridge` forwards reputation change requests from
the `GossipValidator` to the underlying `dyn Network`. This test
triggers a reputation change by calling `GossipValidator::validate` with
an invalid gossip message. After polling the `ObserverWork` which should
poll the `NetworkBridge`, the reputation change should be forwarded to
the test network.

* Nits

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Gavin Wood
2020-01-31 19:12:03 +00:00
committed by GitHub
parent 111207afe8
commit adde404e41
5 changed files with 94 additions and 7 deletions
@@ -62,7 +62,7 @@ pub mod gossip;
mod periodic;
#[cfg(test)]
mod tests;
pub(crate) mod tests;
pub use sp_finality_grandpa::GRANDPA_ENGINE_ID;
@@ -31,7 +31,8 @@ use sp_finality_grandpa::{AuthorityList, GRANDPA_ENGINE_ID};
use super::gossip::{self, GossipValidator};
use super::{AuthorityId, VoterSet, Round, SetId};
enum Event {
#[derive(Debug)]
pub(crate) enum Event {
EventStream(mpsc::UnboundedSender<NetworkEvent>),
WriteNotification(sc_network::PeerId, Vec<u8>),
Report(sc_network::PeerId, sc_network::ReputationChange),
@@ -39,7 +40,7 @@ enum Event {
}
#[derive(Clone)]
struct TestNetwork {
pub(crate) struct TestNetwork {
sender: mpsc::UnboundedSender<Event>,
}
@@ -101,10 +102,10 @@ impl sc_network_gossip::ValidatorContext<Block> for TestNetwork {
fn send_topic(&mut self, _: &sc_network::PeerId, _: Hash, _: bool) { }
}
struct Tester {
net_handle: super::NetworkBridge<Block, TestNetwork>,
pub(crate) struct Tester {
pub(crate) net_handle: super::NetworkBridge<Block, TestNetwork>,
gossip_validator: Arc<GossipValidator<Block>>,
events: mpsc::UnboundedReceiver<Event>,
pub(crate) events: mpsc::UnboundedReceiver<Event>,
}
impl Tester {
@@ -122,6 +123,14 @@ impl Tester {
}
})
}
pub(crate) fn trigger_gossip_validator_reputation_change(&self, p: &PeerId) {
self.gossip_validator.validate(
&mut crate::communication::tests::NoopContext,
p,
&vec![1, 2, 3],
);
}
}
// some random config (not really needed)
@@ -156,7 +165,7 @@ fn voter_set_state() -> SharedVoterSetState<Block> {
}
// needs to run in a tokio runtime.
fn make_test_network(executor: &impl futures::task::Spawn) -> (
pub(crate) fn make_test_network(executor: &impl futures::task::Spawn) -> (
impl Future<Output = Tester>,
TestNetwork,
) {