Propagate RPC through network service.

- Basic network message handler threading
- Correct references
This commit is contained in:
Age Manning
2019-03-17 21:49:56 +11:00
parent 7370306366
commit 9803ab30f2
11 changed files with 68 additions and 60 deletions

View File

@@ -1,4 +1,4 @@
use crate::rpc::{RPCMethod, RPCRequest, RPCResponse, Rpc, RpcEvent};
use crate::rpc::{Rpc, RpcEvent};
use futures::prelude::*;
use libp2p::{
core::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess},
@@ -42,22 +42,7 @@ impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<RpcEvent>
for Behaviour<TSubstream>
{
fn inject_event(&mut self, event: RpcEvent) {
match event {
RpcEvent::Request {
id,
method_id,
body,
} => self.events.push(BehaviourEvent::RPCRequest {
id,
method: RPCMethod::from(method_id),
body,
}),
RpcEvent::Response {
id,
method_id,
result,
} => self.events.push(BehaviourEvent::RPCResponse { id, result }),
}
self.events.push(BehaviourEvent::RPC(event));
}
}
@@ -95,15 +80,7 @@ impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
/// The types of events than can be obtained from polling the behaviour.
pub enum BehaviourEvent {
RPCRequest {
id: u64,
method: RPCMethod,
body: RPCRequest,
},
RPCResponse {
id: u64,
result: RPCResponse,
},
RPC(RpcEvent),
// TODO: This is a stub at the moment
Message(String),
}

View File

@@ -5,7 +5,7 @@
pub mod behaviour;
pub mod error;
mod network_config;
mod rpc;
pub mod rpc;
mod service;
pub use libp2p::{
@@ -13,6 +13,8 @@ pub use libp2p::{
PeerId,
};
pub use network_config::NetworkConfig;
pub use rpc::HelloMessage;
pub use rpc::RpcEvent;
pub use service::Libp2pEvent;
pub use service::Service;
pub use types::multiaddr;

View File

@@ -19,17 +19,17 @@ impl From<u16> for RPCMethod {
#[derive(Debug, Clone)]
pub enum RPCRequest {
Hello(HelloBody),
Hello(HelloMessage),
}
#[derive(Debug, Clone)]
pub enum RPCResponse {
Hello(HelloBody),
Hello(HelloMessage),
}
// request/response structs for RPC methods
#[derive(Encode, Decode, Clone, Debug)]
pub struct HelloBody {
pub struct HelloMessage {
pub network_id: u8,
pub latest_finalized_root: Hash256,
pub latest_finalized_epoch: Epoch,

View File

@@ -11,7 +11,7 @@ use libp2p::core::swarm::{
ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
};
use libp2p::{Multiaddr, PeerId};
pub use methods::{RPCMethod, RPCRequest, RPCResponse};
pub use methods::{HelloMessage, RPCMethod, RPCRequest, RPCResponse};
pub use protocol::{RPCProtocol, RpcEvent};
use std::marker::PhantomData;
use tokio::io::{AsyncRead, AsyncWrite};

View File

@@ -1,4 +1,4 @@
use super::methods::{HelloBody, RPCMethod, RPCRequest, RPCResponse};
use super::methods::{HelloMessage, RPCMethod, RPCRequest, RPCResponse};
use libp2p::core::{upgrade, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use ssz::{ssz_encode, Decodable, Encodable, SszStream};
use std::io;
@@ -78,7 +78,7 @@ fn decode(packet: Vec<u8>) -> Result<RpcEvent, DecodeError> {
if request {
let body = match RPCMethod::from(method_id) {
RPCMethod::Hello => {
let (hello_body, _index) = HelloBody::ssz_decode(&packet, index)?;
let (hello_body, _index) = HelloMessage::ssz_decode(&packet, index)?;
RPCRequest::Hello(hello_body)
}
RPCMethod::Unknown => return Err(DecodeError::UnknownRPCMethod),
@@ -94,7 +94,7 @@ fn decode(packet: Vec<u8>) -> Result<RpcEvent, DecodeError> {
else {
let result = match RPCMethod::from(method_id) {
RPCMethod::Hello => {
let (body, _index) = HelloBody::ssz_decode(&packet, index)?;
let (body, _index) = HelloMessage::ssz_decode(&packet, index)?;
RPCResponse::Hello(body)
}
RPCMethod::Unknown => return Err(DecodeError::UnknownRPCMethod),

View File

@@ -1,6 +1,7 @@
use crate::behaviour::{Behaviour, BehaviourEvent};
use crate::error;
use crate::multiaddr::Protocol;
use crate::rpc::RpcEvent;
use crate::NetworkConfig;
use futures::prelude::*;
use futures::Stream;
@@ -104,8 +105,9 @@ impl Stream for Service {
debug!(self.log, "Message received: {}", m);
return Ok(Async::Ready(Some(Libp2pEvent::Message(m))));
}
// TODO: Fill with all behaviour events
_ => break,
Ok(Async::Ready(Some(BehaviourEvent::RPC(event)))) => {
return Ok(Async::Ready(Some(Libp2pEvent::RPC(event))));
}
Ok(Async::Ready(None)) => unreachable!("Swarm stream shouldn't end"),
Ok(Async::NotReady) => break,
_ => break,
@@ -152,5 +154,7 @@ fn build_transport(
/// Events that can be obtained from polling the Libp2p Service.
pub enum Libp2pEvent {
// We have received an RPC event on the swarm
RPC(RpcEvent),
Message(String),
}