Merge branch 'master' of github.com:gavofyork/ethcore into network
This commit is contained in:
		
						commit
						5d0e186632
					
				@ -15,6 +15,7 @@ flate2 = "0.2"
 | 
			
		||||
rocksdb = "0.2.1"
 | 
			
		||||
heapsize = "0.2.0"
 | 
			
		||||
rust-crypto = "0.2.34"
 | 
			
		||||
time = "0.1"
 | 
			
		||||
 | 
			
		||||
evmjit = { path = "rust-evmjit", optional = true }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										96
									
								
								src/block.rs
									
									
									
									
									
								
							
							
						
						
									
										96
									
								
								src/block.rs
									
									
									
									
									
								
							@ -21,7 +21,16 @@ pub struct Block {
 | 
			
		||||
	uncles: Vec<Header>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// A set of references to `Block` fields that are publicly accessible. 
 | 
			
		||||
pub struct BlockRefMut<'a> {
 | 
			
		||||
	pub header: &'a Header,
 | 
			
		||||
	pub state: &'a mut State,
 | 
			
		||||
	pub archive: &'a Vec<Entry>,
 | 
			
		||||
	pub uncles: &'a Vec<Header>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Block {
 | 
			
		||||
	/// Create a new block from the given `state`.
 | 
			
		||||
	fn new(state: State) -> Block {
 | 
			
		||||
		Block {
 | 
			
		||||
			header: Header::new(),
 | 
			
		||||
@ -32,7 +41,15 @@ impl Block {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn state_mut(&mut self) -> &mut State { &mut self.state }
 | 
			
		||||
	/// Get a structure containing individual references to all public fields.
 | 
			
		||||
	pub fn fields(&mut self) -> BlockRefMut {
 | 
			
		||||
		BlockRefMut {
 | 
			
		||||
			header: &self.header,
 | 
			
		||||
			state: &mut self.state,
 | 
			
		||||
			archive: &self.archive,
 | 
			
		||||
			uncles: &self.uncles,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Trait for a object that is_a `Block`.
 | 
			
		||||
@ -61,18 +78,18 @@ impl IsBlock for Block {
 | 
			
		||||
///
 | 
			
		||||
/// It's a bit like a Vec<Transaction>, eccept that whenever a transaction is pushed, we execute it and
 | 
			
		||||
/// maintain the system `state()`. We also archive execution receipts in preparation for later block creation.
 | 
			
		||||
pub struct OpenBlock<'engine> {
 | 
			
		||||
pub struct OpenBlock<'x, 'y> {
 | 
			
		||||
	block: Block,
 | 
			
		||||
	engine: &'engine Engine,
 | 
			
		||||
	last_hashes: LastHashes,
 | 
			
		||||
	engine: &'x Engine,
 | 
			
		||||
	last_hashes: &'y LastHashes,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Just like OpenBlock, except that we've applied `Engine::on_close_block`, finished up the non-seal header fields,
 | 
			
		||||
/// and collected the uncles.
 | 
			
		||||
///
 | 
			
		||||
/// There is no function available to push a transaction. If you want that you'll need to `reopen()` it.
 | 
			
		||||
pub struct ClosedBlock<'engine> {
 | 
			
		||||
	open_block: OpenBlock<'engine>,
 | 
			
		||||
pub struct ClosedBlock<'x, 'y> {
 | 
			
		||||
	open_block: OpenBlock<'x, 'y>,
 | 
			
		||||
	uncle_bytes: Bytes,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -84,9 +101,9 @@ pub struct SealedBlock {
 | 
			
		||||
	uncle_bytes: Bytes,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'engine> OpenBlock<'engine> {
 | 
			
		||||
impl<'x, 'y> OpenBlock<'x, 'y> {
 | 
			
		||||
	/// Create a new OpenBlock ready for transaction pushing.
 | 
			
		||||
	pub fn new<'a>(engine: &'a Engine, db: OverlayDB, parent: &Header, last_hashes: LastHashes, author: Address, extra_data: Bytes) -> OpenBlock<'a> {
 | 
			
		||||
	pub fn new<'a, 'b>(engine: &'a Engine, db: OverlayDB, parent: &Header, last_hashes: &'b LastHashes, author: Address, extra_data: Bytes) -> OpenBlock<'a, 'b> {
 | 
			
		||||
		let mut r = OpenBlock {
 | 
			
		||||
			block: Block::new(State::from_existing(db, parent.state_root.clone(), engine.account_start_nonce())),
 | 
			
		||||
			engine: engine,
 | 
			
		||||
@ -95,6 +112,8 @@ impl<'engine> OpenBlock<'engine> {
 | 
			
		||||
 | 
			
		||||
		r.block.header.set_author(author);
 | 
			
		||||
		r.block.header.set_extra_data(extra_data);
 | 
			
		||||
		r.block.header.set_timestamp_now();
 | 
			
		||||
 | 
			
		||||
		engine.populate_from_parent(&mut r.block.header, parent);
 | 
			
		||||
		engine.on_new_block(&mut r.block);
 | 
			
		||||
		r
 | 
			
		||||
@ -103,6 +122,9 @@ impl<'engine> OpenBlock<'engine> {
 | 
			
		||||
	/// Alter the author for the block.
 | 
			
		||||
	pub fn set_author(&mut self, author: Address) { self.block.header.set_author(author); }
 | 
			
		||||
 | 
			
		||||
	/// Alter the timestamp of the block.
 | 
			
		||||
	pub fn set_timestamp(&mut self, timestamp: u64) { self.block.header.set_timestamp(timestamp); }
 | 
			
		||||
 | 
			
		||||
	/// Alter the extra_data for the block.
 | 
			
		||||
	pub fn set_extra_data(&mut self, extra_data: Bytes) -> Result<(), BlockError> {
 | 
			
		||||
		if extra_data.len() > self.engine.maximum_extra_data_size() {
 | 
			
		||||
@ -131,9 +153,9 @@ impl<'engine> OpenBlock<'engine> {
 | 
			
		||||
	pub fn env_info(&self) -> EnvInfo {
 | 
			
		||||
		// TODO: memoise.
 | 
			
		||||
		EnvInfo {
 | 
			
		||||
			number: self.block.header.number.clone(),
 | 
			
		||||
			number: self.block.header.number,
 | 
			
		||||
			author: self.block.header.author.clone(),
 | 
			
		||||
			timestamp: self.block.header.timestamp.clone(),
 | 
			
		||||
			timestamp: self.block.header.timestamp,
 | 
			
		||||
			difficulty: self.block.header.difficulty.clone(),
 | 
			
		||||
			last_hashes: self.last_hashes.clone(),
 | 
			
		||||
			gas_used: self.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)),
 | 
			
		||||
@ -157,7 +179,7 @@ impl<'engine> OpenBlock<'engine> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure out the uncles.
 | 
			
		||||
	pub fn close(self) -> ClosedBlock<'engine> {
 | 
			
		||||
	pub fn close(self) -> ClosedBlock<'x, 'y> {
 | 
			
		||||
		let mut s = self;
 | 
			
		||||
		s.engine.on_close_block(&mut s.block);
 | 
			
		||||
		s.block.header.transactions_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.transaction.rlp_bytes()).collect());
 | 
			
		||||
@ -173,16 +195,16 @@ impl<'engine> OpenBlock<'engine> {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'engine> IsBlock for OpenBlock<'engine> {
 | 
			
		||||
impl<'x, 'y> IsBlock for OpenBlock<'x, 'y> {
 | 
			
		||||
	fn block(&self) -> &Block { &self.block }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'engine> IsBlock for ClosedBlock<'engine> {
 | 
			
		||||
impl<'x, 'y> IsBlock for ClosedBlock<'x, 'y> {
 | 
			
		||||
	fn block(&self) -> &Block { &self.open_block.block }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'engine> ClosedBlock<'engine> {
 | 
			
		||||
	fn new<'a>(open_block: OpenBlock<'a>, uncle_bytes: Bytes) -> ClosedBlock<'a> {
 | 
			
		||||
impl<'x, 'y> ClosedBlock<'x, 'y> {
 | 
			
		||||
	fn new<'a, 'b>(open_block: OpenBlock<'a, 'b>, uncle_bytes: Bytes) -> ClosedBlock<'a, 'b> {
 | 
			
		||||
		ClosedBlock {
 | 
			
		||||
			open_block: open_block,
 | 
			
		||||
			uncle_bytes: uncle_bytes,
 | 
			
		||||
@ -205,7 +227,7 @@ impl<'engine> ClosedBlock<'engine> {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Turn this back into an `OpenBlock`.
 | 
			
		||||
	pub fn reopen(self) -> OpenBlock<'engine> { self.open_block }
 | 
			
		||||
	pub fn reopen(self) -> OpenBlock<'x, 'y> { self.open_block }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl SealedBlock {
 | 
			
		||||
@ -217,13 +239,26 @@ impl SealedBlock {
 | 
			
		||||
		for e in self.block.archive.iter() { e.transaction.rlp_append(&mut block_rlp); }
 | 
			
		||||
		block_rlp.append_raw(&self.uncle_bytes, 1);
 | 
			
		||||
		block_rlp.out()
 | 
			
		||||
	}		
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Drop this object and return the underlieing database.
 | 
			
		||||
	pub fn drain(self) -> OverlayDB { self.block.state.drop().1 }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl IsBlock for SealedBlock {
 | 
			
		||||
	fn block(&self) -> &Block { &self.block }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn enact(rlp_bytes: &[u8], engine: &Engine, db: OverlayDB, parent: &Header, last_hashes: &LastHashes) -> Result<SealedBlock, Error> {
 | 
			
		||||
	let block = BlockView::new(rlp_bytes);
 | 
			
		||||
	let header = block.header_view();
 | 
			
		||||
	let mut b = OpenBlock::new(engine, db, parent, last_hashes, header.author(), header.extra_data());
 | 
			
		||||
	b.set_timestamp(header.timestamp());
 | 
			
		||||
	for t in block.transactions().into_iter() { try!(b.push_transaction(t, None)); }
 | 
			
		||||
	for u in block.uncles().into_iter() { try!(b.push_uncle(u)); }
 | 
			
		||||
	Ok(try!(b.close().seal(header.seal())))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn open_block() {
 | 
			
		||||
	use spec::*;
 | 
			
		||||
@ -231,7 +266,30 @@ fn open_block() {
 | 
			
		||||
	let genesis_header = engine.spec().genesis_header();
 | 
			
		||||
	let mut db = OverlayDB::new_temp();
 | 
			
		||||
	engine.spec().ensure_db_good(&mut db);
 | 
			
		||||
	let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()], Address::zero(), vec![]);
 | 
			
		||||
	let last_hashes = vec![genesis_header.hash()];
 | 
			
		||||
	let b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]);
 | 
			
		||||
	let b = b.close();
 | 
			
		||||
	let _ = b.seal(vec![]);
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn enact_block() {
 | 
			
		||||
	use spec::*;
 | 
			
		||||
	let engine = Spec::new_test().to_engine().unwrap();
 | 
			
		||||
	let genesis_header = engine.spec().genesis_header();
 | 
			
		||||
 | 
			
		||||
	let mut db = OverlayDB::new_temp();
 | 
			
		||||
	engine.spec().ensure_db_good(&mut db);
 | 
			
		||||
	let b = OpenBlock::new(engine.deref(), db, &genesis_header, &vec![genesis_header.hash()], Address::zero(), vec![]).close().seal(vec![]).unwrap();
 | 
			
		||||
	let orig_bytes = b.rlp_bytes();
 | 
			
		||||
	let orig_db = b.drain();
 | 
			
		||||
 | 
			
		||||
	let mut db = OverlayDB::new_temp();
 | 
			
		||||
	engine.spec().ensure_db_good(&mut db);
 | 
			
		||||
	let e = enact(&orig_bytes, engine.deref(), db, &genesis_header, &vec![genesis_header.hash()]).unwrap();
 | 
			
		||||
 | 
			
		||||
	assert_eq!(e.rlp_bytes(), orig_bytes);
 | 
			
		||||
	
 | 
			
		||||
	let db = e.drain();
 | 
			
		||||
	assert_eq!(orig_db.keys(), db.keys());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -34,7 +34,7 @@ pub struct CacheSize {
 | 
			
		||||
/// Information about best block gathered together
 | 
			
		||||
struct BestBlock {
 | 
			
		||||
	pub hash: H256,
 | 
			
		||||
	pub number: U256,
 | 
			
		||||
	pub number: BlockNumber,
 | 
			
		||||
	pub total_difficulty: U256
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -42,7 +42,7 @@ impl BestBlock {
 | 
			
		||||
	fn new() -> BestBlock {
 | 
			
		||||
		BestBlock {
 | 
			
		||||
			hash: H256::new(),
 | 
			
		||||
			number: U256::from(0),
 | 
			
		||||
			number: 0,
 | 
			
		||||
			total_difficulty: U256::from(0)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
@ -59,7 +59,7 @@ pub struct BlockChain {
 | 
			
		||||
 | 
			
		||||
	// extra caches
 | 
			
		||||
	block_details: RwLock<HashMap<H256, BlockDetails>>,
 | 
			
		||||
	block_hashes: RwLock<HashMap<U256, H256>>,
 | 
			
		||||
	block_hashes: RwLock<HashMap<BlockNumber, H256>>,
 | 
			
		||||
	transaction_addresses: RwLock<HashMap<H256, TransactionAddress>>,
 | 
			
		||||
	block_logs: RwLock<HashMap<H256, BlockLogBlooms>>,
 | 
			
		||||
	blocks_blooms: RwLock<HashMap<H256, BlocksBlooms>>,
 | 
			
		||||
@ -93,7 +93,7 @@ impl BlockChain {
 | 
			
		||||
	/// 	let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3";
 | 
			
		||||
	/// 	assert_eq!(bc.genesis_hash(), H256::from_str(genesis_hash).unwrap());
 | 
			
		||||
	/// 	assert!(bc.is_known(&bc.genesis_hash()));
 | 
			
		||||
	/// 	assert_eq!(bc.genesis_hash(), bc.block_hash(&U256::from(0u8)).unwrap());
 | 
			
		||||
	/// 	assert_eq!(bc.genesis_hash(), bc.block_hash(0).unwrap());
 | 
			
		||||
	/// }
 | 
			
		||||
	/// ```
 | 
			
		||||
	pub fn new(genesis: &[u8], path: &Path) -> BlockChain {
 | 
			
		||||
@ -342,9 +342,9 @@ impl BlockChain {
 | 
			
		||||
			// it is a fork
 | 
			
		||||
			i if i > 1 => {
 | 
			
		||||
				let ancestor_number = self.block_number(&route.ancestor).unwrap();
 | 
			
		||||
				let start_number = ancestor_number + U256::from(1u8);
 | 
			
		||||
				let start_number = ancestor_number + 1;
 | 
			
		||||
				for (index, hash) in route.blocks.iter().skip(route.index).enumerate() {
 | 
			
		||||
					batch.put_extras(&(start_number + U256::from(index as u64)), hash);
 | 
			
		||||
					batch.put_extras(&(start_number + index as BlockNumber), hash);
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
			// route.blocks.len() could be 0 only if inserted block is best block,
 | 
			
		||||
@ -377,7 +377,7 @@ impl BlockChain {
 | 
			
		||||
 | 
			
		||||
	/// Returns reference to genesis hash.
 | 
			
		||||
	pub fn genesis_hash(&self) -> H256 {
 | 
			
		||||
		self.block_hash(&U256::from(0u8)).expect("Genesis hash should always exist")
 | 
			
		||||
		self.block_hash(0).expect("Genesis hash should always exist")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Get the partial-header of a block.
 | 
			
		||||
@ -415,8 +415,8 @@ impl BlockChain {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Get the hash of given block's number.
 | 
			
		||||
	pub fn block_hash(&self, hash: &U256) -> Option<H256> {
 | 
			
		||||
		self.query_extras(hash, &self.block_hashes)
 | 
			
		||||
	pub fn block_hash(&self, index: BlockNumber) -> Option<H256> {
 | 
			
		||||
		self.query_extras(&index, &self.block_hashes)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Get best block hash.
 | 
			
		||||
@ -425,7 +425,7 @@ impl BlockChain {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Get best block number.
 | 
			
		||||
	pub fn best_block_number(&self) -> U256 {
 | 
			
		||||
	pub fn best_block_number(&self) -> BlockNumber {
 | 
			
		||||
		self.best_block.read().unwrap().number
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -435,7 +435,7 @@ impl BlockChain {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Get the number of given block's hash.
 | 
			
		||||
	pub fn block_number(&self, hash: &H256) -> Option<U256> {
 | 
			
		||||
	pub fn block_number(&self, hash: &H256) -> Option<BlockNumber> {
 | 
			
		||||
		self.block(hash).map(|bytes| BlockView::new(&bytes).header_view().number())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -527,7 +527,6 @@ mod tests {
 | 
			
		||||
	use std::str::FromStr;
 | 
			
		||||
	use rustc_serialize::hex::FromHex;
 | 
			
		||||
	use util::hash::*;
 | 
			
		||||
	use util::uint::*;
 | 
			
		||||
	use blockchain::*;
 | 
			
		||||
 | 
			
		||||
	#[test]
 | 
			
		||||
@ -542,25 +541,24 @@ mod tests {
 | 
			
		||||
		let genesis_hash = H256::from_str("3caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942").unwrap();
 | 
			
		||||
 | 
			
		||||
		assert_eq!(bc.genesis_hash(), genesis_hash.clone());
 | 
			
		||||
		assert_eq!(bc.best_block_number(), U256::from(0u8));
 | 
			
		||||
		assert_eq!(bc.best_block_number(), 0);
 | 
			
		||||
		assert_eq!(bc.best_block_hash(), genesis_hash.clone());
 | 
			
		||||
		assert_eq!(bc.block_hash(&U256::from(0u8)), Some(genesis_hash.clone()));
 | 
			
		||||
		assert_eq!(bc.block_hash(&U256::from(1u8)), None);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		assert_eq!(bc.block_hash(0), Some(genesis_hash.clone()));
 | 
			
		||||
		assert_eq!(bc.block_hash(1), None);
 | 
			
		||||
		
 | 
			
		||||
		let first = "f90285f90219a03caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0bac6177a79e910c98d86ec31a09ae37ac2de15b754fd7bed1ba52362c49416bfa0d45893a296c1490a978e0bd321b5f2635d8280365c1fe9f693d65f233e791344a0c7778a7376099ee2e5c455791c1885b5c361b95713fddcbe32d97fd01334d296b90100000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000400000000000000000000000000000000000000000000000000000008302000001832fefd882560b845627cb99a00102030405060708091011121314151617181920212223242526272829303132a08ccb2837fb2923bd97e8f2d08ea32012d6e34be018c73e49a0f98843e8f47d5d88e53be49fec01012ef866f864800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d8785012a05f200801ba0cb088b8d2ff76a7b2c6616c9d02fb6b7a501afbf8b69d7180b09928a1b80b5e4a06448fe7476c606582039bb72a9f6f4b4fad18507b8dfbd00eebbe151cc573cd2c0".from_hex().unwrap();
 | 
			
		||||
 | 
			
		||||
		bc.insert_block(&first);
 | 
			
		||||
 | 
			
		||||
		let first_hash = H256::from_str("a940e5af7d146b3b917c953a82e1966b906dace3a4e355b5b0a4560190357ea1").unwrap();
 | 
			
		||||
 | 
			
		||||
		assert_eq!(bc.block_hash(&U256::from(0u8)), Some(genesis_hash.clone()));
 | 
			
		||||
		assert_eq!(bc.best_block_number(), U256::from(1u8));
 | 
			
		||||
		assert_eq!(bc.block_hash(0), Some(genesis_hash.clone()));
 | 
			
		||||
		assert_eq!(bc.best_block_number(), 1);
 | 
			
		||||
		assert_eq!(bc.best_block_hash(), first_hash.clone());
 | 
			
		||||
		assert_eq!(bc.block_hash(&U256::from(1u8)), Some(first_hash.clone()));
 | 
			
		||||
		assert_eq!(bc.block_hash(1), Some(first_hash.clone()));
 | 
			
		||||
		assert_eq!(bc.block_details(&first_hash).unwrap().parent, genesis_hash.clone());
 | 
			
		||||
		assert_eq!(bc.block_details(&genesis_hash).unwrap().children, vec![first_hash.clone()]);
 | 
			
		||||
		assert_eq!(bc.block_hash(&U256::from(2u8)), None);
 | 
			
		||||
		assert_eq!(bc.block_hash(2), None);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	#[test]
 | 
			
		||||
@ -590,16 +588,16 @@ mod tests {
 | 
			
		||||
		bc.insert_block(&b3b);
 | 
			
		||||
 | 
			
		||||
		assert_eq!(bc.best_block_hash(), best_block_hash);
 | 
			
		||||
		assert_eq!(bc.block_number(&genesis_hash).unwrap(), U256::from(0));
 | 
			
		||||
		assert_eq!(bc.block_number(&b1_hash).unwrap(), U256::from(1));
 | 
			
		||||
		assert_eq!(bc.block_number(&b2_hash).unwrap(), U256::from(2));
 | 
			
		||||
		assert_eq!(bc.block_number(&b3a_hash).unwrap(), U256::from(3));
 | 
			
		||||
		assert_eq!(bc.block_number(&b3b_hash).unwrap(), U256::from(3));
 | 
			
		||||
		assert_eq!(bc.block_number(&genesis_hash).unwrap(), 0);
 | 
			
		||||
		assert_eq!(bc.block_number(&b1_hash).unwrap(), 1);
 | 
			
		||||
		assert_eq!(bc.block_number(&b2_hash).unwrap(), 2);
 | 
			
		||||
		assert_eq!(bc.block_number(&b3a_hash).unwrap(), 3);
 | 
			
		||||
		assert_eq!(bc.block_number(&b3b_hash).unwrap(), 3);
 | 
			
		||||
 | 
			
		||||
		assert_eq!(bc.block_hash(&U256::from(0)).unwrap(), genesis_hash);
 | 
			
		||||
		assert_eq!(bc.block_hash(&U256::from(1)).unwrap(), b1_hash);
 | 
			
		||||
		assert_eq!(bc.block_hash(&U256::from(2)).unwrap(), b2_hash);
 | 
			
		||||
		assert_eq!(bc.block_hash(&U256::from(3)).unwrap(), b3a_hash);
 | 
			
		||||
		assert_eq!(bc.block_hash(0).unwrap(), genesis_hash);
 | 
			
		||||
		assert_eq!(bc.block_hash(1).unwrap(), b1_hash);
 | 
			
		||||
		assert_eq!(bc.block_hash(2).unwrap(), b2_hash);
 | 
			
		||||
		assert_eq!(bc.block_hash(3).unwrap(), b3a_hash);
 | 
			
		||||
 | 
			
		||||
		// test trie route
 | 
			
		||||
		let r0_1 = bc.tree_route(genesis_hash.clone(), b1_hash.clone()).unwrap();
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,7 @@ use util::*;
 | 
			
		||||
use blockchain::BlockChain;
 | 
			
		||||
use views::BlockView;
 | 
			
		||||
use error::ImportError;
 | 
			
		||||
use header::BlockNumber;
 | 
			
		||||
 | 
			
		||||
/// General block status
 | 
			
		||||
pub enum BlockStatus {
 | 
			
		||||
@ -40,8 +41,6 @@ pub struct BlockQueueStatus {
 | 
			
		||||
 | 
			
		||||
pub type TreeRoute = ::blockchain::TreeRoute;
 | 
			
		||||
 | 
			
		||||
pub type BlockNumber = u64;
 | 
			
		||||
 | 
			
		||||
/// Blockchain database client. Owns and manages a blockchain and a block queue.
 | 
			
		||||
pub trait BlockChainClient : Sync {
 | 
			
		||||
	/// Get raw block header data by block header hash.
 | 
			
		||||
@ -131,19 +130,19 @@ impl BlockChainClient for Client {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> {
 | 
			
		||||
		self.chain.block_hash(&From::from(n)).and_then(|h| self.block_header(&h))
 | 
			
		||||
		self.chain.block_hash(n).and_then(|h| self.block_header(&h))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn block_body_at(&self, n: BlockNumber) -> Option<Bytes> {
 | 
			
		||||
		self.chain.block_hash(&From::from(n)).and_then(|h| self.block_body(&h))
 | 
			
		||||
		self.chain.block_hash(n).and_then(|h| self.block_body(&h))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn block_at(&self, n: BlockNumber) -> Option<Bytes> {
 | 
			
		||||
		self.chain.block_hash(&From::from(n)).and_then(|h| self.block(&h))
 | 
			
		||||
		self.chain.block_hash(n).and_then(|h| self.block(&h))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn block_status_at(&self, n: BlockNumber) -> BlockStatus {
 | 
			
		||||
		match self.chain.block_hash(&From::from(n)) {
 | 
			
		||||
		match self.chain.block_hash(n) {
 | 
			
		||||
			Some(h) => self.block_status(&h),
 | 
			
		||||
			None => BlockStatus::Unknown
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
use util::*;
 | 
			
		||||
use header::BlockNumber;
 | 
			
		||||
 | 
			
		||||
/// Simple vector of hashes, should be at most 256 items large, can be smaller if being used
 | 
			
		||||
/// for a block whose number is less than 257.
 | 
			
		||||
@ -7,11 +8,11 @@ pub type LastHashes = Vec<H256>;
 | 
			
		||||
/// Information concerning the execution environment for a message-call/contract-creation.
 | 
			
		||||
pub struct EnvInfo {
 | 
			
		||||
	/// The block number.
 | 
			
		||||
	pub number: U256,
 | 
			
		||||
	pub number: BlockNumber,
 | 
			
		||||
	/// The block author.
 | 
			
		||||
	pub author: Address,
 | 
			
		||||
	/// The block timestamp.
 | 
			
		||||
	pub timestamp: U256,
 | 
			
		||||
	pub timestamp: u64,
 | 
			
		||||
	/// The block difficulty.
 | 
			
		||||
	pub difficulty: U256,
 | 
			
		||||
	/// The block gas limit.
 | 
			
		||||
@ -20,4 +21,4 @@ pub struct EnvInfo {
 | 
			
		||||
	pub last_hashes: LastHashes,
 | 
			
		||||
	/// The gas used.
 | 
			
		||||
	pub gas_used: U256,
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -17,13 +17,31 @@ impl Ethash {
 | 
			
		||||
 | 
			
		||||
impl Engine for Ethash {
 | 
			
		||||
	fn name(&self) -> &str { "Ethash" }
 | 
			
		||||
	fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) }
 | 
			
		||||
	// Two fields - mix
 | 
			
		||||
	fn seal_fields(&self) -> usize { 2 }
 | 
			
		||||
	// Two empty data items in RLP.
 | 
			
		||||
	fn seal_rlp(&self) -> Bytes { encode(&H64::new()) }
 | 
			
		||||
 | 
			
		||||
	/// Additional engine-specific information for the user/developer concerning `header`.
 | 
			
		||||
	fn extra_info(&self, _header: &Header) -> HashMap<String, String> { HashMap::new() }
 | 
			
		||||
	fn spec(&self) -> &Spec { &self.spec }
 | 
			
		||||
	fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
 | 
			
		||||
 | 
			
		||||
	/// Apply the block reward on finalisation of the block.
 | 
			
		||||
	/// This assumes that all uncles are valid uncles (i.e. of at least one generation before the current).
 | 
			
		||||
	fn on_close_block(&self, block: &mut Block) {
 | 
			
		||||
		let a = block.header().author.clone();
 | 
			
		||||
		block.state_mut().add_balance(&a, &decode(&self.spec().engine_params.get("blockReward").unwrap()));
 | 
			
		||||
		let reward = self.spec().engine_params.get("blockReward").map(|a| decode(&a)).unwrap_or(U256::from(0u64));
 | 
			
		||||
		let fields = block.fields();
 | 
			
		||||
 | 
			
		||||
		// Bestow block reward
 | 
			
		||||
		fields.state.add_balance(&fields.header.author, &(reward + reward / U256::from(32) * U256::from(fields.uncles.len())));
 | 
			
		||||
 | 
			
		||||
		// Bestow uncle rewards
 | 
			
		||||
		let current_number = fields.header.number();
 | 
			
		||||
		for u in fields.uncles.iter() {
 | 
			
		||||
			fields.state.add_balance(u.author(), &(reward * U256::from((8 + u.number() - current_number) / 8)));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -34,7 +52,8 @@ fn on_close_block() {
 | 
			
		||||
	let genesis_header = engine.spec().genesis_header();
 | 
			
		||||
	let mut db = OverlayDB::new_temp();
 | 
			
		||||
	engine.spec().ensure_db_good(&mut db);
 | 
			
		||||
	let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()], Address::zero(), vec![]);
 | 
			
		||||
	let last_hashes = vec![genesis_header.hash()];
 | 
			
		||||
	let b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]);
 | 
			
		||||
	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());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
use util::*;
 | 
			
		||||
use header::BlockNumber;
 | 
			
		||||
use rocksdb::{DB, Writable};
 | 
			
		||||
 | 
			
		||||
/// Represents index of extra data in database
 | 
			
		||||
@ -74,6 +75,13 @@ impl ExtrasSliceConvertable for U256 {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NICE: make less horrible.
 | 
			
		||||
impl ExtrasSliceConvertable for BlockNumber {
 | 
			
		||||
	fn to_extras_slice(&self, i: ExtrasIndex) -> H264 {
 | 
			
		||||
		U256::from(*self).to_extras_slice(i)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Types implementing this trait can be indexed in extras database
 | 
			
		||||
pub trait ExtrasIndexable {
 | 
			
		||||
	fn extras_index() -> ExtrasIndex;
 | 
			
		||||
@ -88,7 +96,7 @@ impl ExtrasIndexable for H256 {
 | 
			
		||||
/// Familial details concerning a block
 | 
			
		||||
#[derive(Debug, Clone)]
 | 
			
		||||
pub struct BlockDetails {
 | 
			
		||||
	pub number: U256,
 | 
			
		||||
	pub number: BlockNumber,
 | 
			
		||||
	pub total_difficulty: U256,
 | 
			
		||||
	pub parent: H256,
 | 
			
		||||
	pub children: Vec<H256>
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,8 @@
 | 
			
		||||
use util::*;
 | 
			
		||||
use basic_types::*;
 | 
			
		||||
use time::now_utc;
 | 
			
		||||
 | 
			
		||||
pub type BlockNumber = u64;
 | 
			
		||||
 | 
			
		||||
/// A block header.
 | 
			
		||||
///
 | 
			
		||||
@ -11,8 +14,8 @@ use basic_types::*;
 | 
			
		||||
pub struct Header {
 | 
			
		||||
	// TODO: make all private.
 | 
			
		||||
	pub parent_hash: H256,
 | 
			
		||||
	pub timestamp: U256,
 | 
			
		||||
	pub number: U256,
 | 
			
		||||
	pub timestamp: u64,
 | 
			
		||||
	pub number: BlockNumber,
 | 
			
		||||
	pub author: Address,
 | 
			
		||||
 | 
			
		||||
	pub transactions_root: H256,
 | 
			
		||||
@ -41,8 +44,8 @@ impl Header {
 | 
			
		||||
	pub fn new() -> Header {
 | 
			
		||||
		Header {
 | 
			
		||||
			parent_hash: ZERO_H256.clone(),
 | 
			
		||||
			timestamp: BAD_U256,
 | 
			
		||||
			number: ZERO_U256,
 | 
			
		||||
			timestamp: 0,
 | 
			
		||||
			number: 0,
 | 
			
		||||
			author: ZERO_ADDRESS.clone(),
 | 
			
		||||
 | 
			
		||||
			transactions_root: SHA3_NULL_RLP,
 | 
			
		||||
@ -61,14 +64,23 @@ impl Header {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn number(&self) -> BlockNumber { self.number }
 | 
			
		||||
	pub fn timestamp(&self) -> u64 { self.timestamp }
 | 
			
		||||
	pub fn author(&self) -> &Address { &self.author }
 | 
			
		||||
 | 
			
		||||
	pub fn extra_data(&self) -> &Bytes { &self.extra_data }
 | 
			
		||||
 | 
			
		||||
	pub fn seal(&self) -> &Vec<Bytes> { &self.seal }
 | 
			
		||||
 | 
			
		||||
	// TODO: seal_at, set_seal_at &c.
 | 
			
		||||
 | 
			
		||||
	pub fn set_number(&mut self, a: BlockNumber) { self.number = a; self.note_dirty(); }
 | 
			
		||||
	pub fn set_timestamp(&mut self, a: u64) { self.timestamp = a; self.note_dirty(); }
 | 
			
		||||
	pub fn set_timestamp_now(&mut self) { self.timestamp = now_utc().to_timespec().sec as u64; self.note_dirty(); }
 | 
			
		||||
	pub fn set_author(&mut self, a: Address) { if a != self.author { self.author = a; self.note_dirty(); } }
 | 
			
		||||
 | 
			
		||||
	pub fn set_extra_data(&mut self, a: Bytes) { if a != self.extra_data { self.extra_data = a; self.note_dirty(); } }
 | 
			
		||||
 | 
			
		||||
	pub fn set_seal(&mut self, a: Vec<Bytes>) { self.seal = a; self.note_dirty(); }
 | 
			
		||||
 | 
			
		||||
	/// Get the hash of this header (sha3 of the RLP).
 | 
			
		||||
 | 
			
		||||
@ -78,6 +78,7 @@ extern crate flate2;
 | 
			
		||||
extern crate rocksdb;
 | 
			
		||||
extern crate heapsize;
 | 
			
		||||
extern crate crypto;
 | 
			
		||||
extern crate time;
 | 
			
		||||
 | 
			
		||||
extern crate env_logger;
 | 
			
		||||
#[cfg(feature = "jit" )]
 | 
			
		||||
 | 
			
		||||
@ -60,7 +60,7 @@ pub struct Spec {
 | 
			
		||||
	pub difficulty: U256,
 | 
			
		||||
	pub gas_limit: U256,
 | 
			
		||||
	pub gas_used: U256,
 | 
			
		||||
	pub timestamp: U256,
 | 
			
		||||
	pub timestamp: u64,
 | 
			
		||||
	pub extra_data: Bytes,
 | 
			
		||||
	pub genesis_state: HashMap<Address, Account>,
 | 
			
		||||
	pub seal_fields: usize,
 | 
			
		||||
@ -92,8 +92,8 @@ impl Spec {
 | 
			
		||||
	pub fn genesis_header(&self) -> Header {
 | 
			
		||||
		Header {
 | 
			
		||||
			parent_hash: self.parent_hash.clone(),
 | 
			
		||||
			timestamp: self.timestamp.clone(),
 | 
			
		||||
			number: U256::from(0u8),
 | 
			
		||||
			timestamp: self.timestamp,
 | 
			
		||||
			number: 0,
 | 
			
		||||
			author: self.author.clone(),
 | 
			
		||||
			transactions_root: SHA3_NULL_RLP.clone(),
 | 
			
		||||
			uncles_hash: RlpStream::new_list(0).out().sha3(),
 | 
			
		||||
@ -181,7 +181,7 @@ impl Spec {
 | 
			
		||||
			difficulty: U256::from_str(&genesis["difficulty"].as_string().unwrap()[2..]).unwrap(),
 | 
			
		||||
			gas_limit: U256::from_str(&genesis["gasLimit"].as_string().unwrap()[2..]).unwrap(),
 | 
			
		||||
			gas_used: U256::from(0u8),
 | 
			
		||||
			timestamp: U256::from_str(&genesis["timestamp"].as_string().unwrap()[2..]).unwrap(),
 | 
			
		||||
			timestamp: u64::from_str(&genesis["timestamp"].as_string().unwrap()[2..]).unwrap(),
 | 
			
		||||
			extra_data: genesis["extraData"].as_string().unwrap()[2..].from_hex().unwrap(),
 | 
			
		||||
			genesis_state: state,
 | 
			
		||||
			seal_fields: seal_fields,
 | 
			
		||||
 | 
			
		||||
@ -16,8 +16,8 @@
 | 
			
		||||
use util::*;
 | 
			
		||||
use std::mem::{replace};
 | 
			
		||||
use views::{HeaderView};
 | 
			
		||||
use header::{Header as BlockHeader};
 | 
			
		||||
use client::{BlockNumber, BlockChainClient, BlockStatus};
 | 
			
		||||
use header::{BlockNumber, Header as BlockHeader};
 | 
			
		||||
use client::{BlockChainClient, BlockStatus};
 | 
			
		||||
use sync::range_collection::{RangeCollection, ToUsize, FromUsize};
 | 
			
		||||
use error::*;
 | 
			
		||||
use sync::io::SyncIo;
 | 
			
		||||
 | 
			
		||||
@ -6,8 +6,8 @@ use util::sha3::Hashable;
 | 
			
		||||
use util::rlp::{self, Rlp, RlpStream, View, Stream};
 | 
			
		||||
use util::network::{PeerId, PacketId};
 | 
			
		||||
use util::error::UtilError;
 | 
			
		||||
use client::{BlockChainClient, BlockStatus, BlockNumber, TreeRoute, BlockQueueStatus, BlockChainInfo, ImportResult};
 | 
			
		||||
use header::Header as BlockHeader;
 | 
			
		||||
use client::{BlockChainClient, BlockStatus, TreeRoute, BlockQueueStatus, BlockChainInfo, ImportResult};
 | 
			
		||||
use header::{Header as BlockHeader, BlockNumber};
 | 
			
		||||
use sync::io::SyncIo;
 | 
			
		||||
use sync::chain::ChainSync;
 | 
			
		||||
 | 
			
		||||
@ -39,7 +39,7 @@ impl TestBlockChainClient {
 | 
			
		||||
			let mut header = BlockHeader::new();
 | 
			
		||||
			header.difficulty = From::from(n);
 | 
			
		||||
			header.parent_hash = self.last_hash.clone();
 | 
			
		||||
			header.number = From::from(n);
 | 
			
		||||
			header.number = n as BlockNumber;
 | 
			
		||||
			let mut uncles = RlpStream::new_list(if empty {0} else {1});
 | 
			
		||||
			if !empty {
 | 
			
		||||
				uncles.append(&H256::from(&U256::from(n)));
 | 
			
		||||
@ -118,7 +118,7 @@ impl BlockChainClient for TestBlockChainClient {
 | 
			
		||||
 | 
			
		||||
	fn import_block(&mut self, b: &[u8]) -> ImportResult {
 | 
			
		||||
		let header = Rlp::new(&b).val_at::<BlockHeader>(0);
 | 
			
		||||
		let number: usize = header.number.low_u64() as usize;
 | 
			
		||||
		let number: usize = header.number as usize;
 | 
			
		||||
		if number > self.blocks.len() {
 | 
			
		||||
			panic!("Unexpected block number. Expected {}, got {}", self.blocks.len(), number);
 | 
			
		||||
		}
 | 
			
		||||
@ -126,7 +126,7 @@ impl BlockChainClient for TestBlockChainClient {
 | 
			
		||||
			match self.blocks.get(&header.parent_hash) {
 | 
			
		||||
				Some(parent) => {
 | 
			
		||||
					let parent = Rlp::new(parent).val_at::<BlockHeader>(0);
 | 
			
		||||
					if parent.number != (header.number - From::from(1)) {
 | 
			
		||||
					if parent.number != (header.number - 1) {
 | 
			
		||||
						panic!("Unexpected block parent");
 | 
			
		||||
					}
 | 
			
		||||
				},
 | 
			
		||||
 | 
			
		||||
@ -113,8 +113,8 @@ impl<'a> HeaderView<'a> {
 | 
			
		||||
	pub fn difficulty(&self) -> U256 { self.rlp.val_at(7) }
 | 
			
		||||
 | 
			
		||||
	/// Returns block number.
 | 
			
		||||
	pub fn number(&self) -> U256 { self.rlp.val_at(8) }
 | 
			
		||||
 | 
			
		||||
	pub fn number(&self) -> BlockNumber { self.rlp.val_at(8) }
 | 
			
		||||
	
 | 
			
		||||
	/// Returns block gas limit.
 | 
			
		||||
	pub fn gas_limit(&self) -> U256 { self.rlp.val_at(9) }
 | 
			
		||||
 | 
			
		||||
@ -122,7 +122,7 @@ impl<'a> HeaderView<'a> {
 | 
			
		||||
	pub fn gas_used(&self) -> U256 { self.rlp.val_at(10) }
 | 
			
		||||
 | 
			
		||||
	/// Returns timestamp.
 | 
			
		||||
	pub fn timestamp(&self) -> U256 { self.rlp.val_at(11) }
 | 
			
		||||
	pub fn timestamp(&self) -> u64 { self.rlp.val_at(11) }
 | 
			
		||||
 | 
			
		||||
	/// Returns block extra data.
 | 
			
		||||
	pub fn extra_data(&self) -> Bytes { self.rlp.val_at(12) }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user