-
Notifications
You must be signed in to change notification settings - Fork 310
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(aztec_noir): abstract storage initialisation #2406
Changes from all commits
a283477
518f357
3221d6f
a86ad99
2f4d6b8
d4ab459
9c0c54a
765e172
0c8e3eb
c431230
8a6b200
5e62043
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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,4 +1,4 @@ | ||
{ | ||
"tag": "0.11.1-aztec.0", | ||
"commit": "2103b2ffb640fe457b24be09b6d63fe6ee1c6ac1" | ||
"tag": "0.12.0-aztec.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can already see the pain out there from this hehe. |
||
"commit": "3b43709ddd5feffdbbc1723ba7dbcf63531aae05" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,16 @@ | ||
mod cards; | ||
mod game; | ||
|
||
use dep::aztec::{ | ||
context::{PrivateContext, PublicContext, Context}, | ||
state_vars::{ | ||
map::Map, | ||
public_state::PublicState, | ||
}, | ||
}; | ||
|
||
use dep::std::option::Option; | ||
|
||
use cards::{Deck}; | ||
use game::{Game, GameSerializationMethods, GAME_SERIALIZED_LEN}; | ||
|
||
struct Storage { | ||
collections: Map<Deck>, | ||
game_decks: Map<Map<Deck>>, | ||
games: Map<PublicState<Game, GAME_SERIALIZED_LEN>>, | ||
} | ||
contract CardGame { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not clear to me why you are changing the entire ordering here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this to work it needs to see the Storage struct within the contract def, previously it was a scope up |
||
use dep::aztec::{ | ||
context::Context, | ||
state_vars::{ | ||
map::Map, | ||
public_state::PublicState, | ||
}, | ||
}; | ||
|
||
impl Storage { | ||
fn init( | ||
context: Context, | ||
) -> Self { | ||
Storage { | ||
collections: Map::new( | ||
context, | ||
1, | ||
|context, slot| { | ||
Deck::new( | ||
context, | ||
slot, | ||
) | ||
}, | ||
), | ||
game_decks: Map::new( | ||
context, | ||
2, | ||
|context, slot| { | ||
Map::new( | ||
context, | ||
slot, | ||
|context, slot|{ | ||
Deck::new( | ||
context, | ||
slot, | ||
) | ||
} | ||
) | ||
}, | ||
), | ||
games: Map::new( | ||
context, | ||
3, | ||
|context, slot| { | ||
PublicState::new( | ||
context, | ||
slot, | ||
GameSerializationMethods, | ||
) | ||
}, | ||
) | ||
} | ||
} | ||
} | ||
|
||
contract CardGame { | ||
use dep::std::option::Option; | ||
use dep::value_note::{ | ||
balance_utils, | ||
|
@@ -82,15 +26,13 @@ contract CardGame { | |
abi::{ | ||
Hasher, PrivateContextInputs, | ||
}, | ||
context::{PrivateContext, Context}, | ||
note::{ | ||
note_header::NoteHeader, | ||
utils as note_utils, | ||
}, | ||
oracle::compute_selector::compute_selector | ||
}; | ||
|
||
use crate::Storage; | ||
use crate::cards::{ | ||
PACK_CARDS, | ||
Deck, | ||
|
@@ -103,8 +45,62 @@ contract CardGame { | |
NUMBER_OF_CARDS_DECK, | ||
PLAYABLE_CARDS, | ||
PlayerEntry, | ||
Game | ||
Game, | ||
GameSerializationMethods, | ||
GAME_SERIALIZED_LEN | ||
}; | ||
|
||
struct Storage { | ||
collections: Map<Deck>, | ||
game_decks: Map<Map<Deck>>, | ||
games: Map<PublicState<Game, GAME_SERIALIZED_LEN>>, | ||
} | ||
|
||
impl Storage { | ||
fn init( | ||
context: Context, | ||
) -> pub Self { | ||
Storage { | ||
collections: Map::new( | ||
context, | ||
1, | ||
|context, slot| { | ||
Deck::new( | ||
context, | ||
slot, | ||
) | ||
}, | ||
), | ||
game_decks: Map::new( | ||
context, | ||
2, | ||
|context, slot| { | ||
Map::new( | ||
context, | ||
slot, | ||
|context, slot|{ | ||
Deck::new( | ||
context, | ||
slot, | ||
) | ||
} | ||
) | ||
}, | ||
), | ||
games: Map::new( | ||
context, | ||
3, | ||
|context, slot| { | ||
PublicState::new( | ||
context, | ||
slot, | ||
GameSerializationMethods, | ||
) | ||
}, | ||
) | ||
} | ||
} | ||
} | ||
|
||
#[aztec(private)] | ||
fn constructor() {} | ||
|
@@ -113,7 +109,7 @@ contract CardGame { | |
fn buy_pack( | ||
seed: Field, // The randomness used to generate the cards. Passed in for now. | ||
) { | ||
let storage = Storage::init(Context::private(&mut context)); | ||
|
||
let buyer = context.msg_sender(); | ||
let mut cards = get_pack_cards(seed, buyer); | ||
|
||
|
@@ -127,7 +123,7 @@ contract CardGame { | |
cards_fields: [Field; 2], | ||
) { | ||
let cards = cards_fields.map(|card_field| Card::from_field(card_field)); | ||
let storage = Storage::init(Context::private(&mut context)); | ||
|
||
let player = context.msg_sender(); | ||
|
||
let mut collection = storage.collections.at(player); | ||
|
@@ -145,7 +141,7 @@ contract CardGame { | |
player: Field, | ||
deck_strength: u32, | ||
) { | ||
let storage = Storage::init(Context::public(&mut context)); | ||
|
||
let game_storage = storage.games.at(game as Field); | ||
|
||
let mut game_data = game_storage.read(); | ||
|
@@ -156,7 +152,7 @@ contract CardGame { | |
|
||
#[aztec(public)] | ||
fn start_game(game: u32) { | ||
let storage = Storage::init(Context::public(&mut context)); | ||
|
||
let game_storage = storage.games.at(game as Field); | ||
|
||
let mut game_data = game_storage.read(); | ||
|
@@ -169,7 +165,7 @@ contract CardGame { | |
game: u32, | ||
card: Card, | ||
) { | ||
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); | ||
|
@@ -181,7 +177,7 @@ contract CardGame { | |
|
||
#[aztec(public)] | ||
internal fn on_card_played(game: u32, player: Field, card_as_field: Field) { | ||
let storage = Storage::init(Context::public(&mut context)); | ||
|
||
let game_storage = storage.games.at(game as Field); | ||
|
||
let mut game_data = game_storage.read(); | ||
|
@@ -199,7 +195,7 @@ contract CardGame { | |
game: u32, | ||
cards_fields: [Field; PLAYABLE_CARDS], | ||
) { | ||
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)); | ||
|
||
|
@@ -216,7 +212,7 @@ contract CardGame { | |
|
||
#[aztec(public)] | ||
internal fn on_cards_claimed(game: u32, player: Field, cards_hash: Field) { | ||
let storage = Storage::init(Context::public(&mut context)); | ||
|
||
let game_storage = storage.games.at(game as Field); | ||
let mut game_data = game_storage.read(); | ||
|
||
|
@@ -235,21 +231,21 @@ contract CardGame { | |
} | ||
|
||
unconstrained fn view_collection_cards(owner: Field, offset: u32) -> [Option<Card>; MAX_NOTES_PER_PAGE] { | ||
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(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(Context::none()).games.at(game as Field).read() | ||
storage.games.at(game as Field).read() | ||
} | ||
|
||
// Computes note hash and nullifier. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need to show this given the macro? Wouldn't it be more confusing?
Or if we do, can we also show how storage init works for public? (this include code just shows for private)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case we should also show the expansion for a public function as the context uses different values too. I think people can infer that a different context goes in there. I can leave a note alluding to it.
I can also update the example such that it actually uses storage if that helps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since in expansion I think its fine to show it 🤷