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: nicer way to fetch slots in TokenWithRefunds #7797

Merged
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
3 changes: 1 addition & 2 deletions boxes/boxes/react/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
contract BoxReact {
use dep::aztec::prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader};
use dep::aztec::protocol_types::point::Point;
use dep::aztec::prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point};
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys;
use dep::value_note::value_note::{ValueNote, VALUE_NOTE_LEN};

Expand Down
3 changes: 1 addition & 2 deletions boxes/boxes/vanilla/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
contract Vanilla {
use dep::aztec::prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader};
use dep::aztec::protocol_types::point::Point;
use dep::aztec::prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point};
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys;
use dep::value_note::value_note::{ValueNote, VALUE_NOTE_LEN};

Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/prelude.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// docs:start:prelude
use dep::protocol_types::{
address::{AztecAddress, EthAddress}, abis::function_selector::FunctionSelector,
address::{AztecAddress, EthAddress}, abis::function_selector::FunctionSelector, point::Point,
traits::{Serialize, Deserialize}
};
use crate::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,7 @@ contract TokenWithRefunds {
// docs:end:balance_of_private

// REFUNDS SPECIFIC FUNCTIONALITY FOLLOWS
use dep::aztec::{
prelude::{FunctionSelector, NoteHeader},
protocol_types::{storage::map::derive_storage_slot_in_map, point::Point}
};
use dep::aztec::prelude::{FunctionSelector, NoteHeader, Point};
use crate::types::token_note::TokenNoteHidingPoint;

/// We need to use different randomness for the user and for the fee payer notes because if the randomness values
Expand Down Expand Up @@ -469,18 +466,13 @@ contract TokenWithRefunds {
// to the user in the `complete_refund(...)` function.
storage.balances.sub(user, U128::from_integer(funded_amount)).emit(encode_and_encrypt_note_with_keys(&mut context, user_ovpk, user_ivpk, user));

// 4. Now we "manually" compute the slots (by setting the slots we insert the notes to the balances map under
// the correct keys)
let fee_payer_balances_slot = derive_storage_slot_in_map(TokenWithRefunds::storage().balances.slot, fee_payer);
let user_balances_slot = derive_storage_slot_in_map(TokenWithRefunds::storage().balances.slot, user);

// 5. We create the partial notes for the fee payer and the user.
// 4. We create the partial notes for the fee payer and the user.
// --> Called "partial" because they don't have the amount set yet (that will be done in `complete_refund(...)`).
let fee_payer_partial_note = TokenNote {
header: NoteHeader {
contract_address: AztecAddress::zero(),
nonce: 0,
storage_slot: fee_payer_balances_slot,
storage_slot: storage.balances.map.at(fee_payer).storage_slot,
note_hash_counter: 0
},
amount: U128::zero(),
Expand All @@ -491,19 +483,19 @@ contract TokenWithRefunds {
header: NoteHeader {
contract_address: AztecAddress::zero(),
nonce: 0,
storage_slot: user_balances_slot,
storage_slot: storage.balances.map.at(user).storage_slot,
note_hash_counter: 0
},
amount: U128::zero(),
npk_m_hash: user_npk_m_hash,
randomness: user_randomness
};

// 6. Now we get the note hiding points.
// 5. Now we get the note hiding points.
let mut fee_payer_point = fee_payer_partial_note.to_note_hiding_point();
let mut user_point = user_partial_note.to_note_hiding_point();

// 7. Set the public teardown function to `complete_refund(...)`. Public teardown is the only time when a public
// 6. Set the public teardown function to `complete_refund(...)`. Public teardown is the only time when a public
// function has access to the final transaction fee, which is needed to compute the actual refund amount.
context.set_public_teardown_function(
context.this_address(),
Expand Down Expand Up @@ -535,6 +527,7 @@ contract TokenWithRefunds {
let tx_fee = U128::from_integer(context.transaction_fee());

// 1. We check that user funded the fee payer contract with at least the transaction fee.
// TODO(#7796): we should try to prevent reverts here
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created an issue and linked it here to not forget.

assert(funded_amount >= tx_fee, "funded amount not enough to cover tx fee");

// 2. We compute the refund amount as the difference between funded amount and tx fee.
Expand Down
Loading