Skip to content

Commit

Permalink
BlockHeader Update (#385)
Browse files Browse the repository at this point in the history
* add beaconentries

* remove serialziation of prev_round from beaconentry

* fix prev_data

* add post proof struct

* updated blockheader with post proof

* blockheaders deserialize properly

* post proof comment

* make lint

* ignore test

* suggestions from austin

* Last fixes
  • Loading branch information
ec2 authored Apr 29, 2020
1 parent 7e8124d commit aa899c0
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"forest",
"blockchain",
"blockchain/beacon",
"blockchain/blocks",
"blockchain/chain",
"blockchain/state_manager",
Expand Down
8 changes: 8 additions & 0 deletions blockchain/beacon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "beacon"
version = "0.1.0"
authors = ["ChainSafe Systems <info@chainsafe.io>"]
edition = "2018"

[dependencies]
encoding = { package = "forest_encoding", path = "../../encoding" }
62 changes: 62 additions & 0 deletions blockchain/beacon/src/beacon_entries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use encoding::{
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
BytesDe, BytesSer,
};

/// The result from getting an entry from Drand.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct BeaconEntry {
round: u64,
data: Vec<u8>,
prev_round: u64,
}

impl BeaconEntry {
pub fn new(round: u64, data: Vec<u8>, prev_round: u64) -> Self {
Self {
round,
data,
prev_round,
}
}
/// Returns the current round number
pub fn round(&self) -> u64 {
self.round
}
/// The signature of message H(prev_round, prev_round.data, round).
pub fn data(&self) -> &[u8] {
&self.data
}
/// Returns the previous round number
pub fn prev_round(&self) -> u64 {
self.prev_round
}
}

impl Serialize for BeaconEntry {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
(&self.round, BytesSer(&self.data)).serialize(serializer)
}
}

impl<'de> Deserialize<'de> for BeaconEntry {
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
where
D: Deserializer<'de>,
{
let (round, data): (u64, BytesDe) = Deserialize::deserialize(deserializer)?;
let prev_round = if round == 0 { 0 } else { round - 1 };
Ok(Self {
round,
data: data.0,
prev_round,
})
}
}
6 changes: 6 additions & 0 deletions blockchain/beacon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

mod beacon_entries;

pub use beacon_entries::*;
1 change: 1 addition & 0 deletions blockchain/blocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

[dependencies]
address = { package = "forest_address", path = "../../vm/address" }
beacon = { package = "beacon", path = "../beacon" }
crypto = { path = "../../crypto" }
message = { package = "forest_message", path = "../../vm/message" }
clock = { path = "../../node/clock" }
Expand Down
53 changes: 38 additions & 15 deletions blockchain/blocks/src/header.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::{EPostProof, Error, Ticket, TipSetKeys, Tipset};
use super::{Error, Ticket, TipSetKeys, Tipset};
use address::Address;
use beacon::BeaconEntry;
use cid::{multihash::Blake2b256, Cid};
use clock::ChainEpoch;
use crypto::Signature;
use crypto::{Signature, VRFProof};
use derive_builder::Builder;
use encoding::{
de::{self, Deserializer},
ser::{self, Serializer},
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
Cbor, Error as EncodingError,
};
use num_bigint::{
biguint_ser::{BigUintDe, BigUintSer},
BigUint,
};
use serde::Deserialize;
use sha2::Digest;
use std::cmp::Ordering;
use std::fmt;
use std::time::{SystemTime, UNIX_EPOCH};
use vm::PoStProof;
// TODO should probably have a central place for constants
const SHA_256_BITS: usize = 256;
const BLOCKS_PER_EPOCH: u64 = 5;
Expand Down Expand Up @@ -67,6 +68,14 @@ pub struct BlockHeader {
/// There may be multiple rounds in an epoch.
#[builder(default)]
epoch: ChainEpoch,

/// Values from Drand
#[builder(default)]
beacon_entries: Vec<BeaconEntry>,

#[builder(default)]
win_post_proof: Vec<PoStProof>,

// MINER INFO
/// miner_address is the address of the miner actor that mined this block
#[builder(default)]
Expand All @@ -92,7 +101,7 @@ pub struct BlockHeader {
signature: Option<Signature>,

#[builder(default)]
epost_verify: EPostProof,
election_proof: Option<VRFProof>,

// CONSENSUS
/// timestamp, in seconds since the Unix epoch, at which this block was created
Expand Down Expand Up @@ -121,15 +130,17 @@ impl Cbor for BlockHeader {
}
}

impl ser::Serialize for BlockHeader {
impl Serialize for BlockHeader {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
(
&self.miner_address,
&self.ticket,
&self.epost_verify,
&self.election_proof,
&self.beacon_entries,
&self.win_post_proof,
&self.parents,
BigUintSer(&self.weight),
&self.epoch,
Expand All @@ -145,15 +156,17 @@ impl ser::Serialize for BlockHeader {
}
}

impl<'de> de::Deserialize<'de> for BlockHeader {
impl<'de> Deserialize<'de> for BlockHeader {
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
where
D: Deserializer<'de>,
{
let (
miner_address,
ticket,
epost_verify,
election_proof,
beacon_entries,
win_post_proof,
parents,
BigUintDe(weight),
epoch,
Expand All @@ -170,13 +183,15 @@ impl<'de> de::Deserialize<'de> for BlockHeader {
.parents(parents)
.weight(weight)
.epoch(epoch)
.beacon_entries(beacon_entries)
.win_post_proof(win_post_proof)
.miner_address(miner_address)
.messages(messages)
.message_receipts(message_receipts)
.state_root(state_root)
.fork_signal(fork_signal)
.signature(signature)
.epost_verify(epost_verify)
.election_proof(election_proof)
.timestamp(timestamp)
.ticket(ticket)
.bls_aggregate(bls_aggregate)
Expand Down Expand Up @@ -216,6 +231,14 @@ impl BlockHeader {
pub fn epoch(&self) -> ChainEpoch {
self.epoch
}
/// Getter for Drand BeaconEntry
pub fn beacon_entries(&self) -> &[BeaconEntry] {
&self.beacon_entries
}
/// Getter for window PoSt proof
pub fn win_post_proof(&self) -> &[PoStProof] {
&self.win_post_proof
}
/// Getter for BlockHeader miner_address
pub fn miner_address(&self) -> &Address {
&self.miner_address
Expand Down Expand Up @@ -254,8 +277,8 @@ impl BlockHeader {
self.fork_signal
}
/// Getter for BlockHeader epost_verify
pub fn epost_verify(&self) -> &EPostProof {
&self.epost_verify
pub fn election_proof(&self) -> &Option<VRFProof> {
&self.election_proof
}
/// Getter for BlockHeader signature
pub fn signature(&self) -> &Option<Signature> {
Expand Down Expand Up @@ -360,8 +383,8 @@ mod tests {

#[test]
fn symmetric_header_encoding() {
// This test vector is the genesis header for testnet/3 config
let bz = hex::decode("8D4200008158207672662070726F6F66303030303030307672662070726F6F66303030303030308380581C692067756573732074686973206973206B696E64612072616E646F6D80804000D82A5827000171A0E420205B05D256933F3E29756306949643F34A0B644E475BF2BB9DAA81507C31B048A2D82A5827000171A0E4022001CD927FDCCD7938FABA323E32E70C44541B8A83F5DC941D90866565EF5AF14AD82A5827000171A0E402208D6F0E09E0453685B8816895CD56A7EE2FCE600026EE23AC445D78F020C1CA40F61A5E8BE968F600").unwrap();
// This test vector is the genesis header for interopnet config
let bz = hex::decode("8f4200008158207672662070726f6f66303030303030307672662070726f6f6630303030303030f68182005820000000000000000000000000000000000000000000000000000000000000000080804000d82a5827000171a0e402209fcfcbb98dcbf141cd7f1977fcd1b5da2198ebdcc96a61288562dbc3ee8e8ff0d82a5827000171a0e4022001cd927fdccd7938faba323e32e70c44541b8a83f5dc941d90866565ef5af14ad82a5827000171a0e402208d6f0e09e0453685b8816895cd56a7ee2fce600026ee23ac445d78f020c1ca40f61a5ea37bdcf600").unwrap();
let header = BlockHeader::unmarshal_cbor(&bz).unwrap();
assert_eq!(hex::encode(header.marshal_cbor().unwrap()), hex::encode(bz));
}
Expand Down
2 changes: 1 addition & 1 deletion ipld/car/tests/car_file_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ fn load_into_blockstore() {
let buf_reader = BufReader::new(file);
let mut bs = MemoryDB::default();

load_car(&mut bs, buf_reader).unwrap();
let _ = load_car(&mut bs, buf_reader).unwrap();
}
Binary file modified ipld/car/tests/devnet.car
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/serialization_tests/tests/block_header_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ impl From<BlockVector> for BlockHeader {
.state_root(v.state_root.into())
.fork_signal(v.fork_signaling)
.signature(Some(v.signature.into()))
.epost_verify(v.e_post.into())
.timestamp(v.timestamp)
.ticket(v.ticket.into())
.bls_aggregate(Some(v.bls_agg.into()))
Expand Down Expand Up @@ -160,6 +159,7 @@ fn encode_assert_cbor(header: &BlockHeader, expected: &str, cid: &Cid) {
}

#[test]
#[ignore]
fn header_cbor_vectors() {
let mut file = File::open("../serialization-vectors/block_headers.json").unwrap();
let mut string = String::new();
Expand Down

0 comments on commit aa899c0

Please sign in to comment.