Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor types out of actors crate to prep for v2 upgrade #790

Merged
merged 9 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions node/rpc/src/state_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use crate::RpcState;
use actor::miner::{
ChainSectorInfo, DeadlineInfo, Deadlines, Fault, MinerInfo, SectorOnChainInfo,
SectorPreCommitOnChainInfo, State,
ChainSectorInfo, Deadlines, Fault, MinerInfo, SectorOnChainInfo, SectorPreCommitOnChainInfo,
State,
};
use address::{json::AddressJson, Address};
use async_std::task;
Expand All @@ -13,7 +13,7 @@ use blocks::{tipset_json::TipsetJson, Tipset, TipsetKeys};
use blockstore::BlockStore;
use cid::{json::CidJson, Cid};
use clock::ChainEpoch;
use fil_types::{verifier::FullVerifier, SectorNumber};
use fil_types::{deadlines::DeadlineInfo, verifier::FullVerifier, SectorNumber};
use jsonrpc_v2::{Data, Error as JsonRpcError, Params};
use message::{
message_receipt::json::MessageReceiptJson,
Expand Down
3 changes: 2 additions & 1 deletion types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ chrono = "0.4.9"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.56"
commcid = { path = "../utils/commcid" }
filecoin-proofs-api = "5.2"
filecoin-proofs-api = { version = "5.2", optional = true }
vm = { package = "forest_vm", path = "../vm" }
cid = { package = "forest_cid", path = "../ipld/cid", features = ["cbor"] }
num-bigint = { path = "../utils/bigint", package = "forest_bigint", features=["json"] }
Expand All @@ -30,3 +30,4 @@ async-std = "1.6.3"

[features]
json = ["base64", "forest_json_utils"]
proofs = ["filecoin-proofs-api"]
155 changes: 155 additions & 0 deletions types/src/deadlines/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

mod quantize;

pub use self::quantize::*;
use clock::ChainEpoch;
use serde::{Deserialize, Serialize};

/// Deadline calculations with respect to a current epoch.
/// "Deadline" refers to the window during which proofs may be submitted.
/// Windows are non-overlapping ranges [Open, Close), but the challenge epoch for a window occurs before
/// the window opens.
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Copy, Clone)]
pub struct DeadlineInfo {
/// Epoch at which this info was calculated.
pub current_epoch: ChainEpoch,
/// First epoch of the proving period (<= CurrentEpoch).
pub period_start: ChainEpoch,
/// Current deadline index, in [0..WPoStProvingPeriodDeadlines).
pub index: u64,
/// First epoch from which a proof may be submitted (>= CurrentEpoch).
pub open: ChainEpoch,
/// First epoch from which a proof may no longer be submitted (>= Open).
pub close: ChainEpoch,
/// Epoch at which to sample the chain for challenge (< Open).
pub challenge: ChainEpoch,
/// First epoch at which a fault declaration is rejected (< Open).
pub fault_cutoff: ChainEpoch,

// Protocol parameters (This is intentionally included in the JSON response for deadlines)
w_post_period_deadlines: u64,
w_post_proving_period: ChainEpoch,
w_post_challenge_window: ChainEpoch,
w_post_challenge_lookback: ChainEpoch,
fault_declaration_cutoff: ChainEpoch,
}

impl DeadlineInfo {
#[allow(clippy::too_many_arguments)]
pub fn new(
period_start: ChainEpoch,
deadline_idx: u64,
current_epoch: ChainEpoch,
w_post_period_deadlines: u64,
w_post_proving_period: ChainEpoch,
w_post_challenge_window: ChainEpoch,
w_post_challenge_lookback: ChainEpoch,
fault_declaration_cutoff: ChainEpoch,
) -> Self {
if deadline_idx < w_post_period_deadlines {
let deadline_open = period_start + (deadline_idx as i64 * w_post_challenge_window);
Self {
current_epoch,
period_start,
index: deadline_idx,
open: deadline_open,
close: deadline_open + w_post_challenge_window,
challenge: deadline_open - w_post_challenge_lookback,
fault_cutoff: deadline_open - fault_declaration_cutoff,
w_post_period_deadlines,
w_post_proving_period,
w_post_challenge_window,
w_post_challenge_lookback,
fault_declaration_cutoff,
}
} else {
let after_last_deadline = period_start + w_post_proving_period;
Self {
current_epoch,
period_start,
index: deadline_idx,
open: after_last_deadline,
close: after_last_deadline,
challenge: after_last_deadline,
fault_cutoff: 0,
w_post_period_deadlines,
w_post_proving_period,
w_post_challenge_window,
w_post_challenge_lookback,
fault_declaration_cutoff,
}
}
}

/// Whether the proving period has begun.
pub fn period_started(&self) -> bool {
self.current_epoch >= self.period_start
}

/// Whether the proving period has elapsed.
pub fn period_elapsed(&self) -> bool {
self.current_epoch >= self.next_period_start()
}

/// The last epoch in the proving period.
pub fn period_end(&self) -> ChainEpoch {
self.period_start + self.w_post_proving_period - 1
}

/// The first epoch in the next proving period.
pub fn next_period_start(&self) -> ChainEpoch {
self.period_start + self.w_post_proving_period
}

/// Whether the current deadline is currently open.
pub fn is_open(&self) -> bool {
self.current_epoch >= self.open && self.current_epoch < self.close
}

/// Whether the current deadline has already closed.
pub fn has_elapsed(&self) -> bool {
self.current_epoch >= self.close
}

/// The last epoch during which a proof may be submitted.
pub fn last(&self) -> ChainEpoch {
self.close - 1
}

/// Epoch at which the subsequent deadline opens.
pub fn next_open(&self) -> ChainEpoch {
self.close
}

/// Whether the deadline's fault cutoff has passed.
pub fn fault_cutoff_passed(&self) -> bool {
self.current_epoch >= self.fault_cutoff
}

/// Returns the next instance of this deadline that has not yet elapsed.
pub fn next_not_elapsed(self) -> Self {
std::iter::successors(Some(self), |info| {
Some(Self::new(
info.next_period_start(),
info.index,
info.current_epoch,
self.w_post_period_deadlines,
self.w_post_proving_period,
self.w_post_challenge_window,
self.w_post_challenge_lookback,
self.fault_declaration_cutoff,
))
})
.find(|info| !info.has_elapsed())
.unwrap() // the iterator is infinite, so `find` won't ever return `None`
}

pub fn quant_spec(&self) -> QuantSpec {
QuantSpec {
unit: self.w_post_proving_period,
offset: self.last(),
}
}
}
5 changes: 4 additions & 1 deletion types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
// SPDX-License-Identifier: Apache-2.0, MIT

pub mod build_version;
pub mod deadlines;
pub mod genesis;
mod piece;
mod randomness;
pub mod sector;
pub mod verifier;
mod version;

#[cfg(feature = "proofs")]
pub mod verifier;

pub use self::piece::*;
pub use self::randomness::*;
pub use self::sector::*;
Expand Down
14 changes: 10 additions & 4 deletions types/src/piece/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

#[cfg(feature = "proofs")]
mod zero;

#[cfg(feature = "proofs")]
pub use zero::zero_piece_commitment;

use cid::Cid;
use commcid::cid_to_piece_commitment_v1;
use encoding::tuple::*;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
pub use zero::zero_piece_commitment;

/// Size of a piece in bytes
#[derive(PartialEq, Debug, Eq, Clone, Copy)]
Expand Down Expand Up @@ -69,17 +70,22 @@ pub struct PieceInfo {
pub cid: Cid,
}

#[cfg(feature = "proofs")]
use std::convert::TryFrom;

#[cfg(feature = "proofs")]
impl TryFrom<&PieceInfo> for filecoin_proofs_api::PieceInfo {
type Error = &'static str;

fn try_from(p: &PieceInfo) -> Result<Self, Self::Error> {
Ok(Self {
commitment: cid_to_piece_commitment_v1(&p.cid)?,
commitment: commcid::cid_to_piece_commitment_v1(&p.cid)?,
size: p.size.unpadded().into(),
})
}
}

#[cfg(feature = "proofs")]
impl From<UnpaddedPieceSize> for filecoin_proofs_api::UnpaddedBytesAmount {
fn from(p: UnpaddedPieceSize) -> Self {
Self(p.0)
Expand Down
9 changes: 6 additions & 3 deletions types/src/sector/registered_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use super::SectorSize;
use filecoin_proofs_api as proofs;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "proofs")]
use std::convert::TryFrom;

#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
Expand Down Expand Up @@ -187,7 +188,8 @@ i64_conversion! {
StackedDRG64GiBV1 => 4,
}

impl TryFrom<RegisteredSealProof> for proofs::RegisteredSealProof {
#[cfg(feature = "proofs")]
impl TryFrom<RegisteredSealProof> for filecoin_proofs_api::RegisteredSealProof {
type Error = String;
fn try_from(p: RegisteredSealProof) -> Result<Self, Self::Error> {
use RegisteredSealProof::*;
Expand All @@ -214,7 +216,8 @@ impl From<SectorSize> for RegisteredSealProof {
}
}

impl TryFrom<RegisteredPoStProof> for proofs::RegisteredPoStProof {
#[cfg(feature = "proofs")]
impl TryFrom<RegisteredPoStProof> for filecoin_proofs_api::RegisteredPoStProof {
type Error = String;
fn try_from(p: RegisteredPoStProof) -> Result<Self, Self::Error> {
use RegisteredPoStProof::*;
Expand Down
3 changes: 3 additions & 0 deletions vm/actor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.1.0"
authors = ["ChainSafe Systems <info@chainsafe.io>"]
edition = "2018"

[features]
v2 = ["ipld_hamt/v2"]

[dependencies]
vm = { package = "forest_vm", path = "../../vm" }
address = { package = "forest_address", path = "../address" }
Expand Down
1 change: 0 additions & 1 deletion vm/actor/src/builtin/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ impl Actor {

impl ActorCode for Actor {
fn invoke_method<BS, RT>(
&self,
rt: &mut RT,
method: MethodNum,
params: &Serialized,
Expand Down
1 change: 0 additions & 1 deletion vm/actor/src/builtin/cron/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ impl Actor {

impl ActorCode for Actor {
fn invoke_method<BS, RT>(
&self,
rt: &mut RT,
method: MethodNum,
params: &Serialized,
Expand Down
1 change: 0 additions & 1 deletion vm/actor/src/builtin/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl Actor {

impl ActorCode for Actor {
fn invoke_method<BS, RT>(
&self,
rt: &mut RT,
method: MethodNum,
params: &Serialized,
Expand Down
1 change: 0 additions & 1 deletion vm/actor/src/builtin/market/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,6 @@ where

impl ActorCode for Actor {
fn invoke_method<BS, RT>(
&self,
rt: &mut RT,
method: MethodNum,
params: &Serialized,
Expand Down
4 changes: 2 additions & 2 deletions vm/actor/src/builtin/market/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::network::{
DEAL_WEIGHT_MULTIPLIER, EPOCHS_IN_DAY, QUALITY_BASE_MULTIPLIER, SECTOR_QUALITY_PRECISION,
VERIFIED_DEAL_WEIGHT_MULTIPLIER,
};
use crate::{DealWeight, TOTAL_FILECOIN};
use crate::DealWeight;
use clock::ChainEpoch;
use fil_types::{NetworkVersion, PaddedPieceSize, StoragePower};
use fil_types::{NetworkVersion, PaddedPieceSize, StoragePower, TOTAL_FILECOIN};
use num_bigint::Integer;
use num_traits::Zero;
use std::cmp::max;
Expand Down
2 changes: 1 addition & 1 deletion vm/actor/src/builtin/miner/bitfield_queue.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::QuantSpec;
use crate::ActorDowncast;
use bitfield::BitField;
use cid::Cid;
use clock::ChainEpoch;
use fil_types::deadlines::QuantSpec;
use ipld_amt::{Amt, Error as AmtError};
use ipld_blockstore::BlockStore;
use std::collections::HashMap;
Expand Down
4 changes: 2 additions & 2 deletions vm/actor/src/builtin/miner/deadline_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

use super::{
BitFieldQueue, ExpirationSet, Partition, PartitionSectorMap, PoStPartition, PowerPair,
QuantSpec, SectorOnChainInfo, Sectors, TerminationResult, WPOST_PERIOD_DEADLINES,
SectorOnChainInfo, Sectors, TerminationResult, WPOST_PERIOD_DEADLINES,
};
use crate::{actor_error, ActorDowncast, ActorError, ExitCode, TokenAmount};
use bitfield::BitField;
use cid::{multihash::Blake2b256, Cid};
use clock::ChainEpoch;
use encoding::tuple::*;
use fil_types::SectorSize;
use fil_types::{deadlines::QuantSpec, SectorSize};
use ipld_amt::Amt;
use ipld_blockstore::BlockStore;
use num_traits::Zero;
Expand Down
Loading