mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-13 21:20:52 +00:00
Add RPC protocol to lh network behaviour.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use crate::rpc::{RPCMethod, RPCRequest, RPCResponse, Rpc, RpcEvent};
|
||||
use futures::prelude::*;
|
||||
use libp2p::{
|
||||
core::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess},
|
||||
@@ -15,7 +16,7 @@ pub struct Behaviour<TSubstream: AsyncRead + AsyncWrite> {
|
||||
gossipsub: Gossipsub<TSubstream>,
|
||||
// TODO: Add Kademlia for peer discovery
|
||||
/// 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)]
|
||||
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> {
|
||||
pub fn new(local_peer_id: PeerId, gs_config: GossipsubConfig) -> Self {
|
||||
Behaviour {
|
||||
gossipsub: Gossipsub::new(local_peer_id, gs_config),
|
||||
serenity_rpc: Rpc::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.
|
||||
pub enum BehaviourEvent {
|
||||
RPCRequest {
|
||||
id: u64,
|
||||
method: RPCMethod,
|
||||
body: RPCRequest,
|
||||
},
|
||||
RPCResponse {
|
||||
id: u64,
|
||||
result: RPCResponse,
|
||||
},
|
||||
// TODO: This is a stub at the moment
|
||||
Message(String),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user