2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-02 15:29:53 +01:00
|
|
|
//! Blockchain DB extras.
|
|
|
|
|
2016-01-09 12:30:41 +01:00
|
|
|
use util::*;
|
2016-01-11 01:07:58 +01:00
|
|
|
use header::BlockNumber;
|
2016-02-17 12:35:37 +01:00
|
|
|
use receipt::Receipt;
|
2016-04-15 18:54:35 +02:00
|
|
|
use db::Key;
|
2015-12-13 17:33:11 +01:00
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
/// Represents index of extra data in database
|
2016-01-18 19:23:28 +01:00
|
|
|
#[derive(Copy, Debug, Hash, Eq, PartialEq, Clone)]
|
2015-12-13 22:39:01 +01:00
|
|
|
pub enum ExtrasIndex {
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Block details index
|
2015-12-13 22:39:01 +01:00
|
|
|
BlockDetails = 0,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Block hash index
|
2015-12-13 22:39:01 +01:00
|
|
|
BlockHash = 1,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Transaction address index
|
2015-12-14 13:32:22 +01:00
|
|
|
TransactionAddress = 2,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Block log blooms index
|
2015-12-14 13:32:22 +01:00
|
|
|
BlockLogBlooms = 3,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Block blooms index
|
2016-02-17 12:35:37 +01:00
|
|
|
BlocksBlooms = 4,
|
|
|
|
/// Block receipts index
|
2016-02-19 14:17:04 +01:00
|
|
|
BlockReceipts = 5,
|
2016-03-11 10:57:58 +01:00
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
fn with_index(hash: &H256, i: ExtrasIndex) -> H264 {
|
|
|
|
let mut slice = H264::from_slice(hash);
|
|
|
|
slice[32] = i as u8;
|
|
|
|
slice
|
2015-12-17 01:54:24 +01:00
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
pub trait ExtrasIndexable {
|
|
|
|
fn index() -> ExtrasIndex;
|
2015-12-17 01:54:24 +01:00
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl ExtrasIndexable for H256 {
|
|
|
|
fn index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::BlockHash
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 10:57:58 +01:00
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl ExtrasIndexable for BlockDetails {
|
|
|
|
fn index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::BlockDetails
|
2015-12-17 01:54:24 +01:00
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl ExtrasIndexable for TransactionAddress {
|
|
|
|
fn index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::TransactionAddress
|
|
|
|
}
|
|
|
|
}
|
2015-12-17 01:54:24 +01:00
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl ExtrasIndexable for BlockLogBlooms {
|
|
|
|
fn index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::BlockLogBlooms
|
2015-12-17 01:54:24 +01:00
|
|
|
}
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
2015-12-17 01:54:24 +01:00
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl ExtrasIndexable for BlocksBlooms {
|
|
|
|
fn index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::BlocksBlooms
|
|
|
|
}
|
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl ExtrasIndexable for BlockReceipts {
|
|
|
|
fn index() -> ExtrasIndex {
|
|
|
|
ExtrasIndex::BlockReceipts
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl Key<H256> for BlockNumber {
|
|
|
|
fn key(&self) -> H264 {
|
|
|
|
with_index(&H256::from(*self), ExtrasIndex::BlockHash)
|
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl Key<BlockDetails> for H256 {
|
|
|
|
fn key(&self) -> H264 {
|
|
|
|
with_index(self, ExtrasIndex::BlockDetails)
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl Key<TransactionAddress> for H256 {
|
|
|
|
fn key(&self) -> H264 {
|
|
|
|
with_index(self, ExtrasIndex::TransactionAddress)
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl Key<BlockLogBlooms> for H256 {
|
|
|
|
fn key(&self) -> H264 {
|
|
|
|
with_index(self, ExtrasIndex::BlockLogBlooms)
|
2016-01-10 22:55:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl Key<BlocksBlooms> for H256 {
|
|
|
|
fn key(&self) -> H264 {
|
|
|
|
with_index(self, ExtrasIndex::BlocksBlooms)
|
|
|
|
}
|
2015-12-17 01:54:24 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
impl Key<BlockReceipts> for H256 {
|
|
|
|
fn key(&self) -> H264 {
|
|
|
|
with_index(self, ExtrasIndex::BlockReceipts)
|
2015-12-17 01:54:24 +01:00
|
|
|
}
|
|
|
|
}
|
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-02-03 16:43:48 +01:00
|
|
|
/// Block number
|
2016-01-11 01:07:58 +01:00
|
|
|
pub number: BlockNumber,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Total difficulty of the block and all its parents
|
2015-12-13 17:33:11 +01:00
|
|
|
pub total_difficulty: U256,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Parent block hash
|
2015-12-13 17:33:11 +01:00
|
|
|
pub parent: H256,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// List of children block hashes
|
2015-12-13 17:33:11 +01:00
|
|
|
pub children: Vec<H256>
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-01-29 13:59:29 +01:00
|
|
|
let d = decoder.as_rlp();
|
2015-12-14 12:18:53 +01:00
|
|
|
let details = BlockDetails {
|
2016-01-29 13:59:29 +01:00
|
|
|
number: try!(d.val_at(0)),
|
|
|
|
total_difficulty: try!(d.val_at(1)),
|
|
|
|
parent: try!(d.val_at(2)),
|
|
|
|
children: try!(d.val_at(3)),
|
2015-12-14 12:18:53 +01:00
|
|
|
};
|
|
|
|
Ok(details)
|
2015-12-13 17:33:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for BlockDetails {
|
2016-01-27 17:22:01 +01:00
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.begin_list(4);
|
|
|
|
s.append(&self.number);
|
|
|
|
s.append(&self.total_difficulty);
|
|
|
|
s.append(&self.parent);
|
2016-01-28 20:13:05 +01:00
|
|
|
s.append(&self.children);
|
2015-12-13 17:33:11 +01:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2016-02-03 16:43:48 +01:00
|
|
|
/// List of log blooms for the block
|
2015-12-14 13:32:22 +01:00
|
|
|
pub blooms: Vec<H2048>
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-01-27 17:22:01 +01:00
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
2016-01-28 20:13:05 +01:00
|
|
|
s.append(&self.blooms);
|
2015-12-14 13:32:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-02-03 16:43:48 +01:00
|
|
|
/// List of block blooms.
|
2016-02-19 14:17:04 +01:00
|
|
|
pub blooms: [H2048; 16],
|
2015-12-14 13:32:22 +01:00
|
|
|
}
|
|
|
|
|
2016-03-11 10:57:58 +01:00
|
|
|
impl Default for BlocksBlooms {
|
|
|
|
fn default() -> Self {
|
|
|
|
BlocksBlooms::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 00:40:45 +01:00
|
|
|
impl BlocksBlooms {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
BlocksBlooms { blooms: unsafe { ::std::mem::zeroed() }}
|
|
|
|
}
|
2015-12-14 13:32:22 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-01-27 17:22:01 +01:00
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
2015-12-14 13:32:22 +01:00
|
|
|
let blooms_ref: &[H2048] = &self.blooms;
|
2016-01-28 20:13:05 +01:00
|
|
|
s.append(&blooms_ref);
|
2015-12-14 13:32:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Block hash
|
2015-12-14 13:32:22 +01:00
|
|
|
pub block_hash: H256,
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Transaction index within the block
|
2016-02-08 15:53:22 +01:00
|
|
|
pub index: usize
|
2015-12-14 13:32:22 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-01-29 13:59:29 +01:00
|
|
|
let d = decoder.as_rlp();
|
2015-12-14 13:32:22 +01:00
|
|
|
let tx_address = TransactionAddress {
|
2016-01-29 13:59:29 +01:00
|
|
|
block_hash: try!(d.val_at(0)),
|
|
|
|
index: try!(d.val_at(1)),
|
2015-12-14 13:32:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(tx_address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for TransactionAddress {
|
2016-01-27 17:22:01 +01:00
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.begin_list(2);
|
|
|
|
s.append(&self.block_hash);
|
|
|
|
s.append(&self.index);
|
2015-12-14 13:32:22 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-17 12:35:37 +01:00
|
|
|
|
|
|
|
/// Contains all block receipts.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct BlockReceipts {
|
2016-02-22 10:14:31 +01:00
|
|
|
pub receipts: Vec<Receipt>,
|
2016-02-17 12:35:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockReceipts {
|
|
|
|
pub fn new(receipts: Vec<Receipt>) -> Self {
|
|
|
|
BlockReceipts {
|
|
|
|
receipts: receipts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for BlockReceipts {
|
|
|
|
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
|
|
|
Ok(BlockReceipts {
|
|
|
|
receipts: try!(Decodable::decode(decoder))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for BlockReceipts {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.append(&self.receipts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HeapSizeOf for BlockReceipts {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
self.receipts.heap_size_of_children()
|
|
|
|
}
|
|
|
|
}
|