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

MASP fee payment #3393

Merged
merged 38 commits into from
Jul 5, 2024
Merged

MASP fee payment #3393

merged 38 commits into from
Jul 5, 2024

Conversation

grarco
Copy link
Contributor

@grarco grarco commented Jun 7, 2024

Describe your changes

Closes #2597.

Introduces fee payment via the MASP. If a transaction can pay fees via a transparent transfer from the wrapper signer's balance than the protocol proceeds like that. Otherwise, we look to see if the first transaction of the batch is a masp transfer and after its changes the transparent fee payment is possible (i.e. we check that the tx unshields enough funds to the fee payer).

Masp fee payment in the SDK is supported for the transactions for which it makes sense: shielded, unshielding and ibc transactions (when shielded action and only on the source chain, where the initial unshield happens).

To limit the DOS changes of an attacker, if masp fee payment is required than we set a custom gas limit (protocol param) to avoid attackers reducing the tps by requesting large gas limit: if fees are successfully paid we bring back the original gas limit set but the user.

Modification have been done to the way we construct masp transaction to account for multiple sources/targets which is required for fee shielded fee payment. Also, to pay fees, the SDK tries to collect the funds in this order:

  1. From changes
  2. From unused notes of the transaction's sources
  3. From the optional, user provided spend notes

There are a few of things that could be improved but are not part of this PR (since I thought it's already quite large). I'm going to open issues for these:

  • the disposable-gas-payer is still a Tx arg but we should instead pull it out of there and move it to the args of the specific transactions for which the SDK supports masp fee payment
  • The new Changes type used when constructing masp transactions could probably be a U128Sum instead of a I128Sum
  • Currently we only support a single additional spending key for gas payment in the CLI but we should allow for multiples. To do that we need to add a new ArgMultiOpt arg which we are currently lacking

Indicate on which release or other PRs this topic is based on

#3356 (diffs for review: https://github.com/anoma/namada/pull/3393/files/1e98403560a297f56f38ff67397a4bb9182068aa..e1b5857845bafa1be9f4ee4500b5544b56bf926f)

Checklist before merging to draft

  • I have added a changelog
  • Git history is in acceptable state

@grarco grarco mentioned this pull request Jun 7, 2024
Copy link

codecov bot commented Jun 7, 2024

Codecov Report

Attention: Patch coverage is 25.86393% with 1373 lines in your changes missing coverage. Please review.

Project coverage is 53.50%. Comparing base (879a326) to head (e1b5857).
Report is 4 commits behind head on main.

Files Patch % Lines
crates/sdk/src/masp.rs 0.00% 572 Missing ⚠️
crates/sdk/src/tx.rs 0.00% 293 Missing ⚠️
crates/sdk/src/signing.rs 0.00% 134 Missing ⚠️
crates/apps_lib/src/cli.rs 0.00% 110 Missing ⚠️
crates/namada/src/ledger/protocol/mod.rs 78.13% 68 Missing ⚠️
crates/light_sdk/src/transaction/transfer.rs 0.00% 32 Missing ⚠️
crates/node/src/bench_utils.rs 0.00% 31 Missing ⚠️
crates/token/src/lib.rs 0.00% 26 Missing ⚠️
crates/sdk/src/lib.rs 0.00% 24 Missing ⚠️
crates/state/src/wl_state.rs 58.82% 21 Missing ⚠️
... and 12 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3393      +/-   ##
==========================================
- Coverage   53.92%   53.50%   -0.42%     
==========================================
  Files         317      317              
  Lines      107575   108596    +1021     
==========================================
+ Hits        58011    58108      +97     
- Misses      49564    50488     +924     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@batconjurer batconjurer left a comment

Choose a reason for hiding this comment

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

The masp.rs file in the sdk crate is too damn large. Can we split out all tx building functionality (of which there is now a lot) into a separate file?

crates/namada/src/ledger/protocol/mod.rs Outdated Show resolved Hide resolved
crates/namada/src/ledger/protocol/mod.rs Outdated Show resolved Hide resolved
crates/namada/src/ledger/protocol/mod.rs Outdated Show resolved Hide resolved
@@ -1130,7 +1135,7 @@ pub mod testing {
let tx_data = match masp_tx_type {
MaspTxType::Shielded => {
tx.add_code_from_hash(code_hash, Some(TX_SHIELDED_TRANSFER_WASM.to_owned()));
let data = ShieldedTransfer { section_hash: shielded_section_hash };
let data = ShieldedTransfer { fee_unshield: transfers.0.first().map(|transfer| UnshieldingTransferData { target: transfer.target.to_owned(), token: transfer.token.to_owned(), amount: transfer.amount }), section_hash: shielded_section_hash };
Copy link
Member

Choose a reason for hiding this comment

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

how the hell did this get past cargo fmt?

.unwrap_or_default();
let total_fee = checked!(fee_amount.amount() * u64::from(args.gas_limit))?;

Ok(match total_fee.checked_sub(balance) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: I think it's clearer to check total_fee > balance and then calculate the diff afterwards

crates/tests/src/integration/masp.rs Show resolved Hide resolved
crates/tests/src/integration/masp.rs Outdated Show resolved Hide resolved
)
.expect("Change is guaranteed to be non-negative");
changes = changes
.map(|prev| prev + change_amt.clone())
Copy link
Member

Choose a reason for hiding this comment

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

curious that unchecked arithmetic is ok here

Copy link
Member

Choose a reason for hiding this comment

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

we don't have the strict clippy checks in the sdk :/ - ideally, we shouldn't be implementing any logic here

Copy link
Member

Choose a reason for hiding this comment

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

imho we should move the sdk masp mod into the shielded_token crate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's actually checked if I recall correctly

@grarco
Copy link
Contributor Author

grarco commented Jun 14, 2024

The masp.rs file in the sdk crate is too damn large. Can we split out all tx building functionality (of which there is now a lot) into a separate file?

I agree but can we do that in a separate PR that does only that?

crates/namada/src/ledger/protocol/mod.rs Outdated Show resolved Hide resolved
crates/namada/src/ledger/protocol/mod.rs Outdated Show resolved Hide resolved
crates/namada/src/ledger/protocol/mod.rs Outdated Show resolved Hide resolved
crates/namada/src/ledger/protocol/mod.rs Outdated Show resolved Hide resolved
crates/namada/src/ledger/protocol/mod.rs Show resolved Hide resolved
crates/namada/src/ledger/protocol/mod.rs Outdated Show resolved Hide resolved
crates/namada/src/ledger/protocol/mod.rs Outdated Show resolved Hide resolved
@grarco grarco requested review from cwgoes and tzemanovic June 21, 2024 16:08
Copy link
Contributor

@cwgoes cwgoes left a comment

Choose a reason for hiding this comment

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

Re-reviewed the core changes.

@brentstone
Copy link
Collaborator

What's the status of this PR? Is it rdy for review and just waiting for some?

@grarco grarco mentioned this pull request Jun 25, 2024
2 tasks
brentstone added a commit that referenced this pull request Jun 28, 2024
* grarco/masp-fee-payment:
  Removes fallback logic when failed fee payment
  Renames misleading gas limit variable
  Removes useless write-log commit in fee payment
  Fixes typo
  Fixes masp amounts conversion
  Fixes broken docs
  Reuses token transfer
  Fixes typo
  Panics in fee payment if balance read fails
  Changelog #3393
  Adds missing gas spending key arg to ibc tx
  Masp fee payment for shielded actions
  Fixes masp tx generation and integration tests
  Updates shielded wasm code to handle fee unshielding
  Removes unused denominate function
  Adds support for masp fee payment in sdk
  Refactors the write log api
  Different gas cost for storage deletes
  Removes write log precommit and leverages the batch log
  Adds integration tests for masp fee payment
  Refactors batch execution in case of masp fee payment
  Skips the execution of the first inner tx when masp fee payment
  Renames fee payment gas limit parameter
  Returns `BatchedTxResult` from masp fee payment
  `check_fees` drop the storage changes in case of failure
  `check_fees` checks masp fee payment
  Reworks masp fee payment to correctly handle errors. Misc refactors
  Passes the correct tx index to masp fee payment check
  Introduces masp fee payment
@yito88 yito88 mentioned this pull request Jul 2, 2024
2 tasks
brentstone added a commit that referenced this pull request Jul 4, 2024
* origin/grarco/masp-fee-payment:
  Removes fallback logic when failed fee payment
  Renames misleading gas limit variable
  Removes useless write-log commit in fee payment
  Fixes typo
  Fixes masp amounts conversion
  Fixes broken docs
  Reuses token transfer
  Fixes typo
  Panics in fee payment if balance read fails
  Changelog #3393
  Adds missing gas spending key arg to ibc tx
  Masp fee payment for shielded actions
  Fixes masp tx generation and integration tests
  Updates shielded wasm code to handle fee unshielding
  Removes unused denominate function
  Adds support for masp fee payment in sdk
  Refactors the write log api
  Different gas cost for storage deletes
  Removes write log precommit and leverages the batch log
  Adds integration tests for masp fee payment
  Refactors batch execution in case of masp fee payment
  Skips the execution of the first inner tx when masp fee payment
  Renames fee payment gas limit parameter
  Returns `BatchedTxResult` from masp fee payment
  `check_fees` drop the storage changes in case of failure
  `check_fees` checks masp fee payment
  Reworks masp fee payment to correctly handle errors. Misc refactors
  Passes the correct tx index to masp fee payment check
  Introduces masp fee payment
@brentstone brentstone merged commit bde8a9a into main Jul 5, 2024
10 of 19 checks passed
@brentstone brentstone deleted the grarco/masp-fee-payment branch July 5, 2024 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Embed masp transactions inside fee payment
5 participants