      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
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196

    use std::collections::HashSet;
    use chain::IndexedBlock;
    use network::ConsensusFork;
    use sigops::transaction_sigops;
    use duplex_store::NoopStore;
    use error::{Error, TransactionError};

    pub struct BlockVerifier<'a> {
    	pub empty: BlockEmpty<'a>,
    	pub coinbase: BlockCoinbase<'a>,
    	pub serialized_size: BlockSerializedSize<'a>,
    	pub extra_coinbases: BlockExtraCoinbases<'a>,
    	pub transactions_uniqueness: BlockTransactionsUniqueness<'a>,
    	pub sigops: BlockSigops<'a>,
    	pub merkle_root: BlockMerkleRoot<'a>,
    }

    impl<'a> BlockVerifier<'a> {
    	pub fn new(block: &'a IndexedBlock) -> Self {
    		BlockVerifier {
    			empty: BlockEmpty::new(block),
    			coinbase: BlockCoinbase::new(block),
    			serialized_size: BlockSerializedSize::new(block, ConsensusFork::absolute_maximum_block_size()),
    			extra_coinbases: BlockExtraCoinbases::new(block),
    			transactions_uniqueness: BlockTransactionsUniqueness::new(block),
    			sigops: BlockSigops::new(block, ConsensusFork::absolute_maximum_block_sigops()),
    			merkle_root: BlockMerkleRoot::new(block),
    		}
    	}

    	pub fn check(&self) -> Result<(), Error> {
    		try!(self.empty.check());
    		try!(self.coinbase.check());
    		try!(self.serialized_size.check());
    		try!(self.extra_coinbases.check());
    		try!(self.transactions_uniqueness.check());
    		try!(self.sigops.check());
    		try!(self.merkle_root.check());
    		Ok(())
    	}
    }

    pub struct BlockEmpty<'a> {
    	block: &'a IndexedBlock,
    }

    impl<'a> BlockEmpty<'a> {
    	fn new(block: &'a IndexedBlock) -> Self {
    		BlockEmpty {
    			block: block,
    		}
    	}

    	fn check(&self) -> Result<(), Error> {
    		if self.block.transactions.is_empty() {
    			Err(Error::Empty)
    		} else {
    			Ok(())
    		}
    	}
    }

    pub struct BlockSerializedSize<'a> {
    	block: &'a IndexedBlock,
    	max_size: usize,
    }

    impl<'a> BlockSerializedSize<'a> {
    	fn new(block: &'a IndexedBlock, max_size: usize) -> Self {
    		BlockSerializedSize {
    			block: block,
    			max_size: max_size,
    		}
    	}

    	fn check(&self) -> Result<(), Error> {
    		let size = self.block.size();
    		if size > self.max_size {
    			Err(Error::Size(size))
    		} else {
    			Ok(())
    		}
    	}
    }

    pub struct BlockCoinbase<'a> {
    	block: &'a IndexedBlock,
    }

    impl<'a> BlockCoinbase<'a> {
    	fn new(block: &'a IndexedBlock) -> Self {
    		BlockCoinbase {
    			block: block,
    		}
    	}

    	fn check(&self) -> Result<(), Error> {
    		if self.block.transactions.first().map(|tx| tx.raw.is_coinbase()).unwrap_or(false) {
    			Ok(())
    		} else {
    			Err(Error::Coinbase)
    		}
    	}
    }

    pub struct BlockExtraCoinbases<'a> {
    	block: &'a IndexedBlock,
    }

    impl<'a> BlockExtraCoinbases<'a> {
    	fn new(block: &'a IndexedBlock) -> Self {
    		BlockExtraCoinbases {
    			block: block,
    		}
    	}

    	fn check(&self) -> Result<(), Error> {
    		let misplaced = self.block.transactions.iter()
    			.skip(1)
    			.position(|tx| tx.raw.is_coinbase());

    		match misplaced {
    			Some(index) => Err(Error::Transaction(index + 1, TransactionError::MisplacedCoinbase)),
    			None => Ok(()),
    		}
    	}
    }

    pub struct BlockTransactionsUniqueness<'a> {
    	block: &'a IndexedBlock,
    }

    impl<'a> BlockTransactionsUniqueness<'a> {
    	fn new(block: &'a IndexedBlock) -> Self {
    		BlockTransactionsUniqueness {
    			block: block,
    		}
    	}

    	fn check(&self) -> Result<(), Error> {
    		let hashes = self.block.transactions.iter().map(|tx| tx.hash.clone()).collect::<HashSet<_>>();
    		if hashes.len() == self.block.transactions.len() {
    			Ok(())
    		} else {
    			Err(Error::DuplicatedTransactions)
    		}
    	}
    }

    pub struct BlockSigops<'a> {
    	block: &'a IndexedBlock,
    	max_sigops: usize,
    }

    impl<'a> BlockSigops<'a> {
    	fn new(block: &'a IndexedBlock, max_sigops: usize) -> Self {
    		BlockSigops {
    			block: block,
    			max_sigops: max_sigops,
    		}
    	}

    	fn check(&self) -> Result<(), Error> {
    		// We cannot know if bip16 is enabled at this point so we disable it.
    		let sigops = self.block.transactions.iter()
    			.map(|tx| transaction_sigops(&tx.raw, &NoopStore, false, false))
    			.sum::<usize>();

    		if sigops > self.max_sigops {
    			Err(Error::MaximumSigops)
    		} else {
    			Ok(())
    		}
    	}
    }

    pub struct BlockMerkleRoot<'a> {
    	block: &'a IndexedBlock,
    }

    impl<'a> BlockMerkleRoot<'a> {
    	fn new(block: &'a IndexedBlock) -> Self {
    		BlockMerkleRoot {
    			block: block,
    		}
    	}

    	fn check(&self) -> Result<(), Error> {
    		if self.block.merkle_root() == self.block.header.raw.merkle_root_hash {
    			Ok(())
    		} else {
    			Err(Error::MerkleRoot)
    		}
    	}
    }

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