Skip to content

Commit

Permalink
chore: token contract storage cleanup (#2536)
Browse files Browse the repository at this point in the history
Fixes #2198.
Cleans the storage usage in the token implementation instead of casting
to and from Fields all the time.
  • Loading branch information
LHerskind authored Sep 29, 2023
1 parent 39f3a91 commit 0b62207
Show file tree
Hide file tree
Showing 15 changed files with 602 additions and 214 deletions.
3 changes: 2 additions & 1 deletion build_manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,6 @@ docs:
- ^.*.ts$
- ^.release-please-manifest.json$
- ^.*/noir-version.json$
- ^.*.nr$
dependencies:
- yarn-project
- yarn-project
6 changes: 3 additions & 3 deletions docs/docs/dev_docs/contracts/syntax/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ This function behaves similarly for both private and public maps. An example cou
Above, we are specifying that we want to get the storage in the Map `at` the `msg_sender()`, read the value stored and check that `msg_sender()` is indeed a minter. Doing a similar operation in Solidity code would look like:

```solidity
require(minters[msg.sender] == 1, "caller is not minter");
require(minters[msg.sender], "caller is not minter");
```

## Public State Variables
Expand Down Expand Up @@ -141,7 +141,7 @@ In this case, specifying that we are dealing with a map of Fields, and that it s

This would be similar to the following in solidity:
```solidity
mapping(address => uint256) internal minters;
mapping(address => bool) internal minters;
```

### `read`
Expand All @@ -159,7 +159,7 @@ For our `admin` example from earlier, this could be used as follows to check tha
#include_code read_admin /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust


#### Reading from out `minters` example
#### Reading from our `minters` example

As we saw in the Map earlier, a very similar operation can be done to perform a lookup in a map.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The `Token` contract also requires two helper files. Copy-them too:

Create `contracts/token/types.nr` and copy-paste the following:

#include_code token_types_all yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr rust
#include_code token_types_all yarn-project/noir-contracts/src/contracts/token_contract/src/types/transparent_note.nr rust

Finally, create `contracts/token/util.nr` and copy-paste the following:

Expand Down
4 changes: 4 additions & 0 deletions yarn-project/aztec-nr/aztec/src/types/address.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ impl AztecAddress {
}
}

fn eq(self: Self, other: Self) -> bool {
self.address == other.address
}

fn serialize(self: Self) -> [Field; 1] {
[self.address]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod bool_serialization;
mod field_serialization;
mod u32_serialization;
mod aztec_address_serialization;

/**
* Before Noir supports traits, a way of specifying the serialization and deserialization methods for a type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::types::type_serialization::TypeSerializationInterface;
use crate::types::address::AztecAddress;

global AZTEC_ADDRESS_SERIALIZED_LEN: Field = 1;

fn deserialize(fields: [Field; AZTEC_ADDRESS_SERIALIZED_LEN]) -> AztecAddress {
AztecAddress::new(fields[0])
}

fn serialize(value: AztecAddress) -> [Field; AZTEC_ADDRESS_SERIALIZED_LEN] {
[value.address]
}

global AztecAddressSerializationMethods = TypeSerializationInterface {
deserialize,
serialize,
};
54 changes: 54 additions & 0 deletions yarn-project/aztec-nr/safe-math/src/safe_u120.nr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ impl SafeU120 {
self.value == other.value
}

fn lt(self: Self, other: Self) -> bool {
self.value < other.value
}

fn le(self: Self, other: Self) -> bool {
self.value <= other.value
}

fn gt(self: Self, other: Self) -> bool {
self.value > other.value
}

fn ge(self: Self, other: Self) -> bool {
self.value >= other.value
}

fn sub(
self: Self,
b: Self,
Expand Down Expand Up @@ -139,6 +155,44 @@ fn test_eq() {
assert(a.eq(b));
}

#[test]
fn test_lt() {
let a = SafeU120::new(1);
let b = SafeU120::new(2);
assert(a.lt(b));
assert(b.lt(a) == false);
}


#[test]
fn test_le() {
let a = SafeU120::new(2);
let b = SafeU120::new(2);
let c = SafeU120::new(5);
assert(a.le(b));
assert(a.le(c));
assert(c.le(a) == false);
}

#[test]
fn test_gt() {
let a = SafeU120::new(1);
let b = SafeU120::new(2);
assert(b.gt(a));
assert(a.gt(b) == false);
}


#[test]
fn test_ge() {
let a = SafeU120::new(2);
let b = SafeU120::new(2);
let c = SafeU120::new(5);
assert(a.ge(b));
assert(a.ge(c) == false);
assert(c.ge(a));
}

#[test(should_fail)]
fn test_init_too_large() {
let b = SafeU120::max().value as Field + 1; // max + 1
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_token_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const hashPayload = async (payload: Fr[]) => {
);
};

const TIMEOUT = 60_000;
const TIMEOUT = 90_000;

describe('e2e_token_contract', () => {
jest.setTimeout(TIMEOUT);
Expand Down
Loading

0 comments on commit 0b62207

Please sign in to comment.