}
state_lock.fail_reason = AddressState::TimeoutDuringRequest;
match msg {
- NetworkMessage::Version(ver) => {
+ Some(NetworkMessage::Version(ver)) => {
if ver.start_height < 0 || ver.start_height as u64 > state_lock.request.0 + 1008*2 {
state_lock.fail_reason = AddressState::HighBlockCount;
return future::err(());
return future::err(());
}
},
- NetworkMessage::Verack => {
+ Some(NetworkMessage::Verack) => {
check_set_flag!(recvd_verack, "verack");
if let Err(_) = write.try_send(NetworkMessage::Ping(state_lock.pong_nonce)) {
return future::err(());
}
},
- NetworkMessage::Ping(v) => {
+ Some(NetworkMessage::Ping(v)) => {
if let Err(_) = write.try_send(NetworkMessage::Pong(v)) {
return future::err(())
}
},
- NetworkMessage::Pong(v) => {
+ Some(NetworkMessage::Pong(v)) => {
if v != state_lock.pong_nonce {
state_lock.fail_reason = AddressState::ProtocolViolation;
state_lock.msg = ("due to invalid pong nonce".to_string(), true);
return future::err(());
}
},
- NetworkMessage::Addr(addrs) => {
+ Some(NetworkMessage::Addr(addrs)) => {
if addrs.len() > 1000 {
state_lock.fail_reason = AddressState::ProtocolViolation;
state_lock.msg = (format!("due to oversized addr: {}", addrs.len()), true);
}
unsafe { DATA_STORE.as_ref().unwrap() }.add_fresh_nodes(&addrs);
},
- NetworkMessage::Block(block) => {
+ Some(NetworkMessage::Block(block)) => {
if block != state_lock.request.2 {
state_lock.fail_reason = AddressState::ProtocolViolation;
state_lock.msg = ("due to bad block".to_string(), true);
check_set_flag!(recvd_block, "block");
return future::err(());
},
- NetworkMessage::Inv(invs) => {
+ Some(NetworkMessage::Inv(invs)) => {
for inv in invs {
if inv.inv_type == InvType::Transaction {
state_lock.fail_reason = AddressState::EvilNode;
}
}
},
- NetworkMessage::Tx(_) => {
+ Some(NetworkMessage::Tx(_)) => {
state_lock.fail_reason = AddressState::EvilNode;
state_lock.msg = ("due to unrequested transaction".to_string(), true);
return future::err(());
return future::err(());
}
match msg {
- NetworkMessage::Version(ver) => {
+ Some(NetworkMessage::Version(ver)) => {
if let Err(_) = trusted_write.try_send(NetworkMessage::Verack) {
return future::err(())
}
starting_height = ver.start_height;
},
- NetworkMessage::Verack => {
+ Some(NetworkMessage::Verack) => {
if let Err(_) = trusted_write.try_send(NetworkMessage::SendHeaders) {
return future::err(());
}
return future::err(());
}
},
- NetworkMessage::Addr(addrs) => {
+ Some(NetworkMessage::Addr(addrs)) => {
unsafe { DATA_STORE.as_ref().unwrap() }.add_fresh_nodes(&addrs);
},
- NetworkMessage::Headers(headers) => {
+ Some(NetworkMessage::Headers(headers)) => {
if headers.is_empty() {
return future::ok(());
}
return future::err(())
}
},
- NetworkMessage::Block(block) => {
+ Some(NetworkMessage::Block(block)) => {
let hash = block.header.bitcoin_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");
}
}
},
- NetworkMessage::Ping(v) => {
+ Some(NetworkMessage::Ping(v)) => {
if let Err(_) = trusted_write.try_send(NetworkMessage::Pong(v)) {
return future::err(())
}
struct MsgCoder<'a>(&'a Printer);
impl<'a> codec::Decoder for MsgCoder<'a> {
- type Item = NetworkMessage;
+ type Item = Option<NetworkMessage>;
type Error = encode::Error;
- fn decode(&mut self, bytes: &mut bytes::BytesMut) -> Result<Option<NetworkMessage>, encode::Error> {
+ fn decode(&mut self, bytes: &mut bytes::BytesMut) -> Result<Option<Option<NetworkMessage>>, encode::Error> {
let mut decoder = BytesDecoder {
buf: bytes,
pos: 0
Ok(res) => {
decoder.buf.advance(decoder.pos);
if res.magic == Network::Bitcoin.magic() {
- Ok(Some(res.payload))
+ Ok(Some(Some(res.payload)))
} else {
Err(encode::Error::UnexpectedNetworkMagic {
expected: Network::Bitcoin.magic(),
//XXX(fixthese): self.0.add_line(format!("rust-bitcoin doesn't support {}!", msg), true);
if msg == "gnop" {
Err(e)
- } else { Ok(None) }
+ } else { Ok(Some(None)) }
},
_ => {
self.0.add_line(format!("Error decoding message: {:?}", e), true);
pub struct Peer {}
impl Peer {
- pub fn new(addr: SocketAddr, tor_proxy: &SocketAddr, timeout: Duration, printer: &'static Printer) -> impl Future<Error=(), Item=(mpsc::Sender<NetworkMessage>, impl Stream<Item=NetworkMessage, Error=encode::Error>)> {
+ pub fn new(addr: SocketAddr, tor_proxy: &SocketAddr, timeout: Duration, printer: &'static Printer) -> impl Future<Error=(), Item=(mpsc::Sender<NetworkMessage>, impl Stream<Item=Option<NetworkMessage>, Error=encode::Error>)> {
let connect_timeout = Delay::new(Instant::now() + timeout.clone()).then(|_| {
future::err(std::io::Error::new(std::io::ErrorKind::TimedOut, "timeout reached"))
});