      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
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444

    use std::collections::VecDeque;
    use std::sync::Arc;
    use std::sync::mpsc::{channel, Sender, Receiver};
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::thread;
    use parking_lot::Mutex;
    use time::get_time;
    use chain::{IndexedBlock, IndexedTransaction};
    use network::ConsensusParams;
    use primitives::hash::H256;
    use verification::{BackwardsCompatibleChainVerifier as ChainVerifier, Verify as VerificationVerify,
    	Error as VerificationError, VerificationLevel};
    use types::{BlockHeight, StorageRef, MemoryPoolRef};
    use utils::MemoryPoolTransactionOutputProvider;
    use VerificationParameters;

    /// Block verification events sink
    pub trait BlockVerificationSink : Send + Sync + 'static {
    	/// When block verification has completed successfully.
    	fn on_block_verification_success(&self, block: IndexedBlock) -> Option<Vec<VerificationTask>>;
    	/// When block verification has failed.
    	fn on_block_verification_error(&self, err: &str, hash: &H256);
    }

    /// Transaction verification events sink
    pub trait TransactionVerificationSink : Send + Sync + 'static {
    	/// When transaction verification has completed successfully.
    	fn on_transaction_verification_success(&self, transaction: IndexedTransaction);
    	/// When transaction verification has failed.
    	fn on_transaction_verification_error(&self, err: &str, hash: &H256);
    }

    /// Verification events sink
    pub trait VerificationSink : BlockVerificationSink + TransactionVerificationSink {
    }

    /// Verification thread tasks
    #[derive(Debug)]
    pub enum VerificationTask {
    	/// Verify single block
    	VerifyBlock(IndexedBlock),
    	/// Verify single transaction
    	VerifyTransaction(BlockHeight, IndexedTransaction),
    	/// Stop verification thread
    	Stop,
    }

    /// Synchronization verifier
    pub trait Verifier : Send + Sync + 'static {
    	/// Verify block
    	fn verify_block(&self, block: IndexedBlock);
    	/// Verify transaction
    	fn verify_transaction(&self, height: BlockHeight, transaction: IndexedTransaction);
    }

    /// Asynchronous synchronization verifier
    pub struct AsyncVerifier {
    	/// Verification work transmission channel.
    	verification_work_sender: Mutex<Sender<VerificationTask>>,
    	/// Verification thread.
    	verification_worker_thread: Option<thread::JoinHandle<()>>,
    }

    /// Chain verifier wrapper to deal with verification parameters.
    pub struct ChainVerifierWrapper {
    	/// Original verifier.
    	pub verifier: Arc<ChainVerifier>,
    	/// Verification parameters.
    	verification_params: VerificationParameters,
    	/// Is verification edge passed.
    	pub enforce_full_verification: AtomicBool,
    }

    impl ChainVerifierWrapper {
    	/// Create new chain verifier wrapper.
    	pub fn new(verifier: Arc<ChainVerifier>, storage: &StorageRef, verification_params: VerificationParameters) -> Self {
    		let enforce_full_verification = AtomicBool::new(storage.contains_block(verification_params.verification_edge.clone().into()));
    		ChainVerifierWrapper {
    			verifier: verifier,
    			verification_params: verification_params,
    			enforce_full_verification: enforce_full_verification,
    		}
    	}

    	/// Verify block.
    	pub fn verify_block(&self, block: &IndexedBlock) -> Result<(), VerificationError> {
    		let enforce_full_verification = if block.hash() == &self.verification_params.verification_edge {
    			self.enforce_full_verification.store(true, Ordering::Relaxed);
    			true
    		} else {
    			self.enforce_full_verification.load(Ordering::Relaxed)
    		};
    		let verification_level = if enforce_full_verification {
    			VerificationLevel::Full
    		} else {
    			self.verification_params.verification_level
    		};

    		self.verifier.verify(verification_level, block)
    	}
    }

    impl VerificationTask {
    	/// Returns transaction reference if it is transaction verification task
    	pub fn transaction(&self) -> Option<&IndexedTransaction> {
    		match *self {
    			VerificationTask::VerifyTransaction(_, ref transaction) => Some(transaction),
    			_ => None,
    		}
    	}
    }

    impl AsyncVerifier {
    	/// Create new async verifier
    	pub fn new<T: VerificationSink>(verifier: Arc<ChainVerifier>, storage: StorageRef, memory_pool: MemoryPoolRef, sink: Arc<T>, verification_params: VerificationParameters) -> Self {
    		let (verification_work_sender, verification_work_receiver) = channel();
    		AsyncVerifier {
    			verification_work_sender: Mutex::new(verification_work_sender),
    			verification_worker_thread: Some(thread::Builder::new()
    				.name("Sync verification thread".to_string())
    				.spawn(move || {
    					let verifier = ChainVerifierWrapper::new(verifier, &storage, verification_params);
    					AsyncVerifier::verification_worker_proc(sink, storage, memory_pool, verifier, verification_work_receiver)
    				})
    				.expect("Error creating sync verification thread"))
    		}
    	}

    	/// Thread procedure for handling verification tasks
    	fn verification_worker_proc<T: VerificationSink>(sink: Arc<T>, storage: StorageRef, memory_pool: MemoryPoolRef, verifier: ChainVerifierWrapper, work_receiver: Receiver<VerificationTask>) {
    		while let Ok(task) = work_receiver.recv() {
    			if !AsyncVerifier::execute_single_task(&sink, &storage, &memory_pool, &verifier, task) {
    				break;
    			}
    		}

    		trace!(target: "sync", "Stopping sync verification thread");
    	}

    	/// Execute single verification task
    	pub fn execute_single_task<T: VerificationSink>(sink: &Arc<T>, storage: &StorageRef, memory_pool: &MemoryPoolRef, verifier: &ChainVerifierWrapper, task: VerificationTask) -> bool {
    		// block verification && insertion can lead to reorganization
    		// => transactions from decanonized blocks should be put back to the MemoryPool
    		// => they must be verified again
    		// => here's sub-tasks queue
    		let mut tasks_queue: VecDeque<VerificationTask> = VecDeque::new();
    		tasks_queue.push_back(task);

    		while let Some(task) = tasks_queue.pop_front() {
    			match task {
    				VerificationTask::VerifyBlock(block) => {
    					// verify block
    					match verifier.verify_block(&block) {
    						Ok(_) => {
    							if let Some(tasks) = sink.on_block_verification_success(block) {
    								tasks_queue.extend(tasks);
    							}
    						},
    						Err(e) => {
    							sink.on_block_verification_error(&format!("{:?}", e), block.hash())
    						}
    					}
    				},
    				VerificationTask::VerifyTransaction(height, transaction) => {
    					// output provider must check previous outputs in both storage && memory pool
    					match MemoryPoolTransactionOutputProvider::for_transaction(storage.clone(), memory_pool, &transaction.raw) {
    						Err(e) => {
    							sink.on_transaction_verification_error(&format!("{:?}", e), &transaction.hash);
    							continue; // with new verification sub-task
    						},
    						Ok(tx_output_provider) => {
    							let time: u32 = get_time().sec as u32;
    							match verifier.verifier.verify_mempool_transaction(storage.as_block_header_provider(), &tx_output_provider, height, time, &transaction.raw) {
    								Ok(_) => sink.on_transaction_verification_success(transaction.into()),
    								Err(e) => sink.on_transaction_verification_error(&format!("{:?}", e), &transaction.hash),
    							}
    						},
    					};
    				},
    				VerificationTask::Stop => return false,
    			}
    		}

    		true
    	}
    }

    impl Drop for AsyncVerifier {
    	fn drop(&mut self) {
    		if let Some(join_handle) = self.verification_worker_thread.take() {
    			{
    				let verification_work_sender = self.verification_work_sender.lock();
    				// ignore send error here <= destructing anyway
    				let _ = verification_work_sender.send(VerificationTask::Stop);
    			}
    			join_handle.join().expect("Clean shutdown.");
    		}
    	}
    }

    impl Verifier for AsyncVerifier {
    	/// Verify block
    	fn verify_block(&self, block: IndexedBlock) {
    		self.verification_work_sender.lock()
    			.send(VerificationTask::VerifyBlock(block))
    			.expect("Verification thread have the same lifetime as `AsyncVerifier`");
    	}

    	/// Verify transaction
    	fn verify_transaction(&self, height: BlockHeight, transaction: IndexedTransaction) {
    		self.verification_work_sender.lock()
    			.send(VerificationTask::VerifyTransaction(height, transaction))
    			.expect("Verification thread have the same lifetime as `AsyncVerifier`");
    	}
    }

    /// Synchronous synchronization verifier
    pub struct SyncVerifier<T: VerificationSink> {
    	/// Verifier
    	verifier: ChainVerifierWrapper,
    	/// Verification sink
    	sink: Arc<T>,
    }

    impl<T> SyncVerifier<T> where T: VerificationSink {
    	/// Create new sync verifier
    	pub fn new(consensus: ConsensusParams, storage: StorageRef, sink: Arc<T>, verification_params: VerificationParameters) -> Self {
    		let verifier = ChainVerifier::new(storage.clone(), consensus);
    		let verifier = ChainVerifierWrapper::new(Arc::new(verifier), &storage, verification_params);
    		SyncVerifier {
    			verifier: verifier,
    			sink: sink,
    		}
    	}
    }

    impl<T> Verifier for SyncVerifier<T> where T: VerificationSink {
    	/// Verify block
    	fn verify_block(&self, block: IndexedBlock) {
    		match self.verifier.verify_block(&block) {
    			Ok(_) => {
    				// SyncVerifier is used for bulk blocks import only
    				// => there are no memory pool
    				// => we could ignore decanonized transactions
    				self.sink.on_block_verification_success(block);
    			},
    			Err(e) => self.sink.on_block_verification_error(&format!("{:?}", e), block.hash()),
    		}
    	}

    	/// Verify transaction
    	fn verify_transaction(&self, _height: BlockHeight, _transaction: IndexedTransaction) {
    		unimplemented!() // sync verifier is currently only used for blocks verification
    	}
    }

    #[cfg(test)]
    pub mod tests {
    	extern crate test_data;

    	use std::sync::Arc;
    	use std::sync::atomic::Ordering;
    	use std::collections::{HashSet, HashMap};
    	use db::BlockChainDatabase;
    	use network::{Network, ConsensusParams, ConsensusFork};
    	use verification::{VerificationLevel, BackwardsCompatibleChainVerifier as ChainVerifier, Error as VerificationError, TransactionError};
    	use synchronization_client_core::CoreVerificationSink;
    	use synchronization_executor::tests::DummyTaskExecutor;
    	use primitives::hash::H256;
    	use chain::{IndexedBlock, IndexedTransaction};
    	use super::{Verifier, BlockVerificationSink, TransactionVerificationSink, AsyncVerifier, VerificationTask, ChainVerifierWrapper};
    	use types::{BlockHeight, StorageRef, MemoryPoolRef};
    	use script::Error as ScriptError;
    	use VerificationParameters;

    	#[derive(Default)]
    	pub struct DummyVerifier {
    		sink: Option<Arc<CoreVerificationSink<DummyTaskExecutor>>>,
    		errors: HashMap<H256, String>,
    		actual_checks: HashSet<H256>,
    		storage: Option<StorageRef>,
    		memory_pool: Option<MemoryPoolRef>,
    		verifier: Option<ChainVerifierWrapper>,
    	}

    	impl DummyVerifier {
    		pub fn set_sink(&mut self, sink: Arc<CoreVerificationSink<DummyTaskExecutor>>) {
    			self.sink = Some(sink);
    		}

    		pub fn set_storage(&mut self, storage: StorageRef) {
    			self.storage = Some(storage);
    		}

    		pub fn set_memory_pool(&mut self, memory_pool: MemoryPoolRef) {
    			self.memory_pool = Some(memory_pool);
    		}

    		pub fn set_verifier(&mut self, verifier: Arc<ChainVerifier>) {
    			self.verifier = Some(ChainVerifierWrapper::new(verifier, self.storage.as_ref().unwrap(), VerificationParameters {
    				verification_level: VerificationLevel::Full,
    				verification_edge: 0u8.into(),
    			}));
    		}

    		pub fn error_when_verifying(&mut self, hash: H256, err: &str) {
    			self.errors.insert(hash, err.into());
    		}

    		pub fn actual_check_when_verifying(&mut self, hash: H256) {
    			self.actual_checks.insert(hash);
    		}
    	}

    	impl Verifier for DummyVerifier {
    		fn verify_block(&self, block: IndexedBlock) {
    			match self.sink {
    				Some(ref sink) => match self.errors.get(&block.hash()) {
    					Some(err) => sink.on_block_verification_error(&err, &block.hash()),
    					None => {
    						if self.actual_checks.contains(block.hash()) {
    							AsyncVerifier::execute_single_task(sink, self.storage.as_ref().unwrap(), self.memory_pool.as_ref().unwrap(), self.verifier.as_ref().unwrap(), VerificationTask::VerifyBlock(block));
    						} else {
    							sink.on_block_verification_success(block);
    						}
    					},
    				},
    				None => panic!("call set_sink"),
    			}
    		}

    		fn verify_transaction(&self, _height: BlockHeight, transaction: IndexedTransaction) {
    			match self.sink {
    				Some(ref sink) => match self.errors.get(&transaction.hash) {
    					Some(err) => sink.on_transaction_verification_error(&err, &transaction.hash),
    					None => {
    						if self.actual_checks.contains(&transaction.hash) {
    							let next_block_height = self.storage.as_ref().unwrap().best_block().number + 1;
    							AsyncVerifier::execute_single_task(sink, self.storage.as_ref().unwrap(), self.memory_pool.as_ref().unwrap(), self.verifier.as_ref().unwrap(), VerificationTask::VerifyTransaction(next_block_height, transaction));
    						} else {
    							sink.on_transaction_verification_success(transaction.into());
    						}
    					},
    				},
    				None => panic!("call set_sink"),
    			}
    		}
    	}

    	#[test]
    	fn verifier_wrapper_switches_to_full_mode() {
    		let storage: StorageRef = Arc::new(BlockChainDatabase::init_test_chain(vec![test_data::genesis().into()]));
    		let verifier = Arc::new(ChainVerifier::new(storage.clone(), ConsensusParams::new(Network::Unitest, ConsensusFork::BitcoinCore)));

    		// switching to full verification when block is already in db
    		assert_eq!(ChainVerifierWrapper::new(verifier.clone(), &storage, VerificationParameters {
    			verification_level: VerificationLevel::NoVerification,
    			verification_edge: test_data::genesis().hash(),
    		}).enforce_full_verification.load(Ordering::Relaxed), true);

    		// switching to full verification when block with given hash is coming
    		let wrapper = ChainVerifierWrapper::new(verifier, &storage, VerificationParameters {
    			verification_level: VerificationLevel::NoVerification,
    			verification_edge: test_data::block_h1().hash(),
    		});
    		assert_eq!(wrapper.enforce_full_verification.load(Ordering::Relaxed), false);
    		let block: IndexedBlock = test_data::block_h1().into();
    		let _ = wrapper.verify_block(&block);
    		assert_eq!(wrapper.enforce_full_verification.load(Ordering::Relaxed), true);
    	}

    	#[test]
    	fn verification_level_header_accept_incorrect_transaction() {
    		let mut blocks: Vec<IndexedBlock> = vec![test_data::genesis().into()];
    		let mut rolling_hash = blocks[0].hash().clone();
    		for _ in 1..101 {
    			let next_block = test_data::block_builder()
    				.transaction()
    					.coinbase()
    					.output().value(5000000000).build()
    					.build()
    				.merkled_header()
    					.parent(rolling_hash.clone())
    					.bits(Network::Unitest.max_bits().into())
    					.build()
    				.build();
    			rolling_hash = next_block.hash();
    			blocks.push(next_block.into());
    		}

    		let coinbase_transaction_hash = blocks[0].transactions[0].hash.clone();
    		let last_block_hash = blocks[blocks.len() - 1].hash().clone();
    		let storage: StorageRef = Arc::new(BlockChainDatabase::init_test_chain(blocks));
    		let verifier = Arc::new(ChainVerifier::new(storage.clone(), ConsensusParams::new(Network::Unitest, ConsensusFork::BitcoinCore)));
    		let bad_transaction_block: IndexedBlock = test_data::block_builder()
    			.transaction().coinbase().output().value(50).build().build()
    			.transaction()
    				.input().hash(coinbase_transaction_hash).build()
    				.output().value(1000).build()
    				.build()
    			.merkled_header()
    				.parent(last_block_hash)
    				.bits(Network::Unitest.max_bits().into())
    				.build()
    			.build().into();

    		// Ok(()) when tx script is not checked
    		let wrapper = ChainVerifierWrapper::new(verifier.clone(), &storage, VerificationParameters {
    			verification_level: VerificationLevel::Header,
    			verification_edge: 1.into(),
    		});
    		assert_eq!(wrapper.verify_block(&bad_transaction_block), Ok(()));

    		// Error when tx script is checked
    		let wrapper = ChainVerifierWrapper::new(verifier, &storage, VerificationParameters {
    			verification_level: VerificationLevel::Full,
    			verification_edge: 1.into(),
    		});
    		assert_eq!(wrapper.verify_block(&bad_transaction_block), Err(VerificationError::Transaction(1, TransactionError::Signature(0, ScriptError::InvalidStackOperation))));
    	}

    	#[test]
    	fn verification_level_none_accept_incorrect_block() {
    		let storage: StorageRef = Arc::new(BlockChainDatabase::init_test_chain(vec![test_data::genesis().into()]));
    		let verifier = Arc::new(ChainVerifier::new(storage.clone(), ConsensusParams::new(Network::Unitest, ConsensusFork::BitcoinCore)));
    		let bad_block: IndexedBlock = test_data::block_builder().header().build().build().into();

    		// Ok(()) when nothing is verified
    		let wrapper = ChainVerifierWrapper::new(verifier.clone(), &storage, VerificationParameters {
    			verification_level: VerificationLevel::NoVerification,
    			verification_edge: 1.into(),
    		});
    		assert_eq!(wrapper.verify_block(&bad_block), Ok(()));

    		// Error when everything is verified
    		let wrapper = ChainVerifierWrapper::new(verifier, &storage, VerificationParameters {
    			verification_level: VerificationLevel::Full,
    			verification_edge: 1.into(),
    		});
    		assert_eq!(wrapper.verify_block(&bad_block), Err(VerificationError::Empty));
    	}
    }

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