coverage & panics avoidance
This commit is contained in:
		
							parent
							
								
									76d51a3da6
								
							
						
					
					
						commit
						1ae7db2e03
					
				@ -23,8 +23,6 @@ use spec::*;
 | 
				
			|||||||
use engine::*;
 | 
					use engine::*;
 | 
				
			||||||
use evm::Schedule;
 | 
					use evm::Schedule;
 | 
				
			||||||
use evm::Factory;
 | 
					use evm::Factory;
 | 
				
			||||||
#[cfg(test)]
 | 
					 | 
				
			||||||
use tests::helpers::*;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Engine using Ethash proof-of-work consensus algorithm, suitable for Ethereum
 | 
					/// Engine using Ethash proof-of-work consensus algorithm, suitable for Ethereum
 | 
				
			||||||
/// mainnet chains in the Olympic, Frontier and Homestead eras.
 | 
					/// mainnet chains in the Olympic, Frontier and Homestead eras.
 | 
				
			||||||
@ -49,6 +47,17 @@ impl Ethash {
 | 
				
			|||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						#[cfg(test)]
 | 
				
			||||||
 | 
						fn new_test(spec: Spec) -> Ethash {
 | 
				
			||||||
 | 
							Ethash {
 | 
				
			||||||
 | 
								spec: spec,
 | 
				
			||||||
 | 
								pow: EthashManager::new(),
 | 
				
			||||||
 | 
								factory: Factory::default(),
 | 
				
			||||||
 | 
								u64_params: RwLock::new(HashMap::new()),
 | 
				
			||||||
 | 
								u256_params: RwLock::new(HashMap::new())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fn u64_param(&self, name: &str) -> u64 {
 | 
						fn u64_param(&self, name: &str) -> u64 {
 | 
				
			||||||
		*self.u64_params.write().unwrap().entry(name.to_owned()).or_insert_with(||
 | 
							*self.u64_params.write().unwrap().entry(name.to_owned()).or_insert_with(||
 | 
				
			||||||
			self.spec().engine_params.get(name).map_or(0u64, |a| decode(&a)))
 | 
								self.spec().engine_params.get(name).map_or(0u64, |a| decode(&a)))
 | 
				
			||||||
@ -123,6 +132,11 @@ impl Engine for Ethash {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
 | 
						fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
 | 
				
			||||||
		// check the seal fields.
 | 
							// check the seal fields.
 | 
				
			||||||
 | 
							if header.seal.len() != self.seal_fields() {
 | 
				
			||||||
 | 
								return Err(From::from(BlockError::InvalidSealArity(
 | 
				
			||||||
 | 
									Mismatch { expected: self.seal_fields(), found: header.seal.len() }
 | 
				
			||||||
 | 
								)));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		try!(UntrustedRlp::new(&header.seal[0]).as_val::<H256>());
 | 
							try!(UntrustedRlp::new(&header.seal[0]).as_val::<H256>());
 | 
				
			||||||
		try!(UntrustedRlp::new(&header.seal[1]).as_val::<H64>());
 | 
							try!(UntrustedRlp::new(&header.seal[1]).as_val::<H64>());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -242,9 +256,22 @@ impl Header {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[test]
 | 
					#[cfg(test)]
 | 
				
			||||||
fn on_close_block() {
 | 
					mod tests {
 | 
				
			||||||
 | 
						extern crate ethash;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						use common::*;
 | 
				
			||||||
 | 
						use block::*;
 | 
				
			||||||
 | 
						use spec::*;
 | 
				
			||||||
 | 
						use engine::*;
 | 
				
			||||||
 | 
						use evm::Schedule;
 | 
				
			||||||
 | 
						use evm::Factory;
 | 
				
			||||||
 | 
						use tests::helpers::*;
 | 
				
			||||||
	use super::*;
 | 
						use super::*;
 | 
				
			||||||
 | 
						use super::super::new_morden;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						#[test]
 | 
				
			||||||
 | 
						fn on_close_block() {
 | 
				
			||||||
		let engine = new_morden().to_engine().unwrap();
 | 
							let engine = new_morden().to_engine().unwrap();
 | 
				
			||||||
		let genesis_header = engine.spec().genesis_header();
 | 
							let genesis_header = engine.spec().genesis_header();
 | 
				
			||||||
		let mut db_result = get_temp_journal_db();
 | 
							let mut db_result = get_temp_journal_db();
 | 
				
			||||||
@ -254,11 +281,10 @@ fn on_close_block() {
 | 
				
			|||||||
		let b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]);
 | 
							let b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]);
 | 
				
			||||||
		let b = b.close();
 | 
							let b = b.close();
 | 
				
			||||||
		assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap());
 | 
							assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap());
 | 
				
			||||||
}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[test]
 | 
						#[test]
 | 
				
			||||||
fn on_close_block_with_uncle() {
 | 
						fn on_close_block_with_uncle() {
 | 
				
			||||||
	use super::*;
 | 
					 | 
				
			||||||
		let engine = new_morden().to_engine().unwrap();
 | 
							let engine = new_morden().to_engine().unwrap();
 | 
				
			||||||
		let genesis_header = engine.spec().genesis_header();
 | 
							let genesis_header = engine.spec().genesis_header();
 | 
				
			||||||
		let mut db_result = get_temp_journal_db();
 | 
							let mut db_result = get_temp_journal_db();
 | 
				
			||||||
@ -274,6 +300,66 @@ fn on_close_block_with_uncle() {
 | 
				
			|||||||
		let b = b.close();
 | 
							let b = b.close();
 | 
				
			||||||
		assert_eq!(b.state().balance(&Address::zero()), U256::from_str("478eae0e571ba000").unwrap());
 | 
							assert_eq!(b.state().balance(&Address::zero()), U256::from_str("478eae0e571ba000").unwrap());
 | 
				
			||||||
		assert_eq!(b.state().balance(&uncle_author), U256::from_str("3cb71f51fc558000").unwrap());
 | 
							assert_eq!(b.state().balance(&uncle_author), U256::from_str("3cb71f51fc558000").unwrap());
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						#[test]
 | 
				
			||||||
 | 
						fn has_valid_metadata() {
 | 
				
			||||||
 | 
							let engine = Ethash::new_boxed(new_morden());
 | 
				
			||||||
 | 
							assert!(!engine.name().is_empty());
 | 
				
			||||||
 | 
							assert!(engine.version().major >= 1);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						#[test]
 | 
				
			||||||
 | 
						fn can_return_params() {
 | 
				
			||||||
 | 
							let engine = Ethash::new_test(new_morden());
 | 
				
			||||||
 | 
							assert!(engine.u64_param("durationLimit") > 0);
 | 
				
			||||||
 | 
							assert!(engine.u256_param("minimumDifficulty") > U256::zero());
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						#[test]
 | 
				
			||||||
 | 
						fn can_return_factory() {
 | 
				
			||||||
 | 
							let engine = Ethash::new_test(new_morden());
 | 
				
			||||||
 | 
							let factory = engine.vm_factory();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						#[test]
 | 
				
			||||||
 | 
						fn can_return_schedule() {
 | 
				
			||||||
 | 
							let engine = Ethash::new_test(new_morden());
 | 
				
			||||||
 | 
							let schedule = engine.schedule(&EnvInfo {
 | 
				
			||||||
 | 
								number: 10000000,
 | 
				
			||||||
 | 
								author: x!(0),
 | 
				
			||||||
 | 
								timestamp: 0,
 | 
				
			||||||
 | 
								difficulty: x!(0),
 | 
				
			||||||
 | 
								last_hashes: vec![],
 | 
				
			||||||
 | 
								gas_used: x!(0),
 | 
				
			||||||
 | 
								gas_limit: x!(0)
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert!(schedule.stack_limit > 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							let schedule = engine.schedule(&EnvInfo {
 | 
				
			||||||
 | 
								number: 100,
 | 
				
			||||||
 | 
								author: x!(0),
 | 
				
			||||||
 | 
								timestamp: 0,
 | 
				
			||||||
 | 
								difficulty: x!(0),
 | 
				
			||||||
 | 
								last_hashes: vec![],
 | 
				
			||||||
 | 
								gas_used: x!(0),
 | 
				
			||||||
 | 
								gas_limit: x!(0)
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert!(!schedule.have_delegate_call);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						#[test]
 | 
				
			||||||
 | 
						fn can_do_basic_verification_fail() {
 | 
				
			||||||
 | 
							let engine = Ethash::new_test(new_morden());
 | 
				
			||||||
 | 
							let header: Header = Header::default();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							let verify_result = engine.verify_block_basic(&header, None);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert!(!verify_result.is_ok());
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TODO: difficulty test
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO: difficulty test
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user