Skip to content

Commit

Permalink
implement reth p2p nodeset info
Browse files Browse the repository at this point in the history
  • Loading branch information
int88 committed Jul 4, 2024
1 parent 6eca557 commit e78ec4f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ auto_impl = "1"
aquamarine = "0.5"
bytes = "1.5"
bitflags = "2.4"
chrono = { version = "0.4.38", features = ["serde"] }
clap = "4"
const_format = { version = "0.2.32", features = ["rust_1_64"] }
dashmap = "5.5"
Expand Down Expand Up @@ -504,6 +505,7 @@ proptest-arbitrary-interop = "0.1.0"
secp256k1 = { version = "0.29", default-features = false, features = [
"global-context",
"recovery",
"serde",
] }
enr = { version = "0.12.1", default-features = false }

Expand Down
6 changes: 6 additions & 0 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ reth-prune.workspace = true

# crypto
alloy-rlp.workspace = true
secp256k1 = { workspace = true, default-features = false, features = [
"global-context",
"recovery",
"serde",
] }

# tracing
tracing.workspace = true
Expand Down Expand Up @@ -103,6 +108,7 @@ futures.workspace = true
aquamarine.workspace = true
eyre.workspace = true
clap = { workspace = true, features = ["derive", "env"] }
chrono = { workspace = true, features = ["serde"] }
tempfile.workspace = true
backon.workspace = true
similar-asserts.workspace = true
Expand Down
9 changes: 8 additions & 1 deletion bin/reth/src/commands/p2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use reth_primitives::BlockHashOrNumber;
use reth_provider::{providers::StaticFileProvider, ProviderFactory};
use std::{path::PathBuf, sync::Arc};

mod nodeset;

/// `reth p2p` command
#[derive(Debug, Parser)]
pub struct Command {
Expand Down Expand Up @@ -71,10 +73,12 @@ pub enum Subcommands {
#[arg(value_parser = hash_or_num_value_parser)]
id: BlockHashOrNumber,
},
/// Node Set Utilities
Nodeset(nodeset::Command),
}
impl Command {
/// Execute `p2p` command
pub async fn execute(&self) -> eyre::Result<()> {
pub async fn execute(self) -> eyre::Result<()> {
let tempdir = tempfile::TempDir::new()?;
let noop_db = Arc::new(create_db(tempdir.into_path(), self.db.database_args())?);

Expand Down Expand Up @@ -162,6 +166,9 @@ impl Command {
let body = result.into_iter().next().unwrap();
println!("Successfully downloaded body: {body:?}")
}
Subcommands::Nodeset(command) => {
let _ = command.execute();
}
}

Ok(())
Expand Down
60 changes: 60 additions & 0 deletions bin/reth/src/commands/p2p/nodeset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use chrono::{DateTime, Utc};
use clap::{Parser, Subcommand};
use discv5::enr::Enr;
use secp256k1::SecretKey;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs::File, io::Read, path::PathBuf};

/// The arguments for the `reth p2p nodeset` command
#[derive(Parser, Debug)]
pub struct Command {
#[clap(subcommand)]
subcommand: Subcommands,
}

#[derive(Serialize, Deserialize, Debug)]
struct Record {
seq: u64,
record: Enr<SecretKey>,
score: i64,
firstResponse: DateTime<Utc>,
lastResponse: DateTime<Utc>,
lastCheck: DateTime<Utc>,
}

impl Command {
/// Execute `p2p nodeset` command
pub fn execute(self) -> eyre::Result<()> {
match self.subcommand {
Subcommands::Info { file } => {
let mut f = File::open(file)?;
let mut content = String::new();
f.read_to_string(&mut content).expect("failed to read file");

let nodes: HashMap<String, Record> =
serde_json::from_str(&content).expect("failed to deserialize json");

for (key, node) in nodes.iter() {
println!("id: {}, node: {:#?}", key, node);
}
}
Subcommands::Filter => {
println!("Filter not implemented yet");
}
}

Ok(())
}
}

#[derive(Subcommand, Debug)]
enum Subcommands {
/// Show statics about a node set
Info {
/// The path of the JSON file used to store node set.
#[arg(long, value_name = "FILE", verbatim_doc_comment)]
file: PathBuf,
},
/// Filters a node set
Filter,
}

0 comments on commit e78ec4f

Please sign in to comment.