Skip to content

Commit

Permalink
feat: add p2p reqres domain
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Jul 4, 2023
1 parent da9c9ba commit 0e78412
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 300 deletions.
479 changes: 263 additions & 216 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions crates/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ rust-version = "1.69.0"
anyhow = "1"
async-trait = "0.1"
aa-bundler-uopool = {path = "../uopool"}
ethereum_ssz = "0.5.0"
ethereum_ssz_derive = "0.5.0"
ssz_rs = "0.8.0"
ssz_rs_derive = "0.8.0"
snap = "1"
aa-bundler-primitives = { path = "../primitives" }

[dependencies.libp2p]
version = "0.51.3"
Expand Down
8 changes: 4 additions & 4 deletions crates/p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::gossipsub::Gossipsub;
use crate::reqrep::{BundlerReponse, BundlerRequest, BundlerRequestResponse};
use crate::reqrep::reqrep::{BundlerRequestResponse, Request, Response};
use libp2p::gossipsub;
use libp2p::request_response;
use libp2p::swarm::NetworkBehaviour;
Expand All @@ -17,14 +17,14 @@ impl From<gossipsub::Event> for Event {
}
}

impl From<request_response::Event<BundlerRequest, BundlerReponse>> for Event {
fn from(value: request_response::Event<BundlerRequest, BundlerReponse>) -> Self {
impl From<request_response::Event<Request, Response>> for Event {
fn from(value: request_response::Event<Request, Response>) -> Self {
Event::Reqrep(value)
}
}

#[derive(Debug)]
pub enum Event {
GossipSub(gossipsub::Event),
Reqrep(request_response::Event<BundlerRequest, BundlerReponse>),
Reqrep(request_response::Event<Request, Response>),
}
77 changes: 0 additions & 77 deletions crates/p2p/src/reqrep.rs

This file was deleted.

2 changes: 2 additions & 0 deletions crates/p2p/src/reqrep/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod protocol;
pub mod reqrep;
85 changes: 85 additions & 0 deletions crates/p2p/src/reqrep/protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::fmt::Display;

use libp2p::request_response::ProtocolName;
const PROTOCOL_PREFIX: &str = "/account_abstraction/erc4337/req";

#[derive(Clone, Debug, Copy)]
pub enum Protocol {
Status,
Goodbye,
Ping,
MetaData,
PooledUserOpHashes,
PooledUserOpsByHash,
}

impl Display for Protocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let result = match self {
Protocol::Status => "status",
Protocol::Goodbye => "goodbye",
Protocol::Ping => "ping",
Protocol::MetaData => "metadata",
Protocol::PooledUserOpHashes => "pooled_user_ops_hashes",
Protocol::PooledUserOpsByHash => "pooled_user_ops_by_hash",
};
f.write_str(result)
}
}

#[derive(Clone, Debug)]
pub struct ProtocolId {
/// The RPC message type/name.
pub message_name: Protocol,

/// The version of the RPC.
pub version: Version,

/// The encoding of the RPC.
pub encoding: Encoding,

protocol_id: String,
}

impl ProtocolId {
fn new(message_name: Protocol, version: Version, encoding: Encoding) -> Self {
let protocol_id = format!(
"{}/{}/{}/{}",
PROTOCOL_PREFIX, message_name, version, encoding
);
Self {
message_name,
version,
encoding,
protocol_id,
}
}
}

#[derive(Clone, Debug)]
pub enum Version {
V1,
}

impl Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("1")
}
}

#[derive(Clone, Debug)]
pub enum Encoding {
SSZSnappy,
}

impl Display for Encoding {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("ssz_snappy")
}
}

impl ProtocolName for ProtocolId {
fn protocol_name(&self) -> &[u8] {
self.protocol_id.as_bytes()
}
}
Loading

0 comments on commit 0e78412

Please sign in to comment.