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(transaction): Validations for transaction requests #447

Merged
merged 26 commits into from
Aug 6, 2024

Conversation

SantiagoPittella
Copy link
Collaborator

Closes #444

crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
@SantiagoPittella SantiagoPittella force-pushed the validations-for-transaction-requests branch from 78e175a to 44cffa1 Compare July 30, 2024 18:43
@SantiagoPittella SantiagoPittella marked this pull request as ready for review July 30, 2024 19:44
@SantiagoPittella SantiagoPittella linked an issue Jul 30, 2024 that may be closed by this pull request
tests/integration/common.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
tests/integration/common.rs Show resolved Hide resolved
@igamigo
Copy link
Collaborator

igamigo commented Jul 30, 2024

BTW maybe we should also add a validation for duplicate expected output notes/own notes as well.

tests/integration/common.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@igamigo igamigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I left a couple non-blocking comments, except for one related to making sure we fail if the asset vault does not contain an asset that the notes require.

crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
@SantiagoPittella SantiagoPittella force-pushed the validations-for-transaction-requests branch from 9250b6a to d1e9047 Compare August 1, 2024 21:52
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Overall, this looks good - but I just realized that there is one thing we are missing: assets may be deposited into the account by input notes and so just because the account doesn't have all outgoing assets, doesn't mean the transaction will fail.

So, I think we could do the following:

  • Sum up all the incoming assets.
  • Sum up all the outgoing assets.
  • Make sure account_assets + incoming_assets >= outgoing_assets.

Also, I think making similar checks for faucets is a bit more involved - so, I'd remove them from this PR and create a separate issue for them.

Comment on lines 380 to 384
fn calculate_outgoing_assets(
&self,
transaction_request: &TransactionRequest,
) -> (BTreeMap<AccountId, u64>, BTreeSet<NonFungibleAsset>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: calculate_ prefix sounds a bit odd here. I'd probably use build_ or get_.

Also, I don't think we are using anything from self here. So, this can probably be a pure function and we could move it to the "Helper functions" section at the end of the file.

Another thought is that this could be a method on TransactionRequest.

let (fungible_balance_map, non_fungible_set) =
self.calculate_outgoing_assets(transaction_request);

// Get client assets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should this say "Get account assets"?

Comment on lines 480 to 487
// Faucet account can only mint one asset (itself)
if own_notes_assets.len() > 1 {
return Err(ClientError::TransactionRequestError(
TransactionRequestError::InvalidFaucetMint(
"Faucet can only mint one asset".to_string(),
),
));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only true for fungible assets. We don't currently have non-fungible faucet fully working, so this may be OK for now - but also maybe we should make the check more sophisticated here.

Comment on lines 471 to 478
let own_notes_assets = match transaction_request.script_template() {
Some(TransactionScriptTemplate::SendNotes(notes)) => notes
.iter()
.map(|note| note.assets())
.flat_map(|assets| assets.iter())
.collect::<Vec<_>>(),
_ => Default::default(),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is sufficient to compute own_notes_assets. In theory, it should be possible for a note script to call the distribute procedure as well.

Comment on lines 312 to 321
#[maybe_async]
pub fn get_incoming_assets(
&self,
client: &Client<
impl NodeRpcClient,
impl FeltRng,
impl Store,
impl TransactionAuthenticator,
>,
) -> Result<(BTreeMap<AccountId, u64>, BTreeSet<NonFungibleAsset>), TransactionRequestError>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh - I forgot that to get incoming note assets we need to query the store. In this case, probably better to have this in the transactions module (rather than transaction request). And if we do this, we should probably move get_outgoing_assets back there for consistency.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I've moved it.

@SantiagoPittella SantiagoPittella force-pushed the validations-for-transaction-requests branch 2 times, most recently from 4f1f228 to 03e1671 Compare August 5, 2024 14:38
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you! I left a few small comments inline. Once these are addressed, we can merge.

crates/rust-client/src/transactions/request.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
crates/rust-client/src/transactions/mod.rs Outdated Show resolved Hide resolved
@SantiagoPittella SantiagoPittella force-pushed the validations-for-transaction-requests branch from b42764e to f63c612 Compare August 6, 2024 14:05
Copy link
Collaborator

@igamigo igamigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Left a very minor non-blocking comment, feel free to disregard

// Check if the account balance plus incoming assets is greater than or equal to the outgoing
// fungible assets
for (faucet_id, amount) in fungible_balance_map {
match account.vault().get_balance(faucet_id) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe you can do get_balance(faucet_id).unwrap_or(0) (as you did below) here and you'd be able to get rid of the second match branch

) -> Result<(), ClientError> {
let (account, _) = maybe_await!(self.get_account(transaction_request.account_id()))?;
if account.is_faucet() {
// TODO(SantiagoPittella): Add faucet validations.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably were tracking this already but just in case: let's open an issue for this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just created #468

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to add whatever you want to the issue.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you!

One thing: let's create an issue to figure out if we want to make it so that own output notes are always a strict subset of expected output notes.

@igamigo
Copy link
Collaborator

igamigo commented Aug 6, 2024

Looks good! Thank you!

One thing: let's create an issue to figure out if we want to make it so that own output notes are always a strict subset of expected output notes.

Created #470 for this

@SantiagoPittella SantiagoPittella merged commit 3bdafca into next Aug 6, 2024
13 checks passed
@SantiagoPittella SantiagoPittella deleted the validations-for-transaction-requests branch August 6, 2024 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: add basic validations for transaction requests
4 participants