Add RPC protocol to lh network behaviour.

This commit is contained in:
Age Manning 2019-03-15 02:48:09 +11:00
parent 24c7f180e2
commit 7b6a653d05
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
3 changed files with 40 additions and 5 deletions

View File

@ -1,3 +1,4 @@
use crate::rpc::{RPCMethod, RPCRequest, RPCResponse, Rpc, RpcEvent};
use futures::prelude::*; use futures::prelude::*;
use libp2p::{ use libp2p::{
core::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess}, core::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess},
@ -15,7 +16,7 @@ pub struct Behaviour<TSubstream: AsyncRead + AsyncWrite> {
gossipsub: Gossipsub<TSubstream>, gossipsub: Gossipsub<TSubstream>,
// TODO: Add Kademlia for peer discovery // TODO: Add Kademlia for peer discovery
/// The events generated by this behaviour to be consumed in the swarm poll. /// The events generated by this behaviour to be consumed in the swarm poll.
// We use gossipsub events for now, generalise later. serenity_rpc: Rpc<TSubstream>,
#[behaviour(ignore)] #[behaviour(ignore)]
events: Vec<BehaviourEvent>, events: Vec<BehaviourEvent>,
} }
@ -37,10 +38,34 @@ impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<GossipsubE
} }
} }
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 }),
}
}
}
impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> { impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
pub fn new(local_peer_id: PeerId, gs_config: GossipsubConfig) -> Self { pub fn new(local_peer_id: PeerId, gs_config: GossipsubConfig) -> Self {
Behaviour { Behaviour {
gossipsub: Gossipsub::new(local_peer_id, gs_config), gossipsub: Gossipsub::new(local_peer_id, gs_config),
serenity_rpc: Rpc::new(),
events: Vec::new(), events: Vec::new(),
} }
} }
@ -70,6 +95,15 @@ impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
/// The types of events than can be obtained from polling the behaviour. /// The types of events than can be obtained from polling the behaviour.
pub enum BehaviourEvent { pub enum BehaviourEvent {
RPCRequest {
id: u64,
method: RPCMethod,
body: RPCRequest,
},
RPCResponse {
id: u64,
result: RPCResponse,
},
// TODO: This is a stub at the moment // TODO: This is a stub at the moment
Message(String), Message(String),
} }

View File

@ -1,9 +1,9 @@
mod handler;
mod methods;
/// RPC Protocol over libp2p. /// RPC Protocol over libp2p.
/// ///
/// This is purpose built for Ethereum 2.0 serenity and the protocol listens on /// This is purpose built for Ethereum 2.0 serenity and the protocol listens on
/// `/eth/serenity/rpc/1.0.0` /// `/eth/serenity/rpc/1.0.0`
mod handler;
mod methods;
mod protocol; mod protocol;
use futures::prelude::*; use futures::prelude::*;
@ -12,8 +12,8 @@ use libp2p::core::swarm::{
ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
}; };
use libp2p::{Multiaddr, PeerId}; use libp2p::{Multiaddr, PeerId};
use methods::RPCRequest; pub use methods::{RPCMethod, RPCRequest, RPCResponse};
use protocol::{RPCProtocol, RpcEvent}; pub use protocol::{RPCProtocol, RpcEvent};
use std::marker::PhantomData; use std::marker::PhantomData;
use tokio::io::{AsyncRead, AsyncWrite}; use tokio::io::{AsyncRead, AsyncWrite};

View File

@ -158,6 +158,7 @@ impl Encodable for RpcEvent {
} }
} }
#[derive(Debug)]
pub enum DecodeError { pub enum DecodeError {
ReadError(upgrade::ReadOneError), ReadError(upgrade::ReadOneError),
SSZDecodeError(ssz::DecodeError), SSZDecodeError(ssz::DecodeError),