2016-01-09 12:30:41 +01:00
|
|
|
use util::*;
|
2016-01-11 01:07:58 +01:00
|
|
|
use header::BlockNumber;
|
2015-12-17 01:54:24 +01:00
|
|
|
use rocksdb::{DB, Writable};
|
2015-12-13 17:33:11 +01:00
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
/// Represents index of extra data in database
|
2015-12-13 22:39:01 +01:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum ExtrasIndex {
|
|
|
|
BlockDetails = 0,
|
|
|
|
BlockHash = 1,
|
2015-12-14 13:32:22 +01:00
|
|
|
TransactionAddress = 2,
|
|
|
|
BlockLogBlooms = 3,
|
|
|
|
BlocksBlooms = 4
|
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
/// trait used to write Extras data to db
|
|
|
|
pub trait ExtrasWritable {
|
|
|
|
fn put_extras<K, T>(&self, hash: &K, value: &T) where
|
|
|
|
T: ExtrasIndexable + Encodable,
|
|
|
|
K: ExtrasSliceConvertable;
|
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
/// trait used to read Extras data from db
|
|
|
|
pub trait ExtrasReadable {
|
|
|
|
fn get_extras<K, T>(&self, hash: &K) -> Option<T> where
|
|
|
|
T: ExtrasIndexable + Decodable,
|
|
|
|
K: ExtrasSliceConvertable;
|
|
|
|
|
|
|
|
fn extras_exists<K, T>(&self, hash: &K) -> bool where
|
|
|
|
T: ExtrasIndexable,
|
|
|
|
K: ExtrasSliceConvertable;
|
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
impl<W> ExtrasWritable for W where W: Writable {
|
|
|
|
fn put_extras<K, T>(&self, hash: &K, value: &T) where
|
|
|
|
T: ExtrasIndexable + Encodable,
|
|
|
|
K: ExtrasSliceConvertable {
|
|
|
|
|
|
|
|
self.put(&hash.to_extras_slice(T::extras_index()), &encode(value)).unwrap()
|
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
impl ExtrasReadable for DB {
|
|
|
|
fn get_extras<K, T>(&self, hash: &K) -> Option<T> where
|
|
|
|
T: ExtrasIndexable + Decodable,
|
|
|
|
K: ExtrasSliceConvertable {
|
|
|
|
|
|
|
|
self.get(&hash.to_extras_slice(T::extras_index())).unwrap()
|
|
|
|
.map(|v| decode(&v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extras_exists<K, T>(&self, hash: &K) -> bool where
|
|
|
|
T: ExtrasIndexable,
|
|
|
|
K: ExtrasSliceConvertable {
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
self.get(&hash.to_extras_slice(T::extras_index())).unwrap().is_some()
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
/// Implementations should convert arbitrary type to database key slice
|
2015-12-13 22:39:01 +01:00
|
|
|
pub trait ExtrasSliceConvertable {
|
|
|
|
fn to_extras_slice(&self, i: ExtrasIndex) -> H264;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExtrasSliceConvertable for H256 {
|
|
|
|
fn to_extras_slice(&self, i: ExtrasIndex) -> H264 {
|
|
|
|
let mut slice = H264::from_slice(self);
|
|
|
|
slice[32] = i as u8;
|
|
|
|
slice
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExtrasSliceConvertable for U256 {
|
|
|
|
fn to_extras_slice(&self, i: ExtrasIndex) -> H264 {
|
|
|
|
H256::from(self).to_extras_slice(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 22:55:07 +01:00
|
|
|
// NICE: make less horrible.
|
2016-01-11 01:07:58 +01:00
|
|
|
impl ExtrasSliceConvertable for BlockNumber {
|
2016-01-10 22:55:07 +01:00
|
|
|
fn to_extras_slice(&self, i: ExtrasIndex) -> H264 {
|
|
|
|
U256::from(*self).to_extras_slice(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
/// Types implementing this trait can be indexed in extras database
|
|
|
|
pub trait ExtrasIndexable {
|
|
|
|
fn extras_index() -> ExtrasIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExtrasIndexable for H256 {
|
|
|
|
fn extras_index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::BlockHash
|
|
|
|
}
|
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
/// Familial details concerning a block
|
2015-12-17 17:20:10 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2015-12-13 17:33:11 +01:00
|
|
|
pub struct BlockDetails {
|
2016-01-11 01:07:58 +01:00
|
|
|
pub number: BlockNumber,
|
2015-12-13 17:33:11 +01:00
|
|
|
pub total_difficulty: U256,
|
|
|
|
pub parent: H256,
|
|
|
|
pub children: Vec<H256>
|
|
|
|
}
|
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
impl ExtrasIndexable for BlockDetails {
|
|
|
|
fn extras_index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::BlockDetails
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:39:15 +01:00
|
|
|
impl HeapSizeOf for BlockDetails {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
self.children.heap_size_of_children()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 17:33:11 +01:00
|
|
|
impl Decodable for BlockDetails {
|
|
|
|
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
2015-12-14 12:18:53 +01:00
|
|
|
let d = try!(decoder.as_list());
|
|
|
|
let details = BlockDetails {
|
|
|
|
number: try!(Decodable::decode(&d[0])),
|
|
|
|
total_difficulty: try!(Decodable::decode(&d[1])),
|
|
|
|
parent: try!(Decodable::decode(&d[2])),
|
|
|
|
children: try!(Decodable::decode(&d[3]))
|
|
|
|
};
|
|
|
|
Ok(details)
|
2015-12-13 17:33:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for BlockDetails {
|
|
|
|
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
|
|
|
encoder.emit_list(| e | {
|
|
|
|
self.number.encode(e);
|
|
|
|
self.total_difficulty.encode(e);
|
|
|
|
self.parent.encode(e);
|
|
|
|
self.children.encode(e);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 13:32:22 +01:00
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
/// Log blooms of certain block
|
2015-12-14 13:32:22 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct BlockLogBlooms {
|
|
|
|
pub blooms: Vec<H2048>
|
|
|
|
}
|
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
impl ExtrasIndexable for BlockLogBlooms {
|
|
|
|
fn extras_index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::BlockLogBlooms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:39:15 +01:00
|
|
|
impl HeapSizeOf for BlockLogBlooms {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
self.blooms.heap_size_of_children()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 13:32:22 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
/// Neighboring log blooms on certain level
|
2015-12-14 13:32:22 +01:00
|
|
|
pub struct BlocksBlooms {
|
|
|
|
pub blooms: [H2048; 16]
|
|
|
|
}
|
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
impl ExtrasIndexable for BlocksBlooms {
|
|
|
|
fn extras_index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::BlocksBlooms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:39:15 +01:00
|
|
|
impl HeapSizeOf for BlocksBlooms {
|
|
|
|
fn heap_size_of_children(&self) -> usize { 0 }
|
|
|
|
}
|
|
|
|
|
2015-12-14 13:32:22 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
/// Represents address of certain transaction within block
|
2015-12-14 13:32:22 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct TransactionAddress {
|
|
|
|
pub block_hash: H256,
|
|
|
|
pub index: u64
|
|
|
|
}
|
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
impl ExtrasIndexable for TransactionAddress {
|
|
|
|
fn extras_index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::TransactionAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:39:15 +01:00
|
|
|
impl HeapSizeOf for TransactionAddress {
|
|
|
|
fn heap_size_of_children(&self) -> usize { 0 }
|
|
|
|
}
|
|
|
|
|
2015-12-14 13:32:22 +01:00
|
|
|
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);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|