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!: init storage macro #4200

Merged
merged 112 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
112 commits
Select commit Hold shift + click to select a range
b61f937
serialize, deserialize, noteinterface as traits
Thunkar Jan 18, 2024
62051a9
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 18, 2024
48c8475
Merge branch 'master' into gj/serialize_deserialize_note-interface
Thunkar Jan 18, 2024
5bf66e1
workaround
Thunkar Jan 18, 2024
326375b
more workarounds
Thunkar Jan 19, 2024
f8c6ea9
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 19, 2024
e5098b4
added issue comments
Thunkar Jan 19, 2024
52622da
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 19, 2024
28f43d9
Merge branch 'master' into gj/serialize_deserialize_note-interface
Thunkar Jan 19, 2024
f30760b
Merge branch 'master' into gj/serialize_deserialize_note-interface
Thunkar Jan 19, 2024
2e6e863
Merge branch 'master' into gj/serialize_deserialize_note-interface
Thunkar Jan 22, 2024
bc6bd95
remove public state length
sirasistant Jan 22, 2024
fca8cf9
fix: note len?
sirasistant Jan 22, 2024
8e827e0
compiler hints as workaround
Thunkar Jan 22, 2024
84f448e
removed comment
Thunkar Jan 22, 2024
50749fe
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 22, 2024
79f885c
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 23, 2024
ccb2ef2
merge changes
Thunkar Jan 23, 2024
c4f6525
WIP
Thunkar Jan 23, 2024
792d0d6
initial auto storage implementation
Thunkar Jan 24, 2024
9bbdb0c
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 24, 2024
594dfe2
merge conflicts
Thunkar Jan 24, 2024
19a5313
merge conflicts
Thunkar Jan 24, 2024
96f9b7e
Merge branch 'gj/serialize_deserialize_note-interface' of github.com:…
Thunkar Jan 24, 2024
182faca
uncommented method
Thunkar Jan 24, 2024
5fd184c
adapt new interface
Thunkar Jan 24, 2024
2b43c73
Merge branch 'gj/serialize_deserialize_note-interface' of github.com:…
Thunkar Jan 24, 2024
85cd6cd
more deserialization hints
Thunkar Jan 24, 2024
a847ecb
Merge branch 'gj/serialize_deserialize_note-interface' of github.com:…
Thunkar Jan 24, 2024
d17cb5e
manually called format
Thunkar Jan 24, 2024
f4efc6b
manually called format
Thunkar Jan 24, 2024
9a9aa01
Merge branch 'gj/serialize_deserialize_note-interface' into gj/init_s…
Thunkar Jan 24, 2024
8788c67
restored bb changes
Thunkar Jan 24, 2024
48002d4
reverted barretenberg to master
Thunkar Jan 24, 2024
9966206
reverted barretenberg to master
Thunkar Jan 24, 2024
06f5e04
Merge branch 'gj/serialize_deserialize_note-interface' into gj/init_s…
Thunkar Jan 24, 2024
c6e3b5c
refactor to allow recursive maps
Thunkar Jan 24, 2024
708c4f7
format
Thunkar Jan 24, 2024
b3f820e
format
Thunkar Jan 24, 2024
c4d1848
better custom type support
Thunkar Jan 24, 2024
0fbd872
Merge branch 'gj/serialize_deserialize_note-interface' into gj/init_s…
Thunkar Jan 24, 2024
214df5f
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 29, 2024
ac6afab
Merge branch 'gj/serialize_deserialize_note-interface' of github.com:…
Thunkar Jan 29, 2024
d6ab45e
formatting
Thunkar Jan 29, 2024
7fd65ec
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 29, 2024
79c3bcf
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 29, 2024
4e9fa91
removed length hints
Thunkar Jan 29, 2024
8386cc6
updated docs
Thunkar Jan 29, 2024
900d622
fixed docs
Thunkar Jan 29, 2024
29672e7
added migration notes
Thunkar Jan 29, 2024
0927091
Merge branch 'master' into gj/serialize_deserialize_note-interface
Thunkar Jan 29, 2024
bdf27c4
Merge branch 'gj/serialize_deserialize_note-interface' of github.com:…
Thunkar Jan 29, 2024
d11357a
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 30, 2024
9b7d069
comments and better error handling in macros
Thunkar Jan 30, 2024
b58e2cc
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 30, 2024
7194abb
Merge branch 'gj/serialize_deserialize_note-interface' into gj/init_s…
Thunkar Jan 30, 2024
7de2270
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 30, 2024
dff2901
docs update
Thunkar Jan 30, 2024
39ae8d6
Merge branch 'gj/serialize_deserialize_note-interface' of github.com:…
Thunkar Jan 30, 2024
28514d4
updated docs and migration guide
Thunkar Jan 30, 2024
cd2fca7
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 30, 2024
6d39b30
Merge branch 'master' into gj/serialize_deserialize_note-interface
Thunkar Jan 30, 2024
5f07ed2
Merge branch 'gj/serialize_deserialize_note-interface' into gj/init_s…
Thunkar Jan 30, 2024
ac1efeb
fixed test
Thunkar Jan 30, 2024
5caf896
reverted name change
Thunkar Jan 30, 2024
c49b3ed
Merge branch 'gj/serialize_deserialize_note-interface' into gj/init_s…
Thunkar Jan 30, 2024
02a0ae5
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 30, 2024
9e6c8cc
refactor for simplyfication
Thunkar Jan 31, 2024
9c5ae89
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 31, 2024
418d89d
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 31, 2024
9b69ac6
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 31, 2024
2f4f077
balancesmap refactor and docs
Thunkar Jan 31, 2024
ff94d18
Merge branch 'master' into gj/init_storage_macro
Thunkar Jan 31, 2024
4dd510f
completely remove balanceset
Thunkar Jan 31, 2024
8d5827f
Merge branch 'gj/init_storage_macro' of github.com:AztecProtocol/azte…
Thunkar Jan 31, 2024
0818a8b
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 31, 2024
92c41c4
fixed slots
Thunkar Jan 31, 2024
c875d7b
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 31, 2024
b81fa5d
actually saved file
Thunkar Jan 31, 2024
853d0b3
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 31, 2024
e858bad
merge conflicts
Thunkar Jan 31, 2024
1fc7b9b
cleanup
Thunkar Jan 31, 2024
5a487cb
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 31, 2024
bcaa588
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Feb 1, 2024
33cdc08
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Feb 1, 2024
0465b02
simplified constructor
Thunkar Feb 1, 2024
d7aa71b
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Feb 1, 2024
a16f0be
finally fixed slots
Thunkar Feb 1, 2024
58c35ea
fixed docs
Thunkar Feb 1, 2024
58df9fd
Merge branch 'master' into gj/init_storage_macro
Thunkar Feb 1, 2024
82a7763
fix
Thunkar Feb 1, 2024
6f2ffa3
fix
Thunkar Feb 1, 2024
ca22d64
added comment
Thunkar Feb 1, 2024
f07d0e0
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Feb 1, 2024
8bbcff7
fixed tests
Thunkar Feb 1, 2024
1ebd83f
yet another hardcoded storageSlot
Thunkar Feb 1, 2024
97a26f0
Merge branch 'master' into gj/init_storage_macro
Thunkar Feb 1, 2024
0f732b5
pr review comments
Thunkar Feb 2, 2024
1a7dbce
Merge branch 'gj/init_storage_macro' of github.com:AztecProtocol/azte…
Thunkar Feb 2, 2024
b29f90b
more pr comments
Thunkar Feb 2, 2024
78e98cb
Merge branch 'master' into gj/init_storage_macro
Thunkar Feb 2, 2024
48fbfaf
clippy
Thunkar Feb 2, 2024
b1f2ea1
more pr comments
Thunkar Feb 2, 2024
cf0fecf
Merge branch 'master' into gj/init_storage_macro
Thunkar Feb 2, 2024
114e118
remove clang after install
Thunkar Feb 2, 2024
3d1d7cb
Merge branch 'gj/init_storage_macro' of github.com:AztecProtocol/azte…
Thunkar Feb 2, 2024
19f65e2
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Feb 2, 2024
6b23d17
chore: use reference to reduce cloning
sirasistant Feb 2, 2024
703e2ea
Merge branch 'master' into gj/init_storage_macro
Thunkar Feb 2, 2024
61b3650
updated boxes
Thunkar Feb 2, 2024
a8c40cf
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Feb 2, 2024
090225d
Merge branch 'gj/init_storage_macro' of github.com:AztecProtocol/azte…
Thunkar Feb 2, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/protocol-circuits-gate-diff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
sudo cp -r clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04/include/* /usr/local/include/
sudo cp -r clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/* /usr/local/lib/
sudo cp -r clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04/share/* /usr/local/share/
rm -rf clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04

- uses: actions/cache@v3
with:
Expand Down
63 changes: 8 additions & 55 deletions boxes/token/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ contract Token {
minters: Map<AztecAddress, PublicState<bool>>,
// docs:end:storage_minters
// docs:start:storage_balances
balances: BalancesMap,
balances: BalancesMap<TokenNote>,
// docs:end:storage_balances
total_supply: PublicState<SafeU120>,
// docs:start:storage_pending_shields
Expand All @@ -65,53 +65,6 @@ contract Token {
}
// docs:end:storage_struct

// docs:start:storage_init
impl Storage {
fn init(context: Context) -> Self {
Storage {
// docs:start:storage_admin_init
admin: PublicState::new(
context,
1,
),
// docs:end:storage_admin_init
// docs:start:storage_minters_init
minters: Map::new(
context,
2,
|context, slot| {
PublicState::new(
context,
slot,
)
},
),
// docs:end:storage_minters_init
// docs:start:storage_balances_init
balances: BalancesMap::new(context, 3),
// docs:end:storage_balances_init
total_supply: PublicState::new(
context,
4,
),
// docs:start:storage_pending_shields_init
pending_shields: Set::new(context, 5),
// docs:end:storage_pending_shields_init
public_balances: Map::new(
context,
6,
|context, slot| {
PublicState::new(
context,
slot,
)
},
),
}
}
}
// docs:end:storage_init

// docs:start:constructor
#[aztec(private)]
fn constructor(admin: AztecAddress) {
Expand Down Expand Up @@ -245,7 +198,7 @@ contract Token {
pending_shields.remove(note);

// Add the token note to user's balances set
storage.balances.at(to).add(SafeU120::new(amount));
storage.balances.add(to, SafeU120::new(amount));
}
// docs:end:redeem_shield

Expand All @@ -258,7 +211,7 @@ contract Token {
assert(nonce == 0, "invalid nonce");
}

storage.balances.at(from).sub(SafeU120::new(amount));
storage.balances.sub(from, SafeU120::new(amount));

let selector = FunctionSelector::from_signature("_increase_public_balance((Field),Field)");
let _void = context.call_public_function(context.this_address(), selector, [to.to_field(), amount]);
Expand All @@ -277,9 +230,9 @@ contract Token {
// docs:end:assert_current_call_valid_authwit

let amount = SafeU120::new(amount);
storage.balances.at(from).sub(amount);
storage.balances.sub(from, amount);
// docs:start:increase_private_balance
storage.balances.at(to).add(amount);
storage.balances.add(to, amount);
// docs:end:increase_private_balance
}
// docs:end:transfer
Expand All @@ -293,7 +246,7 @@ contract Token {
assert(nonce == 0, "invalid nonce");
}

storage.balances.at(from).sub(SafeU120::new(amount));
storage.balances.sub(from, SafeU120::new(amount));

let selector = FunctionSelector::from_signature("_reduce_total_supply(Field)");
let _void = context.call_public_function(context.this_address(), selector, [amount]);
Expand Down Expand Up @@ -350,7 +303,7 @@ contract Token {

// docs:start:balance_of_private
unconstrained fn balance_of_private(owner: AztecAddress) -> pub u120 {
storage.balances.at(owner).balance_of().value
storage.balances.balance_of(owner).value
}
// docs:end:balance_of_private

Expand All @@ -374,7 +327,7 @@ contract Token {
serialized_note: [Field; TOKEN_NOTE_LEN]
) -> pub [Field; 4] {
let note_header = NoteHeader::new(contract_address, nonce, storage_slot);
if (storage_slot == 5) {
if (storage_slot == storage.pending_shields.get_storage_slot()) {
note_utils::compute_note_hash_and_nullifier(TransparentNote::deserialize, note_header, serialized_note)
} else {
note_utils::compute_note_hash_and_nullifier(TokenNote::deserialize, note_header, serialized_note)
Expand Down
1 change: 0 additions & 1 deletion boxes/token/src/contracts/src/types.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod transparent_note;
mod balance_set;
mod balances_map;
mod token_note;
113 changes: 0 additions & 113 deletions boxes/token/src/contracts/src/types/balance_set.nr

This file was deleted.

125 changes: 105 additions & 20 deletions boxes/token/src/contracts/src/types/balances_map.nr
Original file line number Diff line number Diff line change
@@ -1,34 +1,119 @@
use dep::aztec::context::{PrivateContext, PublicContext, Context};
use dep::std::option::Option;
use crate::types::balance_set::BalanceSet;
use dep::aztec::hash::pedersen_hash;
use dep::aztec::protocol_types::address::AztecAddress;

use crate::types::token_note::TokenNote;
use dep::aztec::state_vars::{map::Map, set::Set};
use dep::safe_math::SafeU120;
use dep::aztec::{
context::{PrivateContext, PublicContext, Context},
hash::pedersen_hash,
protocol_types::{
address::AztecAddress,
constants::MAX_READ_REQUESTS_PER_CALL,
traits::{Serialize, Deserialize}
},
state_vars::{
set::Set,
map::Map
},
note::{
note_getter::view_notes,
note_getter_options::{NoteGetterOptions, SortOrder},
note_viewer_options::NoteViewerOptions,
note_header::NoteHeader,
note_interface::NoteInterface,
}
};
use crate::types::token_note::{TokenNote, OwnedNote};

struct BalancesMap {
store: Map<AztecAddress, Set<TokenNote>>,
struct BalancesMap<T> {
map: Map<AztecAddress, Set<T>>
}

impl BalancesMap {
impl<T> BalancesMap<T> {
pub fn new(
context: Context,
storage_slot: Field,
) -> Self {
let store = Map::new(context, storage_slot, |context, storage_slot| {
Set {
context,
storage_slot,
}
});
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
Self {
store,
map: Map::new(context, storage_slot, |context, slot| Set::new(context, slot))
}
}

unconstrained pub fn balance_of<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress) -> SafeU120 where T: Deserialize<T_SERIALIZED_LEN> + Serialize<T_SERIALIZED_LEN> + NoteInterface + OwnedNote {
self.balance_of_with_offset(owner, 0)
}

unconstrained pub fn balance_of_with_offset<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress, offset: u32) -> SafeU120 where T: Deserialize<T_SERIALIZED_LEN> + Serialize<T_SERIALIZED_LEN> + NoteInterface + OwnedNote {
// Same as SafeU120::new(0), but fewer constraints because no check.
let mut balance = SafeU120::min();
// docs:start:view_notes
let options = NoteViewerOptions::new().set_offset(offset);
let opt_notes = self.map.at(owner).view_notes(options);
// docs:end:view_notes
let len = opt_notes.len();
for i in 0..len {
if opt_notes[i].is_some() {
balance = balance.add(opt_notes[i].unwrap_unchecked().get_amount());
}
}
if (opt_notes[len - 1].is_some()) {
balance = balance.add(self.balance_of_with_offset(owner, offset + opt_notes.len() as u32));
}

balance
}

pub fn add<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress, addend: SafeU120) where T: Deserialize<T_SERIALIZED_LEN> + Serialize<T_SERIALIZED_LEN> + NoteInterface + OwnedNote {
let mut addend_note = T::new(addend, owner);

// docs:start:insert
self.map.at(owner).insert(&mut addend_note, true);
// docs:end:insert
}

pub fn sub<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress, subtrahend: SafeU120) where T: Deserialize<T_SERIALIZED_LEN> + Serialize<T_SERIALIZED_LEN> + NoteInterface + OwnedNote{
// docs:start:get_notes
let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend);
let maybe_notes = self.map.at(owner).get_notes(options);
// docs:end:get_notes

let mut minuend: SafeU120 = SafeU120::min();
for i in 0..maybe_notes.len() {
if maybe_notes[i].is_some() {
let note = maybe_notes[i].unwrap_unchecked();

// Removes the note from the owner's set of notes.
// This will call the the `compute_nullifer` function of the `token_note`
// which require knowledge of the secret key (currently the users encryption key).
// The contract logic must ensure that the spending key is used as well.
// docs:start:remove
self.map.at(owner).remove(note);
// docs:end:remove

minuend = minuend.add(note.get_amount());
}
}

// This is to provide a nicer error msg,
// without it minuend-subtrahend would still catch it, but more generic error then.
// without the == true, it includes 'minuend.ge(subtrahend)' as part of the error.
assert(minuend.ge(subtrahend) == true, "Balance too low");

self.add(owner, minuend.sub(subtrahend));
}

pub fn at(self, owner: AztecAddress) -> BalanceSet {
let set = self.store.at(owner);
BalanceSet::new(set, owner)
}

pub fn filter_notes_min_sum<T, T_SERIALIZED_LEN>(
notes: [Option<T>; MAX_READ_REQUESTS_PER_CALL],
min_sum: SafeU120
) -> [Option<T>; MAX_READ_REQUESTS_PER_CALL] where T: Deserialize<T_SERIALIZED_LEN> + Serialize<T_SERIALIZED_LEN> + NoteInterface + OwnedNote {
let mut selected = [Option::none(); MAX_READ_REQUESTS_PER_CALL];
let mut sum = SafeU120::min();
for i in 0..notes.len() {
if notes[i].is_some() & sum.lt(min_sum) {
let note = notes[i].unwrap_unchecked();
selected[i] = Option::some(note);
sum = sum.add(note.get_amount());
}
}
selected
}
Loading
Loading