Skip to content

Commit

Permalink
DagCBOR encoding and decoding for Tickets (#190)
Browse files Browse the repository at this point in the history
* decode and encode tests

* fixed clippy warnings

* suggestions

* fix warning uncaught by clippy

* fixed some suggestions

* more suggestion fixes
  • Loading branch information
ec2 authored Jan 23, 2020
1 parent 58f3e03 commit f1eb515
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 4 deletions.
3 changes: 3 additions & 0 deletions blockchain/blocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ derive_builder = "0.9"
serde = { version = "1.0", features = ["derive"] }
encoding = { path = "../../encoding" }
num-bigint = { git = "https://github.com/austinabell/num-bigint", rev = "f7084a9ed5a2b08d9bfb67790cb4ce9212193f31" }

[dev-dependencies]
base64 = "0.11.0"
193 changes: 190 additions & 3 deletions blockchain/blocks/src/ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
// SPDX-License-Identifier: Apache-2.0

use crypto::VRFResult;
use encoding::{
de::{self, Deserializer},
ser::{self, Serializer},
serde_bytes,
};
use serde::{Deserialize, Serialize};

/// 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, Debug, PartialEq, PartialOrd, Eq, Default, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Default)]
pub struct Ticket {
/// A proof output by running a VRF on the VDFResult of the parent ticket
pub vrfproof: VRFResult,
Expand All @@ -23,18 +28,200 @@ impl Ticket {
}
}

impl ser::Serialize for Ticket {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let value = [self.vrfproof.clone()];
value.serialize(serializer)
}
}

impl<'de> de::Deserialize<'de> for Ticket {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let [cm]: [VRFResult; 1] = Deserialize::deserialize(deserializer)?;
Ok(Self { vrfproof: cm })
}
}

/// PoSt election candidates
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Default)]
pub struct EPostTicket {
partial: Vec<u8>,
sector_id: u64,
challenge_index: u64,
}

/// Proof of Spacetime election proof
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Default)]
pub struct EPostProof {
proof: Vec<u8>,
post_rand: Vec<u8>,
candidates: Vec<EPostTicket>,
}

#[derive(Serialize, Deserialize)]
struct CborEPostTicket(
#[serde(with = "serde_bytes")] Vec<u8>, // partial
u64, // sector_id
u64, // challenge_index
);

#[derive(Serialize, Deserialize)]
struct CborEPostProof(
#[serde(with = "serde_bytes")] Vec<u8>, // proof
#[serde(with = "serde_bytes")] Vec<u8>, // post_rand
Vec<EPostTicket>, // candidates
);

impl ser::Serialize for EPostTicket {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
CborEPostTicket(self.partial.clone(), self.sector_id, self.challenge_index)
.serialize(serializer)
}
}

impl<'de> de::Deserialize<'de> for EPostTicket {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let CborEPostTicket(partial, sector_id, challenge_index) =
Deserialize::deserialize(deserializer)?;
Ok(Self {
partial,
sector_id,
challenge_index,
})
}
}

impl ser::Serialize for EPostProof {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
CborEPostProof(
self.proof.clone(),
self.post_rand.clone(),
self.candidates.clone(),
)
.serialize(serializer)
}
}

impl<'de> de::Deserialize<'de> for EPostProof {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let CborEPostProof(proof, post_rand, candidates) =
CborEPostProof::deserialize(deserializer)?;
Ok(Self {
proof,
post_rand,
candidates,
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use base64;
use encoding::{from_slice, to_vec};

// From Lotus
const TICKET: [u8; 99] = [
0x81, 0x58, 0x60, 0x96, 0x64, 0x49, 0x2f, 0x30, 0xe9, 0xb9, 0x50, 0x3b, 0x71, 0x41, 0x0b,
0x1d, 0x38, 0x2e, 0x2b, 0xd4, 0x85, 0x7f, 0xe2, 0x15, 0x39, 0xac, 0x92, 0x1b, 0xcb, 0x7f,
0xd0, 0x86, 0xd5, 0x78, 0x71, 0xe6, 0xdd, 0x5c, 0x31, 0xcd, 0x23, 0x61, 0x8b, 0x52, 0x52,
0xb6, 0x2c, 0x7b, 0x44, 0x4c, 0x3a, 0x02, 0x9b, 0xba, 0xad, 0xc2, 0x50, 0x57, 0x56, 0x81,
0x06, 0x47, 0x77, 0xf6, 0x04, 0x06, 0xc4, 0xff, 0x00, 0x6f, 0x38, 0xfc, 0x61, 0x71, 0xfe,
0x45, 0xd4, 0x83, 0xe5, 0x15, 0x79, 0xd0, 0xe2, 0x47, 0x8b, 0x7e, 0x5f, 0xde, 0x2c, 0x51,
0xd2, 0xe8, 0x64, 0x63, 0xaf, 0x86, 0xd3, 0xcb, 0xd5,
];
const EPOST_PROOF: [u8; 333] = [
0x83, 0x58, 0xc0, 0xae, 0x7f, 0x39, 0xba, 0x2a, 0x1d, 0x0f, 0x6f, 0x71, 0xbe, 0x02, 0x2e,
0xbc, 0xde, 0x7f, 0x83, 0x7e, 0xc8, 0x5e, 0x08, 0x4f, 0xb5, 0x5b, 0x65, 0xde, 0x58, 0xbd,
0xcb, 0xe9, 0xcf, 0x1c, 0x2b, 0x9e, 0x01, 0x32, 0x35, 0xab, 0x5f, 0xe8, 0x3a, 0x7d, 0x05,
0x10, 0x80, 0xd7, 0x45, 0x61, 0xcb, 0xa5, 0x9e, 0x02, 0xcc, 0x0a, 0x8e, 0x75, 0x08, 0x7d,
0xad, 0xd1, 0xe2, 0x87, 0xe0, 0x48, 0xe4, 0x8b, 0x1d, 0x23, 0x56, 0x29, 0xc1, 0x5f, 0x94,
0x74, 0xd0, 0xec, 0xa4, 0x95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, 0xdd, 0x31, 0x30, 0xd5, 0x2c, 0x3b,
0xff, 0x58, 0x6c, 0x7e, 0x2e, 0x8e, 0x27, 0xfb, 0xb1, 0x8d, 0x0f, 0xf2, 0x98, 0x11, 0x02,
0xe9, 0xe5, 0x32, 0x03, 0xeb, 0xc7, 0xb4, 0xb1, 0x81, 0x83, 0x58, 0x20, 0x4c, 0x59, 0x62,
0x53, 0xaf, 0xe9, 0x75, 0xb8, 0xd1, 0xca, 0x89, 0x9e, 0x8e, 0x55, 0xcc, 0x4b, 0xbe, 0xea,
0x8d, 0x87, 0x4c, 0x0e, 0xdc, 0xb4, 0xee, 0xf8, 0xa0, 0xbd, 0x71, 0xff, 0xbe, 0x32, 0x19,
0x01, 0x1c, 0x05,
];

fn construct_ticket() -> Ticket {
let vrf_result = VRFResult::new(base64::decode("lmRJLzDpuVA7cUELHTguK9SFf+IVOaySG8t/0IbVeHHm3VwxzSNhi1JStix7REw6Apu6rcJQV1aBBkd39gQGxP8Abzj8YXH+RdSD5RV50OJHi35f3ixR0uhkY6+G08vV").unwrap());
Ticket::new(vrf_result)
}

fn construct_epost_proof() -> EPostProof {
let etik = EPostTicket {
partial: base64::decode("TFliU6/pdbjRyomejlXMS77qjYdMDty07vigvXH/vjI=").unwrap(),
sector_id: 284,
challenge_index: 5,
};

EPostProof{
proof: base64::decode("rn85uiodD29xvgIuvN5/g37IXghPtVtl3li9y+nPHCueATI1q1/oOn0FEIDXRWHLpZ4CzAqOdQh9rdHih+BI5IsdI1YpwV+UdNDspJVW/cinVE+ZoiO86ap30l77RLkrEwxUZ5v8apsSRUizoXh1IFrHgK06gk1wl5LaxY2i/CQgBoWIPx9o2EYMBbNfQcu+pRzFmiDjzT6BIhYrPbo+gm6wHFiNhp3FvAuSUH2/N+5MKZo7Eh7LwgGLc0fL4MEI").unwrap(),
post_rand: base64::decode("hdodcCz5kLJYRb9PT7m4z9kRvc9h02KMye9DOklnQ8v05X2ds9rgNhcTV+d/cXS+AvADHpepQODMV/6E1kbT99kdFt0xMNUsO/9YbH4ujif7sY0P8pgRAunlMgPrx7Sx").unwrap(),
candidates: vec![etik]
}
}

#[test]
fn encode_ticket() {
let ticket = construct_ticket();
// Encode Ticket
let encoded_ticket = to_vec(&ticket).unwrap();
assert_eq!(&TICKET[..], &encoded_ticket[..]);
}

#[test]
fn decode_ticket() {
let ticket = construct_ticket();
// Decode Ticket
let decoded_ticket: Ticket = from_slice(&TICKET).unwrap();
assert_eq!(ticket, decoded_ticket);
}

#[test]
fn encode_epost_proof() {
let proof = construct_epost_proof();
// Encode Proof
let encoded_proof = to_vec(&proof).unwrap();
assert_eq!(&EPOST_PROOF[..], &encoded_proof[..]);
}

#[test]
fn decode_epost_proof() {
let proof = construct_epost_proof();
// Decode Proof
let decoded_proof: EPostProof = from_slice(&EPOST_PROOF).unwrap();
assert_eq!(proof, decoded_proof);
}
}
3 changes: 2 additions & 1 deletion crypto/src/vrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::signature::{verify_bls_sig, Signature, BLS_SIG_LEN};
use bls_signatures::{Serialize as BlsSerialize, Signature as BLSSignature};
use encoding::serde_bytes;
use serde::{Deserialize, Serialize};

pub struct VRFPublicKey(Vec<u8>);
Expand All @@ -16,7 +17,7 @@ impl VRFPublicKey {

/// The output from running a VRF
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Default, Serialize, Deserialize)]
pub struct VRFResult(Vec<u8>);
pub struct VRFResult(#[serde(with = "serde_bytes")] Vec<u8>);

// TODO verify format or implement custom serialize/deserialize function (if necessary):
// https://github.com/ChainSafe/ferret/issues/143
Expand Down

0 comments on commit f1eb515

Please sign in to comment.