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

Simplify errors #36

Merged
merged 11 commits into from
Mar 13, 2022
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
58 changes: 26 additions & 32 deletions manta-accounting/src/transfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,13 @@ where
/// Ledger Event
type Event;

/// State Update Error
stechu marked this conversation as resolved.
Show resolved Hide resolved
///
/// This error type is used if the ledger can fail when updating the public state. The
/// [`update_public_balances`](Self::update_public_balances) method uses this error type to
/// track this condition.
type UpdateError;

/// Valid [`AssetValue`] for [`TransferPost`] Source
///
/// # Safety
Expand Down Expand Up @@ -1996,7 +2003,7 @@ where
sinks: Vec<SinkPostingKey<C, Self>>,
proof: Self::ValidProof,
super_key: &TransferLedgerSuperPostingKey<C, Self>,
) -> Result<(), LedgerInternalError>;
) -> Result<(), Self::UpdateError>;
}

/// Transfer Source Posting Key Type
Expand All @@ -2008,21 +2015,6 @@ pub type SinkPostingKey<C, L> = <L as TransferLedger<C>>::ValidSinkAccount;
/// Transfer Ledger Super Posting Key Type
pub type TransferLedgerSuperPostingKey<C, L> = <L as TransferLedger<C>>::SuperPostingKey;

/// Account Balance
#[cfg_attr(
feature = "serde",
derive(Deserialize, Serialize),
serde(crate = "manta_util::serde", deny_unknown_fields)
)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum AccountBalance {
/// Known Balance
Known(AssetValue),

/// Unknown Account
UnknownAccount,
}

/// Invalid Source Accounts
///
/// This `struct` is the error state of the [`TransferLedger::check_source_accounts`] method. See
Expand Down Expand Up @@ -2075,7 +2067,7 @@ pub struct InvalidSinkAccount<AccountId> {
serde(crate = "manta_util::serde", deny_unknown_fields)
)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum TransferPostError<AccountId> {
pub enum TransferPostError<AccountId, UpdateError> {
/// Invalid Transfer Post Shape
InvalidShape,

Expand All @@ -2102,36 +2094,38 @@ pub enum TransferPostError<AccountId> {
/// Validity of the transfer could not be proved by the ledger.
InvalidProof,

/// Ledger Internal Error
LedgerInternalError,
/// Update Error
///
/// Error happens when update ledger
UpdateError(UpdateError),
}

/// Ledger interal error
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct LedgerInternalError;

impl<AccountId> From<InvalidSourceAccount<AccountId>> for TransferPostError<AccountId> {
impl<AccountId, UpdateError> From<InvalidSourceAccount<AccountId>>
for TransferPostError<AccountId, UpdateError>
{
#[inline]
fn from(err: InvalidSourceAccount<AccountId>) -> Self {
Self::InvalidSourceAccount(err)
}
}

impl<AccountId> From<InvalidSinkAccount<AccountId>> for TransferPostError<AccountId> {
impl<AccountId, UpdateError> From<InvalidSinkAccount<AccountId>>
for TransferPostError<AccountId, UpdateError>
{
#[inline]
fn from(err: InvalidSinkAccount<AccountId>) -> Self {
Self::InvalidSinkAccount(err)
}
}

impl<AccountId> From<SenderPostError> for TransferPostError<AccountId> {
impl<AccountId, UpdateError> From<SenderPostError> for TransferPostError<AccountId, UpdateError> {
#[inline]
fn from(err: SenderPostError) -> Self {
Self::Sender(err)
}
}

impl<AccountId> From<ReceiverPostError> for TransferPostError<AccountId> {
impl<AccountId, UpdateError> From<ReceiverPostError> for TransferPostError<AccountId, UpdateError> {
#[inline]
fn from(err: ReceiverPostError) -> Self {
Self::Receiver(err)
Expand Down Expand Up @@ -2229,7 +2223,7 @@ where
ledger: &L,
) -> Result<
(Vec<L::ValidSourceAccount>, Vec<L::ValidSinkAccount>),
TransferPostError<L::AccountId>,
TransferPostError<L::AccountId, L::UpdateError>,
>
where
L: TransferLedger<C>,
Expand Down Expand Up @@ -2271,7 +2265,7 @@ where
source_accounts: Vec<L::AccountId>,
sink_accounts: Vec<L::AccountId>,
ledger: &L,
) -> Result<TransferPostingKey<C, L>, TransferPostError<L::AccountId>>
) -> Result<TransferPostingKey<C, L>, TransferPostError<L::AccountId, L::UpdateError>>
where
L: TransferLedger<C>,
{
Expand Down Expand Up @@ -2344,13 +2338,13 @@ where
sink_accounts: Vec<L::AccountId>,
super_key: &TransferLedgerSuperPostingKey<C, L>,
ledger: &mut L,
) -> Result<L::Event, TransferPostError<L::AccountId>>
) -> Result<L::Event, TransferPostError<L::AccountId, L::UpdateError>>
where
L: TransferLedger<C>,
{
self.validate(source_accounts, sink_accounts, ledger)?
.post(super_key, ledger)
.or(Err(TransferPostError::LedgerInternalError))
.map_err(TransferPostError::UpdateError)
}
}

Expand Down Expand Up @@ -2427,7 +2421,7 @@ where
self,
super_key: &TransferLedgerSuperPostingKey<C, L>,
ledger: &mut L,
) -> Result<L::Event, LedgerInternalError> {
) -> Result<L::Event, L::UpdateError> {
let proof = self.validity_proof;
SenderPostingKey::post_all(self.sender_posting_keys, &(proof, *super_key), ledger);
ReceiverPostingKey::post_all(self.receiver_posting_keys, &(proof, *super_key), ledger);
Expand Down
10 changes: 5 additions & 5 deletions manta-pay/src/simulation/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ use indexmap::IndexSet;
use manta_accounting::{
asset::{Asset, AssetId, AssetList, AssetValue},
transfer::{
canonical::TransferShape, InvalidSinkAccount, InvalidSourceAccount, LedgerInternalError,
Proof, ReceiverLedger, ReceiverPostingKey, SenderLedger, SenderPostingKey, SinkPostingKey,
SourcePostingKey, TransferLedger, TransferLedgerSuperPostingKey, TransferPostingKey,
UtxoAccumulatorOutput,
canonical::TransferShape, InvalidSinkAccount, InvalidSourceAccount, Proof, ReceiverLedger,
ReceiverPostingKey, SenderLedger, SenderPostingKey, SinkPostingKey, SourcePostingKey,
TransferLedger, TransferLedgerSuperPostingKey, TransferPostingKey, UtxoAccumulatorOutput,
},
wallet::{
ledger::{self, PullResponse, PullResult, PushResponse, PushResult},
Expand Down Expand Up @@ -231,6 +230,7 @@ impl TransferLedger<Config> for Ledger {
type ValidSinkAccount = WrapPair<Self::AccountId, AssetValue>;
type ValidProof = Wrap<()>;
type SuperPostingKey = ();
type UpdateError = Infallible;

#[inline]
fn check_source_accounts<I>(
Expand Down Expand Up @@ -333,7 +333,7 @@ impl TransferLedger<Config> for Ledger {
sinks: Vec<SinkPostingKey<Config, Self>>,
proof: Self::ValidProof,
super_key: &TransferLedgerSuperPostingKey<Config, Self>,
) -> Result<(), LedgerInternalError> {
) -> Result<(), Self::UpdateError> {
let _ = (proof, super_key);
for WrapPair(account_id, withdraw) in sources {
*self
Expand Down