Skip to content

Commit

Permalink
chore: extract primitives for proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
th7nder committed Jun 24, 2024
1 parent e6419a8 commit 6f3d785
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 94 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license-file = "LICENSE"
repository = "https://github.com/eigerco/polka-storage"

[workspace]
members = ["cli/polka-storage-provider", "node", "pallets/market", "primitives/cli", "runtime", "storage/mater", "storage/polka-index"]
members = ["cli/polka-storage-provider", "node", "pallets/market", "primitives/cli", "primitives/proofs", "runtime", "storage/mater", "storage/polka-index"]
resolver = "2"

[profile.ci]
Expand Down Expand Up @@ -83,6 +83,7 @@ uuid = "1.8.0"
cli-primitives = { path = "primitives/cli" }
pallet-market = { path = "pallets/market", default-features = false }
polka-storage-runtime = { path = "runtime" }
primitives-proofs = { path = "primitives/proofs" }

# Substrate
pallet-transaction-payment-rpc = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-v1.13.0" }
Expand Down
1 change: 1 addition & 0 deletions pallets/market/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cid = { workspace = true, default-features = false, features = ["scale-codec"] }
codec = { workspace = true, default-features = false, features = ["derive"] }
log = { workspace = true }
multihash-codetable = { workspace = true, features = ["blake2b"] }
primitives-proofs = { workspace = true }
scale-info = { workspace = true, default-features = false, features = ["derive"] }

# frame deps
Expand Down
142 changes: 50 additions & 92 deletions pallets/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub mod pallet {
};
use frame_system::{pallet_prelude::*, Config as SystemConfig, Pallet as System};
use multihash_codetable::{Code, MultihashDigest};
use primitives_proofs::{
DealId, Market, RegisteredSealProof, SectorDeal, SectorNumber, SectorSize,
};
use scale_info::TypeInfo;
use sp_arithmetic::traits::BaseArithmetic;
use sp_std::vec::Vec;
Expand All @@ -49,9 +52,6 @@ pub mod pallet {
pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;

// TODO(@th7nder,17/06/2024): this is likely to be extracted into primitives/ package
type DealId = u64;

#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
Expand Down Expand Up @@ -93,10 +93,6 @@ pub mod pallet {
/// https://github.com/filecoin-project/builtin-actors/blob/c32c97229931636e3097d92cf4c43ac36a7b4b47/actors/market/src/policy.rs#L29
#[pallet::constant]
type MaxDealDuration: Get<BlockNumberFor<Self>>;

/// How many deals can be activated in a single batch.
#[pallet::constant]
type MaxSectorsForActivation: Get<u32>;
}

/// Stores balances info for both Storage Providers and Storage Users
Expand Down Expand Up @@ -220,47 +216,6 @@ pub mod pallet {
pub client_signature: OffchainSignature,
}

// TODO(@th7nder,20/06/2024): this DOES NOT belong here. it should be somewhere else.
#[allow(non_camel_case_types)]
#[derive(Debug, Decode, Encode, TypeInfo, Eq, PartialEq, Clone)]
pub enum RegisteredSealProof {
StackedDRG2KiBV1P1,
}

impl RegisteredSealProof {
pub fn sector_size(&self) -> SectorSize {
SectorSize::_2KiB
}
}

/// SectorSize indicates one of a set of possible sizes in the network.
#[derive(Encode, Decode, TypeInfo, Clone, Debug, PartialEq, Eq, Copy)]
pub enum SectorSize {
_2KiB,
}

impl SectorSize {
/// <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,
}
}
}

// TODO(@th7nder,20/06/2024): this DOES not belong here. it should be somewhere else.
pub type SectorNumber = u64;

#[derive(Debug, Decode, Encode, TypeInfo, Eq, PartialEq, Clone)]
pub struct SectorDeal<BlockNumber> {
pub sector_number: SectorNumber,
pub sector_expiry: BlockNumber,
pub sector_type: RegisteredSealProof,
pub deal_ids: BoundedVec<DealId, ConstU32<128>>,
}
// verify_deals_for_activation is called by Storage Provider Pallllllet!
// it's not an extrinsic then?

#[pallet::pallet]
pub struct Pallet<T>(_);

Expand Down Expand Up @@ -315,10 +270,15 @@ pub mod pallet {
AllProposalsInvalid,
/// `publish_storage_deals`'s core logic was invoked with a broken invariant that should be called by `validate_deals`.
UnexpectedValidationError,
/// There is more than 1 deal of this ID in the Sector
DuplicateDeal,
/// Due to a programmer bug, bounds on Bounded data structures were incorrect so couldn't insert into them.
DealPreconditionFailed,
/// Tried to activate a deal which is not in the system,
DealNotFound,
/// Tried to activate a deal, but data doesn't make sense. Details are in the logs.
DealActivationError,
/// Sum of all of the deals piece sizes for a sector exceeds sector size.
DealsTooLargeToFitIntoSector,
}

Expand Down Expand Up @@ -522,50 +482,6 @@ pub mod pallet {
Ok(())
}

/// Verifies a given set of storage deals is valid for sectors being PreCommitted.
/// Computes UnsealedCID (CommD) for each sector or None for Committed Capacity sectors..
/// Currently UnsealedCID is hardcoded as we `compute_commd` remains unimplemented because of #92.
pub fn verify_deals_for_activation(
storage_provider: &T::AccountId,
sector_deals: BoundedVec<SectorDeal<BlockNumberFor<T>>, T::MaxSectorsForActivation>,
) -> Result<BoundedVec<Option<Cid>, T::MaxSectorsForActivation>, DispatchError> {
// TODO:
// - primitives
// - trait in primitives
// - docs
let curr_block = System::<T>::block_number();
let mut unsealed_cids = BoundedVec::new();
for sector in sector_deals {
let proposals = Self::proposals_for_deals(sector.deal_ids)?;
let sector_size = sector.sector_type.sector_size();
Self::validate_deals_for_sector(
&proposals,
storage_provider,
sector.sector_number,
sector.sector_expiry,
curr_block,
sector_size,
)?;

// Sealing a Sector without Deals, Committed Capacity Only.
let commd = if proposals.is_empty() {
None
} else {
Some(Self::compute_commd(
proposals.iter().map(|(_, deal)| deal),
sector.sector_type,
)?)
};

// PRE-COND: can't fail, unsealed_cids<_, X> == BoundedVec<_ X> == sector_deals<_, X>
unsealed_cids
.try_push(commd)
.map_err(|_| "programmer error, there should be space for Cids")?;
}

Ok(unsealed_cids)
}

/// <https://github.com/filecoin-project/builtin-actors/blob/17ede2b256bc819dc309edf38e031e246a516486/actors/market/src/lib.rs#L1370>
fn compute_commd<'a>(
_proposals: impl IntoIterator<Item = &'a DealProposalOf<T>>,
Expand Down Expand Up @@ -834,4 +750,46 @@ pub mod pallet {
T::Hashing::hash(&bytes)
}
}

impl<T: Config> Market<T::AccountId, BlockNumberFor<T>> for Pallet<T> {
/// Verifies a given set of storage deals is valid for sectors being PreCommitted.
/// Computes UnsealedCID (CommD) for each sector or None for Committed Capacity sectors.
/// Currently UnsealedCID is hardcoded as we `compute_commd` remains unimplemented because of #92.
fn verify_deals_for_activation(
storage_provider: &T::AccountId,
sector_deals: BoundedVec<SectorDeal<BlockNumberFor<T>>, ConstU32<32>>,
) -> Result<BoundedVec<Option<Cid>, ConstU32<32>>, DispatchError> {
let curr_block = System::<T>::block_number();
let mut unsealed_cids = BoundedVec::new();
for sector in sector_deals {
let proposals = Self::proposals_for_deals(sector.deal_ids)?;
let sector_size = sector.sector_type.sector_size();
Self::validate_deals_for_sector(
&proposals,
storage_provider,
sector.sector_number,
sector.sector_expiry,
curr_block,
sector_size,
)?;

// Sealing a Sector without Deals, Committed Capacity Only.
let commd = if proposals.is_empty() {
None
} else {
Some(Self::compute_commd(
proposals.iter().map(|(_, deal)| deal),
sector.sector_type,
)?)
};

// PRE-COND: can't fail, unsealed_cids<_, X> == BoundedVec<_ X> == sector_deals<_, X>
unsealed_cids
.try_push(commd)
.map_err(|_| "programmer error, there should be space for Cids")?;
}

Ok(unsealed_cids)
}
}
}
2 changes: 1 addition & 1 deletion pallets/market/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use frame_support::{
assert_noop, assert_ok,
sp_runtime::{bounded_vec, ArithmeticError, TokenError},
};
use primitives_proofs::{RegisteredSealProof, SectorDeal, Market as MarketTrait};

use crate::{
mock::*, BalanceEntry, BalanceTable, DealProposal, DealState, Error, Event, Proposals,
RegisteredSealProof, SectorDeal,
};

#[test]
Expand Down
28 changes: 28 additions & 0 deletions primitives/proofs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
authors.workspace = true
edition.workspace = true
homepage.workspace = true
license-file.workspace = true
name = "primitives-proofs"
repository.workspace = true
version = "0.1.0"

[dependencies]
cid = { workspace = true, default-features = false }
codec = { workspace = true, default-features = false }
scale-info = { workspace = true, default-features = false }

sp-runtime = { workspace = true, default-features = false }
sp-core = { workspace = true, default-features = false}

[lints]
workspace = true

[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"sp-runtime/std",
"sp-core/std"
]
7 changes: 7 additions & 0 deletions primitives/proofs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![deny(unused_crate_dependencies)]

mod traits;
mod types;

pub use traits::*;
pub use types::*;
26 changes: 26 additions & 0 deletions primitives/proofs/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use cid::Cid;
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_core::ConstU32;
use sp_runtime::{BoundedVec, DispatchError};

use crate::types::{DealId, RegisteredSealProof, SectorNumber};

/// Represents functions that are provided by the Market Provider Pallet
pub trait Market<AccountId, BlockNumber> {
/// Verifies a given set of storage deals is valid for sectors being PreCommitted.
/// Computes UnsealedCID (CommD) for each sector or None for Committed Capacity sectors.
fn verify_deals_for_activation(
storage_provider: &AccountId,
sector_deals: BoundedVec<SectorDeal<BlockNumber>, ConstU32<32>>,
) -> Result<BoundedVec<Option<Cid>, ConstU32<32>>, DispatchError>;
}

/// Binds given Sector with the Deals that it should contain
#[derive(Debug, Decode, Encode, TypeInfo, Eq, PartialEq, Clone)]
pub struct SectorDeal<BlockNumber> {
pub sector_number: SectorNumber,
pub sector_expiry: BlockNumber,
pub sector_type: RegisteredSealProof,
pub deal_ids: BoundedVec<DealId, ConstU32<128>>,
}
34 changes: 34 additions & 0 deletions primitives/proofs/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use codec::{Decode, Encode};
use scale_info::TypeInfo;

pub type DealId = u64;

pub type SectorNumber = u64;

#[allow(non_camel_case_types)]
#[derive(Debug, Decode, Encode, TypeInfo, Eq, PartialEq, Clone)]
pub enum RegisteredSealProof {
StackedDRG2KiBV1P1,
}

impl RegisteredSealProof {
pub fn sector_size(&self) -> SectorSize {
SectorSize::_2KiB
}
}

/// SectorSize indicates one of a set of possible sizes in the network.
#[derive(Encode, Decode, TypeInfo, Clone, Debug, PartialEq, Eq, Copy)]
pub enum SectorSize {
_2KiB,
}

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,
}
}
}

0 comments on commit 6f3d785

Please sign in to comment.