Skip to content

Commit

Permalink
refactor: move some HasLotusJson impls to the right place (#4312)
Browse files Browse the repository at this point in the history
  • Loading branch information
aatifsyed authored May 23, 2024
1 parent b9782f7 commit ca2d6a1
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 247 deletions.
59 changes: 59 additions & 0 deletions src/lotus_json/beneficiary_term.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::*;
use crate::shim::{clock::ChainEpoch, econ::TokenAmount};
use fil_actor_miner_state::v12::BeneficiaryTerm;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "BeneficiaryTerm")]
pub struct BeneficiaryTermLotusJson {
/// The total amount the current beneficiary can withdraw. Monotonic, but reset when beneficiary changes.
#[schemars(with = "LotusJson<TokenAmount>")]
#[serde(with = "crate::lotus_json")]
pub quota: TokenAmount,
/// The amount of quota the current beneficiary has already withdrawn
#[schemars(with = "LotusJson<TokenAmount>")]
#[serde(with = "crate::lotus_json")]
pub used_quota: TokenAmount,
/// The epoch at which the beneficiary's rights expire and revert to the owner
pub expiration: ChainEpoch,
}

impl HasLotusJson for BeneficiaryTerm {
type LotusJson = BeneficiaryTermLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![(
json!({
"Quota": "0",
"UsedQuota": "0",
"Expiration": 0,
}),
Default::default(),
)]
}

fn into_lotus_json(self) -> Self::LotusJson {
BeneficiaryTermLotusJson {
used_quota: self.used_quota.into(),
quota: self.quota.into(),
expiration: self.expiration,
}
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
Self {
used_quota: lotus_json.used_quota.into(),
quota: lotus_json.quota.into(),
expiration: lotus_json.expiration,
}
}
}

#[test]
fn snapshots() {
assert_all_snapshots::<BeneficiaryTerm>();
}
159 changes: 159 additions & 0 deletions src/lotus_json/miner_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::*;

use crate::{
rpc::types::AddressOrEmpty,
shim::{address::Address, clock::ChainEpoch, sector::SectorSize},
};
use fil_actor_interface::miner::MinerInfo;
use fil_actor_miner_state::v12::{BeneficiaryTerm, PendingBeneficiaryChange};
use fvm_ipld_encoding::BytesDe;
use libp2p::PeerId;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "MinerInfo")]
pub struct MinerInfoLotusJson {
#[schemars(with = "LotusJson<Address>")]
#[serde(with = "crate::lotus_json")]
pub owner: Address,
#[schemars(with = "LotusJson<Address>")]
#[serde(with = "crate::lotus_json")]
pub worker: Address,
#[schemars(with = "LotusJson<Option<Address>>")]
pub new_worker: AddressOrEmpty,
#[schemars(with = "LotusJson<Vec<Address>>")]
#[serde(with = "crate::lotus_json")]
pub control_addresses: Vec<Address>, // Must all be ID addresses.
pub worker_change_epoch: ChainEpoch,
#[schemars(with = "LotusJson<Option<String>>")]
#[serde(with = "crate::lotus_json")]
pub peer_id: Option<String>,
#[schemars(with = "LotusJson<Vec<Vec<u8>>>")]
#[serde(with = "crate::lotus_json")]
pub multiaddrs: Vec<Vec<u8>>,
#[schemars(with = "String")]
pub window_po_st_proof_type: fvm_shared2::sector::RegisteredPoStProof,
#[schemars(with = "LotusJson<SectorSize>")]
#[serde(with = "crate::lotus_json")]
pub sector_size: SectorSize,
pub window_po_st_partition_sectors: u64,
pub consensus_fault_elapsed: ChainEpoch,
#[schemars(with = "LotusJson<Option<Address>>")]
#[serde(with = "crate::lotus_json")]
pub pending_owner_address: Option<Address>,
#[schemars(with = "LotusJson<Address>")]
#[serde(with = "crate::lotus_json")]
pub beneficiary: Address,
#[schemars(with = "LotusJson<BeneficiaryTerm>")]
#[serde(with = "crate::lotus_json")]
pub beneficiary_term: BeneficiaryTerm,
#[schemars(with = "LotusJson<Option<PendingBeneficiaryChange>>")]
#[serde(with = "crate::lotus_json")]
pub pending_beneficiary_term: Option<PendingBeneficiaryChange>,
}

impl HasLotusJson for MinerInfo {
type LotusJson = MinerInfoLotusJson;
#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![(
json!({
"Beneficiary": "f00",
"BeneficiaryTerm": {
"Expiration": 0,
"Quota": "0",
"UsedQuota": "0"
},
"ConsensusFaultElapsed": 0,
"ControlAddresses": null,
"Multiaddrs": null,
"NewWorker": "<empty>",
"Owner": "f00",
"PeerId": null,
"PendingBeneficiaryTerm": null,
"PendingOwnerAddress": null,
"SectorSize": 2048,
"WindowPoStPartitionSectors": 0,
"WindowPoStProofType": 0,
"Worker": "f00",
"WorkerChangeEpoch": 0
}),
Self {
owner: Address::default().into(),
worker: Address::default().into(),
new_worker: Default::default(),
control_addresses: Default::default(),
worker_change_epoch: Default::default(),
peer_id: Default::default(),
multiaddrs: Default::default(),
window_post_proof_type:
fvm_shared2::sector::RegisteredPoStProof::StackedDRGWinning2KiBV1,
sector_size: crate::shim::sector::SectorSize::_2KiB.into(),
window_post_partition_sectors: Default::default(),
consensus_fault_elapsed: Default::default(),
pending_owner_address: Default::default(),
beneficiary: Address::default().into(),
beneficiary_term: Default::default(),
pending_beneficiary_term: Default::default(),
},
)]
}
fn into_lotus_json(self) -> Self::LotusJson {
MinerInfoLotusJson {
owner: self.owner.into(),
worker: self.worker.into(),
new_worker: AddressOrEmpty(self.new_worker.map(|addr| addr.into())),
control_addresses: self
.control_addresses
.into_iter()
.map(|a| a.into())
.collect(),
worker_change_epoch: self.worker_change_epoch,
peer_id: PeerId::try_from(self.peer_id).map(|id| id.to_base58()).ok(),
multiaddrs: self.multiaddrs.into_iter().map(|addr| addr.0).collect(),
window_po_st_proof_type: self.window_post_proof_type,
sector_size: self.sector_size.into(),
window_po_st_partition_sectors: self.window_post_partition_sectors,
consensus_fault_elapsed: self.consensus_fault_elapsed,
// NOTE: In Lotus this field is never set for any of the versions, so we have to ignore
// it too.
// See: <https://github.com/filecoin-project/lotus/blob/b6a77dfafcf0110e95840fca15a775ed663836d8/chain/actors/builtin/miner/v12.go#L370>.
pending_owner_address: None,
beneficiary: self.beneficiary.into(),
beneficiary_term: self.beneficiary_term,
pending_beneficiary_term: self.pending_beneficiary_term,
}
}
fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
MinerInfo {
owner: lotus_json.owner.into(),
worker: lotus_json.worker.into(),
new_worker: lotus_json.new_worker.0.map(|addr| addr.into()),
control_addresses: lotus_json
.control_addresses
.into_iter()
.map(|a| a.into())
.collect(),
worker_change_epoch: lotus_json.worker_change_epoch,
peer_id: lotus_json.peer_id.map_or_else(Vec::new, |s| s.into_bytes()),
multiaddrs: lotus_json.multiaddrs.into_iter().map(BytesDe).collect(),
window_post_proof_type: lotus_json.window_po_st_proof_type,
sector_size: lotus_json.sector_size.into(),
window_post_partition_sectors: lotus_json.window_po_st_partition_sectors,
consensus_fault_elapsed: lotus_json.consensus_fault_elapsed,
// Ignore this field as it is never set on Lotus side.
pending_owner_address: None,
beneficiary: lotus_json.beneficiary.into(),
beneficiary_term: lotus_json.beneficiary_term,
pending_beneficiary_term: lotus_json.pending_beneficiary_term,
}
}
}

#[test]
fn snapshots() {
assert_all_snapshots::<MinerInfo>();
}
47 changes: 47 additions & 0 deletions src/lotus_json/miner_power.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::*;
use fil_actor_interface::miner::MinerPower;
use fil_actor_interface::power::Claim;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "MinerPower")]
pub struct MinerPowerLotusJson {
#[schemars(with = "LotusJson<Claim>")]
#[serde(with = "crate::lotus_json")]
miner_power: Claim,
#[schemars(with = "LotusJson<Claim>")]
#[serde(with = "crate::lotus_json")]
total_power: Claim,
has_min_power: bool,
}

impl HasLotusJson for MinerPower {
type LotusJson = MinerPowerLotusJson;
#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
unimplemented!("see commented-out test, below")
}
fn into_lotus_json(self) -> Self::LotusJson {
MinerPowerLotusJson {
miner_power: self.miner_power,
total_power: self.total_power,
has_min_power: self.has_min_power,
}
}
fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
MinerPower {
miner_power: lotus_json.miner_power,
total_power: lotus_json.total_power,
has_min_power: lotus_json.has_min_power,
}
}
}

// MinerPower: !Debug
// #[test]
// fn snapshots() {
// assert_all_snapshots::<MinerPower>();
// }
9 changes: 8 additions & 1 deletion src/lotus_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ decl_and_test!(
registered_po_st_proof for crate::shim::sector::RegisteredPoStProof,
registered_seal_proof for crate::shim::sector::RegisteredSealProof,
sector_info for crate::shim::sector::SectorInfo,
sector_size for crate::shim::sector::SectorSize,
signature for crate::shim::crypto::Signature,
signature_type for crate::shim::crypto::SignatureType,
signed_message for crate::message::SignedMessage,
Expand All @@ -213,12 +214,18 @@ decl_and_test!(
vrf_proof for crate::blocks::VRFProof,
);

// If a module cannot be tested normally above, you MAY declare it separately here
// but you MUST document any tech debt - the reason WHY it cannot be tested above.
mod beneficiary_term; // fil_actor_miner_state::v12::BeneficiaryTerm: !quickcheck::Arbitrary
mod bit_field; // fil_actors_shared::fvm_ipld_bitfield::BitField: !quickcheck::Arbitrary
mod cid; // can't make snapshots of generic type
mod hash_map;
mod ipld; // NaN != NaN
mod nonempty;
mod miner_info; // fil_actor_miner_state::v12::MinerInfo: !quickcheck::Arbitrary
mod miner_power; // fil_actor_interface::miner::MinerInfo: !quickcheck::Arbitrary
mod nonempty; // can't make snapshots of generic type
mod opt; // can't make snapshots of generic type
mod pending_beneficiary_change; // fil_actor_miner_state::v12::PendingBeneficiaryChange: !quickcheck::Arbitrary
mod power_claim; // fil_actor_interface::power::Claim: !quickcheck::Arbitrary
mod raw_bytes; // fvm_ipld_encoding::RawBytes: !quickcheck::Arbitrary
mod receipt; // shim type roundtrip is wrong - see module
Expand Down
71 changes: 71 additions & 0 deletions src/lotus_json/pending_beneficiary_change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::*;

use crate::shim::{address::Address, clock::ChainEpoch, econ::TokenAmount};
use fil_actor_miner_state::v12::PendingBeneficiaryChange;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "PendingBeneficiaryChange")]
pub struct PendingBeneficiaryChangeLotusJson {
#[schemars(with = "LotusJson<Address>")]
#[serde(with = "crate::lotus_json")]
pub new_beneficiary: Address,
#[schemars(with = "LotusJson<TokenAmount>")]
#[serde(with = "crate::lotus_json")]
pub new_quota: TokenAmount,
pub new_expiration: ChainEpoch,
pub approved_by_beneficiary: bool,
pub approved_by_nominee: bool,
}

impl HasLotusJson for PendingBeneficiaryChange {
type LotusJson = PendingBeneficiaryChangeLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![(
json!({
"NewBeneficiary": "f00",
"NewQuota": "0",
"NewExpiration": 0,
"ApprovedByBeneficiary": false,
"ApprovedByNominee": false,
}),
Self {
new_beneficiary: Default::default(),
new_quota: Default::default(),
new_expiration: Default::default(),
approved_by_beneficiary: Default::default(),
approved_by_nominee: Default::default(),
},
)]
}

fn into_lotus_json(self) -> Self::LotusJson {
PendingBeneficiaryChangeLotusJson {
new_beneficiary: self.new_beneficiary.into(),
new_quota: self.new_quota.into(),
new_expiration: self.new_expiration,
approved_by_beneficiary: self.approved_by_beneficiary,
approved_by_nominee: self.approved_by_nominee,
}
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
Self {
new_beneficiary: lotus_json.new_beneficiary.into(),
new_quota: lotus_json.new_quota.into(),
new_expiration: lotus_json.new_expiration,
approved_by_beneficiary: lotus_json.approved_by_beneficiary,
approved_by_nominee: lotus_json.approved_by_nominee,
}
}
}

#[test]
fn snapshots() {
assert_all_snapshots::<PendingBeneficiaryChange>();
}
27 changes: 27 additions & 0 deletions src/lotus_json/sector_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::*;
use crate::shim::sector::SectorSize;

#[derive(Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "SectorSize")]
// This should probably be a JSON Schema `enum`
pub struct SectorSizeLotusJson(#[schemars(with = "u64")] SectorSize);

impl HasLotusJson for SectorSize {
type LotusJson = SectorSizeLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![(json!(2048), Self::_2KiB)]
}

fn into_lotus_json(self) -> Self::LotusJson {
SectorSizeLotusJson(self)
}

fn from_lotus_json(SectorSizeLotusJson(inner): Self::LotusJson) -> Self {
inner
}
}
Loading

0 comments on commit ca2d6a1

Please sign in to comment.