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;
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) => { {
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(());
}
}
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;
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(())
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 {
}
},
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 {
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)