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

fix(primitives): extract PreCommitSectorInfo #679

Merged
merged 3 commits into from
Jan 17, 2025
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
1 change: 1 addition & 0 deletions pallets/storage-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ env_logger = { workspace = true }
multihash-codetable = { workspace = true, features = ["blake2b"] }
pallet-balances = { workspace = true, default-features = false }
pallet-market = { workspace = true, default-features = false }
primitives = { workspace = true, default-features = false, features = ["builder"] }
rstest = { workspace = true }
sp-io = { workspace = true }

Expand Down
6 changes: 3 additions & 3 deletions pallets/storage-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub mod pallet {
},
proofs::{derive_prover_id, PublicReplicaInfo, RegisteredPoStProof},
randomness::{draw_randomness, AuthorVrfHistory, DomainSeparationTag},
sector::SectorNumber,
sector::{SectorNumber, SectorPreCommitInfo},
PartitionNumber, MAX_PARTITIONS_PER_DEADLINE, MAX_SEAL_PROOF_BYTES, MAX_SECTORS,
MAX_SECTORS_PER_CALL,
};
Expand All @@ -78,8 +78,8 @@ pub mod pallet {
},
proofs::{assign_proving_period_offset, SubmitWindowedPoStParams},
sector::{
ProveCommitResult, ProveCommitSector, SectorOnChainInfo, SectorPreCommitInfo,
SectorPreCommitOnChainInfo, TerminateSectorsParams, TerminationDeclaration,
ProveCommitResult, ProveCommitSector, SectorOnChainInfo, SectorPreCommitOnChainInfo,
TerminateSectorsParams, TerminationDeclaration,
},
sector_map::DeadlineSectorMap,
storage_provider::{
Expand Down
29 changes: 4 additions & 25 deletions pallets/storage-provider/src/sector.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
use codec::{Decode, Encode};
use frame_support::{pallet_prelude::*, BoundedVec};
use primitives::{
pallets::SectorDeal, proofs::RegisteredSealProof, sector::SectorNumber, DealId,
PartitionNumber, CID_SIZE_IN_BYTES, MAX_DEALS_PER_SECTOR, MAX_SEAL_PROOF_BYTES,
MAX_TERMINATIONS_PER_CALL,
pallets::SectorDeal,
proofs::RegisteredSealProof,
sector::{SectorNumber, SectorPreCommitInfo},
PartitionNumber, CID_SIZE_IN_BYTES, MAX_SEAL_PROOF_BYTES, MAX_TERMINATIONS_PER_CALL,
};
use scale_info::TypeInfo;

use crate::pallet::DECLARATIONS_MAX;

/// This type is passed into the pre commit function on the storage provider pallet
#[derive(Clone, RuntimeDebug, Decode, Encode, PartialEq, Eq, TypeInfo)]
pub struct SectorPreCommitInfo<BlockNumber> {
pub seal_proof: RegisteredSealProof,
/// Which sector number this SP is pre-committing.
pub sector_number: SectorNumber,
/// This value is also known as `commR` or "commitment of replication". The terms `commR` and `sealed_cid` are interchangeable.
/// Using sealed_cid as I think that is more descriptive.
/// Some docs on commR here: <https://proto.school/verifying-storage-on-filecoin/03>
pub sealed_cid: BoundedVec<u8, ConstU32<CID_SIZE_IN_BYTES>>,
/// The block number at which we requested the randomness when sealing the sector.
pub seal_randomness_height: BlockNumber,
/// Deals Ids that are supposed to be activated.
/// If any of those is invalid, whole activation is rejected.
pub deal_ids: BoundedVec<DealId, ConstU32<MAX_DEALS_PER_SECTOR>>,
/// Expiration of the pre-committed sector.
pub expiration: BlockNumber,
/// This value is also known as `commD` or "commitment of data".
/// Once a sector is full `commD` is produced representing the root node of all of the piece CIDs contained in the sector.
pub unsealed_cid: BoundedVec<u8, ConstU32<CID_SIZE_IN_BYTES>>,
}

/// Information stored on-chain for a pre-committed sector.
#[derive(Clone, RuntimeDebug, Decode, Encode, TypeInfo)]
pub struct SectorPreCommitOnChainInfo<Balance, BlockNumber> {
Expand Down
83 changes: 4 additions & 79 deletions pallets/storage-provider/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
extern crate alloc;
use alloc::collections::BTreeSet;
use core::str::FromStr;

use cid::Cid;
use codec::Encode;
use frame_support::{
assert_ok, derive_impl,
Expand All @@ -19,9 +17,8 @@ use primitives::{
pallets::ProofVerification,
proofs::{ProverId, PublicReplicaInfo, RegisteredPoStProof, RegisteredSealProof, Ticket},
sector::SectorNumber,
DealId, PartitionNumber, CID_SIZE_IN_BYTES, MAX_DEALS_PER_SECTOR, MAX_PARTITIONS_PER_DEADLINE,
MAX_POST_PROOF_BYTES, MAX_PROOFS_PER_BLOCK, MAX_REPLICAS_PER_BLOCK, MAX_SEAL_PROOF_BYTES,
MAX_TERMINATIONS_PER_CALL, PEER_ID_MAX_BYTES,
PartitionNumber, MAX_PARTITIONS_PER_DEADLINE, MAX_POST_PROOF_BYTES, MAX_PROOFS_PER_BLOCK,
MAX_REPLICAS_PER_BLOCK, MAX_SEAL_PROOF_BYTES, MAX_TERMINATIONS_PER_CALL, PEER_ID_MAX_BYTES,
};
use sp_arithmetic::traits::Zero;
use sp_core::{bounded_vec, Pair};
Expand All @@ -37,7 +34,6 @@ use crate::{
},
pallet::DECLARATIONS_MAX,
proofs::{PoStProof, SubmitWindowedPoStParams},
sector::SectorPreCommitInfo,
};

mod deadline;
Expand All @@ -53,6 +49,8 @@ mod storage_provider_registration;
mod submit_windowed_post;
mod terminate_sectors;

pub type SectorPreCommitInfoBuilder = primitives::sector::builder::SectorPreCommitInfoBuilder<u64>;

type Block = frame_system::mocking::MockBlock<Test>;
type BlockNumber = u64;

Expand Down Expand Up @@ -374,79 +372,6 @@ fn publish_deals(storage_provider: &str) {
System::reset_events();
}

struct SectorPreCommitInfoBuilder {
seal_proof: RegisteredSealProof,
sector_number: SectorNumber,
sealed_cid: BoundedVec<u8, ConstU32<CID_SIZE_IN_BYTES>>,
deal_ids: BoundedVec<DealId, ConstU32<MAX_DEALS_PER_SECTOR>>,
expiration: u64,
unsealed_cid: BoundedVec<u8, ConstU32<CID_SIZE_IN_BYTES>>,
seal_randomness_height: u64,
}

impl Default for SectorPreCommitInfoBuilder {
fn default() -> Self {
let unsealed_cid =
Cid::from_str("baga6ea4seaqmruupwrxaeck7m3f5jtswpr7jv6bvwqeu5jinzjlcybh6er3ficq")
.unwrap()
.to_bytes()
.try_into()
.expect("hash is always 32 bytes");

let sealed_cid =
Cid::from_str("bagboea4b5abcamxmh7exq7vrvacvajooeapagr3a4g3tpjhw73iny47hvafw76gr")
.unwrap()
.to_bytes()
.try_into()
.expect("hash is always 32 bytes");

Self {
seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1,
sector_number: SectorNumber::new(1).unwrap(),
sealed_cid,
deal_ids: bounded_vec![0, 1],
expiration: 120 * MINUTES,
unsealed_cid,
seal_randomness_height: 1,
}
}
}

impl SectorPreCommitInfoBuilder {
pub fn sector_number(mut self, sector_number: SectorNumber) -> Self {
self.sector_number = sector_number;
self
}

pub fn deals(mut self, deal_ids: Vec<u64>) -> Self {
self.deal_ids = BoundedVec::try_from(deal_ids).unwrap();
self
}

pub fn expiration(mut self, expiration: u64) -> Self {
self.expiration = expiration;
self
}

pub fn unsealed_cid(mut self, unsealed_cid: &str) -> Self {
let cid = Cid::from_str(unsealed_cid).expect("valid unsealed_cid");
self.unsealed_cid = BoundedVec::try_from(cid.to_bytes()).unwrap();
self
}

pub fn build(self) -> SectorPreCommitInfo<u64> {
SectorPreCommitInfo {
seal_proof: self.seal_proof,
sector_number: self.sector_number,
sealed_cid: self.sealed_cid,
deal_ids: self.deal_ids,
expiration: self.expiration,
unsealed_cid: self.unsealed_cid,
seal_randomness_height: self.seal_randomness_height,
}
}
}

/// Builder to simplify writing complex tests of [`DealProposal`].
/// Exclusively uses [`Test`] for simplification purposes.
struct DealProposalBuilder {
Expand Down
3 changes: 1 addition & 2 deletions pallets/storage-provider/src/tests/pre_commit_sectors.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use frame_support::{assert_noop, assert_ok, pallet_prelude::*};
use frame_system::pallet_prelude::BlockNumberFor;
use primitives::MAX_SECTORS_PER_CALL;
use primitives::{sector::SectorPreCommitInfo, MAX_SECTORS_PER_CALL};
use sp_core::bounded_vec;
use sp_runtime::{BoundedVec, DispatchError};

use super::new_test_ext;
use crate::{
pallet::{Error, Event, StorageProviders},
sector::SectorPreCommitInfo,
tests::{
account, events, publish_deals, register_storage_provider, run_to_block, Balances,
MaxProveCommitDuration, MaxSectorExpiration, RuntimeEvent, RuntimeOrigin,
Expand Down
4 changes: 2 additions & 2 deletions pallets/storage-provider/src/tests/prove_commit_sectors.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use frame_support::{assert_noop, assert_ok, pallet_prelude::*};
use frame_system::pallet_prelude::BlockNumberFor;
use primitives::MAX_SECTORS_PER_CALL;
use primitives::{sector::SectorPreCommitInfo, MAX_SECTORS_PER_CALL};
use sp_core::bounded_vec;

use super::{new_test_ext, MaxProveCommitDuration};
use crate::{
deadline::deadline_is_mutable,
error::GeneralPalletError,
pallet::{Error, Event, StorageProviders},
sector::{ProveCommitResult, ProveCommitSector, SectorPreCommitInfo},
sector::{ProveCommitResult, ProveCommitSector},
tests::{
account, events, publish_deals, register_storage_provider, run_to_block, Balances,
RuntimeEvent, RuntimeOrigin, SectorPreCommitInfoBuilder, StorageProvider, System, Test,
Expand Down
1 change: 1 addition & 0 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde_json = { workspace = true, default-features = true }
workspace = true

[features]
builder = []
clap = ["dep:clap", "std"]
default = ["std"]
serde = ["dep:serde"]
Expand Down
14 changes: 14 additions & 0 deletions primitives/src/sector/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mod number;
mod pre_commit;
mod size;

// NOTE(@jmg-duarte,16/01/2025): unsure if the visitor should be exposed
pub use number::{SectorNumber, SectorNumberError};
pub use pre_commit::SectorPreCommitInfo;
pub use size::SectorSize;

// `test` is only useful locally
#[cfg(any(test, feature = "builder"))]
pub mod builder {
pub use crate::sector::pre_commit::builder::SectorPreCommitInfoBuilder;
}
53 changes: 5 additions & 48 deletions primitives/src/sector.rs → primitives/src/sector/number.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use core::{fmt::Display, marker::PhantomData};
//! Sector number primitive.

use core::marker::PhantomData;

use codec::{Decode, Encode, MaxEncodedLen};
use scale_decode::{
visitor::{self},
DecodeAsType, ToString, TypeResolver, Visitor,
ToString, TypeResolver, Visitor,
};
use scale_encode::EncodeAsType;
use scale_info::TypeInfo;
Expand Down Expand Up @@ -164,53 +166,8 @@ impl From<SectorNumber> for u64 {
}
}

impl Display for SectorNumber {
impl core::fmt::Display for SectorNumber {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}

/// SectorSize indicates one of a set of possible sizes in the network.
#[derive(
Encode, Decode, DecodeAsType, EncodeAsType, TypeInfo, Clone, RuntimeDebug, PartialEq, Eq, Copy,
)]
#[cfg_attr(feature = "serde", derive(::serde::Deserialize, ::serde::Serialize))]
#[codec(crate = ::codec)]
#[decode_as_type(crate_path = "::scale_decode")]
#[encode_as_type(crate_path = "::scale_encode")]
pub enum SectorSize {
_2KiB,
_8MiB,
_512MiB,
_32GiB,
_64GiB,
}

impl SectorSize {
/// Returns the size of a sector in bytes
/// <https://github.com/filecoin-project/ref-fvm/blob/5659196fa94accdf1e7f10e00586a8166c44a60d/shared/src/sector/mod.rs#L40>
pub fn bytes(&self) -> u64 {
match self {
SectorSize::_2KiB => 2 << 10,
SectorSize::_8MiB => 8 << 20,
SectorSize::_512MiB => 512 << 20,
SectorSize::_32GiB => 32 << 30,
SectorSize::_64GiB => 2 * (32 << 30),
}
}
}

impl core::fmt::Display for SectorSize {
fn fmt(
&self,
f: &mut scale_info::prelude::fmt::Formatter<'_>,
) -> scale_info::prelude::fmt::Result {
match self {
SectorSize::_2KiB => write!(f, "2KiB"),
SectorSize::_8MiB => write!(f, "8MiB"),
SectorSize::_512MiB => write!(f, "512MiB"),
SectorSize::_32GiB => write!(f, "32GiB"),
SectorSize::_64GiB => write!(f, "64GiB"),
}
}
}
Loading
Loading