bc extras: TransactionAddress, BlockLogBlooms, BlocksBlooms

This commit is contained in:
debris 2015-12-14 13:32:22 +01:00
parent fe9c8f8c11
commit 01ee84b7d0
2 changed files with 100 additions and 8 deletions

View File

@ -18,7 +18,6 @@ use genesis::*;
use extras::*;
pub struct BlockChain {
// TODO: consider wrapping `genesis_info` into Arc<GenesisInfo>
// rlp list of 3
genesis_block: Vec<u8>,
// genesis block header
@ -29,8 +28,12 @@ pub struct BlockChain {
last_block_number: U256,
// extras
blocks_details: Extras<H256, BlockDetails>,
blocks_hashes: Extras<U256, H256>,
block_details_hash: Extras<H256, BlockDetails>,
block_hashes_hash: Extras<U256, H256>,
transaction_addresses_hash: Extras<H256, TransactionAddress>,
block_logs_hash: Extras<H256, BlockLogBlooms>,
blocks_blooms_hash: Extras<H256, BlocksBlooms>,
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<H256> {
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
}

View File

@ -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<H2048>
}
impl Decodable for BlockLogBlooms {
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
let block_blooms = BlockLogBlooms {
blooms: try!(Decodable::decode(decoder))
};
Ok(block_blooms)
}
}
impl Encodable for BlockLogBlooms {
fn encode<E>(&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<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
let blocks_blooms = BlocksBlooms {
blooms: try!(Decodable::decode(decoder))
};
Ok(blocks_blooms)
}
}
impl Encodable for BlocksBlooms {
fn encode<E>(&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<D>(decoder: &D) -> Result<Self, DecoderError> 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<E>(&self, encoder: &mut E) where E: Encoder {
encoder.emit_list(| e | {
self.block_hash.encode(e);
self.index.encode(e);
})
}
}