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

chore: Run nargo fmt on "formatting:fix" #3372

Merged
merged 9 commits into from
Nov 22, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// would alter the metrics we're capturing in the benchmarks, and we want to keep the
// subject being tested as unmodified as possible so we can detect metric changes that
// arise from code changes.

contract Benchmarking {
use dep::value_note::{
utils::{increment, decrement},
Expand Down Expand Up @@ -50,7 +49,7 @@ contract Benchmarking {
// by @rahul-kothari for a full explanation on why this is needed.
#[aztec(private)]
fn recreate_note(owner: Field, index: u32) {
let owner_notes = storage.notes.at(owner);
let owner_notes = storage.notes.at(owner);
let getter_options = NoteGetterOptions::new().set_limit(1).set_offset(index);
let notes = owner_notes.get_notes(getter_options);
let note = notes[0].unwrap_unchecked();
Expand All @@ -63,7 +62,7 @@ contract Benchmarking {
fn increment_balance(owner: Field, value: Field) {
let current = storage.balances.at(owner).read();
storage.balances.at(owner).write(current + value);
let _callStackItem1 = context.call_public_function(context.this_address(), compute_selector("broadcast(Field)"), [owner]);
let _callStackItem1 = context.call_public_function(context.this_address(), compute_selector("broadcast(Field)"), [owner]);
}

// Emits a public log.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fn test_to_from_field() {
assert(card.to_field() == field);
}


struct CardNote {
card: Card,
note: ValueNote,
Expand Down Expand Up @@ -96,23 +95,19 @@ pub fn filter_cards<N>(notes: [Option<ValueNote>; MAX_READ_REQUESTS_PER_CALL], d
for i in 0..notes.len() {
let note = notes[i];
if note.is_some() {
let card_note = CardNote::from_note(
note.unwrap_unchecked()
);
let card_note = CardNote::from_note(note.unwrap_unchecked());
for j in 0..N {
if !found[j] & (card_note.card.strength == desired_cards[j].strength) & (card_note.card.points == desired_cards[j].points) {
selected[i] = note;
found[j] = true;
}
}
}

}

selected
}


impl Deck {
pub fn new(
context: Context,
Expand Down Expand Up @@ -189,7 +184,8 @@ impl Deck {

}

global PACK_CARDS = 3; // Limited by number of write requests (max 4)
// Limited by number of write requests (max 4)
global PACK_CARDS = 3;

pub fn get_pack_cards(
seed: Field,
Expand All @@ -201,6 +197,7 @@ pub fn get_pack_cards(
let random_bytes = std::hash::sha256(mix.to_le_bytes(32));

let mut cards = [Card::from_field(0); PACK_CARDS];

// we generate PACK_CARDS cards
assert((PACK_CARDS as u64) < 8, "Cannot generate more than 8 cards");
for i in 0..PACK_CARDS {
Expand All @@ -218,4 +215,4 @@ pub fn compute_deck_strength<N>(cards: [Card; N]) -> Field {
cards.fold(0, |acc, card: Card| {
acc + card.strength as Field
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,25 @@ struct Game {
global GAME_SERIALIZED_LEN: Field = 15;

fn deserializeGame(fields: [Field; GAME_SERIALIZED_LEN]) -> Game {
let players = [
PlayerEntry {
address: fields[0],
deck_strength: fields[1] as u32,
points: fields[2] as u120,
},
PlayerEntry {
address: fields[3],
deck_strength: fields[4] as u32,
points: fields[5] as u120,
},
];
let rounds_cards = [
Card::from_field(fields[6]), Card::from_field(fields[7]),
Card::from_field(fields[8]), Card::from_field(fields[9]),
];
Game {
let players = [PlayerEntry { address: fields[0], deck_strength: fields[1] as u32, points: fields[2] as u120 },
PlayerEntry { address: fields[3], deck_strength: fields[4] as u32, points: fields[5] as u120 }];
benesjan marked this conversation as resolved.
Show resolved Hide resolved
let rounds_cards = [Card::from_field(fields[6]),
Card::from_field(fields[7]),
Card::from_field(fields[8]),
Card::from_field(fields[9])];
Game {
players,
rounds_cards,
started: fields[10] as bool,
finished: fields[11] as bool,
claimed: fields[12] as bool,
current_player: fields[13] as u32,
current_round: fields[14] as u32,
current_round: fields[14] as u32
}
}

fn serializeGame(game: Game) -> [Field; GAME_SERIALIZED_LEN] {
[
game.players[0].address,
[game.players[0].address,
benesjan marked this conversation as resolved.
Show resolved Hide resolved
game.players[0].deck_strength as Field,
game.players[0].points as Field,
game.players[1].address,
Expand All @@ -74,8 +63,7 @@ fn serializeGame(game: Game) -> [Field; GAME_SERIALIZED_LEN] {
game.finished as Field,
game.claimed as Field,
game.current_player as Field,
game.current_round as Field,
]
game.current_round as Field]
}

impl Game {
Expand Down Expand Up @@ -169,4 +157,4 @@ impl Game {
global GameSerializationMethods = TypeSerializationInterface {
deserialize: deserializeGame,
serialize: serializeGame,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ contract CardGame {
},
};


use dep::std::option::Option;
use dep::value_note::{
balance_utils,
Expand Down Expand Up @@ -50,7 +49,7 @@ contract CardGame {
GameSerializationMethods,
GAME_SERIALIZED_LEN
};

struct Storage {
collections: Map<Deck>,
game_decks: Map<Map<Deck>>,
Expand Down Expand Up @@ -109,8 +108,8 @@ contract CardGame {
#[aztec(private)]
fn buy_pack(
seed: Field, // The randomness used to generate the cards. Passed in for now.
) {
)
{
let buyer = context.msg_sender();
let mut cards = get_pack_cards(seed, buyer);

Expand All @@ -122,9 +121,9 @@ contract CardGame {
fn join_game(
game: u32,
cards_fields: [Field; 2],
) {
) {
let cards = cards_fields.map(|card_field| Card::from_field(card_field));

let player = context.msg_sender();

let mut collection = storage.collections.at(player);
Expand All @@ -141,21 +140,19 @@ contract CardGame {
game: u32,
player: Field,
deck_strength: u32,
) {

) {
let game_storage = storage.games.at(game as Field);

let mut game_data = game_storage.read();
assert(game_data.add_player(PlayerEntry {address: player, deck_strength, points: 0}), "Game full");
assert(game_data.add_player(PlayerEntry { address: player, deck_strength, points: 0 }), "Game full");

game_storage.write(game_data);
}

#[aztec(public)]
fn start_game(game: u32) {

fn start_game(game: u32) {
let game_storage = storage.games.at(game as Field);

let mut game_data = game_storage.read();
game_data.start_game();
game_storage.write(game_data);
Expand All @@ -165,8 +162,7 @@ contract CardGame {
fn play_card(
game: u32,
card: Card,
) {

) {
let player = context.msg_sender();

let mut game_deck = storage.game_decks.at(game as Field).at(player);
Expand All @@ -178,9 +174,8 @@ contract CardGame {

#[aztec(public)]
internal fn on_card_played(game: u32, player: Field, card_as_field: Field) {

let game_storage = storage.games.at(game as Field);

let mut game_data = game_storage.read();

let card = Card::from_field(card_as_field);
Expand All @@ -195,35 +190,26 @@ contract CardGame {
fn claim_cards(
game: u32,
cards_fields: [Field; PLAYABLE_CARDS],
) {

) {
let player = context.msg_sender();
let cards = cards_fields.map(|card_field| Card::from_field(card_field));

let mut collection = storage.collections.at(player);
let _inserted_cards = collection.add_cards(cards, player);

let selector = compute_selector("on_cards_claimed(u32,Field,Field)");
context.call_public_function(
context.this_address(),
selector,
[game as Field, player, pedersen_hash(cards_fields,0)]
);
context.call_public_function(context.this_address(), selector, [game as Field, player, pedersen_hash(cards_fields, 0)]);
}

#[aztec(public)]
internal fn on_cards_claimed(game: u32, player: Field, cards_hash: Field) {

let game_storage = storage.games.at(game as Field);
let mut game_data = game_storage.read();

assert(!game_data.claimed, "Already claimed");
game_data.claimed = true;

assert_eq(
cards_hash,
pedersen_hash(game_data.rounds_cards.map(|card: Card| card.to_field()),0)
);
assert_eq(cards_hash, pedersen_hash(game_data.rounds_cards.map(|card: Card| card.to_field()), 0));

let winner = game_data.winner();
assert_eq(player, winner.address, "Not the winner");
Expand All @@ -232,22 +218,20 @@ contract CardGame {
}

unconstrained fn view_collection_cards(owner: Field, offset: u32) -> [Option<Card>; MAX_NOTES_PER_PAGE] {

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 game_deck = storage.game_decks.at(game as Field).at(player);

game_deck.view_cards(offset)
}
}

unconstrained fn view_game(game: u32) -> Game {
storage.games.at(game as Field).read()
}
}

// Computes note hash and nullifier.
// Note 1: Needs to be defined by every contract producing logs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract Child {
}

#[aztec(private)]
fn constructor() {}
fn constructor() {}

// Returns a sum of the input and the chain id and version of the contract in private circuit public input's return_values.
#[aztec(private)]
Expand Down Expand Up @@ -63,17 +63,15 @@ contract Child {
// Sets `current_value` to `new_value`
#[aztec(public)]
fn pubSetValue(new_value: Field) -> Field {

storage.current_value.write(new_value);
emit_unencrypted_log(&mut context, new_value);

new_value
}

// Increments `current_value` by `new_value`
#[aztec(public)]
fn pubIncValue(new_value: Field) -> Field {

let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
emit_unencrypted_log(&mut context, new_value);
Expand All @@ -84,12 +82,11 @@ contract Child {
// Increments `current_value` by `new_value`. Can only be called from this contract.
#[aztec(public)]
fn pubIncValueInternal(new_value: Field) -> Field {

check_sender(inputs.call_context);
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
emit_unencrypted_log(&mut context, new_value);

new_value
}

Expand All @@ -98,14 +95,12 @@ contract Child {
let pubSetValueSelector = compute_selector("pubSetValue(Field)");
let _ret = context.call_public_function(context.this_address(), pubSetValueSelector, [10]);


storage.current_value.write(20);
emit_unencrypted_log(&mut context, 20);
}

#[aztec(public)]
fn setValueTwiceWithNestedLast() {

storage.current_value.write(20);
emit_unencrypted_log(&mut context, 20);

Expand Down
Loading