Skip to content

Commit

Permalink
rework check to delete temp files, add output options, testing
Browse files Browse the repository at this point in the history
  • Loading branch information
yeastplume committed Nov 26, 2024
1 parent 0840f2c commit 2ffc6d1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
20 changes: 13 additions & 7 deletions src/bin/grin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern crate clap;
extern crate log;
use crate::config::config::SERVER_CONFIG_FILE_NAME;
use crate::core::global;
use crate::tools::check_seeds;
use crate::util::init_logger;
use clap::App;
use futures::channel::oneshot;
Expand All @@ -32,6 +33,8 @@ use grin_p2p as p2p;
use grin_servers as servers;
use grin_util as util;
use grin_util::logger::LogEntry;
use std::fs::File;
use std::io::Write;
use std::sync::mpsc;

#[macro_use]
Expand Down Expand Up @@ -205,13 +208,16 @@ fn real_main() -> i32 {
// seedcheck command
("seedcheck", Some(seedcheck_args)) => {
let is_testnet = seedcheck_args.is_present("testnet");
let results = tools::check_seeds(is_testnet);
println!(
"Generating seed check report, testnet={}. (This make take a few minutes)",
is_testnet
);
for result in results {
println!("{}", serde_json::to_string_pretty(&result).unwrap());
let results = check_seeds(is_testnet);
let output =
serde_json::to_string_pretty(&results).expect("Unable to serialize results");

if let Some(output_file) = seedcheck_args.value_of("output") {
let mut file = File::create(output_file).expect("Unable to create file");
writeln!(file, "{}", output).expect("Unable to write data");
println!("Results written to {}", output_file);
} else {
println!("{}", output);
}
0
}
Expand Down
4 changes: 4 additions & 0 deletions src/bin/grin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ subcommands:
help: Run seed check against Testnet (as opposed to Mainnet)
long: testnet
takes_value: false
- output:
help: Output file to write the results to
long: output
takes_value: true
31 changes: 21 additions & 10 deletions src/bin/tools/seedcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use grin_core::pow::Difficulty;
use grin_core::{genesis, global};
use grin_p2p as p2p;
use grin_servers::{resolve_dns_to_addrs, MAINNET_DNS_SEEDS, TESTNET_DNS_SEEDS};
use std::fs;
use std::net::{SocketAddr, TcpStream};
use std::time::Duration;

Expand Down Expand Up @@ -100,6 +101,14 @@ pub fn check_seeds(is_testnet: bool) -> Vec<SeedCheckResult> {
global::set_local_chain_type(global::ChainTypes::Testnet);
}

let config = p2p::types::P2PConfig::default();
let adapter = Arc::new(p2p::DummyAdapter {});
let peers = Arc::new(p2p::Peers::new(
p2p::store::PeerStore::new(".__grintmp__/peer_store_root").unwrap(),
adapter,
config.clone(),
));

for s in default_seeds.iter() {
info!("Checking seed health for {}", s);
let mut seed_result = SeedCheckResult::default();
Expand All @@ -113,7 +122,7 @@ pub fn check_seeds(is_testnet: bool) -> Vec<SeedCheckResult> {
seed_result.dns_resolutions_found = true;
// Check backwards, last contains the latest (at least on my machine!)
for r in resolved_dns_entries.iter().rev() {
let res = check_seed_health(*r, is_testnet);
let res = check_seed_health(*r, is_testnet, &peers);
if let Ok(p) = res {
info!(
"SUCCESS - Performed Handshake with seed for {} at {}. {} - {:?}",
Expand Down Expand Up @@ -150,18 +159,20 @@ pub fn check_seeds(is_testnet: bool) -> Vec<SeedCheckResult> {

result.push(seed_result);
}

// Clean up temporary files
fs::remove_dir_all(".__grintmp__").expect("Unable to delete temporary files");

result
}

fn check_seed_health(addr: p2p::PeerAddr, is_testnet: bool) -> Result<p2p::Peer, SeedCheckError> {
let capabilities = p2p::types::Capabilities::default();
fn check_seed_health(
addr: p2p::PeerAddr,
is_testnet: bool,
peers: &Arc<p2p::Peers>,
) -> Result<p2p::Peer, SeedCheckError> {
let config = p2p::types::P2PConfig::default();
let adapter = Arc::new(p2p::DummyAdapter {});
let peers = Arc::new(p2p::Peers::new(
p2p::store::PeerStore::new("peer_store_root")?,
adapter,
config.clone(),
));
let capabilities = p2p::types::Capabilities::default();
let genesis_hash = match is_testnet {
true => genesis::genesis_test().hash(),
false => genesis::genesis_main().hash(),
Expand All @@ -180,7 +191,7 @@ fn check_seed_health(addr: p2p::PeerAddr, is_testnet: bool) -> Result<p2p::Peer,
total_diff,
p2p::PeerAddr(addr),
&handshake,
peers,
peers.clone(),
)?;
Ok(peer)
}
Expand Down

0 comments on commit 2ffc6d1

Please sign in to comment.