Skip to content

Commit

Permalink
refactor(market): Move errors into pallet error
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan46 committed Nov 25, 2024
1 parent 2fa74be commit 4d198c0
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 228 deletions.
194 changes: 21 additions & 173 deletions pallets/market/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,133 +1,33 @@
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, PalletError};
use primitives_commitment::piece::PaddedPieceSizeError as PrimitivesPieceSizeError;
use scale_info::TypeInfo;

#[derive(thiserror::Error)]
pub enum DealActivationError {
/// Deal was tried to be activated by a provider which does not own it
#[error("DealActivationError: Invalid Provider")]
InvalidProvider,
/// Deal should have been activated earlier, it's too late
#[error("DealActivationError: Start Block Elapsed")]
StartBlockElapsed,
/// Sector containing the deal will expire before the deal is supposed to end
#[error("DealActivationError: Sector Expires Before Deal")]
SectorExpiresBeforeDeal,
/// Deal needs to be [`DealState::Published`] if it's to be activated
#[error("DealActivationError: Invalid Deal State")]
InvalidDealState,
/// Tried to activate a deal which is not in the Pending Proposals
#[error("DealActivationError: Deal Not Pending")]
DealNotPending,
#[derive(thiserror::Error, Decode, Encode, PalletError, TypeInfo, RuntimeDebug, PartialEq)]
pub enum PaddedPieceSizeError {
#[error("minimum piece size is 128 bytes")]
SizeTooSmall,
#[error("padded piece size must be a power of 2")]
SizeNotPowerOfTwo,
#[error("padded_piece_size is not multiple of NODE_SIZE")]
NotAMultipleOfNodeSize,
#[error("piece size is invalid")]
InvalidPieceCid,
#[error("unable to create CommD from CID")]
UnableToCreateCommD,
}

impl core::fmt::Debug for DealActivationError {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
DealActivationError::InvalidProvider => {
write!(f, "DealActivationError: Invalid Provider")
}
DealActivationError::StartBlockElapsed => {
write!(f, "DealActivationError: Start Block Elapsed")
}
DealActivationError::SectorExpiresBeforeDeal => {
write!(f, "DealActivationError: Sector Expires Before Deal")
}
DealActivationError::InvalidDealState => {
write!(f, "DealActivationError: Invalid Deal State")
}
DealActivationError::DealNotPending => {
write!(f, "DealActivationError: Deal Not Pending")
}
}
}
}

/// Errors related to [`DealProposal`] and [`ClientDealProposal`]
/// This is error does not surface externally, only in the logs.
/// Mostly used for Deal Validation [`Self::<T>::validate_deals`].
#[derive(thiserror::Error)]
pub enum ProposalError {
/// ClientDealProposal.client_signature did not match client's public key and data.
#[error("ProposalError::WrongClientSignatureOnProposal")]
WrongClientSignatureOnProposal,
/// Provider of one of the deals is different than the Provider of the first deal.
#[error("ProposalError::DifferentProvider")]
DifferentProvider,
/// Deal's block_start > block_end, so it doesn't make sense.
#[error("ProposalError::DealEndBeforeStart")]
DealEndBeforeStart,
/// Deal's start block is in the past, it should be in the future.
#[error("ProposalError::DealStartExpired")]
DealStartExpired,
/// Deal has to be [`DealState::Published`] when being Published
#[error("ProposalError::DealNotPublished")]
DealNotPublished,
/// Deal's duration must be within `Config::MinDealDuration` < `Config:MaxDealDuration`.
#[error("ProposalError::DealDurationOutOfBounds")]
DealDurationOutOfBounds,
/// Deal's piece_cid is invalid.
#[error("ProposalError::InvalidPieceCid")]
InvalidPieceCid(cid::Error),
/// Deal's piece_size is invalid.
#[error("ProposalError::InvalidPieceSize: {0}")]
InvalidPieceSize(&'static str),
/// CommD related error
#[error("ProposalError::CommD: {0}")]
CommD(&'static str),
}

impl core::fmt::Debug for ProposalError {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
ProposalError::WrongClientSignatureOnProposal => {
write!(f, "ProposalError::WrongClientSignatureOnProposal")
}
ProposalError::DifferentProvider => {
write!(f, "ProposalError::DifferentProvider")
}
ProposalError::DealEndBeforeStart => {
write!(f, "ProposalError::DealEndBeforeStart")
}
ProposalError::DealStartExpired => {
write!(f, "ProposalError::DealStartExpired")
}
ProposalError::DealNotPublished => {
write!(f, "ProposalError::DealNotPublished")
}
ProposalError::DealDurationOutOfBounds => {
write!(f, "ProposalError::DealDurationOutOfBounds")
}
ProposalError::InvalidPieceCid(_err) => {
write!(f, "ProposalError::InvalidPieceCid")
}
ProposalError::InvalidPieceSize(err) => {
write!(f, "ProposalError::InvalidPieceSize: {}", err)
}
ProposalError::CommD(err) => {
write!(f, "ProposalError::CommD: {}", err)
impl From<PrimitivesPieceSizeError> for PaddedPieceSizeError {
fn from(value: PrimitivesPieceSizeError) -> Self {
match value {
PrimitivesPieceSizeError::SizeTooSmall => PaddedPieceSizeError::SizeTooSmall,
PrimitivesPieceSizeError::SizeNotPowerOfTwo => PaddedPieceSizeError::SizeNotPowerOfTwo,
PrimitivesPieceSizeError::NotAMultipleOfNodeSize => {
PaddedPieceSizeError::NotAMultipleOfNodeSize
}
}
}
}

impl From<ProposalError> for DispatchError {
fn from(value: ProposalError) -> Self {
DispatchError::Other(match value {
ProposalError::WrongClientSignatureOnProposal => {
"ProposalError::WrongClientSignatureOnProposal"
}
ProposalError::DifferentProvider => "ProposalError::DifferentProvider",
ProposalError::DealEndBeforeStart => "ProposalError::DealEndBeforeStart",
ProposalError::DealStartExpired => "ProposalError::DealStartExpired",
ProposalError::DealNotPublished => "ProposalError::DealNotPublished",
ProposalError::DealDurationOutOfBounds => "ProposalError::DealDurationOutOfBounds",
ProposalError::InvalidPieceCid(_err) => "ProposalError::InvalidPieceCid",
ProposalError::InvalidPieceSize(_err) => "ProposalError::InvalidPieceSize",
ProposalError::CommD(_err) => "ProposalError::CommD",
})
}
}

// Clone and PartialEq required because of the BoundedVec<(DealId, DealSettlementError)>
#[derive(TypeInfo, Encode, Decode, Clone, PartialEq, thiserror::Error)]
pub enum DealSettlementError {
Expand Down Expand Up @@ -175,55 +75,3 @@ impl core::fmt::Debug for DealSettlementError {
}
}
}

impl From<DealSettlementError> for DispatchError {
fn from(value: DealSettlementError) -> Self {
DispatchError::Other(match value {
DealSettlementError::SlashedDeal => "DealSettlementError: Slashed Deal",
DealSettlementError::FutureLastUpdate => "DealSettlementError: Future Last Update",
DealSettlementError::DealNotFound => "DealSettlementError: Deal Not Found",
DealSettlementError::EarlySettlement => "DealSettlementError: Early Settlement",
DealSettlementError::ExpiredDeal => "DealSettlementError: Expired Deal",
DealSettlementError::DealNotActive => "DealSettlementError: Deal Not Active",
})
}
}

#[derive(thiserror::Error)]
pub enum SectorTerminateError {
/// Deal was not found in the [`Proposals`] table.
#[error("SectorTerminateError: Deal Not Found")]
DealNotFound,
/// Caller is not the provider.
#[error("SectorTerminateError: Invalid Caller")]
InvalidCaller,
/// Deal is not active
#[error("SectorTerminateError: Deal Is Not Active")]
DealIsNotActive,
}

impl core::fmt::Debug for SectorTerminateError {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
SectorTerminateError::DealNotFound => {
write!(f, "SectorTerminateError: Deal Not Found")
}
SectorTerminateError::InvalidCaller => {
write!(f, "SectorTerminateError: Invalid Caller")
}
SectorTerminateError::DealIsNotActive => {
write!(f, "SectorTerminateError: Deal Is Not Active")
}
}
}
}

impl From<SectorTerminateError> for DispatchError {
fn from(value: SectorTerminateError) -> Self {
DispatchError::Other(match value {
SectorTerminateError::DealNotFound => "SectorTerminateError: Deal Not Found",
SectorTerminateError::InvalidCaller => "SectorTerminateError: Invalid Caller",
SectorTerminateError::DealIsNotActive => "SectorTerminateError: Deal Is Not Active",
})
}
}
Loading

0 comments on commit 4d198c0

Please sign in to comment.