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: distinguish between panic-errors and possible-fix-errors #294

Merged
merged 13 commits into from
Jan 11, 2023
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: rustup update ${{ matrix.channel }} && rustup default ${{ matrix.channel }} && rustup component add clippy
- run: cargo install cargo-hakari
- run: cargo install cargo-hakari --locked
- run: cargo hakari init workspace-hack --yes
- run: cargo hakari generate
- run: cargo hakari manage-deps --yes
- run: cargo hakari verify
- run: cargo install cargo-hack
- run: cargo install cargo-hack --locked
- run: cargo hack clippy --workspace --feature-powerset
- run: cargo hack clippy --workspace --feature-powerset --bins
- run: cargo hack clippy --workspace --feature-powerset --examples
Expand All @@ -79,7 +79,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: rustup update ${{ matrix.channel }} && rustup default ${{ matrix.channel }}
- run: cargo install cargo-nextest
- run: cargo install cargo-nextest --locked
- run: cargo nextest run --workspace --release --all-features
compile-bench:
name: Compile Benchmarks (${{ matrix.os }} + ${{ matrix.channel }})
Expand Down
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Fixed
- [\#296](https://github.com/Manta-Network/manta-rs/pull/296) Fix AssetMetadata display for values less than 1
- [\#294](https://github.com/Manta-Network/manta-rs/pull/294) Distinguish between panic-errors and possible-fix-errors

### Security

Expand All @@ -24,7 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Changed
- [\#283](https://github.com/Manta-Network/manta-rs/pull/283) Upgrade asset system.
- [\#284](https://github.com/Manta-Network/manta-rs/pull/284) Moved `R1CS` implementation to `manta-crypto`
- [\#284](https://github.com/Manta-Network/manta-rs/pull/284) Moved `R1CS` implementation to `manta-crypto`
- [\#282](https://github.com/Manta-Network/manta-rs/pull/282) Upgrade key system.

## [0.5.7] - 2022-11-04
Expand Down Expand Up @@ -53,7 +54,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [\#197](https://github.com/Manta-Network/manta-rs/pull/197) Add ECLAIR utilities for next circuit upgrade
- [\#196](https://github.com/Manta-Network/manta-rs/pull/172) Add fixed base scalar multiplication using precomputed bases
- [\#193](https://github.com/Manta-Network/manta-rs/pull/193) Add Bn254 curve backend for Groth16 trusted setup
- [\#172](https://github.com/Manta-Network/manta-rs/pull/172) Add abstract Phase 2 for Groth16 trusted setup
- [\#172](https://github.com/Manta-Network/manta-rs/pull/172) Add abstract Phase 2 for Groth16 trusted setup

### Changed
- [\#247](https://github.com/Manta-Network/manta-rs/pull/247) Moved BLS12-381 and BN254 curves (and Edwards counterparts) to `manta-crypto`
Expand Down
139 changes: 82 additions & 57 deletions manta-accounting/src/transfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,13 +1052,6 @@ where
/// Ledger Event
type Event;

/// State Update Error
///
/// 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`](Configuration::AssetValue) for [`TransferPost`] Source
///
/// # Safety
Expand Down Expand Up @@ -1086,6 +1079,19 @@ where
/// [`check_sink_accounts`](Self::check_sink_accounts) and [`is_valid`](Self::is_valid).
type ValidProof: Copy;

/// Error Type
type Error: From<<Self as ReceiverLedger<Parameters<C>>>::Error>
+ From<<Self as SenderLedger<Parameters<C>>>::Error>
+ Into<
TransferPostError<
C,
Self::AccountId,
<Self as SenderLedger<Parameters<C>>>::Error,
<Self as ReceiverLedger<Parameters<C>>>::Error,
<Self as TransferLedger<C>>::Error,
>,
>;

/// Checks that the balances associated to the source accounts are sufficient to withdraw the
/// amount given in `sources`.
fn check_source_accounts<I>(
Expand All @@ -1109,7 +1115,7 @@ where
fn is_valid(
&self,
posting_key: TransferPostingKeyRef<C, Self>,
) -> Option<(Self::ValidProof, Self::Event)>;
) -> Result<(Self::ValidProof, Self::Event), <Self as TransferLedger<C>>::Error>;

/// Updates the public balances in the ledger, finishing the transaction.
///
Expand All @@ -1125,7 +1131,7 @@ where
sources: Vec<SourcePostingKey<C, Self>>,
sinks: Vec<SinkPostingKey<C, Self>>,
proof: Self::ValidProof,
) -> Result<(), Self::UpdateError>;
) -> Result<(), <Self as TransferLedger<C>>::Error>;
}

/// Transfer Source Posting Key Type
Expand Down Expand Up @@ -1225,6 +1231,15 @@ where
pub deposit: C::AssetValue,
}

/// Transfer Ledger Post Error
pub type TransferLedgerPostError<C, L> = TransferPostError<
C,
<L as TransferLedger<C>>::AccountId,
<L as SenderLedger<Parameters<C>>>::Error,
<L as ReceiverLedger<Parameters<C>>>::Error,
<L as TransferLedger<C>>::Error,
>;

/// Transfer Post Error
///
/// This `enum` is the error state of the [`TransferPost::validate`] method. See its documentation
Expand All @@ -1236,12 +1251,16 @@ where
bound(
deserialize = r"
AccountId: Deserialize<'de>,
UpdateError: Deserialize<'de>,
SenderError: Deserialize<'de>,
ReceiverError: Deserialize<'de>,
Error: Deserialize<'de>,
C::AssetId: Deserialize<'de>,
C::AssetValue: Deserialize<'de>",
serialize = r"
AccountId: Serialize,
UpdateError: Serialize,
SenderError: Serialize,
ReceiverError: Serialize,
Error: Serialize,
C::AssetId: Serialize,
C::AssetValue: Serialize",
),
Expand All @@ -1251,16 +1270,26 @@ where
)]
#[derive(derivative::Derivative)]
#[derivative(
Clone(bound = "AccountId: Clone, UpdateError: Clone, C::AssetId: Clone, C::AssetValue: Clone"),
Copy(bound = "AccountId: Copy, UpdateError: Copy, C::AssetId: Copy, C::AssetValue: Copy"),
Debug(bound = "AccountId: Debug, UpdateError: Debug, C::AssetId: Debug, C::AssetValue: Debug"),
Eq(bound = "AccountId: Eq, UpdateError: Eq, C::AssetId: Eq, C::AssetValue: Eq"),
Hash(bound = "AccountId: Hash, UpdateError: Hash, C::AssetId: Hash, C::AssetValue: Hash"),
Clone(
bound = "AccountId: Clone, SenderError: Clone, ReceiverError: Clone, Error: Clone, C::AssetId: Clone, C::AssetValue: Clone"
),
Copy(
bound = "AccountId: Copy, SenderError: Copy, ReceiverError: Copy, Error: Copy, C::AssetId: Copy, C::AssetValue: Copy"
),
Debug(
bound = "AccountId: Debug, SenderError: Debug, ReceiverError: Debug, Error: Debug, C::AssetId: Debug, C::AssetValue: Debug"
),
Eq(
bound = "AccountId: Eq, SenderError: Eq, ReceiverError: Eq, Error: Eq, C::AssetId: Eq, C::AssetValue: Eq"
),
Hash(
bound = "AccountId: Hash, SenderError: Hash, ReceiverError: Hash, Error: Hash, C::AssetId: Hash, C::AssetValue: Hash"
),
PartialEq(
bound = "AccountId: PartialEq, UpdateError: PartialEq, C::AssetId: PartialEq, C::AssetValue: PartialEq"
bound = "AccountId: PartialEq, SenderError: PartialEq, ReceiverError: PartialEq, Error: PartialEq, C::AssetId: PartialEq, C::AssetValue: PartialEq"
)
)]
pub enum TransferPostError<C, AccountId, UpdateError>
pub enum TransferPostError<C, AccountId, SenderError, ReceiverError, Error>
where
C: Configuration + ?Sized,
{
Expand All @@ -1279,10 +1308,10 @@ where
InvalidSinkAccount(InvalidSinkAccount<C, AccountId>),

/// Sender Post Error
Sender(SenderPostError),
Sender(SenderPostError<SenderError>),

/// Receiver Post Error
Receiver(ReceiverPostError),
Receiver(ReceiverPostError<ReceiverError>),

/// Duplicate Spend Error
DuplicateSpend,
Expand All @@ -1295,14 +1324,14 @@ where
/// Validity of the transfer could not be proved by the ledger.
InvalidProof,

/// Update Error
/// Unexpected Error
///
/// An error occured while updating the ledger state.
UpdateError(UpdateError),
/// An unexpected error occured.
UnexpectedError(Error),
}

impl<C, AccountId, UpdateError> From<InvalidAuthorizationSignature>
for TransferPostError<C, AccountId, UpdateError>
impl<C, AccountId, SenderError, ReceiverError, Error> From<InvalidAuthorizationSignature>
for TransferPostError<C, AccountId, SenderError, ReceiverError, Error>
where
C: Configuration + ?Sized,
{
Expand All @@ -1312,8 +1341,8 @@ where
}
}

impl<C, AccountId, UpdateError> From<InvalidSourceAccount<C, AccountId>>
for TransferPostError<C, AccountId, UpdateError>
impl<C, AccountId, SenderError, ReceiverError, Error> From<InvalidSourceAccount<C, AccountId>>
for TransferPostError<C, AccountId, SenderError, ReceiverError, Error>
where
C: Configuration + ?Sized,
{
Expand All @@ -1323,8 +1352,8 @@ where
}
}

impl<C, AccountId, UpdateError> From<InvalidSinkAccount<C, AccountId>>
for TransferPostError<C, AccountId, UpdateError>
impl<C, AccountId, SenderError, ReceiverError, Error> From<InvalidSinkAccount<C, AccountId>>
for TransferPostError<C, AccountId, SenderError, ReceiverError, Error>
where
C: Configuration + ?Sized,
{
Expand All @@ -1334,24 +1363,24 @@ where
}
}

impl<C, AccountId, UpdateError> From<SenderPostError>
for TransferPostError<C, AccountId, UpdateError>
impl<C, AccountId, SenderError, ReceiverError, Error> From<SenderPostError<SenderError>>
for TransferPostError<C, AccountId, SenderError, ReceiverError, Error>
where
C: Configuration + ?Sized,
{
#[inline]
fn from(err: SenderPostError) -> Self {
fn from(err: SenderPostError<SenderError>) -> Self {
Self::Sender(err)
}
}

impl<C, AccountId, UpdateError> From<ReceiverPostError>
for TransferPostError<C, AccountId, UpdateError>
impl<C, AccountId, SenderError, ReceiverError, Error> From<ReceiverPostError<ReceiverError>>
for TransferPostError<C, AccountId, SenderError, ReceiverError, Error>
where
C: Configuration + ?Sized,
{
#[inline]
fn from(err: ReceiverPostError) -> Self {
fn from(err: ReceiverPostError<ReceiverError>) -> Self {
Self::Receiver(err)
}
}
Expand Down Expand Up @@ -1683,10 +1712,7 @@ where
sink_accounts: Vec<L::AccountId>,
sink_values: Vec<C::AssetValue>,
ledger: &L,
) -> Result<
(Vec<L::ValidSourceAccount>, Vec<L::ValidSinkAccount>),
TransferPostError<C, L::AccountId, L::UpdateError>,
>
) -> Result<(Vec<L::ValidSourceAccount>, Vec<L::ValidSinkAccount>), TransferLedgerPostError<C, L>>
where
L: TransferLedger<C>,
{
Expand Down Expand Up @@ -1729,7 +1755,7 @@ where
ledger: &L,
source_accounts: Vec<L::AccountId>,
sink_accounts: Vec<L::AccountId>,
) -> Result<TransferPostingKey<C, L>, TransferPostError<C, L::AccountId, L::UpdateError>>
) -> Result<TransferPostingKey<C, L>, TransferLedgerPostError<C, L>>
where
L: TransferLedger<C>,
{
Expand Down Expand Up @@ -1762,18 +1788,17 @@ where
.into_iter()
.map(move |r| r.validate(ledger))
.collect::<Result<Vec<_>, _>>()?;
let (proof, event) = match ledger.is_valid(TransferPostingKeyRef {
authorization_key: &self.authorization_signature.map(|s| s.authorization_key),
asset_id: &self.body.asset_id,
sources: &source_posting_keys,
senders: &sender_posting_keys,
receivers: &receiver_posting_keys,
sinks: &sink_posting_keys,
proof: self.body.proof,
}) {
Some((proof, event)) => (proof, event),
_ => return Err(TransferPostError::InvalidProof),
};
let (proof, event) = ledger
.is_valid(TransferPostingKeyRef {
authorization_key: &self.authorization_signature.map(|s| s.authorization_key),
asset_id: &self.body.asset_id,
sources: &source_posting_keys,
senders: &sender_posting_keys,
receivers: &receiver_posting_keys,
sinks: &sink_posting_keys,
proof: self.body.proof,
})
.map_err(|x| x.into())?;
Ok(TransferPostingKey {
asset_id: self.body.asset_id,
source_posting_keys,
Expand All @@ -1795,13 +1820,13 @@ where
super_key: &TransferLedgerSuperPostingKey<C, L>,
source_accounts: Vec<L::AccountId>,
sink_accounts: Vec<L::AccountId>,
) -> Result<L::Event, TransferPostError<C, L::AccountId, L::UpdateError>>
) -> Result<L::Event, TransferLedgerPostError<C, L>>
where
L: TransferLedger<C>,
{
self.validate(parameters, ledger, source_accounts, sink_accounts)?
.post(ledger, super_key)
.map_err(TransferPostError::UpdateError)
.map_err(TransferPostError::UnexpectedError)
}
}

Expand Down Expand Up @@ -1957,14 +1982,14 @@ where
self,
ledger: &mut L,
super_key: &TransferLedgerSuperPostingKey<C, L>,
) -> Result<L::Event, L::UpdateError> {
) -> Result<L::Event, <L as TransferLedger<C>>::Error> {
let proof = self.proof;
SenderPostingKey::<C, _>::post_all(self.sender_posting_keys, ledger, &(proof, *super_key));
SenderPostingKey::<C, _>::post_all(self.sender_posting_keys, ledger, &(proof, *super_key))?;
ReceiverPostingKey::<C, _>::post_all(
self.receiver_posting_keys,
ledger,
&(proof, *super_key),
);
)?;
if let Some(asset_id) = self.asset_id {
ledger.update_public_balances(
super_key,
Expand Down
Loading