generalizable data for peers in test network

This commit is contained in:
Robert Habermeier
2018-10-29 16:25:22 +01:00
parent 0302a1fc5e
commit 84925067f4
3 changed files with 29 additions and 20 deletions
+23 -15
View File
@@ -140,23 +140,26 @@ pub struct TestPacket {
pub type PeersClient = client::Client<test_client::Backend, test_client::Executor, Block>;
pub struct Peer<V: Verifier<Block>> {
pub struct Peer<V: Verifier<Block>, D> {
client: Arc<PeersClient>,
pub sync: Arc<Protocol<Block, DummySpecialization, Hash>>,
pub queue: Arc<RwLock<VecDeque<TestPacket>>>,
import_queue: Arc<SyncImportQueue<Block, V>>,
executor: Arc<DummyContextExecutor>,
/// Some custom data set up at initialization time.
pub data: D,
}
impl<V: 'static + Verifier<Block>> Peer<V> {
impl<V: 'static + Verifier<Block>, D> Peer<V, D> {
fn new(
client: Arc<PeersClient>,
sync: Arc<Protocol<Block, DummySpecialization, Hash>>,
queue: Arc<RwLock<VecDeque<TestPacket>>>,
import_queue: Arc<SyncImportQueue<Block, V>>,
data: D,
) -> Self {
let executor = Arc::new(DummyContextExecutor(sync.clone(), queue.clone()));
Peer { client, sync, queue, import_queue, executor}
Peer { client, sync, queue, import_queue, executor, data }
}
/// Called after blockchain has been populated to updated current state.
fn start(&self) {
@@ -311,6 +314,7 @@ impl TransactionPool<Hash, Block> for EmptyTransactionPool {
pub trait TestNetFactory: Sized {
type Verifier: 'static + Verifier<Block>;
type PeerData: Default;
/// These two need to be implemented!
fn from_config(config: &ProtocolConfig) -> Self;
@@ -318,16 +322,18 @@ pub trait TestNetFactory: Sized {
/// Get reference to peer.
fn peer(&self, i: usize) -> &Peer<Self::Verifier>;
fn peers(&self) -> &Vec<Arc<Peer<Self::Verifier>>>;
fn mut_peers<F: Fn(&mut Vec<Arc<Peer<Self::Verifier>>>)>(&mut self, closure: F );
fn peer(&self, i: usize) -> &Peer<Self::Verifier, Self::PeerData>;
fn peers(&self) -> &Vec<Arc<Peer<Self::Verifier, Self::PeerData>>>;
fn mut_peers<F: Fn(&mut Vec<Arc<Peer<Self::Verifier, Self::PeerData>>>)>(&mut self, closure: F );
fn started(&self) -> bool;
fn set_started(&mut self, now: bool);
/// Get custom block import handle for fresh client.
fn make_block_import(&self, client: Arc<PeersClient>) -> Arc<BlockImport<Block,Error=ClientError> + Send + Sync> {
client
/// Get custom block import handle for fresh client, along with peer data.
fn make_block_import(&self, client: Arc<PeersClient>)
-> (Arc<BlockImport<Block,Error=ClientError> + Send + Sync>, Self::PeerData)
{
(client, Default::default())
}
fn default_config() -> ProtocolConfig {
@@ -350,7 +356,7 @@ pub trait TestNetFactory: Sized {
let client = Arc::new(test_client::new());
let tx_pool = Arc::new(EmptyTransactionPool);
let verifier = self.make_verifier(client.clone(), config);
let block_import = self.make_block_import(client.clone());
let (block_import, data) = self.make_block_import(client.clone());
let import_queue = Arc::new(SyncImportQueue::new(verifier, block_import));
let specialization = DummySpecialization {
@@ -369,7 +375,8 @@ pub trait TestNetFactory: Sized {
client,
Arc::new(sync),
Arc::new(RwLock::new(VecDeque::new())),
import_queue
import_queue,
data,
));
self.mut_peers(|peers| {
@@ -479,12 +486,13 @@ pub trait TestNetFactory: Sized {
}
pub struct TestNet {
peers: Vec<Arc<Peer<PassThroughVerifier>>>,
peers: Vec<Arc<Peer<PassThroughVerifier, ()>>>,
started: bool
}
impl TestNetFactory for TestNet {
type Verifier = PassThroughVerifier;
type PeerData = ();
/// Create new test network with peers and given config.
fn from_config(_config: &ProtocolConfig) -> Self {
@@ -500,15 +508,15 @@ impl TestNetFactory for TestNet {
Arc::new(PassThroughVerifier(false))
}
fn peer(&self, i: usize) -> &Peer<Self::Verifier> {
fn peer(&self, i: usize) -> &Peer<Self::Verifier, ()> {
&self.peers[i]
}
fn peers(&self) -> &Vec<Arc<Peer<Self::Verifier>>> {
fn peers(&self) -> &Vec<Arc<Peer<Self::Verifier, ()>>> {
&self.peers
}
fn mut_peers<F: Fn(&mut Vec<Arc<Peer<Self::Verifier>>>)>(&mut self, closure: F ) {
fn mut_peers<F: Fn(&mut Vec<Arc<Peer<Self::Verifier, ()>>>)>(&mut self, closure: F ) {
closure(&mut self.peers);
}