Skip to content

Commit

Permalink
refactor: rename public state serialisation interface structs for cla…
Browse files Browse the repository at this point in the history
…rity (#1338)

# Description

I found some naming confusing, so have renamed things.
These structs relate to Noir not yet having traits, so we need to create
serialisation / deserialisation (serde) methods for any type which you
wish to store as a public state.

Edit: I did a git mv to rename some files, but it doesn't seem to have
tracked that correctly, so unfortunately there's a deletion of a file
and then a creation of a file. Sorry about that.

# Checklist:

- [x] I have reviewed my diff in github, line by line.
- [x] Every change is related to the PR description.
- [x] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to the issue(s) that it resolves.
- [x] There are no unexpected formatting changes, superfluous debug
logs, or commented-out code.
- [x] The branch has been merged or rebased against the head of its
merge target.
- [x] I'm happy for the PR to be merged at the reviewer's next
convenience.
  • Loading branch information
iAmMichaelConnor authored Aug 1, 2023
1 parent bb86205 commit cb2d210
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use dep::aztec::state_vars::public_state::PublicState;
use dep::aztec::state_vars::public_state_value::field_value::FieldValueInterface;
use dep::aztec::state_vars::public_state_value::field_value::FIELD_VALUE_LEN;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FieldSerialisationInterface;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FIELD_SERIALISED_LEN;

struct Storage {
current_value: PublicState<Field, FIELD_VALUE_LEN>,
current_value: PublicState<Field, FIELD_SERIALISED_LEN>,
}

impl Storage {
fn init() -> Self {
Storage {
current_value: PublicState::new(1, FieldValueInterface),
current_value: PublicState::new(1, FieldSerialisationInterface),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use dep::aztec::state_vars::map::Map;
use dep::aztec::state_vars::public_state::PublicState;
use dep::aztec::state_vars::public_state_value::PublicStateValueInterface;
use dep::aztec::state_vars::public_state_value::field_value::FieldValueInterface;
use dep::aztec::state_vars::type_serialisation_interface::TypeSerialisationInterface;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FieldSerialisationInterface;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FIELD_SERIALISED_LEN;
use dep::std::hash::pedersen;

// Utility struct used to easily get a "id" for a private user that sits in the same
Expand Down Expand Up @@ -29,7 +30,7 @@ impl Account {
}


global TOT_SIZE: Field = 2;
global TOT_SERIALISED_LEN: Field = 2;

// Struct to be used to represent "totals". Generally, there should be one per asset.
// It stores the global values that are shared among all users, such as an accumulator
Expand All @@ -41,61 +42,61 @@ struct Tot {
last_updated_ts: u120,
}

fn deserialiseTot(fields: [Field; TOT_SIZE]) -> Tot {
fn deserialiseTot(fields: [Field; TOT_SERIALISED_LEN]) -> Tot {
Tot {
interest_accumulator: fields[0] as u120,
last_updated_ts: fields[1] as u120,
}
}

fn serialiseTot(tot: Tot) -> [Field; TOT_SIZE] {
fn serialiseTot(tot: Tot) -> [Field; TOT_SERIALISED_LEN] {
[tot.interest_accumulator as Field, tot.last_updated_ts as Field]
}

global TotInterface = PublicStateValueInterface {
global TotSerialisationInterface = TypeSerialisationInterface {
deserialise: deserialiseTot,
serialise: serialiseTot,
};


// Struct to be used to represent positions when we have more reads.
global POS_SIZE: Field = 2;
global POS_SERIALISED_LEN: Field = 2;

struct Pos {
owner: Field,
value: Field,
}

fn deserialisePos(fields: [Field; POS_SIZE]) -> Pos {
fn deserialisePos(fields: [Field; POS_SERIALISED_LEN]) -> Pos {
Pos {
owner: fields[0],
value: fields[1],
}
}

fn serialisePos(pos: Pos) -> [Field; POS_SIZE] {
fn serialisePos(pos: Pos) -> [Field; POS_SERIALISED_LEN] {
[pos.owner, pos.value]
}

global PosInterface = PublicStateValueInterface {
global PosSerialisationInterface = TypeSerialisationInterface {
deserialise: deserialisePos,
serialise: serialisePos,
};


// Storage structure, containing all storage, and specifying what slots they use.
struct Storage {
assets: Map<PublicState<Tot, TOT_SIZE>>,
collateral: Map<PublicState<Field, 1>>,
static_debt: Map<PublicState<Field, 1>>, // abusing keys very heavily
assets: Map<PublicState<Tot, TOT_SERIALISED_LEN>>,
collateral: Map<PublicState<Field, FIELD_SERIALISED_LEN>>,
static_debt: Map<PublicState<Field, FIELD_SERIALISED_LEN>>, // abusing keys very heavily
}

impl Storage {
fn init() -> Self {
Storage {
assets: Map::new(1, |s| PublicState::new(s, TotInterface)), // uses 2 storage slots.
collateral: Map::new(2, |s| PublicState::new(s, FieldValueInterface)), // uses 1 storage slots.
static_debt: Map::new(3, |s| PublicState::new(s, FieldValueInterface)), // uses 1 storage slots.
assets: Map::new(1, |slot| PublicState::new(slot, TotSerialisationInterface)), // uses 2 storage slots.
collateral: Map::new(2, |slot| PublicState::new(slot, FieldSerialisationInterface)), // uses 1 storage slots.
static_debt: Map::new(3, |slot| PublicState::new(slot, FieldSerialisationInterface)), // uses 1 storage slots.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use dep::aztec::state_vars::{
map::Map,
set::Set,
public_state::PublicState,
public_state_value::field_value::{
FIELD_VALUE_LEN,
FieldValueInterface,
type_serialisation_interface::field_serialisation_interface::{
FIELD_SERIALISED_LEN,
FieldSerialisationInterface,
},
};

Expand All @@ -17,14 +17,14 @@ use dep::value_note::value_note::{
struct Storage {
balances: Map<Set<ValueNote, VALUE_NOTE_LEN>>,

public_balances: Map<PublicState<Field, FIELD_VALUE_LEN>>,
public_balances: Map<PublicState<Field, FIELD_SERIALISED_LEN>>,
}

impl Storage {
fn init() -> Self {
Storage {
balances: Map::new(1, |s| Set::new(s, ValueNoteInterface)),
public_balances: Map::new(2, |s| PublicState::new(s, FieldValueInterface)),
balances: Map::new(1, |slot| Set::new(slot, ValueNoteInterface)),
public_balances: Map::new(2, |slot| PublicState::new(slot, FieldSerialisationInterface)),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use dep::aztec::state_vars::map::Map;
use dep::aztec::state_vars::public_state::PublicState;
use dep::aztec::state_vars::public_state_value::field_value::FieldValueInterface;
use dep::aztec::state_vars::public_state_value::field_value::FIELD_VALUE_LEN;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FieldSerialisationInterface;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FIELD_SERIALISED_LEN;

struct Storage {
balances: Map<PublicState<Field, FIELD_VALUE_LEN>>,
balances: Map<PublicState<Field, FIELD_SERIALISED_LEN>>,
}


impl Storage {
fn init() -> Self {
Storage {
balances: Map::new(1, |s| PublicState::new(s, FieldValueInterface)),
balances: Map::new(1, |slot| PublicState::new(slot, FieldSerialisationInterface)),
}
}
}
2 changes: 1 addition & 1 deletion yarn-project/noir-libs/noir-aztec/src/state_vars.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod immutable_singleton;
mod map;
mod public_state;
mod public_state_value;
mod type_serialisation_interface;
mod set;
mod singleton;
12 changes: 6 additions & 6 deletions yarn-project/noir-libs/noir-aztec/src/state_vars/public_state.nr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use crate::oracle::storage::storage_read;
use crate::oracle::storage::storage_write;
use crate::state_vars::public_state_value::PublicStateValueInterface;
use crate::state_vars::type_serialisation_interface::TypeSerialisationInterface;

struct PublicState<T, N> {
storage_slot: Field,
value_interface: PublicStateValueInterface<T, N>,
serialisation_interface: TypeSerialisationInterface<T, N>,
}

impl<T, N> PublicState<T, N> {
fn new(storage_slot: Field, value_interface: PublicStateValueInterface<T, N>) -> Self {
PublicState { storage_slot, value_interface }
fn new(storage_slot: Field, serialisation_interface: TypeSerialisationInterface<T, N>) -> Self {
PublicState { storage_slot, serialisation_interface }
}

fn read(self) -> T {
storage_read(self.storage_slot, self.value_interface.deserialise)
storage_read(self.storage_slot, self.serialisation_interface.deserialise)
}

fn write(self, value: T) {
let serialise = self.value_interface.serialise;
let serialise = self.serialisation_interface.serialise;
let fields = serialise(value);
storage_write(self.storage_slot, fields);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod field_serialisation_interface;

/**
* Before Noir supports traits, a way of specifying the serialisation and deserialisation methods for a type.
*/
struct TypeSerialisationInterface<T, N> {
deserialise: fn ([Field; N]) -> T,
serialise: fn (T) -> [Field; N],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::state_vars::type_serialisation_interface::TypeSerialisationInterface;

global FIELD_SERIALISED_LEN: Field = 1;

fn deserialiseField(fields: [Field; FIELD_SERIALISED_LEN]) -> Field {
fields[0]
}

fn serialiseField(value: Field) -> [Field; FIELD_SERIALISED_LEN] {
[value]
}

global FieldSerialisationInterface = TypeSerialisationInterface {
deserialise: deserialiseField,
serialise: serialiseField,
};

0 comments on commit cb2d210

Please sign in to comment.