Skip to content

Commit

Permalink
chore: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Nov 9, 2023
1 parent e266891 commit 37c1f23
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
- name: Build
run: |
make build
make build-debug-mode
- name: Upload artifact
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
build:
cargo build --release
build-debug-mode:
cargo build

run-silius:
cargo run --release -- bundler --eth-client-address http://127.0.0.1:8545 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws

Expand Down
20 changes: 0 additions & 20 deletions P2P.md

This file was deleted.

12 changes: 10 additions & 2 deletions bin/silius/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use silius_grpc::{
uopool_service_run,
};
use silius_primitives::{
bundler::SendBundleMode, consts::flashbots_relay_endpoints, provider::BlockStream, Chain,
Wallet,
bundler::SendBundleMode,
consts::{flashbots_relay_endpoints, p2p::DISCOVERY_SECRET_FILE_NAME},
provider::BlockStream,
Chain, Wallet,
};
use silius_rpc::{
debug_api::{DebugApiServer, DebugApiServerImpl},
Expand Down Expand Up @@ -153,6 +155,11 @@ where

let datadir = unwrap_path_or_home(args.datadir)?;

let node_key_file = match args.p2p_opts.node_key.clone() {
Some(key_file) => key_file,
None => datadir.join(DISCOVERY_SECRET_FILE_NAME),
};

uopool_service_run(
SocketAddr::new(args.uopool_addr, args.uopool_port),
datadir,
Expand All @@ -166,6 +173,7 @@ where
args.whitelist,
args.uopool_mode,
args.p2p_opts.enable_p2p,
node_key_file,
args.p2p_opts.to_config(),
args.p2p_opts.bootnodes,
)
Expand Down
27 changes: 17 additions & 10 deletions bin/silius/src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct UoPoolArgs {

/// P2P configuration
#[clap(flatten)]
pub p2p_opts: P2POpts,
pub p2p_opts: P2PArgs,
}

/// Common CLI args for bundler and uopool
Expand Down Expand Up @@ -220,7 +220,7 @@ pub struct CreateWalletArgs {
}

#[derive(Clone, Debug, Parser, PartialEq)]
pub struct P2POpts {
pub struct P2PArgs {
/// enable p2p
#[clap(long)]
pub enable_p2p: bool,
Expand All @@ -234,19 +234,23 @@ pub struct P2POpts {
pub p2p_broadcast_address: Option<Ipv4Addr>,

/// The udp4 port to broadcast to peers in order to reach back for discovery.
#[clap(long, default_value = "9000")]
#[clap(long = "discovery.port", default_value = "9000")]
pub udp4_port: u16,

/// The tcp4 port to boardcast to peers in order to reach back for discovery.
#[clap(long, default_value = "9000")]
#[clap(long = "p2p.port", default_value = "9000")]
pub tcp4_port: u16,

/// The initial bootnodes to connect to for the p2p network
#[clap(long, value_delimiter = ',', value_parser=parse_enr)]
pub bootnodes: Vec<Enr>,

/// The path to the file where the p2p private key is stored.
#[clap(long)]
pub node_key: Option<PathBuf>,
}

impl P2POpts {
impl P2PArgs {
/// Convert the P2POpts to [silius_p2p::config::Config]
pub fn to_config(&self) -> Config {
// TODO: support ipv6
Expand Down Expand Up @@ -586,23 +590,26 @@ mod tests {
"0.0.0.0",
"--p2p-broadcast-address",
"127.0.0.1",
"--tcp4-port",
"--discovery.port",
"4337",
"--udp4-port",
"--p2p.port",
"4337",
"--bootnodes",
&binding,
"--node-key",
"~/.silius/discovery-secret",
];
assert_eq!(
P2POpts {
P2PArgs {
enable_p2p: true,
p2p_listen_address: Ipv4Addr::new(0, 0, 0, 0),
p2p_broadcast_address: Some(Ipv4Addr::new(127, 0, 0, 1)),
tcp4_port: 4337,
udp4_port: 4337,
bootnodes: vec![enr]
bootnodes: vec![enr],
node_key: Some(PathBuf::from("~/.silius/discovery-secret")),
},
P2POpts::try_parse_from(args).unwrap()
P2PArgs::try_parse_from(args).unwrap()
)
}
}
10 changes: 7 additions & 3 deletions crates/contracts/src/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ mod tests {
};
use silius_primitives::UserOperation;
use std::{str::FromStr, sync::Arc};

#[tokio::test]
#[ignore]
async fn simulate_validation() {
Expand Down Expand Up @@ -453,10 +452,15 @@ mod tests {
}

#[test]
fn deserialize_failedop() -> eyre::Result<()> {
fn deserialize_failed_op() -> eyre::Result<()> {
let err_msg = Bytes::from_str("0x220266b600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001e41413430206f76657220766572696669636174696f6e4761734c696d69740000")?;
let res = EntryPointAPIErrors::decode(err_msg)?;
println!("res: {:?}", res);
match res {
EntryPointAPIErrors::FailedOp(f) => {
assert_eq!(f.reason, "AA40 over verificationGasLimit")
}
_ => panic!("Invalid error message"),
}
Ok(())
}
}
24 changes: 12 additions & 12 deletions crates/grpc/src/uopool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use futures::StreamExt;
use libp2p_identity::Keypair;
use silius_p2p::config::Config;
use silius_p2p::network::{EntrypointChannels, Network};
use silius_primitives::consts::p2p::DB_FOLDER_NAME;
use silius_primitives::provider::BlockStream;
use silius_primitives::reputation::ReputationEntry;
use silius_primitives::UserOperation;
Expand All @@ -29,15 +30,12 @@ use silius_uopool::{
UoPool as UserOperationPool, UoPoolBuilder,
};
use std::fmt::{Debug, Display};
use std::os::unix::prelude::PermissionsExt;
use std::path::PathBuf;
use std::{net::SocketAddr, sync::Arc, time::Duration};
use tonic::{Code, Request, Response, Status};
use tracing::{error, info};

/// The dafault database folder name used for storing the database files
pub const DB_FOLDER_NAME: &str = "db";
/// The default discovery secret file name used for storing the discovery secret
pub const DISCOVERY_SECRET_FILE_NAME: &str = "discovery-secret";

type StandardUserPool<M, P, R, E> =
UserOperationPool<M, StandardUserOperationValidator<M, P, R, E>, P, R, E>;

Expand Down Expand Up @@ -371,6 +369,7 @@ pub async fn uopool_service_run<M>(
whitelist: Vec<Address>,
upool_mode: UoPoolMode,
p2p_enabled: bool,
node_key_file: PathBuf,
config: Config,
bootnodes: Vec<Enr>,
) -> Result<()>
Expand All @@ -390,9 +389,9 @@ where
env.create_tables()
.expect("Create mdbx database tables failed");

let mut entrypoint_channels: EntrypointChannels = Vec::new();

if p2p_enabled {
let mut entrypoint_channels: EntrypointChannels = Vec::new();

for (ep, block_stream) in eps.into_iter().zip(block_streams.into_iter()) {
let id = mempool_id(&ep, &U256::from(chain.id()));
let (waiting_to_pub_sd, waiting_to_pub_rv) = unbounded::<(UserOperation, U256)>();
Expand Down Expand Up @@ -428,20 +427,21 @@ where
entrypoint_channels.push((chain, ep, waiting_to_pub_rv, p2p_userop_sd))
}

let discovery_secret_file = datadir.join(DISCOVERY_SECRET_FILE_NAME);
let discovery_secret = if discovery_secret_file.exists() {
let content =
std::fs::read(discovery_secret_file).expect("discovery secret file currupted");
let discovery_secret = if node_key_file.exists() {
let content = std::fs::read(node_key_file).expect("discovery secret file currupted");
Keypair::from_protobuf_encoding(&content).expect("discovery secret file currupted")
} else {
info!("The p2p spec private key is not exist. Creating one now!");
let keypair = Keypair::generate_secp256k1();
std::fs::write(
discovery_secret_file,
node_key_file.clone(),
keypair
.to_protobuf_encoding()
.expect("discovery secret encode failed"),
)
.expect("write discoveray secret file failed");
std::fs::set_permissions(node_key_file, std::fs::Permissions::from_mode(0o600))
.expect("Setting key file permission failed");
keypair
};
let listen_addrs = config.listen_addr.to_multi_addr();
Expand Down
13 changes: 6 additions & 7 deletions crates/p2p/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
[package]
name = "silius-p2p"
version = "0.1.0"
authors = ["Vid Kersic <vid.kersic@yahoo.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/Vid201/aa-bundler"
readme = "README.md"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
description = """
AA (ERC-4337) Bundler p2p components
"""
rust-version = "1.69.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions crates/p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ impl From<request_response::Event> for Event {
}
}

impl From<discovery::DiscoverPeers> for Event {
fn from(value: discovery::DiscoverPeers) -> Self {
impl From<discovery::DiscoveredPeers> for Event {
fn from(value: discovery::DiscoveredPeers) -> Self {
Event::Discovery(value)
}
}
Expand All @@ -78,7 +78,7 @@ pub enum Event {
/// Request/Response protocol event
Reqrep(request_response::Event),
/// Discovery protocol event
Discovery(discovery::DiscoverPeers),
Discovery(discovery::DiscoveredPeers),
/// Peer manager event
PeerManager(PeerManagerEvent),
}
4 changes: 2 additions & 2 deletions crates/p2p/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ impl Discovery {
}

#[derive(Debug)]
pub struct DiscoverPeers {
pub struct DiscoveredPeers {
pub peers: Vec<Enr>,
}

impl NetworkBehaviour for Discovery {
type ConnectionHandler = ConnectionHandler;
type ToSwarm = DiscoverPeers;
type ToSwarm = DiscoveredPeers;

fn poll(
&mut self,
Expand Down
8 changes: 3 additions & 5 deletions crates/p2p/src/gossipsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ use libp2p::gossipsub::{
Behaviour, ConfigBuilder, DataTransform, IdentTopic, Message, MessageAuthenticity, RawMessage,
TopicHash, ValidationMode, WhitelistSubscriptionFilter,
};
use silius_primitives::consts::p2p::{
MAX_GOSSIP_SNAP_SIZE, SSZ_SNAPPY_ENCODING, TOPIC_PREFIX, USER_OPS_WITH_ENTRY_POINT_TOPIC,
};
use snap::raw::{decompress_len, Decoder, Encoder};

pub type Gossipsub = Behaviour<SnappyTransform, WhitelistSubscriptionFilter>;

const TOPIC_PREFIX: &str = "account_abstraction";
const USER_OPS_WITH_ENTRY_POINT_TOPIC: &str = "user_ops_with_entry_point";
const SSZ_SNAPPY_ENCODING: &str = "ssz_snappy";
const MAX_GOSSIP_SNAP_SIZE: usize = 1048576; // bytes

// Highly inspired by https://github.com/sigp/lighthouse/blob/stable/beacon_node/lighthouse_network/src/types/pubsub.rs#L45-L103
// Implements the `DataTransform` trait of gossipsub to employ snappy compression
pub struct SnappyTransform {
Expand Down
10 changes: 5 additions & 5 deletions crates/p2p/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Network {
.iter()
.find_map(|(_, ep, _, new_coming_uos_ch)| {
if *ep == userops.entrypoint_address() {
for user_op in userops.clone().user_ops().into_iter() {
for user_op in userops.clone().user_operations().into_iter() {
new_coming_uos_ch
.unbounded_send(user_op)
.expect("new useop channel should be open all the time");
Expand Down Expand Up @@ -223,7 +223,7 @@ impl Network {
}

// TODO: discovery peer connect
fn handle_discovery_event(&self, _event: discovery::DiscoverPeers) -> Option<NetworkEvent> {
fn handle_discovery_event(&self, _event: discovery::DiscoveredPeers) -> Option<NetworkEvent> {
None
}

Expand All @@ -250,7 +250,7 @@ impl Network {
}

pub fn poll_network(&mut self, cx: &mut Context) -> Poll<NetworkEvent> {
let mut msg_to_publich = Vec::new();
let mut msg_to_publish = Vec::new();
for (chain, ep, waiting_to_publish_ch, _) in self.entrypoint_channels.iter_mut() {
while let Ok(Some((pub_userop, verified_block))) = waiting_to_publish_ch.try_next() {
info!("Got userop {pub_userop:?} from ep {ep:} verified in {verified_block:?} to publish to p2p network!");
Expand All @@ -260,11 +260,11 @@ impl Network {
chain.id().into(),
vec![pub_userop],
);
msg_to_publich.push(pub_msg);
msg_to_publish.push(pub_msg);
}
}

for pub_msg in msg_to_publich.into_iter() {
for pub_msg in msg_to_publish.into_iter() {
match self.publish(pub_msg) {
Ok(_) => {}
Err(err) => match err {
Expand Down
4 changes: 2 additions & 2 deletions crates/p2p/src/request_response/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lazy_static::lazy_static;
use silius_primitives::consts::p2p::REQREP_PROTOCOL_PREFIX;
use std::fmt::Display;
const PROTOCOL_PREFIX: &str = "/account_abstraction/req";

lazy_static! {
pub static ref SUPPORTED_PROTOCOL: Vec<ProtocolId> = vec![
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct ProtocolId {
impl ProtocolId {
pub fn new(message_name: Protocol) -> Self {
let protocol_id = format!(
"{PROTOCOL_PREFIX}/{message_name}/{}/{}",
"{REQREP_PROTOCOL_PREFIX}/{message_name}/{}/{}",
Version::V1,
Encoding::SSZSnappy
);
Expand Down

0 comments on commit 37c1f23

Please sign in to comment.