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

feat(noir): Introduce context union to simplify storage declarations #2143

Merged
merged 1 commit into from
Sep 9, 2023
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
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
@@ -1,5 +1,5 @@
use dep::aztec::{
context::{PrivateContext, PublicContext},
context::{PrivateContext, PublicContext, Context},
constants_gen::{MAX_NOTES_PER_PAGE, MAX_READ_REQUESTS_PER_CALL},
log::emit_encrypted_log,
note::{
Expand Down Expand Up @@ -119,13 +119,11 @@ fn filter_cards<N>(notes: [Option<ValueNote>; MAX_READ_REQUESTS_PER_CALL], desir

impl Deck {
fn new(
private_context: Option<&mut PrivateContext>,
public_context: Option<&mut PublicContext>,
context: Context,
storage_slot: Field,
) -> Self {
let set = Set {
private_context,
public_context,
context,
storage_slot,
note_interface: ValueNoteMethods,
};
Expand All @@ -136,7 +134,7 @@ impl Deck {

fn add_cards<N>(&mut self, cards: [Card; N], owner: Field) -> [CardNote]{
let owner_key = get_public_key(owner);
let context = self.set.private_context.unwrap();
let context = self.set.context.private.unwrap();

let mut inserted_cards = [];
for card in cards {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod cards;
mod game;

use dep::aztec::{
context::{PrivateContext, PublicContext},
context::{PrivateContext, PublicContext, Context},
state_vars::{
map::Map,
public_state::PublicState,
Expand All @@ -22,49 +22,41 @@ struct Storage {

impl Storage {
fn init(
private_context: Option<&mut PrivateContext>,
public_context: Option<&mut PublicContext>,
context: Context,
) -> Self {
Storage {
collections: Map::new(
private_context,
public_context,
context,
1,
|private_context, public_context, slot| {
|context, slot| {
Deck::new(
private_context,
public_context,
context,
slot,
)
},
),
game_decks: Map::new(
private_context,
public_context,
context,
2,
|private_context, public_context, slot| {
|context, slot| {
Map::new(
private_context,
public_context,
context,
slot,
|private_context, public_context, slot|{
|context, slot|{
Deck::new(
private_context,
public_context,
context,
slot,
)
}
)
},
),
games: Map::new(
private_context,
public_context,
context,
3,
|private_context, public_context, slot| {
|context, slot| {
PublicState::new(
private_context,
public_context,
context,
slot,
GameSerialisationMethods,
)
Expand All @@ -90,7 +82,7 @@ contract CardGame {
abi::{
Hasher, PrivateContextInputs,
},
context::PrivateContext,
context::{PrivateContext, Context},
note::{
note_header::NoteHeader,
utils as note_utils,
Expand Down Expand Up @@ -121,7 +113,7 @@ contract CardGame {
fn buy_pack(
seed: Field, // The randomness used to generate the cards. Passed in for now.
) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));
let buyer = context.msg_sender();
let mut cards = get_pack_cards(seed, buyer);

Expand All @@ -135,7 +127,7 @@ contract CardGame {
cards_fields: [Field; 2],
) {
let cards = cards_fields.map(|card_field| Card::from_field(card_field));
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));
let player = context.msg_sender();

let mut collection = storage.collections.at(player);
Expand All @@ -153,7 +145,7 @@ contract CardGame {
player: Field,
deck_strength: u32,
) {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
let game_storage = storage.games.at(game as Field);

let mut game_data = game_storage.read();
Expand All @@ -164,7 +156,7 @@ contract CardGame {

#[aztec(public)]
fn start_game(game: u32) {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
let game_storage = storage.games.at(game as Field);

let mut game_data = game_storage.read();
Expand All @@ -177,7 +169,7 @@ contract CardGame {
game: u32,
card: Card,
) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));
let player = context.msg_sender();

let mut game_deck = storage.game_decks.at(game as Field).at(player);
Expand All @@ -189,7 +181,7 @@ contract CardGame {

#[aztec(public)]
internal fn on_card_played(game: u32, player: Field, card_as_field: Field) {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
let game_storage = storage.games.at(game as Field);

let mut game_data = game_storage.read();
Expand All @@ -207,7 +199,7 @@ contract CardGame {
game: u32,
cards_fields: [Field; PLAYABLE_CARDS],
) {
let storage = Storage::init(Option::some(&mut context), Option::none());
let storage = Storage::init(Context::private(&mut context));
let player = context.msg_sender();
let cards = cards_fields.map(|card_field| Card::from_field(card_field));

Expand All @@ -224,7 +216,7 @@ contract CardGame {

#[aztec(public)]
internal fn on_cards_claimed(game: u32, player: Field, cards_hash: Field) {
let storage = Storage::init(Option::none(), Option::some(&mut context));
let storage = Storage::init(Context::public(&mut context));
let game_storage = storage.games.at(game as Field);
let mut game_data = game_storage.read();

Expand All @@ -243,21 +235,21 @@ contract CardGame {
}

unconstrained fn view_collection_cards(owner: Field, offset: u32) -> [Option<Card>; MAX_NOTES_PER_PAGE] {
let storage = Storage::init(Option::none(), Option::none());
let storage = Storage::init(Context::none());
let collection = storage.collections.at(owner);

collection.view_cards(offset)
}

unconstrained fn view_game_cards(game: u32, player: Field, offset: u32) -> [Option<Card>; MAX_NOTES_PER_PAGE] {
let storage = Storage::init(Option::none(), Option::none());
let storage = Storage::init(Context::none());
let game_deck = storage.game_decks.at(game as Field).at(player);

game_deck.view_cards(offset)
}

unconstrained fn view_game(game: u32) -> Game {
Storage::init(Option::none(), Option::none()).games.at(game as Field).read()
Storage::init(Context::none()).games.at(game as Field).read()
}

// Computes note hash and nullifier.
Expand Down
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
Loading