|
| 1 | +// Copyright 2024 The Grin Developers |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/// Relatively self-contained seed health checker |
| 16 | +use std::sync::Arc; |
| 17 | + |
| 18 | +use grin_core::core::hash::Hashed; |
| 19 | +use grin_core::pow::Difficulty; |
| 20 | +use grin_core::{genesis, global}; |
| 21 | +use grin_p2p as p2p; |
| 22 | +use grin_servers::{resolve_dns_to_addrs, MAINNET_DNS_SEEDS, TESTNET_DNS_SEEDS}; |
| 23 | +use std::fs; |
| 24 | +use std::net::{SocketAddr, TcpStream}; |
| 25 | +use std::time::Duration; |
| 26 | + |
| 27 | +use thiserror::Error; |
| 28 | + |
| 29 | +#[derive(Error, Debug)] |
| 30 | +pub enum SeedCheckError { |
| 31 | + #[error("Seed Connect Error {0}")] |
| 32 | + SeedConnectError(String), |
| 33 | + #[error("Grin Store Error {0}")] |
| 34 | + StoreError(String), |
| 35 | +} |
| 36 | + |
| 37 | +impl From<p2p::Error> for SeedCheckError { |
| 38 | + fn from(e: p2p::Error) -> Self { |
| 39 | + SeedCheckError::SeedConnectError(format!("{:?}", e)) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl From<grin_store::lmdb::Error> for SeedCheckError { |
| 44 | + fn from(e: grin_store::lmdb::Error) -> Self { |
| 45 | + SeedCheckError::StoreError(format!("{:?}", e)) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Serialize, Deserialize, Debug)] |
| 50 | +pub struct SeedCheckResults { |
| 51 | + pub mainnet: Vec<SeedCheckResult>, |
| 52 | + pub testnet: Vec<SeedCheckResult>, |
| 53 | +} |
| 54 | + |
| 55 | +impl Default for SeedCheckResults { |
| 56 | + fn default() -> Self { |
| 57 | + Self { |
| 58 | + mainnet: vec![], |
| 59 | + testnet: vec![], |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Debug, Serialize, Deserialize)] |
| 65 | +pub struct SeedCheckResult { |
| 66 | + pub url: String, |
| 67 | + pub dns_resolutions_found: bool, |
| 68 | + pub success: bool, |
| 69 | + pub successful_attempts: Vec<SeedCheckConnectAttempt>, |
| 70 | + pub unsuccessful_attempts: Vec<SeedCheckConnectAttempt>, |
| 71 | +} |
| 72 | + |
| 73 | +impl Default for SeedCheckResult { |
| 74 | + fn default() -> Self { |
| 75 | + Self { |
| 76 | + url: "".into(), |
| 77 | + dns_resolutions_found: false, |
| 78 | + success: false, |
| 79 | + successful_attempts: vec![], |
| 80 | + unsuccessful_attempts: vec![], |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[derive(Debug, Serialize, Deserialize)] |
| 86 | +pub struct SeedCheckConnectAttempt { |
| 87 | + pub ip_addr: String, |
| 88 | + pub handshake_success: bool, |
| 89 | + pub user_agent: Option<String>, |
| 90 | + pub capabilities: Option<String>, |
| 91 | +} |
| 92 | + |
| 93 | +pub fn check_seeds(is_testnet: bool) -> Vec<SeedCheckResult> { |
| 94 | + let mut result = vec![]; |
| 95 | + let (default_seeds, port) = match is_testnet { |
| 96 | + true => (TESTNET_DNS_SEEDS, "13414"), |
| 97 | + false => (MAINNET_DNS_SEEDS, "3414"), |
| 98 | + }; |
| 99 | + |
| 100 | + if is_testnet { |
| 101 | + global::set_local_chain_type(global::ChainTypes::Testnet); |
| 102 | + } |
| 103 | + |
| 104 | + let config = p2p::types::P2PConfig::default(); |
| 105 | + let adapter = Arc::new(p2p::DummyAdapter {}); |
| 106 | + let peers = Arc::new(p2p::Peers::new( |
| 107 | + p2p::store::PeerStore::new(".__grintmp__/peer_store_root").unwrap(), |
| 108 | + adapter, |
| 109 | + config.clone(), |
| 110 | + )); |
| 111 | + |
| 112 | + for s in default_seeds.iter() { |
| 113 | + info!("Checking seed health for {}", s); |
| 114 | + let mut seed_result = SeedCheckResult::default(); |
| 115 | + seed_result.url = s.to_string(); |
| 116 | + let resolved_dns_entries = resolve_dns_to_addrs(&vec![format!("{}:{}", s, port)]); |
| 117 | + if resolved_dns_entries.is_empty() { |
| 118 | + info!("FAIL - No dns entries found for {}", s); |
| 119 | + result.push(seed_result); |
| 120 | + continue; |
| 121 | + } |
| 122 | + seed_result.dns_resolutions_found = true; |
| 123 | + // Check backwards, last contains the latest (at least on my machine!) |
| 124 | + for r in resolved_dns_entries.iter().rev() { |
| 125 | + let res = check_seed_health(*r, is_testnet, &peers); |
| 126 | + if let Ok(p) = res { |
| 127 | + info!( |
| 128 | + "SUCCESS - Performed Handshake with seed for {} at {}. {} - {:?}", |
| 129 | + s, r, p.info.user_agent, p.info.capabilities |
| 130 | + ); |
| 131 | + //info!("{:?}", p); |
| 132 | + seed_result.success = true; |
| 133 | + seed_result |
| 134 | + .successful_attempts |
| 135 | + .push(SeedCheckConnectAttempt { |
| 136 | + ip_addr: r.to_string(), |
| 137 | + handshake_success: true, |
| 138 | + user_agent: Some(p.info.user_agent), |
| 139 | + capabilities: Some(format!("{:?}", p.info.capabilities)), |
| 140 | + }); |
| 141 | + } else { |
| 142 | + seed_result |
| 143 | + .unsuccessful_attempts |
| 144 | + .push(SeedCheckConnectAttempt { |
| 145 | + ip_addr: r.to_string(), |
| 146 | + handshake_success: false, |
| 147 | + user_agent: None, |
| 148 | + capabilities: None, |
| 149 | + }); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if !seed_result.success { |
| 154 | + info!( |
| 155 | + "FAIL - Unable to handshake at any known DNS resolutions for {}", |
| 156 | + s |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + result.push(seed_result); |
| 161 | + } |
| 162 | + |
| 163 | + // Clean up temporary files |
| 164 | + fs::remove_dir_all(".__grintmp__").expect("Unable to delete temporary files"); |
| 165 | + |
| 166 | + result |
| 167 | +} |
| 168 | + |
| 169 | +fn check_seed_health( |
| 170 | + addr: p2p::PeerAddr, |
| 171 | + is_testnet: bool, |
| 172 | + peers: &Arc<p2p::Peers>, |
| 173 | +) -> Result<p2p::Peer, SeedCheckError> { |
| 174 | + let config = p2p::types::P2PConfig::default(); |
| 175 | + let capabilities = p2p::types::Capabilities::default(); |
| 176 | + let genesis_hash = match is_testnet { |
| 177 | + true => genesis::genesis_test().hash(), |
| 178 | + false => genesis::genesis_main().hash(), |
| 179 | + }; |
| 180 | + |
| 181 | + let handshake = p2p::handshake::Handshake::new(genesis_hash, config.clone()); |
| 182 | + |
| 183 | + match TcpStream::connect_timeout(&addr.0, Duration::from_secs(5)) { |
| 184 | + Ok(stream) => { |
| 185 | + let addr = SocketAddr::new(config.host, config.port); |
| 186 | + let total_diff = Difficulty::from_num(1); |
| 187 | + |
| 188 | + let peer = p2p::Peer::connect( |
| 189 | + stream, |
| 190 | + capabilities, |
| 191 | + total_diff, |
| 192 | + p2p::PeerAddr(addr), |
| 193 | + &handshake, |
| 194 | + peers.clone(), |
| 195 | + )?; |
| 196 | + Ok(peer) |
| 197 | + } |
| 198 | + Err(e) => { |
| 199 | + trace!( |
| 200 | + "connect_peer: on {}:{}. Could not connect to {}: {:?}", |
| 201 | + config.host, |
| 202 | + config.port, |
| 203 | + addr, |
| 204 | + e |
| 205 | + ); |
| 206 | + Err(p2p::Error::Connection(e).into()) |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments