     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64

    use std::io;
    use futures::{Future, Poll, Async};
    use tokio_io::AsyncRead;
    use tokio_io::io::{ReadExact, read_exact};
    use message::{MessageHeader, MessageResult};
    use network::Magic;

    pub fn read_header<A>(a: A, magic: Magic) -> ReadHeader<A> where A: AsyncRead {
    	ReadHeader {
    		reader: read_exact(a, [0u8; 24]),
    		magic: magic,
    	}
    }

    pub struct ReadHeader<A> {
    	reader: ReadExact<A, [u8; 24]>,
    	magic: Magic,
    }

    impl<A> Future for ReadHeader<A> where A: AsyncRead {
    	type Item = (A, MessageResult<MessageHeader>);
    	type Error = io::Error;

    	fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
    		let (read, data) = try_ready!(self.reader.poll());
    		let header = MessageHeader::deserialize(&data, self.magic);
    		Ok(Async::Ready((read, header)))
    	}
    }

    #[cfg(test)]
    mod tests {
    	use futures::Future;
    	use bytes::Bytes;
    	use network::{Network, ConsensusFork};
    	use message::{MessageHeader, Error};
    	use super::read_header;

    	#[test]
    	fn test_read_header() {
    		let raw: Bytes = "f9beb4d96164647200000000000000001f000000ed52399b".into();
    		let expected = MessageHeader {
    			magic: Network::Mainnet.magic(&ConsensusFork::BitcoinCore),
    			command: "addr".into(),
    			len: 0x1f,
    			checksum: "ed52399b".into(),
    		};

    		assert_eq!(read_header(raw.as_ref(), Network::Mainnet.magic(&ConsensusFork::BitcoinCore)).wait().unwrap().1, Ok(expected));
    		assert_eq!(read_header(raw.as_ref(), Network::Testnet.magic(&ConsensusFork::BitcoinCore)).wait().unwrap().1, Err(Error::InvalidMagic));
    	}

    	#[test]
    	fn test_read_header_with_invalid_magic() {
    		let raw: Bytes = "f9beb4d86164647200000000000000001f000000ed52399b".into();
    		assert_eq!(read_header(raw.as_ref(), Network::Testnet.magic(&ConsensusFork::BitcoinCore)).wait().unwrap().1, Err(Error::InvalidMagic));
    	}

    	#[test]
    	fn test_read_too_short_header() {
    		let raw: Bytes = "f9beb4d96164647200000000000000001f000000ed5239".into();
    		assert!(read_header(raw.as_ref(), Network::Mainnet.magic(&ConsensusFork::BitcoinCore)).wait().is_err());
    	}
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)