]> git.bitcoin.ninja Git - dnsseed-rust/commitdiff
Bump rust-bitcoin, add addrv2
authorMatt Corallo <git@bluematt.me>
Thu, 25 Feb 2021 05:47:57 +0000 (00:47 -0500)
committerMatt Corallo <git@bluematt.me>
Thu, 25 Feb 2021 05:47:57 +0000 (00:47 -0500)
Cargo.toml
src/datastore.rs
src/main.rs
src/peer.rs

index 9709bfbf9b090f6990d506a6c489bbe0d14c4854..7240bd1b9a19fda149aedb9545a9c81765a98599 100644 (file)
@@ -5,7 +5,7 @@ authors = ["matt"]
 edition = "2018"
 
 [dependencies]
-bitcoin = "0.23"
+bitcoin = "0.26"
 bgp-rs = "0.6"
 tokio = "0.1"
 bytes = "0.4"
index 90ebbd923585b74b340bdd21a7784b6493d2b034..27987714d347f566ea79d662f6bc4343b9c314d8 100644 (file)
@@ -5,7 +5,7 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV
 use std::time::{Duration, Instant};
 use std::io::{BufRead, BufReader};
 
-use bitcoin::network::address::Address;
+use bitcoin::network::address::{Address, AddrV2Message};
 
 use rand::thread_rng;
 use rand::seq::{SliceRandom, IteratorRandom};
@@ -380,6 +380,14 @@ impl Store {
                        }
                }));
        }
+       pub fn add_fresh_nodes_v2(&self, addresses: &Vec<AddrV2Message>) {
+               self.add_fresh_addrs(addresses.iter().filter_map(|addr| {
+                       match addr.socket_addr() {
+                               Ok(socketaddr) => Some(socketaddr),
+                               Err(_) => None, // TODO: Handle onions
+                       }
+               }));
+       }
 
        pub fn set_node_state(&self, sockaddr: SocketAddr, state: AddressState, services: u64) -> AddressState {
                let addr: SockAddr = sockaddr.into();
index 96a74fd76c0501c025674bba0e8e04eeb0f8b4f2..288cdd24d229168e09ee270e9fdb988b430cf23a 100644 (file)
@@ -18,7 +18,7 @@ use bitcoin::hash_types::{BlockHash};
 use bitcoin::network::constants::{Network, ServiceFlags};
 use bitcoin::network::message::NetworkMessage;
 use bitcoin::network::message_blockdata::{GetHeadersMessage, Inventory};
-use bitcoin::util::hash::BitcoinHash;
+//use bitcoin::util::hash::BitcoinHash;
 
 use printer::{Printer, Stat};
 use peer::Peer;
@@ -122,19 +122,8 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) {
                Peer::new(node.clone(), unsafe { TOR_PROXY.as_ref().unwrap() }, Duration::from_secs(timeout), printer)
        });
        tokio::spawn(peer.and_then(move |(mut write, read)| {
-               TimeoutStream::new_timeout(read, scan_time + Duration::from_secs(store.get_u64(U64Setting::RunTimeout))).map_err(move |err| {
-                       match err {
-                               bitcoin::consensus::encode::Error::UnrecognizedNetworkCommand(ref msg) => {
-                                       // If we got here, we hit one of the explicitly disallowed messages indicating
-                                       // a bogus "node".
-                                       let mut state_lock = err_peer_state.lock().unwrap();
-                                       state_lock.msg = (format!("(bad msg type {})", msg), true);
-                                       state_lock.fail_reason = AddressState::EvilNode;
-                               },
-                               _ => {},
-                       }
-                       ()
-               }).for_each(move |msg| {
+               TimeoutStream::new_timeout(read, scan_time + Duration::from_secs(store.get_u64(U64Setting::RunTimeout)))
+                       .map_err(|_| ()).for_each(move |msg| {
                        let mut state_lock = peer_state.lock().unwrap();
                        macro_rules! check_set_flag {
                                ($recvd_flag: ident, $msg: expr) => { {
@@ -179,6 +168,9 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) {
                                        check_set_flag!(recvd_version, "version");
                                        state_lock.node_services = ver.services.as_u64();
                                        state_lock.msg = (format!("(subver: {})", safe_ua), false);
+                                       if let Err(_) = write.try_send(NetworkMessage::SendAddrV2) {
+                                               return future::err(());
+                                       }
                                        if let Err(_) = write.try_send(NetworkMessage::Verack) {
                                                return future::err(());
                                        }
@@ -222,6 +214,23 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) {
                                        }
                                        unsafe { DATA_STORE.as_ref().unwrap() }.add_fresh_nodes(&addrs);
                                },
+                               Some(NetworkMessage::AddrV2(addrs)) => {
+                                       if addrs.len() > 1000 {
+                                               state_lock.fail_reason = AddressState::ProtocolViolation;
+                                               state_lock.msg = (format!("due to oversized addr: {}", addrs.len()), true);
+                                               state_lock.recvd_addrs = false;
+                                               return future::err(());
+                                       }
+                                       if addrs.len() > 10 {
+                                               if !state_lock.recvd_addrs {
+                                                       if let Err(_) = write.try_send(NetworkMessage::GetData(vec![Inventory::WitnessBlock(state_lock.request.1)])) {
+                                                               return future::err(());
+                                                       }
+                                               }
+                                               state_lock.recvd_addrs = true;
+                                       }
+                                       unsafe { DATA_STORE.as_ref().unwrap() }.add_fresh_nodes_v2(&addrs);
+                               },
                                Some(NetworkMessage::Block(block)) => {
                                        if block != state_lock.request.2 {
                                                state_lock.fail_reason = AddressState::ProtocolViolation;
@@ -248,6 +257,14 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) {
                                        state_lock.msg = ("due to unrequested transaction".to_string(), true);
                                        return future::err(());
                                },
+                               Some(NetworkMessage::Unknown { command, .. }) => {
+                                       if command.as_ref() == "gnop" {
+                                               let mut state_lock = err_peer_state.lock().unwrap();
+                                               state_lock.msg = (format!("(bad msg type {})", command), true);
+                                               state_lock.fail_reason = AddressState::EvilNode;
+                                               return future::err(());
+                                       }
+                               },
                                _ => {},
                        }
                        future::ok(())
@@ -386,17 +403,17 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr, bgp_client: Arc<BGPClient>) {
 
                                        if let Some(height) = header_map.get(&headers[0].prev_blockhash).cloned() {
                                                for i in 0..headers.len() {
-                                                       let hash = headers[i].bitcoin_hash();
+                                                       let hash = headers[i].block_hash();
                                                        if i < headers.len() - 1 && headers[i + 1].prev_blockhash != hash {
                                                                return future::err(());
                                                        }
-                                                       header_map.insert(headers[i].bitcoin_hash(), height + 1 + (i as u64));
-                                                       height_map.insert(height + 1 + (i as u64), headers[i].bitcoin_hash());
+                                                       header_map.insert(headers[i].block_hash(), height + 1 + (i as u64));
+                                                       height_map.insert(height + 1 + (i as u64), headers[i].block_hash());
                                                }
 
                                                let top_height = height + headers.len() as u64;
                                                *unsafe { HIGHEST_HEADER.as_ref().unwrap() }.lock().unwrap()
-                                                       = (headers.last().unwrap().bitcoin_hash(), top_height);
+                                                       = (headers.last().unwrap().block_hash(), top_height);
                                                printer.set_stat(printer::Stat::HeaderCount(top_height));
 
                                                if top_height >= starting_height as u64 {
@@ -419,7 +436,7 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr, bgp_client: Arc<BGPClient>) {
                                        }
                                },
                                Some(NetworkMessage::Block(block)) => {
-                                       let hash = block.header.bitcoin_hash();
+                                       let hash = block.block_hash();
                                        let header_map = unsafe { HEADER_MAP.as_ref().unwrap() }.lock().unwrap();
                                        let height = *header_map.get(&hash).expect("Got loose block from trusted peer we coulnd't have requested");
                                        if height == unsafe { HIGHEST_HEADER.as_ref().unwrap() }.lock().unwrap().1 - 216 {
@@ -458,10 +475,10 @@ fn main() {
 
        unsafe { HEADER_MAP = Some(Box::new(Mutex::new(HashMap::with_capacity(600000)))) };
        unsafe { HEIGHT_MAP = Some(Box::new(Mutex::new(HashMap::with_capacity(600000)))) };
-       unsafe { HEADER_MAP.as_ref().unwrap() }.lock().unwrap().insert(genesis_block(Network::Bitcoin).bitcoin_hash(), 0);
-       unsafe { HEIGHT_MAP.as_ref().unwrap() }.lock().unwrap().insert(0, genesis_block(Network::Bitcoin).bitcoin_hash());
-       unsafe { HIGHEST_HEADER = Some(Box::new(Mutex::new((genesis_block(Network::Bitcoin).bitcoin_hash(), 0)))) };
-       unsafe { REQUEST_BLOCK = Some(Box::new(Mutex::new(Arc::new((0, genesis_block(Network::Bitcoin).bitcoin_hash(), genesis_block(Network::Bitcoin)))))) };
+       unsafe { HEADER_MAP.as_ref().unwrap() }.lock().unwrap().insert(genesis_block(Network::Bitcoin).block_hash(), 0);
+       unsafe { HEIGHT_MAP.as_ref().unwrap() }.lock().unwrap().insert(0, genesis_block(Network::Bitcoin).block_hash());
+       unsafe { HIGHEST_HEADER = Some(Box::new(Mutex::new((genesis_block(Network::Bitcoin).block_hash(), 0)))) };
+       unsafe { REQUEST_BLOCK = Some(Box::new(Mutex::new(Arc::new((0, genesis_block(Network::Bitcoin).block_hash(), genesis_block(Network::Bitcoin)))))) };
 
        let trt = tokio::runtime::Builder::new()
                .blocking_threads(2).core_threads(num_cpus::get().max(1) + 1)
index ff0d502191dd5fa3b7eb00f7439b805c2631691a..4e81175e0f3baa582ec8b7cd064d2e9693c52557 100644 (file)
@@ -67,13 +67,6 @@ impl<'a> codec::Decoder for MsgCoder<'a> {
                        },
                        Err(e) => match e {
                                encode::Error::Io(_) => Ok(None),
-                               encode::Error::UnrecognizedNetworkCommand(ref msg) => {
-                                       decoder.buf.advance(decoder.pos);
-                                       //XXX(fixthese): self.0.add_line(format!("rust-bitcoin doesn't support {}!", msg), true);
-                                       if msg == "gnop" {
-                                               Err(e)
-                                       } else { Ok(Some(None)) }
-                               },
                                _ => {
                                        self.0.add_line(format!("Error decoding message: {:?}", e), true);
                                        Err(e)