2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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-05-26 18:24:51 +02:00
|
|
|
use bloomchain;
|
2017-06-28 13:17:36 +02:00
|
|
|
use blooms::{GroupPosition, BloomGroup};
|
|
|
|
use db::Key;
|
|
|
|
use engines::epoch::{Transition as EpochTransition};
|
2016-01-11 01:07:58 +01:00
|
|
|
use header::BlockNumber;
|
2016-02-17 12:35:37 +01:00
|
|
|
use receipt::Receipt;
|
2017-06-28 13:17:36 +02:00
|
|
|
|
|
|
|
use rlp::*;
|
|
|
|
use util::*;
|
|
|
|
use util::kvdb::PREFIX_LEN as DB_PREFIX_LEN;
|
|
|
|
|
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 blooms index
|
2016-05-26 18:24:51 +02:00
|
|
|
BlocksBlooms = 3,
|
2016-02-17 12:35:37 +01:00
|
|
|
/// Block receipts index
|
2016-05-26 18:24:51 +02:00
|
|
|
BlockReceipts = 4,
|
2017-04-19 14:58:19 +02:00
|
|
|
/// Epoch transition data index.
|
|
|
|
EpochTransitions = 5,
|
2017-06-28 13:17:36 +02:00
|
|
|
/// Pending epoch transition data index.
|
|
|
|
PendingEpochTransition = 6,
|
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 {
|
2016-05-26 18:24:51 +02:00
|
|
|
let mut result = H264::default();
|
|
|
|
result[0] = i as u8;
|
2016-08-05 17:00:46 +02:00
|
|
|
(*result)[1..].clone_from_slice(hash);
|
2016-05-26 18:24:51 +02:00
|
|
|
result
|
2015-12-17 01:54:24 +01:00
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
pub struct BlockNumberKey([u8; 5]);
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
impl Deref for BlockNumberKey {
|
|
|
|
type Target = [u8];
|
2016-03-11 10:57:58 +01:00
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
2015-12-17 01:54:24 +01:00
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
impl Key<H256> for BlockNumber {
|
|
|
|
type Target = BlockNumberKey;
|
2015-12-17 01:54:24 +01:00
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
fn key(&self) -> Self::Target {
|
|
|
|
let mut result = [0u8; 5];
|
|
|
|
result[0] = ExtrasIndex::BlockHash as u8;
|
|
|
|
result[1] = (self >> 24) as u8;
|
|
|
|
result[2] = (self >> 16) as u8;
|
|
|
|
result[3] = (self >> 8) as u8;
|
|
|
|
result[4] = *self as u8;
|
|
|
|
BlockNumberKey(result)
|
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-05-26 18:24:51 +02:00
|
|
|
impl Key<BlockDetails> for H256 {
|
|
|
|
type Target = H264;
|
2015-12-13 22:39:01 +01:00
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
fn key(&self) -> H264 {
|
|
|
|
with_index(self, ExtrasIndex::BlockDetails)
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
pub struct LogGroupKey([u8; 6]);
|
2016-04-30 17:41:24 +02:00
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
impl Deref for LogGroupKey {
|
|
|
|
type Target = [u8];
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
2016-04-15 18:54:35 +02:00
|
|
|
}
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
|
|
|
pub struct LogGroupPosition(GroupPosition);
|
2016-04-30 17:41:24 +02:00
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
impl From<bloomchain::group::GroupPosition> for LogGroupPosition {
|
|
|
|
fn from(position: bloomchain::group::GroupPosition) -> Self {
|
|
|
|
LogGroupPosition(From::from(position))
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
impl HeapSizeOf for LogGroupPosition {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
self.0.heap_size_of_children()
|
2015-12-13 22:39:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
impl Key<BloomGroup> for LogGroupPosition {
|
|
|
|
type Target = LogGroupKey;
|
2016-04-30 17:41:24 +02:00
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
fn key(&self) -> Self::Target {
|
|
|
|
let mut result = [0u8; 6];
|
|
|
|
result[0] = ExtrasIndex::BlocksBlooms as u8;
|
|
|
|
result[1] = self.0.level;
|
|
|
|
result[2] = (self.0.index >> 24) as u8;
|
|
|
|
result[3] = (self.0.index >> 16) as u8;
|
|
|
|
result[4] = (self.0.index >> 8) as u8;
|
|
|
|
result[5] = self.0.index as u8;
|
|
|
|
LogGroupKey(result)
|
2016-01-10 22:55:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
impl Key<TransactionAddress> for H256 {
|
2016-04-30 17:41:24 +02:00
|
|
|
type Target = H264;
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
fn key(&self) -> H264 {
|
2016-05-26 18:24:51 +02:00
|
|
|
with_index(self, ExtrasIndex::TransactionAddress)
|
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 Key<BlockReceipts> for H256 {
|
2016-04-30 17:41:24 +02:00
|
|
|
type Target = H264;
|
|
|
|
|
2016-04-15 18:54:35 +02:00
|
|
|
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
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
impl Key<::engines::epoch::PendingTransition> for H256 {
|
|
|
|
type Target = H264;
|
|
|
|
|
|
|
|
fn key(&self) -> H264 {
|
|
|
|
with_index(self, ExtrasIndex::PendingEpochTransition)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 14:58:19 +02:00
|
|
|
/// length of epoch keys.
|
|
|
|
pub const EPOCH_KEY_LEN: usize = DB_PREFIX_LEN + 16;
|
|
|
|
|
|
|
|
/// epoch key prefix.
|
|
|
|
/// used to iterate over all epoch transitions in order from genesis.
|
2017-04-19 16:27:45 +02:00
|
|
|
pub const EPOCH_KEY_PREFIX: &'static [u8; DB_PREFIX_LEN] = &[
|
|
|
|
ExtrasIndex::EpochTransitions as u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
];
|
2017-04-19 14:58:19 +02:00
|
|
|
|
|
|
|
pub struct EpochTransitionsKey([u8; EPOCH_KEY_LEN]);
|
|
|
|
impl Deref for EpochTransitionsKey {
|
|
|
|
type Target = [u8];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[u8] { &self.0[..] }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Key<EpochTransitions> for u64 {
|
|
|
|
type Target = EpochTransitionsKey;
|
|
|
|
|
|
|
|
fn key(&self) -> Self::Target {
|
|
|
|
let mut arr = [0u8; EPOCH_KEY_LEN];
|
2017-04-19 16:27:45 +02:00
|
|
|
arr[..DB_PREFIX_LEN].copy_from_slice(&EPOCH_KEY_PREFIX[..]);
|
2017-04-19 14:58:19 +02:00
|
|
|
|
|
|
|
write!(&mut arr[DB_PREFIX_LEN..], "{:016x}", self)
|
|
|
|
.expect("format arg is valid; no more than 16 chars will be written; qed");
|
|
|
|
|
|
|
|
EpochTransitionsKey(arr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2017-04-19 14:58:19 +02:00
|
|
|
pub children: Vec<H256>,
|
2015-12-13 17:33:11 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-03-22 14:41:46 +01:00
|
|
|
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
2015-12-14 12:18:53 +01:00
|
|
|
let details = BlockDetails {
|
2017-03-22 14:41:46 +01:00
|
|
|
number: rlp.val_at(0)?,
|
|
|
|
total_difficulty: rlp.val_at(1)?,
|
|
|
|
parent: rlp.val_at(2)?,
|
|
|
|
children: rlp.list_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);
|
2017-03-20 19:14:29 +01:00
|
|
|
s.append_list(&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
|
|
|
/// Represents address of certain transaction within block
|
2016-08-31 16:55:43 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2015-12-14 13:32:22 +01:00
|
|
|
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 {
|
2017-03-22 14:41:46 +01:00
|
|
|
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
2015-12-14 13:32:22 +01:00
|
|
|
let tx_address = TransactionAddress {
|
2017-03-22 14:41:46 +01:00
|
|
|
block_hash: rlp.val_at(0)?,
|
|
|
|
index: rlp.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 {
|
2017-03-22 14:41:46 +01:00
|
|
|
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
2016-02-17 12:35:37 +01:00
|
|
|
Ok(BlockReceipts {
|
2017-03-22 14:41:46 +01:00
|
|
|
receipts: rlp.as_list()?,
|
2016-02-17 12:35:37 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for BlockReceipts {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
2017-03-20 19:14:29 +01:00
|
|
|
s.append_list(&self.receipts);
|
2016-02-17 12:35:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HeapSizeOf for BlockReceipts {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
self.receipts.heap_size_of_children()
|
|
|
|
}
|
|
|
|
}
|
2017-01-30 20:01:32 +01:00
|
|
|
|
2017-04-19 14:58:19 +02:00
|
|
|
/// Candidate transitions to an epoch with specific number.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct EpochTransitions {
|
|
|
|
pub number: u64,
|
|
|
|
pub candidates: Vec<EpochTransition>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for EpochTransitions {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.begin_list(2).append(&self.number).append_list(&self.candidates);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for EpochTransitions {
|
|
|
|
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
|
|
|
Ok(EpochTransitions {
|
|
|
|
number: rlp.val_at(0)?,
|
|
|
|
candidates: rlp.list_at(1)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 20:01:32 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use rlp::*;
|
|
|
|
use super::BlockReceipts;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_block_receipts() {
|
|
|
|
let br = BlockReceipts::new(Vec::new());
|
|
|
|
|
|
|
|
let mut s = RlpStream::new_list(2);
|
|
|
|
s.append(&br);
|
|
|
|
assert!(!s.is_finished(), "List shouldn't finished yet");
|
|
|
|
s.append(&br);
|
|
|
|
assert!(s.is_finished(), "List should be finished now");
|
|
|
|
s.out();
|
|
|
|
}
|
|
|
|
}
|