From 01ee84b7d09a6ec2830cff57f76853cf88dc8a1d Mon Sep 17 00:00:00 2001 From: debris Date: Mon, 14 Dec 2015 13:32:22 +0100 Subject: [PATCH] bc extras: TransactionAddress, BlockLogBlooms, BlocksBlooms --- src/blockchain.rs | 20 +++++++---- src/extras.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 8 deletions(-) diff --git a/src/blockchain.rs b/src/blockchain.rs index 075177b33..9f0dd92ea 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -18,7 +18,6 @@ use genesis::*; use extras::*; pub struct BlockChain { - // TODO: consider wrapping `genesis_info` into Arc // rlp list of 3 genesis_block: Vec, // genesis block header @@ -29,8 +28,12 @@ pub struct BlockChain { last_block_number: U256, // extras - blocks_details: Extras, - blocks_hashes: Extras, + block_details_hash: Extras, + block_hashes_hash: Extras, + transaction_addresses_hash: Extras, + block_logs_hash: Extras, + blocks_blooms_hash: Extras, + extras_db: DB } @@ -87,8 +90,11 @@ impl BlockChain { genesis_hash: genesis_hash, genesis_state: genesis_state, last_block_number: U256::from(0u8), - blocks_details: Extras::new(ExtrasIndex::BlockDetails), - blocks_hashes: Extras::new(ExtrasIndex::BlockHash), + block_details_hash: Extras::new(ExtrasIndex::BlockDetails), + block_hashes_hash: Extras::new(ExtrasIndex::BlockHash), + transaction_addresses_hash: Extras::new(ExtrasIndex::TransactionAddress), + block_logs_hash: Extras::new(ExtrasIndex::BlockLogBlooms), + blocks_blooms_hash: Extras::new(ExtrasIndex::BlocksBlooms), extras_db: db } } @@ -135,7 +141,7 @@ impl BlockChain { /// Get the hash of given block's number pub fn number_hash(&self, hash: &U256) -> Option { - self.query_extras(hash, &self.blocks_hashes) + self.query_extras(hash, &self.block_hashes_hash) } /// Returns true if the given block is known @@ -144,7 +150,7 @@ impl BlockChain { // TODO: first do lookup in blocks_db for given hash // TODO: consider taking into account current block - match self.query_extras(hash, &self.blocks_details) { + match self.query_extras(hash, &self.block_details_hash) { None => false, Some(details) => details.number <= self.last_block_number } diff --git a/src/extras.rs b/src/extras.rs index f394e790b..5606f2e10 100644 --- a/src/extras.rs +++ b/src/extras.rs @@ -11,7 +11,10 @@ use util::rlp::*; pub enum ExtrasIndex { BlockDetails = 0, BlockHash = 1, -} + TransactionAddress = 2, + BlockLogBlooms = 3, + BlocksBlooms = 4 +} /// rw locked extra data with slice suffix // consifer if arc needed here, since blockchain itself will be wrapped @@ -83,3 +86,86 @@ impl Encodable for BlockDetails { }) } } + +#[derive(Clone)] +pub struct BlockLogBlooms { + pub blooms: Vec +} + +impl Decodable for BlockLogBlooms { + fn decode(decoder: &D) -> Result where D: Decoder { + let block_blooms = BlockLogBlooms { + blooms: try!(Decodable::decode(decoder)) + }; + + Ok(block_blooms) + } +} + +impl Encodable for BlockLogBlooms { + fn encode(&self, encoder: &mut E) where E: Encoder { + self.blooms.encode(encoder); + } +} + +pub struct BlocksBlooms { + pub blooms: [H2048; 16] +} + +impl Clone for BlocksBlooms { + fn clone(&self) -> Self { + let mut blooms: [H2048; 16] = unsafe { ::std::mem::uninitialized() }; + + for i in 0..self.blooms.len() { + blooms[i] = self.blooms[i].clone(); + } + + BlocksBlooms { + blooms: blooms + } + } +} + +impl Decodable for BlocksBlooms { + fn decode(decoder: &D) -> Result where D: Decoder { + let blocks_blooms = BlocksBlooms { + blooms: try!(Decodable::decode(decoder)) + }; + + Ok(blocks_blooms) + } +} + +impl Encodable for BlocksBlooms { + fn encode(&self, encoder: &mut E) where E: Encoder { + let blooms_ref: &[H2048] = &self.blooms; + blooms_ref.encode(encoder); + } +} + +#[derive(Clone)] +pub struct TransactionAddress { + pub block_hash: H256, + pub index: u64 +} + +impl Decodable for TransactionAddress { + fn decode(decoder: &D) -> Result where D: Decoder { + let d = try!(decoder.as_list()); + let tx_address = TransactionAddress { + block_hash: try!(Decodable::decode(&d[0])), + index: try!(Decodable::decode(&d[1])) + }; + + Ok(tx_address) + } +} + +impl Encodable for TransactionAddress { + fn encode(&self, encoder: &mut E) where E: Encoder { + encoder.emit_list(| e | { + self.block_hash.encode(e); + self.index.encode(e); + }) + } +}