2015-12-08 16:31:36 +01:00
|
|
|
use util::hash::*;
|
2015-12-13 16:44:14 +01:00
|
|
|
use util::bytes::*;
|
2015-12-08 16:31:36 +01:00
|
|
|
use util::uint::*;
|
|
|
|
use util::rlp::*;
|
2015-12-11 03:51:23 +01:00
|
|
|
use util::sha3;
|
2015-12-08 16:31:36 +01:00
|
|
|
|
2015-12-09 19:03:25 +01:00
|
|
|
/// view onto block header rlp
|
|
|
|
pub struct BlockView<'a> {
|
|
|
|
rlp: Rlp<'a>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BlockView<'a> {
|
|
|
|
pub fn new(bytes: &'a [u8]) -> BlockView<'a> {
|
|
|
|
BlockView {
|
|
|
|
rlp: Rlp::new(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 15:52:37 +01:00
|
|
|
pub fn new_from_rlp(rlp: Rlp<'a>) -> BlockView<'a> {
|
|
|
|
BlockView {
|
|
|
|
rlp: rlp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:03:25 +01:00
|
|
|
pub fn parent_hash(&self) -> H256 { self.rlp.val_at(0) }
|
|
|
|
pub fn uncles_hash(&self) -> H256 { self.rlp.val_at(1) }
|
|
|
|
pub fn coinbase(&self) -> Address { self.rlp.val_at(2) }
|
|
|
|
pub fn state_root(&self) -> H256 { self.rlp.val_at(3) }
|
|
|
|
pub fn transactions_root(&self) -> H256 { self.rlp.val_at(4) }
|
|
|
|
pub fn receipts_root(&self) -> H256 { self.rlp.val_at(5) }
|
|
|
|
pub fn log_bloom(&self) -> H2048 { self.rlp.val_at(6) }
|
|
|
|
pub fn difficulty(&self) -> U256 { self.rlp.val_at(7) }
|
|
|
|
pub fn number(&self) -> U256 { self.rlp.val_at(8) }
|
|
|
|
pub fn gas_limit(&self) -> U256 { self.rlp.val_at(9) }
|
|
|
|
pub fn gas_usd(&self) -> U256 { self.rlp.val_at(10) }
|
|
|
|
pub fn timestamp(&self) -> U256 { self.rlp.val_at(11) }
|
|
|
|
pub fn mix_hash(&self) -> H256 { self.rlp.val_at(12) }
|
|
|
|
pub fn nonce(&self) -> H64 { self.rlp.val_at(13) }
|
|
|
|
}
|
|
|
|
|
2015-12-11 03:51:23 +01:00
|
|
|
impl<'a> sha3::Hashable for BlockView<'a> {
|
|
|
|
fn sha3(&self) -> H256 {
|
2015-12-12 15:52:37 +01:00
|
|
|
self.rlp.raw().sha3()
|
2015-12-11 03:51:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 16:44:14 +01:00
|
|
|
pub static ZERO_ADDRESS: Address = Address([0x00; 20]);
|
|
|
|
pub static ZERO_H256: H256 = H256([0x00; 32]);
|
|
|
|
pub static ZERO_LOGBLOOM: LogBloom = H2048([0x00; 256]);
|
|
|
|
|
|
|
|
pub type LogBloom = H2048;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Header {
|
2015-12-08 16:31:36 +01:00
|
|
|
parent_hash: H256,
|
2015-12-13 16:44:14 +01:00
|
|
|
timestamp: U256,
|
|
|
|
number: U256,
|
|
|
|
author: Address,
|
|
|
|
|
2015-12-08 16:31:36 +01:00
|
|
|
transactions_root: H256,
|
2015-12-09 19:03:25 +01:00
|
|
|
uncles_hash: H256,
|
2015-12-13 16:44:14 +01:00
|
|
|
extra_data: Bytes,
|
|
|
|
|
2015-12-08 16:31:36 +01:00
|
|
|
state_root: H256,
|
|
|
|
receipts_root: H256,
|
2015-12-13 16:44:14 +01:00
|
|
|
log_bloom: LogBloom,
|
2015-12-08 16:31:36 +01:00
|
|
|
gas_used: U256,
|
2015-12-13 16:44:14 +01:00
|
|
|
gas_limit: U256,
|
|
|
|
|
|
|
|
difficulty: U256,
|
|
|
|
seal: Vec<Bytes>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Header {
|
|
|
|
pub fn new() -> Header {
|
|
|
|
Header {
|
|
|
|
parent_hash: ZERO_H256.clone(),
|
|
|
|
timestamp: BAD_U256.clone(),
|
|
|
|
number: ZERO_U256.clone(),
|
|
|
|
author: ZERO_ADDRESS.clone(),
|
|
|
|
|
|
|
|
transactions_root: ZERO_H256.clone(),
|
|
|
|
uncles_hash: ZERO_H256.clone(),
|
|
|
|
extra_data: vec![],
|
|
|
|
|
|
|
|
state_root: ZERO_H256.clone(),
|
|
|
|
receipts_root: ZERO_H256.clone(),
|
|
|
|
log_bloom: ZERO_LOGBLOOM.clone(),
|
|
|
|
gas_used: ZERO_U256.clone(),
|
|
|
|
gas_limit: ZERO_U256.clone(),
|
|
|
|
|
|
|
|
difficulty: ZERO_U256.clone(),
|
|
|
|
seal: vec![],
|
|
|
|
}
|
|
|
|
}
|
2015-12-08 16:31:36 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 16:44:14 +01:00
|
|
|
impl Decodable for Header {
|
|
|
|
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
2015-12-08 16:31:36 +01:00
|
|
|
decoder.read_list(| d | {
|
2015-12-13 16:44:14 +01:00
|
|
|
let blockheader = Header {
|
2015-12-08 16:36:44 +01:00
|
|
|
parent_hash: try!(Decodable::decode(&d[0])),
|
2015-12-09 19:03:25 +01:00
|
|
|
uncles_hash: try!(Decodable::decode(&d[1])),
|
2015-12-13 16:44:14 +01:00
|
|
|
author: try!(Decodable::decode(&d[2])),
|
2015-12-08 16:36:44 +01:00
|
|
|
state_root: try!(Decodable::decode(&d[3])),
|
|
|
|
transactions_root: try!(Decodable::decode(&d[4])),
|
|
|
|
receipts_root: try!(Decodable::decode(&d[5])),
|
|
|
|
log_bloom: try!(Decodable::decode(&d[6])),
|
|
|
|
difficulty: try!(Decodable::decode(&d[7])),
|
|
|
|
number: try!(Decodable::decode(&d[8])),
|
|
|
|
gas_limit: try!(Decodable::decode(&d[9])),
|
|
|
|
gas_used: try!(Decodable::decode(&d[10])),
|
|
|
|
timestamp: try!(Decodable::decode(&d[11])),
|
2015-12-13 16:44:14 +01:00
|
|
|
extra_data: try!(Decodable::decode(&d[12])),
|
|
|
|
seal: vec![],
|
2015-12-08 16:31:36 +01:00
|
|
|
};
|
2015-12-13 16:44:14 +01:00
|
|
|
// TODO: fill blockheader.seal with (raw) list items index 12..)
|
2015-12-08 16:31:36 +01:00
|
|
|
Ok(blockheader)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 16:44:14 +01:00
|
|
|
impl Encodable for Header {
|
2015-12-08 16:31:36 +01:00
|
|
|
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
|
|
|
encoder.emit_list(| e | {
|
|
|
|
self.parent_hash.encode(e);
|
2015-12-09 19:03:25 +01:00
|
|
|
self.uncles_hash.encode(e);
|
2015-12-13 16:44:14 +01:00
|
|
|
self.author.encode(e);
|
2015-12-08 16:31:36 +01:00
|
|
|
self.state_root.encode(e);
|
|
|
|
self.transactions_root.encode(e);
|
|
|
|
self.receipts_root.encode(e);
|
|
|
|
self.log_bloom.encode(e);
|
|
|
|
self.difficulty.encode(e);
|
|
|
|
self.number.encode(e);
|
|
|
|
self.gas_limit.encode(e);
|
|
|
|
self.gas_used.encode(e);
|
|
|
|
self.timestamp.encode(e);
|
2015-12-13 16:44:14 +01:00
|
|
|
self.extra_data.encode(e);
|
|
|
|
// TODO: emit raw seal items.
|
2015-12-08 16:31:36 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 00:45:33 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
fn encoding_and_decoding() {
|
|
|
|
}
|
|
|
|
}
|