Skip to content

Commit

Permalink
feat(noir): Introduce context union to simplify storage declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Sep 8, 2023
1 parent 0898335 commit b96ac97
Show file tree
Hide file tree
Showing 25 changed files with 259 additions and 321 deletions.
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/abis/ecdsa_account_contract.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ contract Child {

use dep::aztec::{
abi::CallContext,
context::{PrivateContext, PublicContext},
context::{PrivateContext, PublicContext, Context},
oracle::{
logs::emit_unencrypted_log,
compute_selector::compute_selector,
Expand All @@ -18,14 +18,10 @@ contract Child {
}

impl Storage {
fn init(
private_context: Option<&mut PrivateContext>,
public_context: Option<&mut PublicContext>,
) -> pub Self {
fn init(context: Context) -> pub Self {
Storage {
current_value: PublicState::new(
private_context,
public_context,
context,
1,
FieldSerialisationMethods,
),
Expand Down Expand Up @@ -69,7 +65,7 @@ contract Child {
// Sets `current_value` to `new_value`
#[aztec(public)]
fn pubSetValue(new_value: Field) -> Field {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
storage.current_value.write(new_value);
let _hash = emit_unencrypted_log(new_value);

Expand All @@ -79,7 +75,7 @@ contract Child {
// Increments `current_value` by `new_value`
#[aztec(public)]
fn pubIncValue(new_value: Field) -> Field {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
let _hash = emit_unencrypted_log(new_value);
Expand All @@ -90,7 +86,7 @@ contract Child {
// Increments `current_value` by `new_value`. Can only be called from this contract.
#[aztec(public)]
fn pubIncValueInternal(new_value: Field) -> Field {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
check_sender(inputs.call_context);
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
Expand All @@ -104,14 +100,14 @@ contract Child {
let pubSetValueSelector = compute_selector("pubSetValue(Field)");
let _ret = context.call_public_function(context.this_address(), pubSetValueSelector, [10]);

let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
storage.current_value.write(20);
let _hash = emit_unencrypted_log(20);
}

#[aztec(public)]
fn setValueTwiceWithNestedLast() {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
storage.current_value.write(20);
let _hash = emit_unencrypted_log(20);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod types;
contract DocsExample {
use dep::std::option::Option;
use dep::aztec::{
context::{PrivateContext, PublicContext},
context::{PrivateContext, PublicContext, Context},
state_vars::{
immutable_singleton::ImmutableSingleton, map::Map, public_state::PublicState, set::Set,
singleton::Singleton,
Expand Down Expand Up @@ -44,33 +44,28 @@ contract DocsExample {
// docs:start:state_vars-Set
// docs:start:state_vars-MapSingleton
impl Storage {
fn init(
private_context: Option<&mut PrivateContext>,
public_context: Option<&mut PublicContext>,
) -> pub Self {
fn init(context: Context) -> pub Self {
Storage {
// highlight-next-line:state_vars-PublicState
locked: PublicState::new(private_context, public_context, 1, BoolSerialisationMethods),
locked: PublicState::new(context, 1, BoolSerialisationMethods),
// highlight-next-line:state_vars-PublicStateCustomStruct
queen: PublicState::new(
private_context,
public_context,
context,
2,
QueenSerialisationMethods,
),
// highlight-next-line:state_vars-ImmutableSingleton
game_rules: ImmutableSingleton::new(private_context, 3, RulesNoteMethods),
game_rules: ImmutableSingleton::new(context, 3, RulesNoteMethods),
// highlight-next-line:state_vars-Singleton
legendary_card: Singleton::new(private_context, public_context, 4, CardNoteMethods),
legendary_card: Singleton::new(context, 4, CardNoteMethods),
// highlight-next-line:state_vars-Set
cards: Set::new(private_context, public_context, 5, CardNoteMethods),
cards: Set::new(context, 5, CardNoteMethods),
// highlight-next-line:state_vars-MapSingleton
profiles: Map::new(
private_context,
public_context,
context,
6,
|private_context, public_context, slot| {
Singleton::new(private_context, public_context, slot, ProfileNoteMethods)
|context, slot| {
Singleton::new(context, slot, ProfileNoteMethods)
},
),
}
Expand All @@ -93,7 +88,7 @@ contract DocsExample {
max_points: u8,
legendary_card_secret: Field,
) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));

let mut game_rules = RulesNote::new(min_points, max_points);
actions::init_game_rules(storage.game_rules, &mut game_rules);
Expand All @@ -106,15 +101,15 @@ contract DocsExample {
#[aztec(public)]
fn lock() {
// highlight-next-line:storage-init
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
storage.locked.write(true);
}
// docs:end:storage-init

// docs:start:functions-OpenFunction
#[aztec(public)]
fn unlock() {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
actions::unlock(storage.locked);
}
// docs:end:functions-OpenFunction
Expand All @@ -124,7 +119,7 @@ contract DocsExample {
account: Field,
points: u8,
) {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));

let new_queen = Queen { account, points };

Expand All @@ -136,7 +131,7 @@ contract DocsExample {
// docs:start:state_vars-PublicStateWriteBeforeCall
#[aztec(public)]
fn replace_queen_unsafe() {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));

let account = context.msg_sender();
let points = actions::get_total_points(storage.cards, account, 0);
Expand All @@ -155,7 +150,7 @@ contract DocsExample {
// docs:start:functions-SecretFunction
#[aztec(private)]
fn add_common_cards(secrets: [Field; 4]) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));

for i in 0..secrets.len() as u8 {
let mut card = CardNote::new(0, secrets[i], 0);
Expand All @@ -169,7 +164,7 @@ contract DocsExample {
new_points: u8,
new_secret: Field,
) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));

let owner = inputs.call_context.msg_sender;
let mut updated_card = CardNote::new(new_points, new_secret, owner);
Expand All @@ -181,7 +176,7 @@ contract DocsExample {

#[aztec(private)]
fn become_queen() {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));

let legendary_card = actions::get_legendary_card(storage.legendary_card);

Expand All @@ -205,7 +200,7 @@ contract DocsExample {
account: Field,
offset: u32,
) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));

let mut total_points = 0;
let options = create_account_card_getter_options(account, offset);
Expand All @@ -224,7 +219,7 @@ contract DocsExample {
// docs:start:state_vars-check_return_notes
#[aztec(private)]
fn discard_largest_card() {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));

let account = context.msg_sender();
let options = create_largest_account_card_getter_options(account);
Expand All @@ -238,7 +233,7 @@ contract DocsExample {

// docs:start:functions-UncontrainedFunction
unconstrained fn get_total_points(account: Field) -> u8 {
let storage = Storage::init(Option::none(), Option::none());
let storage = Storage::init(Context::none());
actions::get_total_points(storage.cards, account, 0)
}
// docs:end:functions-UncontrainedFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
contract EasyPrivateToken {
use dep::std::option::Option;
use dep::aztec::{
context::{PrivateContext, PublicContext},
context::{PrivateContext, PublicContext, Context},
note::{
note_header::NoteHeader,
utils as note_utils,
Expand All @@ -23,17 +23,13 @@ contract EasyPrivateToken {
}

impl Storage {
fn init(
private_context: Option<&mut PrivateContext>,
public_context: Option<&mut PublicContext>,
) -> pub Self {
fn init(context: Context) -> pub Self {
Storage {
balances: Map::new(
private_context,
public_context,
context,
1,
|private_context, public_context, slot| {
EasyPrivateUint::new(private_context, public_context, slot)
|context, slot| {
EasyPrivateUint::new(context, slot)
},
),
}
Expand All @@ -48,7 +44,7 @@ contract EasyPrivateToken {
initial_supply: u120,
owner: Field,
) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));
let balances = storage.balances;

balances.at(owner).add(initial_supply, owner);
Expand All @@ -60,7 +56,7 @@ contract EasyPrivateToken {
amount: u120,
owner: Field,
) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));
let balances = storage.balances;

balances.at(owner).add(amount, owner);
Expand All @@ -73,7 +69,7 @@ contract EasyPrivateToken {
sender: Field,
recipient: Field,
) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));
let balances = storage.balances;

balances.at(sender).sub(amount, sender);
Expand All @@ -84,7 +80,7 @@ contract EasyPrivateToken {
unconstrained fn getBalance(
owner: Field,
) -> Field {
let storage = Storage::init(Option::none(), Option::none());
let storage = Storage::init(Context::none());
let balances = storage.balances;

// Return the sum of all notes in the set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ contract EcdsaAccount {
use dep::aztec::{
abi::CallContext,
constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD,
context::{PrivateContext, PublicContext},
context::{PrivateContext, PublicContext, Context},
entrypoint::{EntrypointPayload, ENTRYPOINT_PAYLOAD_SIZE},
log::emit_encrypted_log,
note::{
Expand All @@ -28,9 +28,9 @@ contract EcdsaAccount {
}

impl Storage {
fn init(private_context: Option<&mut PrivateContext>, _: Option<&mut PublicContext>) -> pub Self {
fn init(context: Context) -> pub Self {
Storage {
public_key: ImmutableSingleton::new(private_context, 1, EcdsaPublicKeyNoteInterface),
public_key: ImmutableSingleton::new(context, 1, EcdsaPublicKeyNoteInterface),
}
}
}
Expand All @@ -42,7 +42,7 @@ contract EcdsaAccount {
signature: pub [u8;64],
) {
// Load public key from storage
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));
let public_key = storage.public_key.get_note();

// Verify payload signature using Ethereum's signing scheme
Expand All @@ -69,7 +69,7 @@ contract EcdsaAccount {
signing_pub_key_x: pub [u8;32],
signing_pub_key_y: pub [u8;32],
) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));

let this = context.this_address();
let mut pub_key_note = EcdsaPublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ contract Escrow {
use dep::std::option::Option;

use dep::aztec::{
context::{PrivateContext, PublicContext},
context::{PrivateContext, PublicContext, Context},
log::emit_encrypted_log,
note::{
note_getter_options::NoteGetterOptions,
Expand All @@ -30,12 +30,9 @@ contract Escrow {
}

impl Storage {
fn init(
private_context: Option<&mut PrivateContext>,
public_context: Option<&mut PublicContext>,
) -> pub Self {
fn init(context: Context) -> pub Self {
Storage {
owners: Set::new(private_context, public_context, 1, AddressNoteMethods),
owners: Set::new(context, 1, AddressNoteMethods),
}
}
}
Expand All @@ -47,7 +44,7 @@ contract Escrow {
) {
let this = context.this_address();

let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));
let mut note = AddressNote::new(owner, this);
storage.owners.insert(&mut note);
emit_encrypted_log(
Expand All @@ -68,7 +65,7 @@ contract Escrow {
) {
let this = context.this_address();
let sender = context.msg_sender();
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));

// We don't remove note from the owners set. If a note exists, the owner and recipient are legit.
let options = NoteGetterOptions::new().select(0, sender).select(1, this).set_limit(1);
Expand Down
Loading

0 comments on commit b96ac97

Please sign in to comment.