     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
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77

    use hex::FromHex;
    use hash::H256;
    use ser::{deserialize};
    use merkle_root::merkle_root;
    use {BlockHeader, Transaction};
    use super::RepresentH256;

    #[derive(Debug, PartialEq, Clone, Serializable, Deserializable)]
    pub struct Block {
    	pub block_header: BlockHeader,
    	pub transactions: Vec<Transaction>,
    }

    impl From<&'static str> for Block {
    	fn from(s: &'static str) -> Self {
    		deserialize(&s.from_hex::<Vec<u8>>().unwrap() as &[u8]).unwrap()
    	}
    }

    impl RepresentH256 for Block {
    	fn h256(&self) -> H256 { self.hash() }
    }

    impl Block {
    	pub fn new(header: BlockHeader, transactions: Vec<Transaction>) -> Self {
    		Block { block_header: header, transactions: transactions }
    	}

    	/// Returns block's merkle root.
    	pub fn merkle_root(&self) -> H256 {
    		let hashes = self.transactions.iter().map(Transaction::hash).collect::<Vec<H256>>();
    		merkle_root(&hashes)
    	}

    	/// Returns block's witness merkle root.
    	pub fn witness_merkle_root(&self) -> H256 {
    		let hashes = match self.transactions.split_first() {
    			None => vec![],
    			Some((_, rest)) => {
    				let mut hashes = vec![H256::from(0)];
    				hashes.extend(rest.iter().map(Transaction::witness_hash));
    				hashes
    			},
    		};
    		merkle_root(&hashes)
    	}

    	pub fn transactions(&self) -> &[Transaction] {
    		&self.transactions
    	}

    	pub fn header(&self) -> &BlockHeader {
    		&self.block_header
    	}

    	pub fn hash(&self) -> H256 {
    		self.block_header.hash()
    	}
    }

    #[cfg(test)]
    mod tests {
    	use hash::H256;
    	use super::Block;

    	// Block 80000
    	// https://blockchain.info/rawblock/000000000043a8c0fd1d6f726790caa2a406010d19efd2780db27bdbbd93baf6
    	// https://blockchain.info/rawblock/000000000043a8c0fd1d6f726790caa2a406010d19efd2780db27bdbbd93baf6?format=hex
    	#[test]
    	fn test_block_merkle_root_and_hash() {
    		let block: Block = " ".into();
    		let merkle_root = H256::from_reversed_str("8fb300e3fdb6f30a4c67233b997f99fdd518b968b9a3fd65857bfe78b2600719");
    		let hash = H256::from_reversed_str("000000000043a8c0fd1d6f726790caa2a406010d19efd2780db27bdbbd93baf6");
    		assert_eq!(block.merkle_root(), merkle_root);
    		assert_eq!(block.hash(), hash);
    	}
    }

# 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`)