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: use mutable refs in noir-contracts #1099

Merged
merged 1 commit into from
Jul 31, 2023

Conversation

iAmMichaelConnor
Copy link
Contributor

@iAmMichaelConnor iAmMichaelConnor commented Jul 18, 2023

Description

Refactoring:

  • Pass context everywhere by mutable reference
  • Push to arrays by mutable reference
  • And a small change that I initially made: moving call from PrivateCallStackItem and PublicCallStackItem to context.

Checklist:

  • I have reviewed my diff in github, line by line.
  • Every change is related to the PR description.
  • I have linked this pull request to the issue(s) that it resolves.
  • There are no unexpected formatting changes, superfluous debug logs, or commented-out code.
  • The branch has been merged or rebased against the head of its merge target.
  • I'm happy for the PR to be merged at the reviewer's next convenience.

@iAmMichaelConnor iAmMichaelConnor force-pushed the mc/mutable-refs-refactor branch from 82999ea to f6c114a Compare July 18, 2023 11:12
@iAmMichaelConnor iAmMichaelConnor linked an issue Jul 18, 2023 that may be closed by this pull request
@iAmMichaelConnor iAmMichaelConnor marked this pull request as ready for review July 18, 2023 11:22
@iAmMichaelConnor iAmMichaelConnor force-pushed the mc/mutable-refs-refactor branch from f6c114a to 55e2ebe Compare July 18, 2023 12:04
@iAmMichaelConnor
Copy link
Contributor Author

Blocked by noir-lang/noir#1961, so converting this PR back to 'draft'.

@iAmMichaelConnor iAmMichaelConnor marked this pull request as draft July 18, 2023 16:20
@iAmMichaelConnor iAmMichaelConnor force-pushed the mc/mutable-refs-refactor branch 2 times, most recently from 843bd9a to c8de12d Compare July 20, 2023 21:19
@iAmMichaelConnor
Copy link
Contributor Author

iAmMichaelConnor commented Jul 29, 2023

Soon to be unblocked by noir-lang/noir#2087

@iAmMichaelConnor iAmMichaelConnor force-pushed the mc/mutable-refs-refactor branch from 7af2051 to ec762d0 Compare July 29, 2023 17:29
@iAmMichaelConnor iAmMichaelConnor changed the title Mc/mutable refs refactor refactor: use mutable refs in noir-contracts Jul 29, 2023
@iAmMichaelConnor
Copy link
Contributor Author

iAmMichaelConnor commented Jul 29, 2023

Only compiles with Jake's PR's branch. Need to wait until that's merged before merging this.

Edit: it's merged!

@iAmMichaelConnor iAmMichaelConnor added the T-refactor Type: this code needs refactoring label Jul 31, 2023
@iAmMichaelConnor iAmMichaelConnor force-pushed the mc/mutable-refs-refactor branch from 06ac22e to 5a2c2c3 Compare July 31, 2023 14:51
@iAmMichaelConnor iAmMichaelConnor marked this pull request as ready for review July 31, 2023 14:52
@iAmMichaelConnor
Copy link
Contributor Author

Not sure why these tests are failing. They're working locally...

Copy link
Collaborator

@spalladino spalladino left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this, it was a major change! I really like the result, and agree with moving the call_function methods into context.

One thing I hadn't noticed before is how some methods silently add the header to a note, which is now made explicit since they require a mutable reference to it. Does it make sense to use different types to represent notes with and without headers, to make it more clear which methods expect the header to be set, and which ones set it? Not for this PR of course.

@@ -36,9 +36,9 @@ contract EasyZkToken {
let storage = Storage::init();
let balances = storage.balances;

context = balances.at(owner).add(context, initial_supply, owner);
balances.at(owner).add(&mut context, initial_supply, owner);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yay, new syntax! Just to check: the compiler will complain if we don't include this &mut keyword, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct! It results in something slightly more verbose and cumbersome than I'd have liked, because many end users won't be familiar with references, but now they have to be. But I guess Noir emulates Rust syntax, so this is unavoidable.
I think it's to prevent foot guns. The developer has to consciously say "I am aware that the context I am passing to this function could get mutated"

@@ -32,16 +29,16 @@ contract ZkToken {
inputs: PrivateContextInputs,
//*********************************/
initial_supply: Field,
owner: Field,
owner: Field
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curious if you removed this manually or you have a noir formatter set up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This must have been a manual change.

let note = get_note_internal(storage_slot, note_interface);
let mut unique_note_hash = 0;
let is_dummy = note_interface.is_dummy;
if is_dummy(note) == false {
check_note_header(context, storage_slot, note_interface, note);
check_note_header(*context, storage_slot, note_interface, note);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hold on, why *? Do we need to manually dereference the mutable reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, since check_note_header doesn't mutate the context, we want to pass in a Context type rather than an &mut Context type

@ludamad
Copy link
Collaborator

ludamad commented Jul 31, 2023

I like overall how we decided to expose a pretty low level interface and now we're cleaning it up as we go (versus making everything magic from the start, I suppose) LGTM at a glance, really like the change

@iAmMichaelConnor
Copy link
Contributor Author

One thing I hadn't noticed before is how some methods silently add the header to a note, which is now made explicit since they require a mutable reference to it. Does it make sense to use different types to represent notes with and without headers, to make it more clear which methods expect the header to be set, and which ones set it? Not for this PR of course.

I'll tag @LeilaWang on this comment, because Leila did a lot of the lovely work introducing headers.

@iAmMichaelConnor iAmMichaelConnor merged commit 0bd1772 into master Jul 31, 2023
@iAmMichaelConnor iAmMichaelConnor deleted the mc/mutable-refs-refactor branch July 31, 2023 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-refactor Type: this code needs refactoring
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Tidy Noir Contract examples to make use of mutable references
3 participants