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

Callbacks via composability #428

Merged
merged 23 commits into from
Sep 29, 2022
Merged

Callbacks via composability #428

merged 23 commits into from
Sep 29, 2022

Conversation

mitschabaude
Copy link
Member

@mitschabaude mitschabaude commented Sep 20, 2022

  • Closes Dex: Internals continued #402
  • Enables to pass AccountUpdates to methods (by making the class implement Provable<AccountUpdate>)
  • Add an experimental authorize method, which can (should) be used on AccountUpdates that are passed to methods:
@method authorizingMethod(accountUpdate: AccountUpdate) {
  this.experimental.authorize(accountUpdate);
}

In this case, authorize simply takes the AccountUpdate and makes it a child of the zkApp ("adopts it").

There's also an advanced second parameter to witness a certain layout of child updates:

this.experimental.authorize(accountUpdate, [0, 1, [null]]);

The weird layout syntax is documented here:
https://github.com/o1-labs/snarkyjs/blob/2bf421ba36780c4dc644552e02a6d2dcfe51036c/src/lib/account_update.ts#L1211-L1230

authorize can be used to achieve what the callback was all about: instead of taking a "zkApp callback" and extracting the account update in the authorizing method, we let it be up to the method's caller to get the account update, and pass it in directly.
The caller could, for example, do this

  • call a third zkApp
  • take the account update off of thirdZkapp.self
  • pass that account update to a method like authorizingMethod above, which uses authorize (and then does checks on the witnessed account update tree, like summing up the balances)

If this is done, the functionality is equivalent to a callback, with the difference that the caller can also see the result of the callback! (which, it turns out, is what we often want)

Let's call this flow "manual callback".

The "manual callback" flow is safer than the old callback, because our zkApps composability machinery ensures that we properly prove in the caller's circuit that thirdZkapp was really called, with certain arguments and outputs.

This PR also:

  • Refactors the DEX to use "manual callback" instead of callbacks
  • Refactor callbacks to use the "manual callback" flow under the hood, to be secure
  • Adds helpers for pretty-printing a transaction or account update, with .toPretty()
  • Unrelatedly, makes some tweaks to the ./run script
  • Unrelatedly, tweaks how Circuit.witness works

@mitschabaude mitschabaude marked this pull request as ready for review September 28, 2022 20:59
Copy link
Contributor

@MartinMinkov MartinMinkov left a comment

Choose a reason for hiding this comment

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

Pre approving! Looks great, awesome work 🚀

src/examples/zkapps/dex/dex.ts Outdated Show resolved Hide resolved
src/build/buildExample.js Show resolved Hide resolved
src/examples/zkapps/dex/dex.ts Outdated Show resolved Hide resolved
src/lib/zkapp.ts Outdated Show resolved Hide resolved
@mitschabaude mitschabaude merged commit 3196fef into main Sep 29, 2022
@mitschabaude mitschabaude deleted the feature/prove-callback branch September 29, 2022 11:56
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.

Dex: Internals continued
2 participants