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

Commit

Permalink
Make tests compile.
Browse files Browse the repository at this point in the history
  • Loading branch information
twittner committed Sep 10, 2019
1 parent 877419c commit dabb6d8
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 26 deletions.
3 changes: 2 additions & 1 deletion core/client/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub(crate) struct ImportSummary<Block: BlockT> {
pub(crate) is_new_best: bool,
pub(crate) storage_changes: Option<(StorageCollection, ChildStorageCollection)>,
pub(crate) retracted: Vec<Block::Hash>,
pub(crate) associated_data: Vec<u8>,
}

/// Import operation wrapper
Expand Down Expand Up @@ -144,7 +145,7 @@ pub trait Finalizer<Block: BlockT, H: Hasher<Out=Block::Hash>, B: Backend<Block,
notify: bool,
) -> error::Result<()>;


/// Finalize a block. This will implicitly finalize all blocks up to it and
/// fire finality notifications.
///
Expand Down
9 changes: 3 additions & 6 deletions core/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
is_new_best,
storage_changes,
retracted,
Vec::new(), // TODO
associated_data: Vec::new(), // TODO
})
}

Expand Down Expand Up @@ -1173,10 +1173,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
Ok(())
}

fn notify_imported(
&self,
notify_import: ImportSummary<Block>,
) -> error::Result<()> {
fn notify_imported(&self, notify_import: ImportSummary<Block>) -> error::Result<()> {
if let Some(storage_changes) = notify_import.storage_changes {
// TODO [ToDr] How to handle re-orgs? Should we re-emit all storage changes?
self.storage_notifications.lock()
Expand All @@ -1193,7 +1190,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
header: notify_import.header,
is_new_best: notify_import.is_new_best,
retracted: notify_import.retracted,
associated_data: associated,
associated_data: notify_import.associated_data,
};

self.import_notification_sinks.lock()
Expand Down
11 changes: 6 additions & 5 deletions core/consensus/common/src/block_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Block announcement validation.
use crate::BlockStatus;
use sr_primitives::{generic::BlockId, traits::{Block, Header}};
use sr_primitives::{generic::BlockId, traits::Block};
use std::{error::Error, sync::Arc};

/// A type which provides access to chain information.
Expand All @@ -39,8 +41,6 @@ pub enum Validation {
Success,
/// Invalid block announcement.
Failure,
/// At this moment, validation is inconclusive.
Unknown
}

/// Type which checks incoming block announcements.
Expand All @@ -49,6 +49,7 @@ pub trait BlockAnnounceValidator<B: Block> {
fn validate(&mut self, header: &B::Header, data: &[u8]) -> Result<Validation, Box<dyn Error + Send>>;
}

/// Default implementation of `BlockAnnounceValidator`.
#[derive(Debug)]
pub struct DefaultBlockAnnounceValidator<C> {
chain: C
Expand All @@ -61,7 +62,7 @@ impl<C> DefaultBlockAnnounceValidator<C> {
}

impl<B: Block, C: Chain<B>> BlockAnnounceValidator<B> for DefaultBlockAnnounceValidator<C> {
fn validate(&mut self, header: &B::Header, data: &[u8]) -> Result<Validation, Box<dyn Error + Send>> {
Ok(Validation::Success) // TODO: proper validation
fn validate(&mut self, _h: &B::Header, _d: &[u8]) -> Result<Validation, Box<dyn Error + Send>> {
Ok(Validation::Success)
}
}
4 changes: 2 additions & 2 deletions core/finality-grandpa/src/communication/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ impl super::Network<Block> for TestNetwork {
}

/// Inform peers that a block with given hash should be downloaded.
fn announce(&self, block: Hash) {
let _ = self.sender.unbounded_send(Event::Announce(block));
fn announce(&self, block: Hash, associated_data: Vec<u8>) {
let _ = self.sender.unbounded_send(Event::Announce(block)); // TODO
}
}

Expand Down
1 change: 1 addition & 0 deletions core/finality-grandpa/src/until_imported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ mod tests {
header,
is_new_best: false,
retracted: vec![],
associated_data: vec![],
}).unwrap();
}
}
Expand Down
5 changes: 2 additions & 3 deletions core/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,10 +1073,9 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
match self.block_announce_validator.validate(&header, &announce.data) {
Ok(Validation::Success) => (),
Ok(Validation::Failure) => return CustomMessageOutcome::None,
Ok(Validation::Unknown) => (), // TODO: Is this correct?
Err(e) => {
error!(target: "sync", "block validation failed: {}", e);
return CustomMessageOutcome::None // TODO: not sure, `sync` maps this to "unknown block"
warn!(target: "sync", "block validation failed: {}", e);
return CustomMessageOutcome::None
}
}

Expand Down
13 changes: 8 additions & 5 deletions core/network/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use client::error::Result as ClientResult;
use client::block_builder::BlockBuilder;
use client::backend::{AuxStore, Backend, Finalizer};
use crate::config::Roles;
use consensus::block_validation::DefaultBlockAnnounceValidator;
use consensus::import_queue::BasicQueue;
use consensus::import_queue::{
BoxBlockImport, BoxJustificationImport, Verifier, BoxFinalityProofImport,
Expand Down Expand Up @@ -245,8 +246,8 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
}

/// Announces an important block on the network.
pub fn announce_block(&self, hash: <Block as BlockT>::Hash) {
self.network.service().announce_block(hash);
pub fn announce_block(&self, hash: <Block as BlockT>::Hash, data: Vec<u8>) {
self.network.service().announce_block(hash, data);
}

/// Add blocks to the peer -- edit the block before adding
Expand Down Expand Up @@ -293,11 +294,11 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
Default::default()
};
self.block_import.import_block(import_block, cache).expect("block_import failed");
self.network.on_block_imported(hash, header);
self.network.on_block_imported(hash, header, Vec::new()); // TODO
at = hash;
}

self.network.service().announce_block(at.clone());
self.network.service().announce_block(at.clone(), Vec::new()); // TODO
at
}

Expand Down Expand Up @@ -530,6 +531,7 @@ pub trait TestNetFactory: Sized {
protocol_id: ProtocolId::from(&b"test-protocol-name"[..]),
import_queue,
specialization: self::SpecializationFactory::create(),
block_announce_validator: Box::new(DefaultBlockAnnounceValidator::new(client.clone()))
}).unwrap();

self.mut_peers(|peers| {
Expand Down Expand Up @@ -593,6 +595,7 @@ pub trait TestNetFactory: Sized {
protocol_id: ProtocolId::from(&b"test-protocol-name"[..]),
import_queue,
specialization: self::SpecializationFactory::create(),
block_announce_validator: Box::new(DefaultBlockAnnounceValidator::new(client.clone()))
}).unwrap();

self.mut_peers(|peers| {
Expand Down Expand Up @@ -655,7 +658,7 @@ pub trait TestNetFactory: Sized {

// We poll `imported_blocks_stream`.
while let Ok(Async::Ready(Some(notification))) = peer.imported_blocks_stream.poll() {
peer.network.on_block_imported(notification.hash, notification.header);
peer.network.on_block_imported(notification.hash, notification.header, notification.associated_data);
}

// We poll `finality_notification_stream`, but we only take the last event.
Expand Down
4 changes: 2 additions & 2 deletions core/network/src/test/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ fn can_sync_small_non_best_forks() {
assert!(net.peer(0).client().header(&BlockId::Hash(small_hash)).unwrap().is_some());
assert!(!net.peer(1).client().header(&BlockId::Hash(small_hash)).unwrap().is_some());

net.peer(0).announce_block(small_hash);
net.peer(0).announce_block(small_hash, Vec::new()); // TODO

// after announcing, peer 1 downloads the block.

Expand Down Expand Up @@ -499,7 +499,7 @@ fn light_peer_imports_header_from_announce() {
let mut runtime = current_thread::Runtime::new().unwrap();

fn import_with_announce(net: &mut TestNet, runtime: &mut current_thread::Runtime, hash: H256) {
net.peer(0).announce_block(hash);
net.peer(0).announce_block(hash, Vec::new()); // TODO

runtime.block_on(futures::future::poll_fn::<(), (), _>(|| {
net.poll();
Expand Down
6 changes: 4 additions & 2 deletions core/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use futures::sync::mpsc;
use parking_lot::Mutex;

use client::{runtime_api::BlockT, Client};
use consensus_common::block_validation::DefaultBlockAnnounceValidator;
use exit_future::Signal;
use futures::prelude::*;
use futures03::stream::{StreamExt as _, TryStreamExt as _};
Expand Down Expand Up @@ -189,6 +188,9 @@ macro_rules! new_impl {
network::config::ProtocolId::from(protocol_id_full)
};

let block_announce_validator =
Box::new(consensus_common::block_validation::DefaultBlockAnnounceValidator::new(client.clone()));

let network_params = network::config::Params {
roles: $config.roles,
network_config: $config.network.clone(),
Expand All @@ -200,7 +202,7 @@ macro_rules! new_impl {
import_queue,
protocol_id,
specialization: network_protocol,
block_announce_validator: Box::new(DefaultBlockAnnounceValidator::new(client.clone()))
block_announce_validator,
};

let has_bootnodes = !network_params.network_config.boot_nodes.is_empty();
Expand Down

0 comments on commit dabb6d8

Please sign in to comment.