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

refactor to adapt manta-pay integration #35

Merged
merged 3 commits into from
Mar 9, 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
43 changes: 32 additions & 11 deletions manta-accounting/src/transfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1962,9 +1962,10 @@ where
where
I: Iterator<Item = (Self::AccountId, AssetValue)>;

/// 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<I>(
&self,
asset_id: AssetId,
sinks: I,
) -> Result<Vec<Self::ValidSinkAccount>, InvalidSinkAccount<Self::AccountId>>
where
Expand Down Expand Up @@ -1995,7 +1996,7 @@ where
sinks: Vec<SinkPostingKey<C, Self>>,
proof: Self::ValidProof,
super_key: &TransferLedgerSuperPostingKey<C, Self>,
);
) -> Result<(), LedgerInternalError>;
}

/// Transfer Source Posting Key Type
Expand Down Expand Up @@ -2036,8 +2037,8 @@ pub struct InvalidSourceAccount<AccountId> {
/// 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,
Expand All @@ -2056,6 +2057,12 @@ pub struct InvalidSourceAccount<AccountId> {
pub struct InvalidSinkAccount<AccountId> {
/// Account Id
pub account_id: AccountId,

/// Asset Id
pub asset_id: AssetId,

/// Amount Attempting to Deposit
pub deposit: AssetValue,
}

/// Transfer Post Error
Expand Down Expand Up @@ -2094,8 +2101,15 @@ pub enum TransferPostError<AccountId> {
///
/// 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<AccountId> From<InvalidSourceAccount<AccountId>> for TransferPostError<AccountId> {
#[inline]
fn from(err: InvalidSourceAccount<AccountId>) -> Self {
Expand Down Expand Up @@ -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()
};
Expand Down Expand Up @@ -2331,9 +2348,9 @@ where
where
L: TransferLedger<C>,
{
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))
}
}

Expand Down Expand Up @@ -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<C, L>, ledger: &mut L) -> L::Event {
pub fn post(
self,
super_key: &TransferLedgerSuperPostingKey<C, L>,
ledger: &mut L,
) -> Result<L::Event, LedgerInternalError> {
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 All @@ -2417,8 +2438,8 @@ where
self.sink_posting_keys,
proof,
super_key,
);
)?;
}
self.event
Ok(self.event)
}
}
22 changes: 14 additions & 8 deletions manta-pay/src/simulation/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -248,7 +248,7 @@ impl TransferLedger<Config> for Ledger {
} else {
Err(InvalidSourceAccount {
account_id,
balance: AccountBalance::Known(*balance),
asset_id,
withdraw,
})
}
Expand All @@ -257,14 +257,14 @@ impl TransferLedger<Config> 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,
}),
}
Expand All @@ -275,6 +275,7 @@ impl TransferLedger<Config> for Ledger {
#[inline]
fn check_sink_accounts<I>(
&self,
asset_id: AssetId,
sinks: I,
) -> Result<Vec<Self::ValidSinkAccount>, InvalidSinkAccount<Self::AccountId>>
where
Expand All @@ -285,7 +286,11 @@ impl TransferLedger<Config> 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()
Expand Down Expand Up @@ -325,7 +330,7 @@ impl TransferLedger<Config> for Ledger {
sinks: Vec<SinkPostingKey<Config, Self>>,
proof: Self::ValidProof,
super_key: &TransferLedgerSuperPostingKey<Config, Self>,
) {
) -> Result<(), LedgerInternalError> {
let _ = (proof, super_key);
for WrapPair(account_id, withdraw) in sources {
*self
Expand All @@ -343,6 +348,7 @@ impl TransferLedger<Config> for Ledger {
.entry(asset_id)
.or_default() += deposit;
}
Ok(())
}
}

Expand Down Expand Up @@ -420,7 +426,7 @@ impl ledger::Connection<Config> 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);
Expand Down