Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Tipset w/ unit tests #56

Merged
merged 16 commits into from
Dec 3, 2019
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/*.rs.bk
/Cargo.lock
/Cargo.lock
.idea/
1 change: 1 addition & 0 deletions blockchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ edition = "2018"
vm = {path = "../vm"}
address = {path = "../vm/address"}
cid = "0.3.1"
multihash = "0.8.0"
47 changes: 40 additions & 7 deletions blockchain/src/blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

use super::ticket::{Ticket, VRFProofIndex};
use super::TipSetKeys;

use address::Address;
use cid::Cid;
use cid::{Cid, Codec, Prefix, Version};
use multihash::Hash;
use vm::message::Message;

// DefaultHashFunction represents the default hashing function to use
// TODO SHOULD BE BLAKE2B
const DEFAULT_HASH_FUNCTION: Hash = Hash::Keccak256;

/// BlockHeader defines header of a block in the Filecoin blockchain
#[derive(Clone, Debug)]
pub struct BlockHeader {
/// CHAIN LINKING
///
Expand All @@ -17,23 +24,23 @@ pub struct BlockHeader {
/// weight is the aggregate chain weight of the parent set
pub weight: u64,
/// epoch is the period in which a new block is generated. There may be multiple rounds in an epoch
epoch: u64,
pub epoch: u64,
/// height is the block height
pub height: u64,

/// MINER INFO
///
/// miner_address is the address of the miner actor that mined this block
miner_address: Address,
pub miner_address: Address,

/// STATE
///
/// messages is the Cid of the root of an array of Messages
messages: Cid,
pub messages: Cid,
/// message_receipts is the Cid of the root of an array of MessageReceipts
message_receipts: Cid,
pub message_receipts: Cid,
/// state_root is a cid pointer to the state tree after application of the transactions state transitions
state_root: Cid,
pub state_root: Cid,

/// CONSENSUS
///
Expand All @@ -43,15 +50,41 @@ pub struct BlockHeader {
pub ticket: Ticket,
/// election_proof is the "scratched ticket" proving that this block won
/// an election
election_proof: VRFProofIndex,
pub election_proof: VRFProofIndex,
// SIGNATURES
//
// block_sig filCrypto Signature
// BLSAggregateSig
/// CACHE
///
pub cached_cid: Cid,

pub cached_bytes: u8,
}

/// Block defines a full block
pub struct Block {
header: BlockHeader,
messages: Vec<Message>,
}

impl BlockHeader {
/// cid returns the content id of this header
pub fn cid(&mut self) -> Cid {
// TODO
dutterbutter marked this conversation as resolved.
Show resolved Hide resolved
// Encode blockheader into cache_bytes
// Update codec to use DagCBOR
// Change DEFAULT_HASH_FUNCTION to utilize blake2b
//
// Currently content id for headers will be incomplete until encoding and supporting libraries are completed
let c = Prefix {
version: Version::V1,
codec: Codec::DagProtobuf,
mh_type: DEFAULT_HASH_FUNCTION,
mh_len: 0,
};
let new_cid = Cid::new_from_prefix(&c, &[self.cached_bytes]);
self.cached_cid = new_cid;
self.cached_cid.clone()
}
}
4 changes: 2 additions & 2 deletions blockchain/src/blocks/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::fmt;

#[derive(Debug, PartialEq)]
pub enum Error {
UndefinedTipSet,
UndefinedTipSet(String),
NoBlocks,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Error::UndefinedTipSet => write!(f, "Undefined tipset"),
Error::UndefinedTipSet(ref msg) => write!(f, "Invalid tipset: {}", msg),
Error::NoBlocks => write!(f, "No blocks for tipset"),
}
}
Expand Down
5 changes: 4 additions & 1 deletion blockchain/src/blocks/ticket.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#![allow(unused_variables)]
#![allow(dead_code)]
dutterbutter marked this conversation as resolved.
Show resolved Hide resolved

/// VRFProofIndex is the proof output from running a VRF
pub type VRFProofIndex = Vec<u8>;

/// A Ticket is a marker of a tick of the blockchain's clock. It is the source
/// of randomness for proofs of storage and leader election. It is generated
/// by the miner of a block using a VRF and a VDF.
#[derive(Clone)]
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq)]
pub struct Ticket {
/// A proof output by running a VRF on the VDFResult of the parent ticket
pub vrfproof: VRFProofIndex,
Expand Down
Loading