Skip to content

Commit

Permalink
New Tipset w/ unit tests (#56)
Browse files Browse the repository at this point in the history
* Blockchain Interfaces WIP

* Added multihash package to retrieve blake2b hash
* Added sort_key method for Ticket struct
* Added equals method for TipSetKeys struct type
* WIP for Tipset new fn
* WIP cid method for block type which would return CID of block

* New Tipset fn WIP

* Added new conditional checks as per spec update
* WIP sorted and compare logic for ticket size
* Fix cid method for blockHeader
* WIP new fn tests

* Added unit tests

* Added unit tests for tipset methods

* WIP tipset new fn

* Added unit tests

* Added unit tests for tipset methods

* Added tipset new logic

* Added new tipset logic, includes conditional checks and sorting based on ticket size
* Added unit tests for tipset methods
* Added cid fn although it is incomplete as we need cbor encoding

* Fixed equals fn

* Fixed equals fn check
* Linted

* Remove typo

* Made requested changes
* Updated multihash to specific version
* Removed extern crate statement
* Improved commenting
* Updated sort statement
* Improved unit test setup
* Updated error messages for tipset condition checks

* Made requested changes
* Updated cid reference
* Removed pub from mod.rs

* Requested changes made
* Updated error to handle String
* Removed unnecessary import
* Removed sort_key method for Ticket

* Requested changes made
* Removed dead code and unused variable tag

* Remove reverse in sort

* improved tests
  • Loading branch information
dutterbutter authored Dec 3, 2019
1 parent f777233 commit 9fba7d9
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 27 deletions.
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"
46 changes: 39 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,40 @@ 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
// Encode blockheader into cache_bytes
// 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::DagCBOR,
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
2 changes: 1 addition & 1 deletion blockchain/src/blocks/ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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

0 comments on commit 9fba7d9

Please sign in to comment.