From 82808e099a8e239d27425a4307d8b5ec361fae38 Mon Sep 17 00:00:00 2001 From: Shumo Chu Date: Wed, 9 Mar 2022 14:29:01 -0500 Subject: [PATCH] refactor to adapt manta-pay integration (#35) * refactor to adapt manta-pay integration Signed-off-by: Shumo Chu --- manta-accounting/src/transfer/mod.rs | 43 +++++++++++++++++++------- manta-pay/src/simulation/ledger/mod.rs | 22 ++++++++----- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/manta-accounting/src/transfer/mod.rs b/manta-accounting/src/transfer/mod.rs index a4e45b0ba..719be98c3 100644 --- a/manta-accounting/src/transfer/mod.rs +++ b/manta-accounting/src/transfer/mod.rs @@ -1962,9 +1962,10 @@ where where I: Iterator; - /// Checks that the sink accounts exist. + /// Checks that the sink accounts exist and balance can be increased by the specified amounts. fn check_sink_accounts( &self, + asset_id: AssetId, sinks: I, ) -> Result, InvalidSinkAccount> where @@ -1995,7 +1996,7 @@ where sinks: Vec>, proof: Self::ValidProof, super_key: &TransferLedgerSuperPostingKey, - ); + ) -> Result<(), LedgerInternalError>; } /// Transfer Source Posting Key Type @@ -2036,8 +2037,8 @@ pub struct InvalidSourceAccount { /// Account Id pub account_id: AccountId, - /// Current Balance if Known - pub balance: AccountBalance, + /// Asset Id + pub asset_id: AssetId, /// Amount Attempting to Withdraw pub withdraw: AssetValue, @@ -2056,6 +2057,12 @@ pub struct InvalidSourceAccount { pub struct InvalidSinkAccount { /// Account Id pub account_id: AccountId, + + /// Asset Id + pub asset_id: AssetId, + + /// Amount Attempting to Deposit + pub deposit: AssetValue, } /// Transfer Post Error @@ -2094,8 +2101,15 @@ pub enum TransferPostError { /// /// Validity of the transfer could not be proved by the ledger. InvalidProof, + + /// Ledger Internal Error + LedgerInternalError, } +/// Ledger interal error +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +pub struct LedgerInternalError; + impl From> for TransferPostError { #[inline] fn from(err: InvalidSourceAccount) -> Self { @@ -2240,7 +2254,10 @@ where Vec::new() }; let sinks = if sinks > 0 { - ledger.check_sink_accounts(sink_accounts.into_iter().zip(sink_values))? + ledger.check_sink_accounts( + asset_id.unwrap(), + sink_accounts.into_iter().zip(sink_values), + )? } else { Vec::new() }; @@ -2331,9 +2348,9 @@ where where L: TransferLedger, { - Ok(self - .validate(source_accounts, sink_accounts, ledger)? - .post(super_key, ledger)) + self.validate(source_accounts, sink_accounts, ledger)? + .post(super_key, ledger) + .or(Err(TransferPostError::LedgerInternalError)) } } @@ -2406,7 +2423,11 @@ where /// [`SenderLedger::spend`] and [`ReceiverLedger::register`] for more information on the /// contract for this method. #[inline] - pub fn post(self, super_key: &TransferLedgerSuperPostingKey, ledger: &mut L) -> L::Event { + pub fn post( + self, + super_key: &TransferLedgerSuperPostingKey, + ledger: &mut L, + ) -> Result { 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); @@ -2417,8 +2438,8 @@ where self.sink_posting_keys, proof, super_key, - ); + )?; } - self.event + Ok(self.event) } } diff --git a/manta-pay/src/simulation/ledger/mod.rs b/manta-pay/src/simulation/ledger/mod.rs index 39ca495ce..c232c4cd7 100644 --- a/manta-pay/src/simulation/ledger/mod.rs +++ b/manta-pay/src/simulation/ledger/mod.rs @@ -29,8 +29,8 @@ use indexmap::IndexSet; use manta_accounting::{ asset::{Asset, AssetId, AssetList, AssetValue}, transfer::{ - canonical::TransferShape, AccountBalance, InvalidSinkAccount, InvalidSourceAccount, Proof, - ReceiverLedger, ReceiverPostingKey, SenderLedger, SenderPostingKey, SinkPostingKey, + canonical::TransferShape, InvalidSinkAccount, InvalidSourceAccount, LedgerInternalError, + Proof, ReceiverLedger, ReceiverPostingKey, SenderLedger, SenderPostingKey, SinkPostingKey, SourcePostingKey, TransferLedger, TransferLedgerSuperPostingKey, TransferPostingKey, UtxoAccumulatorOutput, }, @@ -248,7 +248,7 @@ impl TransferLedger for Ledger { } else { Err(InvalidSourceAccount { account_id, - balance: AccountBalance::Known(*balance), + asset_id, withdraw, }) } @@ -257,14 +257,14 @@ impl TransferLedger for Ledger { // FIXME: What about zero values in `sources`? Err(InvalidSourceAccount { account_id, - balance: AccountBalance::Known(AssetValue(0)), + asset_id, withdraw, }) } }, _ => Err(InvalidSourceAccount { account_id, - balance: AccountBalance::UnknownAccount, + asset_id, withdraw, }), } @@ -275,6 +275,7 @@ impl TransferLedger for Ledger { #[inline] fn check_sink_accounts( &self, + asset_id: AssetId, sinks: I, ) -> Result, InvalidSinkAccount> where @@ -285,7 +286,11 @@ impl TransferLedger for Ledger { if self.accounts.contains_key(&account_id) { Ok(WrapPair(account_id, deposit)) } else { - Err(InvalidSinkAccount { account_id }) + Err(InvalidSinkAccount { + account_id, + asset_id, + deposit, + }) } }) .collect() @@ -325,7 +330,7 @@ impl TransferLedger for Ledger { sinks: Vec>, proof: Self::ValidProof, super_key: &TransferLedgerSuperPostingKey, - ) { + ) -> Result<(), LedgerInternalError> { let _ = (proof, super_key); for WrapPair(account_id, withdraw) in sources { *self @@ -343,6 +348,7 @@ impl TransferLedger for Ledger { .entry(asset_id) .or_default() += deposit; } + Ok(()) } } @@ -420,7 +426,7 @@ impl ledger::Connection for LedgerConnection { }; match post.validate(sources, sinks, &*ledger) { Ok(posting_key) => { - posting_key.post(&(), &mut *ledger); + posting_key.post(&(), &mut *ledger).unwrap(); } Err(err) => { println!("ERROR: {:?}", err);