Skip to content

Commit

Permalink
Remove a bajillion manual serde implementations (#433)
Browse files Browse the repository at this point in the history
* Add serde_tuple and serde_repr

* Add a couple extern crate lines

* Turn Randomness into a type

* Add bitvec_serde::{serialize, deserialize}

* Remove Byte32De

* Remove a bajillion manual serde implementations

* Rename types/src/sector/serde.rs to serde_tests.rs

* Mark EPostTicket.partial with serde_bytes

* Bring back Byte32De

* Bring back BigIntDe

* Serialize some types transparently

* Derive Debug for Randomness

* Derive serialization for Serialized

* Derive serialization for TipsetKeys
  • Loading branch information
timvermeulen authored May 22, 2020
1 parent c9a089e commit acedcf0
Show file tree
Hide file tree
Showing 50 changed files with 328 additions and 2,592 deletions.
26 changes: 2 additions & 24 deletions blockchain/blocks/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

use super::BlockHeader;
use cid::Cid;
use encoding::{de::Deserializer, ser::Serializer};
use encoding::tuple::*;
use message::{SignedMessage, UnsignedMessage};
use serde::{Deserialize, Serialize};

/// A complete block
#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -35,29 +34,8 @@ impl Block {
}

/// Tracks the merkleroots of both secp and bls messages separately
#[derive(Serialize_tuple, Deserialize_tuple)]
pub struct TxMeta {
pub bls_message_root: Cid,
pub secp_message_root: Cid,
}

impl Serialize for TxMeta {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
(&self.bls_message_root, &self.secp_message_root).serialize(serializer)
}
}

impl<'de> Deserialize<'de> for TxMeta {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let (bls_message_root, secp_message_root) = Deserialize::deserialize(deserializer)?;
Ok(TxMeta {
bls_message_root,
secp_message_root,
})
}
}
3 changes: 3 additions & 0 deletions blockchain/blocks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

// workaround for a compiler bug, see https://github.com/rust-lang/rust/issues/55779
extern crate serde;

mod block;
mod errors;
mod header;
Expand Down
84 changes: 8 additions & 76 deletions blockchain/blocks/src/ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use crypto::VRFProof;
use encoding::{BytesDe, BytesSer};
use encoding::{serde_bytes, tuple::*};
use fil_types::PoStProof;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// 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, Ord)]
#[derive(
Clone, Debug, PartialEq, PartialOrd, Eq, Default, Ord, Serialize_tuple, Deserialize_tuple,
)]
pub struct Ticket {
/// A proof output by running a VRF on the VDFResult of the parent ticket
pub vrfproof: VRFProof,
Expand All @@ -22,89 +23,20 @@ impl Ticket {
}
}

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

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

/// PoSt election candidates
#[derive(Clone, Debug, PartialEq, Default, Eq)]
#[derive(Clone, Debug, PartialEq, Default, Eq, Serialize_tuple, Deserialize_tuple)]
pub struct EPostTicket {
#[serde(with = "serde_bytes")]
pub partial: Vec<u8>,
pub sector_id: u64,
pub challenge_index: u64,
}

/// Proof of Spacetime election proof
#[derive(Clone, Debug, PartialEq, Default, Eq)]
#[derive(Clone, Debug, PartialEq, Default, Eq, Serialize_tuple, Deserialize_tuple)]
pub struct EPostProof {
pub proof: Vec<PoStProof>,
#[serde(with = "serde_bytes")]
pub post_rand: Vec<u8>,
pub candidates: Vec<EPostTicket>,
}

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

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

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

impl<'de> Deserialize<'de> for EPostProof {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let (proof, BytesDe(post_rand), candidates) = Deserialize::deserialize(deserializer)?;
Ok(Self {
proof,
post_rand,
candidates,
})
}
}
30 changes: 4 additions & 26 deletions blockchain/blocks/src/tipset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
use super::{Block, BlockHeader, Error, Ticket};
use cid::Cid;
use clock::ChainEpoch;
use encoding::{
de::{self, Deserializer},
ser::{self, Serializer},
Cbor,
};
use encoding::Cbor;
use num_bigint::BigUint;
use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// A set of CIDs forming a unique key for a Tipset.
/// Equal keys will have equivalent iteration order, but note that the CIDs are *not* maintained in
/// the same order as the canonical iteration order of blocks in a tipset (which is by ticket)
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct TipsetKeys {
pub cids: Vec<Cid>,
}
Expand All @@ -33,25 +30,6 @@ impl TipsetKeys {
}
}

impl ser::Serialize for TipsetKeys {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.cids.serialize(serializer)
}
}

impl<'de> de::Deserialize<'de> for TipsetKeys {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let cids: Vec<Cid> = Deserialize::deserialize(deserializer)?;
Ok(TipsetKeys { cids })
}
}

impl Cbor for TipsetKeys {}

/// An immutable set of blocks at the same height with the same parent set.
Expand Down
2 changes: 2 additions & 0 deletions encoding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ blake2b_simd = "0.5.9"
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11.3"
serde_cbor = { version = "0.11.0", features = ["tags"] }
serde_tuple = "0.5"
serde_repr = "0.1"
cid = { package = "forest_cid", path = "../ipld/cid", version = "0.1" }
thiserror = "1.0"
2 changes: 1 addition & 1 deletion encoding/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde_bytes::ByteBuf;
pub struct BytesSer<'a>(#[serde(with = "serde_bytes")] pub &'a [u8]);

/// Wrapper for deserializing dynamic sized Bytes.
#[derive(Deserialize, Serialize)]
#[derive(Deserialize)]
#[serde(transparent)]
pub struct BytesDe(#[serde(with = "serde_bytes")] pub Vec<u8>);

Expand Down
8 changes: 8 additions & 0 deletions encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ pub use self::bytes::*;
pub use self::cbor::*;
pub use self::errors::*;
pub use self::hash::*;

pub mod tuple {
pub use serde_tuple::{self, Deserialize_tuple, Serialize_tuple};
}

pub mod repr {
pub use serde_repr::{Deserialize_repr, Serialize_repr};
}
25 changes: 4 additions & 21 deletions ipld/hamt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use self::hamt::Hamt;
pub use self::hash::*;

use forest_ipld::Ipld;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Serialize};
use std::borrow::Borrow;
use std::hash::Hasher;
use std::ops::Deref;
Expand Down Expand Up @@ -51,8 +51,9 @@ impl<K> KeyValuePair<K> {

/// Key type to be used to isolate usage of unsafe code and allow non utf-8 bytes to be
/// serialized as a string.
#[derive(Eq, PartialOrd, Clone, Debug)]
pub struct BytesKey(pub Vec<u8>);
#[derive(Eq, PartialOrd, Clone, Debug, Serialize, Deserialize)]
#[serde(transparent)]
pub struct BytesKey(#[serde(with = "serde_bytes")] pub Vec<u8>);

impl PartialEq for BytesKey {
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -85,24 +86,6 @@ impl Deref for BytesKey {
}
}

impl Serialize for BytesKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_bytes::Serialize::serialize(&self.0, serializer)
}
}

impl<'de> Deserialize<'de> for BytesKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Ok(Self(serde_bytes::Deserialize::deserialize(deserializer)?))
}
}

impl From<Vec<u8>> for BytesKey {
fn from(bz: Vec<u8>) -> Self {
BytesKey(bz)
Expand Down
Loading

0 comments on commit acedcf0

Please sign in to comment.