X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain.rs;h=316ca12b3b29b1405b3b863abc713838275e4cb3;hb=2e9a6353ff6b189ed4c046c4d2765c044b9cd593;hp=f9de3d4e1b95f23a11d6c2c777db81570aff9c73;hpb=866152d7203c8f11a686d073a2c3f617f9131675;p=dnsseed-rust diff --git a/src/main.rs b/src/main.rs index f9de3d4..316ca12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,13 +12,12 @@ use std::sync::atomic::{Ordering, AtomicBool}; use std::time::{Duration, Instant}; use std::net::{SocketAddr, ToSocketAddrs}; -use bitcoin_hashes::sha256d; - use bitcoin::blockdata::block::Block; use bitcoin::blockdata::constants::genesis_block; -use bitcoin::network::constants::Network; +use bitcoin::hash_types::{BlockHash}; +use bitcoin::network::constants::{Network, ServiceFlags}; use bitcoin::network::message::NetworkMessage; -use bitcoin::network::message_blockdata::{GetHeadersMessage, Inventory, InvType}; +use bitcoin::network::message_blockdata::{GetHeadersMessage, Inventory}; use bitcoin::util::hash::BitcoinHash; use printer::{Printer, Stat}; @@ -31,18 +30,60 @@ use bgp_client::BGPClient; use tokio::prelude::*; use tokio::timer::Delay; -static mut REQUEST_BLOCK: Option>>> = None; -static mut HIGHEST_HEADER: Option>> = None; -static mut HEADER_MAP: Option>>> = None; -static mut HEIGHT_MAP: Option>>> = None; +static mut REQUEST_BLOCK: Option>>> = None; +static mut HIGHEST_HEADER: Option>> = None; +static mut HEADER_MAP: Option>>> = None; +static mut HEIGHT_MAP: Option>>> = None; static mut DATA_STORE: Option> = None; static mut PRINTER: Option> = None; static mut TOR_PROXY: Option = None; pub static START_SHUTDOWN: AtomicBool = AtomicBool::new(false); static SCANNING: AtomicBool = AtomicBool::new(false); + +use std::alloc::{GlobalAlloc, Layout, System}; +use std::ptr; +use std::sync::atomic::AtomicUsize; + +// We keep track of all memory allocated by Rust code, refusing new allocations if it exceeds +// 1.75GB. +// +// Note that while Rust's std, in general, should panic in response to a null allocation, it +// is totally conceivable that some code will instead dereference this null pointer, which +// would violate our guarantees that Rust modules should never crash the entire application. +// +// In the future, as upstream Rust explores a safer allocation API (eg the Alloc API which +// returns Results instead of raw pointers, or redefining the GlobalAlloc API to allow +// panic!()s inside of alloc calls), we should switch to those, however these APIs are +// currently unstable. +const TOTAL_MEM_LIMIT_BYTES: usize = (1024 + 756) * 1024 * 1024; +static TOTAL_MEM_ALLOCD: AtomicUsize = AtomicUsize::new(0); +struct MemoryLimitingAllocator; +unsafe impl GlobalAlloc for MemoryLimitingAllocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + let len = layout.size(); + if len > TOTAL_MEM_LIMIT_BYTES { + return ptr::null_mut(); + } + if TOTAL_MEM_ALLOCD.fetch_add(len, Ordering::AcqRel) + len > TOTAL_MEM_LIMIT_BYTES { + TOTAL_MEM_ALLOCD.fetch_sub(len, Ordering::AcqRel); + return ptr::null_mut(); + } + System.alloc(layout) + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + System.dealloc(ptr, layout); + TOTAL_MEM_ALLOCD.fetch_sub(layout.size(), Ordering::AcqRel); + } +} + +#[global_allocator] +static ALLOC: MemoryLimitingAllocator = MemoryLimitingAllocator; + + struct PeerState { - request: Arc<(u64, sha256d::Hash, Block)>, + request: Arc<(u64, BlockHash, Block)>, node_services: u64, msg: (String, bool), fail_reason: AddressState, @@ -125,7 +166,7 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) { state_lock.fail_reason = AddressState::LowVersion; return future::err(()); } - if ver.services & (1 | (1 << 10)) == 0 { + if !ver.services.has(ServiceFlags::NETWORK) && !ver.services.has(ServiceFlags::NETWORK_LIMITED) { state_lock.msg = (format!("({}: services {:x})", safe_ua, ver.services), true); state_lock.fail_reason = AddressState::NotFullNode; return future::err(()); @@ -136,7 +177,7 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) { return future::err(()); } check_set_flag!(recvd_version, "version"); - state_lock.node_services = ver.services; + state_lock.node_services = ver.services.as_u64(); state_lock.msg = (format!("(subver: {})", safe_ua), false); if let Err(_) = write.try_send(NetworkMessage::Verack) { return future::err(()); @@ -173,10 +214,7 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) { } if addrs.len() > 10 { if !state_lock.recvd_addrs { - if let Err(_) = write.try_send(NetworkMessage::GetData(vec![Inventory { - inv_type: InvType::WitnessBlock, - hash: state_lock.request.1, - }])) { + if let Err(_) = write.try_send(NetworkMessage::GetData(vec![Inventory::WitnessBlock(state_lock.request.1)])) { return future::err(()); } } @@ -195,10 +233,13 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) { }, Some(NetworkMessage::Inv(invs)) => { for inv in invs { - if inv.inv_type == InvType::Transaction { - state_lock.fail_reason = AddressState::EvilNode; - state_lock.msg = ("due to unrequested inv tx".to_string(), true); - return future::err(()); + match inv { + Inventory::Transaction(_) | Inventory::WitnessTransaction(_) => { + state_lock.fail_reason = AddressState::EvilNode; + state_lock.msg = ("due to unrequested inv tx".to_string(), true); + return future::err(()); + } + _ => {}, } } }, @@ -359,10 +400,9 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr, bgp_client: Arc) { printer.set_stat(printer::Stat::HeaderCount(top_height)); if top_height >= starting_height as u64 { - if let Err(_) = trusted_write.try_send(NetworkMessage::GetData(vec![Inventory { - inv_type: InvType::WitnessBlock, - hash: height_map.get(&(top_height - 216)).unwrap().clone(), - }])) { + if let Err(_) = trusted_write.try_send(NetworkMessage::GetData(vec![ + Inventory::WitnessBlock(height_map.get(&(top_height - 216)).unwrap().clone()) + ])) { return future::err(()); } } @@ -424,7 +464,7 @@ fn main() { unsafe { REQUEST_BLOCK = Some(Box::new(Mutex::new(Arc::new((0, genesis_block(Network::Bitcoin).bitcoin_hash(), genesis_block(Network::Bitcoin)))))) }; let trt = tokio::runtime::Builder::new() - .blocking_threads(2).core_threads(num_cpus::get().max(1) * 3) + .blocking_threads(2).core_threads(num_cpus::get().max(1) + 1) .build().unwrap(); let _ = trt.block_on_all(future::lazy(|| {