Skip to content

Commit

Permalink
refactor: timestamp as u64 instead of a Field
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Mar 26, 2024
1 parent 73d05f0 commit 938290c
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/context/avm_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl PublicContextInterface for AVMContext {
block_number()
}

fn timestamp(self) -> Field {
fn timestamp(self) -> u64 {
timestamp()
}

Expand Down Expand Up @@ -238,7 +238,7 @@ fn version() -> Field {}
fn block_number() -> Field {}

#[oracle(avmOpcodeTimestamp)]
fn timestamp() -> Field {}
fn timestamp() -> u64 {}

#[oracle(avmOpcodeNoteHashExists)]
fn note_hash_exists(note_hash: Field, leaf_index: Field) -> u8 {}
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/context/interface.nr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait ContextInterface {
// If you are adding something here, then you should also implement it in the AVMContext.
trait PublicContextInterface {
fn block_number(self) -> Field;
fn timestamp(self) -> Field;
fn timestamp(self) -> u64;
fn coinbase(self) -> EthAddress;
fn fee_recipient(self) -> AztecAddress;
fn push_nullifier_read_request(&mut self, nullifier: Field);
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/context/public_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl PublicContextInterface for PublicContext {
self.inputs.public_global_variables.block_number
}

fn timestamp(self) -> Field {
fn timestamp(self) -> u64 {
self.inputs.public_global_variables.timestamp
}

Expand Down
8 changes: 4 additions & 4 deletions noir-projects/aztec-nr/slow-updates-tree/src/slow_map.nr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<N,M> SlowMap<N,M> {

// Reads the "CURRENT" value of the root
pub fn current_root(self: Self) -> Field {
let time = self.context.public.unwrap().timestamp() as u64;
let time = self.context.public.unwrap().timestamp();
let root_object = self.read_root();
if time <= root_object.next_change as u64 {
root_object.before
Expand All @@ -66,7 +66,7 @@ impl<N,M> SlowMap<N,M> {
// docs:start:read_at
// Reads the "CURRENT" value of the leaf
pub fn read_at(self: Self, key: Field) -> Field {
let time = self.context.public.unwrap().timestamp() as u64;
let time = self.context.public.unwrap().timestamp();
let leaf = self.read_leaf_at(key);
if time <= leaf.next_change as u64 {
leaf.before
Expand All @@ -87,7 +87,7 @@ impl<N,M> SlowMap<N,M> {
// The calling function should ensure that the index is within the tree.
// This must be done separately to ensure we are not constraining too tight here.

let time = self.context.public.unwrap().timestamp() as u64;
let time = self.context.public.unwrap().timestamp();
let next_change = compute_next_change(time as Field);

let mut root = self.read_root();
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<N,M> SlowMap<N,M> {
// in public, limiting the cost of the update.
pub fn update_unsafe_at(self: Self, index: Field, leaf_value: Field, new_root: Field) {
// User must ensure that the checks from update_at is performed for safety
let time = self.context.public.unwrap().timestamp() as u64;
let time = self.context.public.unwrap().timestamp();
let next_change = compute_next_change(time as Field);

let mut root = self.read_root();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ contract AvmTest {
}

#[aztec(public-vm)]
fn get_timestamp() -> pub Field {
fn get_timestamp() -> pub u64 {
context.timestamp()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ contract Child {
+ context.chain_id()
+ context.version()
+ context.block_number()
+ context.timestamp();
+ context.timestamp() as Field;

return_value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ contract Crowdfunding {
#[aztec(internal)]
fn _check_deadline() {
let deadline = storage.deadline.read();
assert(context.timestamp() as u64 < deadline, "Deadline has passed");
assert(context.timestamp() < deadline, "Deadline has passed");
}

#[aztec(private)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ contract Lending {
assert(asset.last_updated_ts == 0);
assert(asset.interest_accumulator == U128::from_integer(0));

let last_updated_ts = context.timestamp() as u64;
let last_updated_ts = context.timestamp();

asset_loc.write(
Asset { interest_accumulator: U128::from_integer(1000000000), last_updated_ts, loan_to_value, oracle }
Expand All @@ -72,7 +72,7 @@ contract Lending {
let asset_loc = storage.assets.at(0);
let mut asset = asset_loc.read();

let timestamp = context.timestamp() as u64;
let timestamp = context.timestamp();
let dt = timestamp - asset.last_updated_ts;

// Only update if time has passed.
Expand All @@ -84,7 +84,7 @@ contract Lending {

// accumulator *= multiplier, and multiplier >= 1
asset.interest_accumulator = (asset.interest_accumulator * multiplier) / precision;
asset.last_updated_ts = context.timestamp() as u64;
asset.last_updated_ts = context.timestamp();

asset_loc.write(asset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ contract Test {

// docs:start:is-time-equal
#[aztec(public)]
fn is_time_equal(time: Field) -> Field {
fn is_time_equal(time: u64) -> u64 {
assert(context.timestamp() == time);
time
}
Expand Down Expand Up @@ -334,7 +334,7 @@ contract Test {
chain_id: Field,
version: Field,
block_number: Field,
timestamp: Field,
timestamp: u64,
coinbase: EthAddress,
fee_recipient: AztecAddress
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct GlobalVariables {
chain_id : Field,
version : Field,
block_number : Field,
timestamp : Field,
timestamp : u64,
coinbase : EthAddress,
fee_recipient : AztecAddress,
}
Expand All @@ -22,7 +22,7 @@ impl Serialize<GLOBAL_VARIABLES_LENGTH> for GlobalVariables {
self.chain_id,
self.version,
self.block_number,
self.timestamp,
self.timestamp as Field,
self.coinbase.to_field(),
self.fee_recipient.to_field(),
]
Expand All @@ -35,7 +35,7 @@ impl Deserialize<GLOBAL_VARIABLES_LENGTH> for GlobalVariables {
chain_id: serialized[0],
version: serialized[1],
block_number: serialized[2],
timestamp: serialized[3],
timestamp: serialized[3] as u64,
coinbase: EthAddress::from_field(serialized[4]),
fee_recipient: AztecAddress::from_field(serialized[5]),
}
Expand Down

0 comments on commit 938290c

Please sign in to comment.