diff --git a/Cargo.lock b/Cargo.lock index 2014b38d..b51cf14d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14917,6 +14917,7 @@ dependencies = [ "sp-core 34.0.0 (git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-stable2409-2)", "sp-runtime 39.0.1", "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-stable2409-2)", + "thiserror 2.0.3", ] [[package]] diff --git a/cli/polka-storage-provider/server/src/db.rs b/cli/polka-storage-provider/server/src/db.rs index 59034877..f0d8d188 100644 --- a/cli/polka-storage-provider/server/src/db.rs +++ b/cli/polka-storage-provider/server/src/db.rs @@ -3,7 +3,7 @@ use std::{ sync::atomic::{AtomicU32, Ordering}, }; -use primitives_proofs::SectorNumber; +use primitives_proofs::{SectorNumber, SectorNumberError}; use rocksdb::{ColumnFamily, ColumnFamilyDescriptor, Options as DBOptions, DB as RocksDB}; use serde::{de::DeserializeOwned, Serialize}; use storagext::types::market::{ConversionError, DealProposal}; @@ -184,7 +184,7 @@ impl DealDB { /// Atomically increments sector_id counter, so it can be used as an identifier by a sector. /// Prior to all of the calls to this function, `initialize_biggest_sector_id` must be called at the node start-up. - pub fn next_sector_number(&self) -> Result { + pub fn next_sector_number(&self) -> Result { // [`Ordering::Relaxed`] can be used here, as it's an update on a single variable. // It does not depend on other Atomic variables and it does not matter which thread makes it first. // We just need it to be different on every thread that calls it concurrently, so the ids are not duplicated. diff --git a/primitives/commitment/src/piece.rs b/primitives/commitment/src/piece.rs index 7ef3b959..f4c9b4a9 100644 --- a/primitives/commitment/src/piece.rs +++ b/primitives/commitment/src/piece.rs @@ -35,6 +35,14 @@ impl From for filecoin_proofs::PieceInfo { } } +#[derive(PartialEq, Debug, Eq, Clone, Copy, thiserror::Error)] +pub enum UnpaddedPieceError { + #[error("minimum piece size is 127 bytes")] + TooSmall, + #[error("unpadded piece size must be a power of 2 multiple of 127")] + WrongSize, +} + /// Size of a piece in bytes. Unpadded piece size should be power of two /// multiple of 127. #[cfg_attr(feature = "serde", derive(::serde::Deserialize, ::serde::Serialize))] @@ -47,14 +55,14 @@ impl UnpaddedPieceSize { /// Initialize new unpadded piece size. Error is returned if the size is /// invalid. - pub fn new(size: u64) -> Result { + pub fn new(size: u64) -> Result { if size < 127 { - return Err("minimum piece size is 127 bytes"); + return Err(UnpaddedPieceError::TooSmall); } // is 127 * 2^n if size >> size.trailing_zeros() != 127 { - return Err("unpadded piece size must be a power of 2 multiple of 127"); + return Err(UnpaddedPieceError::WrongSize); } Ok(Self(size)) @@ -236,7 +244,7 @@ mod tests { ); assert_eq!( UnpaddedPieceSize::new(126), - Err("minimum piece size is 127 bytes") + Err(UnpaddedPieceError::TooSmall) ); assert_eq!( PaddedPieceSize::new(0b10000001), @@ -244,7 +252,7 @@ mod tests { ); assert_eq!( UnpaddedPieceSize::new(0b1110111000), - Err("unpadded piece size must be a power of 2 multiple of 127") + Err(UnpaddedPieceError::WrongSize) ); assert!(UnpaddedPieceSize::new(0b1111111000).is_ok()); } diff --git a/primitives/proofs/Cargo.toml b/primitives/proofs/Cargo.toml index 3916659c..9a29c296 100644 --- a/primitives/proofs/Cargo.toml +++ b/primitives/proofs/Cargo.toml @@ -18,6 +18,7 @@ sp-api = { workspace = true, default-features = false } sp-core = { workspace = true, default-features = false } sp-runtime = { workspace = true, default-features = false } sp-std = { workspace = true, default-features = false } +thiserror = { workspace = true, default-features = false } clap = { workspace = true, features = ["derive"], optional = true } serde = { workspace = true, features = ["derive"], optional = true } diff --git a/primitives/proofs/src/types.rs b/primitives/proofs/src/types.rs index 8099825f..b5cfa10c 100644 --- a/primitives/proofs/src/types.rs +++ b/primitives/proofs/src/types.rs @@ -37,9 +37,9 @@ impl SectorNumber { /// /// Returns a `Result` containing the new `SectorNumber` if valid, /// or an error message if the sector number exceeds `MAX_SECTORS`. - pub fn new(sector_number: u32) -> Result { + pub fn new(sector_number: u32) -> Result { if sector_number > MAX_SECTORS { - return Err("Sector number is too large"); + return Err(SectorNumberError::NumberTooLarge); } Ok(Self(sector_number)) @@ -69,6 +69,25 @@ impl Decode for SectorNumber { } } +#[derive( + Clone, + Copy, + PartialEq, + Ord, + PartialOrd, + Eq, + Encode, + EncodeAsType, + TypeInfo, + RuntimeDebug, + MaxEncodedLen, + thiserror::Error, +)] +pub enum SectorNumberError { + #[error("Sector number is too large")] + NumberTooLarge, +} + // Implement the `Visitor` trait to define how to go from SCALE // values into this type. pub struct SectorNumberVisitor(PhantomData); @@ -130,7 +149,7 @@ impl From for SectorNumber { } impl TryFrom for SectorNumber { - type Error = &'static str; + type Error = SectorNumberError; fn try_from(value: u32) -> Result { Self::new(value)