Skip to content

Commit

Permalink
chore: persistent enr, rename CLI subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
Vid201 committed Dec 7, 2023
1 parent ec8c6b7 commit 78d9a5e
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rust-version = "1.73.0"
alloy-chains = "0.1.2"
async-stream = "0.3.5"
async-trait = "0.1"
discv5 = { version = "0.3.0", features = ["libp2p"] }
discv5 = { version = "0.3.1", features = ["libp2p"] }
ethers = { git = "https://github.com/gakonst/ethers-rs", rev = "fa3017715a298728d9fb341933818a5d0d84c2dc", features = [
"ws",
] }
Expand Down
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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
cargo run --release -- node --eth-client-address http://127.0.0.1:8545 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws

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

run-silius-uopool:
cargo run --release -- uopool --eth-client-address http://127.0.0.1:8545 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789
Expand All @@ -18,10 +20,10 @@ run-silius-create-wallet:
cargo run --release -- create-wallet --output-path ${HOME}/.silius

run-silius-debug:
cargo run --release -- bundler --eth-client-address ws://127.0.0.1:8546 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws --http.api eth,debug,web3 --ws.api eth,debug,web3
cargo run --release -- node --eth-client-address ws://127.0.0.1:8546 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws --http.api eth,debug,web3 --ws.api eth,debug,web3

run-silius-debug-mode:
cargo run --profile debug-fast -- bundler --verbosity 4 --eth-client-address ws://127.0.0.1:8546 --mnemonic-file /home/vid/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws --http.api eth,debug,web3 --ws.api eth,debug,web3
cargo run --profile debug-fast -- node --verbosity 4 --eth-client-address ws://127.0.0.1:8546 --mnemonic-file /home/vid/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws --http.api eth,debug,web3 --ws.api eth,debug,web3

fetch-thirdparty:
git submodule update --init
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ cargo run --release -- create-wallet --output-path ${HOME}/.silius --chain-id 5
Run bundler (with user operation pool and JSON-RPC API):

```bash
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
cargo run --release -- node --eth-client-address http://127.0.0.1:8545 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws
```

Run only bundling component:

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

Run only user operation pool:
Expand All @@ -69,7 +69,7 @@ cargo run --release -- rpc --http --ws
### Docker

```bash
docker run --net=host -v ./bundler-spec-tests/keys/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266:/data/silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 -v ./db:/data/silius/db ghcr.io/silius-rs/silius:latest bundler --eth-client-address http://127.0.0.1:8545 --datadir data/silius --mnemonic-file data/silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789 --http --http.addr 0.0.0.0 --http.port 3000 --http.api eth,debug,web3 --ws --ws.addr 0.0.0.0 --ws.port 3001 --ws.api eth,debug,web3 --eth-client-proxy-address http://127.0.0.1:8545
docker run --net=host -v ./bundler-spec-tests/keys/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266:/data/silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 -v ./db:/data/silius/db ghcr.io/silius-rs/silius:latest node --eth-client-address http://127.0.0.1:8545 --datadir data/silius --mnemonic-file data/silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789 --http --http.addr 0.0.0.0 --http.port 3000 --http.api eth,debug,web3 --ws --ws.addr 0.0.0.0 --ws.port 3001 --ws.api eth,debug,web3 --eth-client-proxy-address http://127.0.0.1:8545
```

## Supported networks
Expand Down
15 changes: 13 additions & 2 deletions bin/silius/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use silius_grpc::{
};
use silius_primitives::{
bundler::SendBundleMode,
consts::{entry_point, flashbots_relay_endpoints, p2p::DISCOVERY_SECRET_FILE_NAME},
consts::{
entry_point, flashbots_relay_endpoints,
p2p::{NODE_ENR_FILE_NAME, NODE_KEY_FILE_NAME},
},
consts::{
p2p::DB_FOLDER_NAME,
reputation::{
Expand Down Expand Up @@ -182,7 +185,11 @@ where

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

let entrypoint_api = EntryPoint::new(
Expand Down Expand Up @@ -236,6 +243,7 @@ where
validator,
args.p2p_opts.enable_p2p,
node_key_file,
node_enr_file,
args.p2p_opts.to_config(),
args.p2p_opts.bootnodes,
)
Expand Down Expand Up @@ -288,6 +296,7 @@ where
validator,
args.p2p_opts.enable_p2p,
node_key_file,
node_enr_file,
args.p2p_opts.to_config(),
args.p2p_opts.bootnodes,
)
Expand Down Expand Up @@ -343,6 +352,7 @@ where
validator,
args.p2p_opts.enable_p2p,
node_key_file,
node_enr_file,
args.p2p_opts.to_config(),
args.p2p_opts.bootnodes,
)
Expand Down Expand Up @@ -395,6 +405,7 @@ where
validator,
args.p2p_opts.enable_p2p,
node_key_file,
node_enr_file,
args.p2p_opts.to_config(),
args.p2p_opts.bootnodes,
)
Expand Down
15 changes: 11 additions & 4 deletions bin/silius/src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,12 @@ pub struct P2PArgs {
pub bootnodes: Vec<Enr>,

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

/// The path to the file where the p2p enr is stored.
#[clap(long = "nodeenr")]
pub node_enr: Option<PathBuf>,
}

impl P2PArgs {
Expand Down Expand Up @@ -608,8 +612,10 @@ mod tests {
"4337",
"--bootnodes",
&binding,
"--node-key",
"~/.silius/discovery-secret",
"--nodekey",
"~/.silius/p2p/node-key",
"--nodeenr",
"~/.silius/p2p/node-enr",
];
assert_eq!(
P2PArgs {
Expand All @@ -619,7 +625,8 @@ mod tests {
tcp4_port: 4337,
udp4_port: 4337,
bootnodes: vec![enr],
node_key: Some(PathBuf::from("~/.silius/discovery-secret")),
node_key: Some(PathBuf::from("~/.silius/p2p/node-key")),
node_enr: Some(PathBuf::from("~/.silius/p2p/node-enr"))
},
P2PArgs::try_parse_from(args).unwrap()
)
Expand Down
8 changes: 4 additions & 4 deletions bin/silius/src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{future::pending, sync::Arc};

/// Start the bundler with all components (bundling component, user operation mempool, RPC server)
#[derive(Debug, Parser)]
pub struct BundlerCommand {
pub struct NodeCommand {
/// All Bundler specific args
#[clap(flatten)]
bundler: BundlerArgs,
Expand All @@ -26,7 +26,7 @@ pub struct BundlerCommand {
rpc: RpcArgs,
}

impl BundlerCommand {
impl NodeCommand {
/// Execute the command
pub async fn execute(self) -> eyre::Result<()> {
if self.common.eth_client_address.clone().starts_with("http") {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl BundlerCommand {

/// Start the bundling component
#[derive(Debug, Parser)]
pub struct BundlingCommand {
pub struct BundlerCommand {
/// All Bundler specific args
#[clap(flatten)]
bundler: BundlerArgs,
Expand All @@ -77,7 +77,7 @@ pub struct BundlingCommand {
pub uopool_grpc_listen_address: String,
}

impl BundlingCommand {
impl BundlerCommand {
/// Execute the command
pub async fn execute(self) -> eyre::Result<()> {
if self.common.eth_client_address.clone().starts_with("http") {
Expand Down
10 changes: 5 additions & 5 deletions bin/silius/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ impl Cli {
#[derive(Debug, Subcommand)]
pub enum Commands {
/// Start the bundler with all components (bundling component, user operation mempool, RPC server)
#[command(name = "bundler")]
Bundler(Box<commands::BundlerCommand>),
#[command(name = "node")]
Node(Box<commands::NodeCommand>),

/// Start the bundling component
#[command(name = "bundling")]
Bundling(commands::BundlingCommand),
#[command(name = "bundler")]
Bundler(commands::BundlerCommand),

/// Start the user operation mempool
#[command(name = "uopool")]
Expand Down Expand Up @@ -76,8 +76,8 @@ pub fn run() -> eyre::Result<()> {

let task = async move {
match cli.command {
Commands::Node(command) => command.execute().await,
Commands::Bundler(command) => command.execute().await,
Commands::Bundling(command) => command.execute().await,
Commands::UoPool(command) => command.execute().await,
Commands::Rpc(command) => command.execute().await,
Commands::CreateWallet(command) => command.execute(),
Expand Down
2 changes: 1 addition & 1 deletion bundler-spec-tests/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case $1 in
start)
docker-compose up -d
sleep 2
silius bundler \
silius node \
--verbosity 4 \
--eth-client-address http://127.0.0.1:8545 \
--mnemonic-file keys/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 \
Expand Down
1 change: 1 addition & 0 deletions bundler-spec-tests/p2p/node-enr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enr:-Iu4QCDkGSYoIMe5ISDuqXgwHwGpkj2lYsUDbmR7f3GJRV_6YyKvoTFVgjV77l07stYbgBgCq0SHa0DuYzSoGQFzD2oBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQN5PrrJ6hQerKiUV6eULP8BwP0KEIbykGS1bVzjr3GVSoN0Y3CCIyiDdWRwgiMo
1 change: 1 addition & 0 deletions bundler-spec-tests/p2p/node-key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
 z}��U�SP�'6���gJB��p��� +Zd��
31 changes: 27 additions & 4 deletions crates/grpc/src/uopool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use futures::StreamExt;
use libp2p_identity::{secp256k1, Keypair};
use parking_lot::RwLock;
use silius_p2p::config::Config;
use silius_p2p::enr::{build_enr, keypair_to_combined};
use silius_p2p::network::{EntrypointChannels, Network};
use silius_primitives::provider::BlockStream;
use silius_primitives::uopool::AddError;
Expand All @@ -32,6 +33,7 @@ use std::collections::HashMap;
use std::env;
use std::os::unix::prelude::PermissionsExt;
use std::path::PathBuf;
use std::str::FromStr;
use std::{net::SocketAddr, sync::Arc, time::Duration};
use tonic::{Code, Request, Response, Status};
use tracing::{debug, error, info};
Expand Down Expand Up @@ -415,6 +417,7 @@ pub async fn uopool_service_run<M, T, Y, X, Z, H, R, SanCk, SimCk, SimTrCk>(
validator: StandardUserOperationValidator<M, SanCk, SimCk, SimTrCk>,
p2p_enabled: bool,
node_key_file: PathBuf,
node_enr_file: PathBuf,
config: Config,
bootnodes: Vec<Enr>,
) -> Result<()>
Expand Down Expand Up @@ -484,30 +487,49 @@ where
.into();
keypair.into()
} else {
info!("The p2p spec private key is not exist. Creating one now!");
info!("The p2p spec private key doesn't exist. Creating one now!");

let keypair = Keypair::generate_secp256k1();
std::fs::create_dir_all(node_key_file.parent().expect("Key file path error"))
.expect("Creating key file directory failed");
std::fs::write(
node_key_file.clone(),
keypair
.to_protobuf_encoding()
.expect("discovery secret encode failed"),
.expect("Discovery secret encoding failed"),
)
.expect("write discoveray secret file failed");
.expect("Discovery secret writing failed");
std::fs::set_permissions(node_key_file, std::fs::Permissions::from_mode(0o600))
.expect("Setting key file permission failed");

keypair
};

let enr = if node_enr_file.exists() {
let content = std::fs::read_to_string(node_enr_file).expect("enr file currupted");
Enr::from_str(&content).expect("enr file currupted")
} else {
let combined_key =
keypair_to_combined(discovery_secret.clone()).expect("key error");
let enr = build_enr(&combined_key, &config).expect("enr building failed");
std::fs::create_dir_all(node_enr_file.parent().expect("Key file path error"))
.expect("Creating key file directory failed");
std::fs::write(node_enr_file, enr.to_base64()).expect("enr writing failed");
enr
};
info!("Enr: {}", enr);

let listen_addrs = config.listen_addr.to_multi_addr();
let mut p2p_network = Network::new(
enr,
discovery_secret,
config,
entrypoint_channels,
Duration::from_secs(10),
30,
)
.expect("p2p network init failed");
info!("Enr: {}", p2p_network.local_enr().to_base64());

for listen_addr in listen_addrs.into_iter() {
info!("P2P node listened on {}", listen_addr);
p2p_network
Expand All @@ -518,6 +540,7 @@ where
if bootnodes.is_empty() {
info!("Start p2p mode without bootnodes");
}

for enr in bootnodes {
info!("Trying to dial p2p node {enr:}");
p2p_network.dial(enr).expect("Dial bootnode failed");
Expand Down
4 changes: 2 additions & 2 deletions crates/p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::config::Config;
use crate::discovery::{self, Discovery};
use crate::enr::build_enr;
use crate::gossipsub::{create_gossisub, Gossipsub};
use crate::peer_manager::{PeerManager, PeerManagerEvent};
use crate::request_response;
use discv5::enr::CombinedKey;
use discv5::Enr;
use libp2p::gossipsub;
use libp2p::swarm::NetworkBehaviour;
use std::time::Duration;
Expand All @@ -25,13 +25,13 @@ pub struct Behaviour {

impl Behaviour {
pub fn new(
enr: Enr,
key: CombinedKey,
config: Config,
p2p_mempool_id: Vec<String>,
ping_interval: Duration,
target_peers: usize,
) -> eyre::Result<Self> {
let enr = build_enr(&key, &config)?;
let gossipsub = create_gossisub(p2p_mempool_id).map_err(|e| eyre::anyhow!(e))?;
let reqrep = request_response::Behaviour::new(Default::default());
let discovery = Discovery::new(enr, key, config)?;
Expand Down
2 changes: 2 additions & 0 deletions crates/p2p/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct ListenAddr<Ip> {
}

/// Variant of ListenAddr that can be ipv4, ipv6 or dual.
#[derive(Clone, Debug)]
pub enum ListenAddress {
Ipv4(ListenAddr<Ipv4Addr>),
Ipv6(ListenAddr<Ipv6Addr>),
Expand Down Expand Up @@ -46,6 +47,7 @@ impl Default for ListenAddress {
}
}

#[derive(Clone, Debug)]
pub struct Config {
pub listen_addr: ListenAddress,

Expand Down
4 changes: 3 additions & 1 deletion crates/p2p/src/enr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use libp2p::{
};

/// Convert a libp2p Keypair into a discv5 CombinedKey
pub fn keypair_to_combine(keypair: Keypair) -> eyre::Result<CombinedKey> {
pub fn keypair_to_combined(keypair: Keypair) -> eyre::Result<CombinedKey> {
match keypair.try_into_secp256k1() {
Ok(key) => {
let secret =
Expand All @@ -28,6 +28,7 @@ pub fn keypair_to_combine(keypair: Keypair) -> eyre::Result<CombinedKey> {
/// Build an ENR from a libp2p Keypair and config
pub fn build_enr(enr_key: &CombinedKey, config: &Config) -> eyre::Result<Enr> {
let mut enr_builder = EnrBuilder::new("v4");

if let Some(ip) = config.ipv4_addr {
enr_builder.ip4(ip);
}
Expand All @@ -48,6 +49,7 @@ pub fn build_enr(enr_key: &CombinedKey, config: &Config) -> eyre::Result<Enr> {
}

let enr = enr_builder.build(enr_key)?;

Ok(enr)
}

Expand Down
Loading

0 comments on commit 78d9a5e

Please sign in to comment.