Skip to content

Commit

Permalink
refactor: schema names should be Tipset, not TipsetLotusJson (#4296)
Browse files Browse the repository at this point in the history
  • Loading branch information
aatifsyed authored May 8, 2024
1 parent dd44a8a commit 16c7add
Show file tree
Hide file tree
Showing 26 changed files with 78 additions and 89 deletions.
39 changes: 10 additions & 29 deletions src/blocks/tipset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,41 +515,25 @@ mod lotus_json {
use crate::blocks::{CachingBlockHeader, Tipset};
use crate::lotus_json::*;
use nunny::Vec as NonEmpty;
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use schemars::JsonSchema;
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};

use super::TipsetKey;

#[derive(Clone)]
pub struct TipsetLotusJson(Tipset);
#[derive(Clone, JsonSchema)]
#[schemars(rename = "Tipset")]
pub struct TipsetLotusJson(#[schemars(with = "TipsetLotusJsonInner")] Tipset);

impl JsonSchema for TipsetLotusJson {
fn schema_name() -> String {
String::from("TipsetLotusJson")
}
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
// can't impl JsonSchema for NonEmpty...
#[derive(JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[allow(unused)]
struct Helper {
cids: LotusJson<TipsetKey>,
blocks: LotusJson<Vec<CachingBlockHeader>>,
height: LotusJson<i64>,
}
Helper::json_schema(gen)
}
}

// NOTE: keep this in sync with JsonSchema implementation above
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "Tipset")]
#[serde(rename_all = "PascalCase")]
struct TipsetLotusJsonInner {
#[serde(with = "crate::lotus_json")]
#[schemars(with = "LotusJson<TipsetKey>")]
cids: TipsetKey,
#[serde(with = "crate::lotus_json")]
#[schemars(with = "LotusJson<NonEmpty<CachingBlockHeader>>")]
blocks: NonEmpty<CachingBlockHeader>,
#[serde(with = "crate::lotus_json")]
height: i64,
}

Expand All @@ -564,10 +548,7 @@ mod lotus_json {
height: _ignored1,
} = Deserialize::deserialize(deserializer)?;

Ok(Self(Tipset {
headers: blocks,
key: Default::default(),
}))
Ok(Self(Tipset::new(blocks).map_err(D::Error::custom)?))
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lotus_json/actor_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ::cid::Cid;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "ActorState")]
pub struct ActorStateLotusJson {
#[schemars(with = "LotusJson<Cid>")]
#[serde(with = "crate::lotus_json")]
Expand Down
15 changes: 11 additions & 4 deletions src/lotus_json/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use super::*;

use crate::shim::address::Address;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "Address")]
pub struct AddressLotusJson(
#[schemars(with = "String")]
#[serde(with = "crate::lotus_json::stringify")]
Address,
);

impl HasLotusJson for Address {
type LotusJson = Stringify<Address>;
type LotusJson = AddressLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![(json!("f00"), Address::default())]
}

fn into_lotus_json(self) -> Self::LotusJson {
self.into()
AddressLotusJson(self)
}

fn from_lotus_json(Stringify(address): Self::LotusJson) -> Self {
fn from_lotus_json(AddressLotusJson(address): Self::LotusJson) -> Self {
address
}
}
6 changes: 2 additions & 4 deletions src/lotus_json/beacon_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::*;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "BeaconEntry")]
pub struct BeaconEntryLotusJson {
round: u64,
#[schemars(with = "LotusJson<Vec<u8>>")]
Expand All @@ -24,10 +25,7 @@ impl HasLotusJson for BeaconEntry {

fn into_lotus_json(self) -> Self::LotusJson {
let (round, data) = self.into_parts();
Self::LotusJson {
round,
data,
}
Self::LotusJson { round, data }
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
Expand Down
14 changes: 11 additions & 3 deletions src/lotus_json/big_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ use super::*;

use num::BigInt;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "BigInt")]
pub struct BigIntLotusJson(
#[schemars(with = "String")]
#[serde(with = "crate::lotus_json::stringify")]
BigInt,
);

impl HasLotusJson for BigInt {
type LotusJson = Stringify<BigInt>;
type LotusJson = BigIntLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![(json!("1"), BigInt::from(1))]
}

fn into_lotus_json(self) -> Self::LotusJson {
self.into()
BigIntLotusJson(self)
}

fn from_lotus_json(Stringify(big_int): Self::LotusJson) -> Self {
fn from_lotus_json(BigIntLotusJson(big_int): Self::LotusJson) -> Self {
big_int
}
}
1 change: 1 addition & 0 deletions src/lotus_json/bit_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::*;
use fil_actors_shared::fvm_ipld_bitfield::{json::BitFieldJson, BitField};

#[derive(Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "BitField")]
pub struct BitFieldLotusJson(#[schemars(with = "Option<Vec<u8>>")] pub BitFieldJson);

impl Clone for BitFieldLotusJson {
Expand Down
1 change: 1 addition & 0 deletions src/lotus_json/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::blocks::{CachingBlockHeader, RawBlockHeader};

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "BlockHeader")]
pub struct BlockHeaderLotusJson {
#[schemars(with = "LotusJson<Address>")]
#[serde(with = "crate::lotus_json")]
Expand Down
10 changes: 6 additions & 4 deletions src/lotus_json/cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
use super::*;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "Cid")]
pub struct CidLotusJsonGeneric<const S: usize> {
#[serde(rename = "/")]
slash: Stringify<::cid::CidGeneric<S>>,
#[schemars(with = "String")]
#[serde(rename = "/", with = "crate::lotus_json::stringify")]
slash: ::cid::CidGeneric<S>,
}

impl<const S: usize> HasLotusJson for ::cid::CidGeneric<S> {
Expand All @@ -18,12 +20,12 @@ impl<const S: usize> HasLotusJson for ::cid::CidGeneric<S> {
}

fn into_lotus_json(self) -> Self::LotusJson {
Self::LotusJson { slash: self.into() }
Self::LotusJson { slash: self }
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
let Self::LotusJson { slash } = lotus_json;
slash.into_inner()
slash
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lotus_json/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::*;

#[derive(Default, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "Claim")]
pub struct ClaimLotusJson {
#[schemars(with = "LotusJson<num::BigInt>")]
#[serde(with = "crate::lotus_json")]
Expand Down
1 change: 1 addition & 0 deletions src/lotus_json/election_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::*;

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "ElectionProof")]
pub struct ElectionProofLotusJson {
#[schemars(with = "LotusJson<VRFProof>")]
#[serde(with = "crate::lotus_json")]
Expand Down
1 change: 1 addition & 0 deletions src/lotus_json/gossip_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ::cid::Cid;

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "GossipBlock")]
pub struct GossipBlockLotusJson {
#[schemars(with = "LotusJson<CachingBlockHeader>")]
#[serde(with = "crate::lotus_json")]
Expand Down
1 change: 1 addition & 0 deletions src/lotus_json/ipld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use libipld::{ipld, Ipld};
use serde::de;

#[derive(Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "Ipld")]
pub struct IpldLotusJson(
#[serde(with = "self")]
#[schemars(with = "serde_json::Value")] // opt-out of JsonSchema for now
Expand Down
1 change: 1 addition & 0 deletions src/lotus_json/key_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{key_management::KeyInfo, shim::crypto::SignatureType};

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "KeyInfo")]
pub struct KeyInfoLotusJson {
#[schemars(with = "LotusJson<SignatureType>")]
#[serde(with = "crate::lotus_json")]
Expand Down
1 change: 1 addition & 0 deletions src/lotus_json/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use fvm_ipld_encoding::RawBytes;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "Message")]
pub struct MessageLotusJson {
version: u64,
#[schemars(with = "LotusJson<Address>")]
Expand Down
27 changes: 0 additions & 27 deletions src/lotus_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,33 +467,6 @@ impl<T> LotusJson<T> {
}
}

/// A struct that is (de) serialized through its [`Display`] and [`FromStr`] implementations.
#[derive(Serialize, Deserialize, From, Default)]
#[serde(bound = "T: Display + FromStr, T::Err: Display")]
pub struct Stringify<T>(#[serde(with = "stringify")] pub T);

impl<T> Stringify<T> {
pub fn into_inner(self) -> T {
self.0
}
}

impl<T> JsonSchema for Stringify<T> {
fn schema_name() -> String {
String::schema_name()
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
String::json_schema(gen)
}
}

impl<T: Clone> Clone for Stringify<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

macro_rules! lotus_json_with_self {
($($domain_ty:ty),* $(,)?) => {
$(
Expand Down
1 change: 1 addition & 0 deletions src/lotus_json/po_st_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::*;

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "PoStProof")]
pub struct PoStProofLotusJson {
#[schemars(with = "LotusJson<RegisteredPoStProof>")]
#[serde(with = "crate::lotus_json")]
Expand Down
1 change: 1 addition & 0 deletions src/lotus_json/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::shim::executor::Receipt;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "Receipt")]
pub struct ReceiptLotusJson {
exit_code: u32,
#[schemars(with = "LotusJson<RawBytes>")]
Expand Down
7 changes: 2 additions & 5 deletions src/lotus_json/sector_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ::cid::Cid;

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "SectorInfo")]
pub struct SectorInfoLotusJson {
#[schemars(with = "LotusJson<RegisteredSealProof>")]
#[serde(with = "crate::lotus_json")]
Expand Down Expand Up @@ -57,10 +58,6 @@ impl HasLotusJson for SectorInfo {
sector_number,
sealed_c_i_d,
} = lotus_json;
Self::new(
seal_proof.into(),
sector_number,
sealed_c_i_d,
)
Self::new(seal_proof.into(), sector_number, sealed_c_i_d)
}
}
1 change: 1 addition & 0 deletions src/lotus_json/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::shim::crypto::{Signature, SignatureType};

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "Signature")]
pub struct SignatureLotusJson {
#[schemars(with = "LotusJson<SignatureType>")]
#[serde(with = "crate::lotus_json")]
Expand Down
10 changes: 7 additions & 3 deletions src/lotus_json/signature_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ use crate::shim::crypto::SignatureType;

#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(untagged)] // try an int, then a string
#[schemars(rename = "SignatureType")]
pub enum SignatureTypeLotusJson {
Integer(SignatureType),
String(Stringify<SignatureType>),
String(
#[serde(with = "crate::lotus_json::stringify")]
#[schemars(with = "String")]
SignatureType,
),
}

impl HasLotusJson for SignatureType {
Expand All @@ -32,8 +37,7 @@ impl HasLotusJson for SignatureType {

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
match lotus_json {
SignatureTypeLotusJson::Integer(inner)
| SignatureTypeLotusJson::String(Stringify(inner)) => inner,
SignatureTypeLotusJson::Integer(inner) | SignatureTypeLotusJson::String(inner) => inner,
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/lotus_json/signed_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::*;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "SignedMessage")]
pub struct SignedMessageLotusJson {
#[schemars(with = "LotusJson<Message>")]
#[serde(with = "crate::lotus_json")]
Expand Down Expand Up @@ -82,9 +83,6 @@ impl HasLotusJson for SignedMessage {
signature,
cid: _ignored, // See notes on Message
} = lotus_json;
Self {
message,
signature,
}
Self { message, signature }
}
}
Loading

0 comments on commit 16c7add

Please sign in to comment.