Skip to content

Commit

Permalink
refactor: timestamp as u64 instead of a Field (#5453)
Browse files Browse the repository at this point in the history
Fixes #5446
  • Loading branch information
benesjan committed Mar 26, 2024
1 parent 753cb78 commit d80dbbf
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ library Constants {
uint256 internal constant DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631;
uint256 internal constant DEPLOYER_CONTRACT_ADDRESS =
0x157b38eb13f082bd359153447e0b6a3e9af803585ec5cb83903faa04415d5d2e;
0x1ad693effc2a4a40fdf5911ff29e19d2b6cd3cf73e6c3685519a6fb783144dcb;
uint256 internal constant L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH = 17;
uint256 internal constant MAX_NOTE_FIELDS_LENGTH = 20;
uint256 internal constant GET_NOTE_ORACLE_RETURN_LENGTH = 23;
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ global REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_MAGIC_VALUE = 0xe7af8166354
// CONTRACT INSTANCE CONSTANTS
// sha224sum 'struct ContractInstanceDeployed'
global DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE = 0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631;
global DEPLOYER_CONTRACT_ADDRESS = 0x157b38eb13f082bd359153447e0b6a3e9af803585ec5cb83903faa04415d5d2e;
global DEPLOYER_CONTRACT_ADDRESS = 0x1ad693effc2a4a40fdf5911ff29e19d2b6cd3cf73e6c3685519a6fb783144dcb;

// NOIR CONSTANTS - constants used only in yarn-packages/noir-contracts
// Some are defined here because Noir doesn't yet support globals referencing other globals yet.
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_MAGIC_VALUE =
0xe7af816635466f128568edb04c9fa024f6c87fb9010fdbffa68b3d99n;
export const DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631n;
export const DEPLOYER_CONTRACT_ADDRESS = 0x157b38eb13f082bd359153447e0b6a3e9af803585ec5cb83903faa04415d5d2en;
export const DEPLOYER_CONTRACT_ADDRESS = 0x1ad693effc2a4a40fdf5911ff29e19d2b6cd3cf73e6c3685519a6fb783144dcbn;
export const L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH = 17;
export const MAX_NOTE_FIELDS_LENGTH = 20;
export const GET_NOTE_ORACLE_RETURN_LENGTH = 23;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('ArtifactHash', () => {
it('calculates the artifact hash', () => {
const artifact = getBenchmarkContractArtifact();
expect(computeArtifactHash(artifact).toString()).toMatchInlineSnapshot(
`"0x01016cb361c5694f739c0fc6143dd871ea2a8cd7a4ee69b090eac319ec2f18ac"`,
`"0x1c4308cf4acda970916ac5dd5ae32106bd782b17d2da4f50090ff5b77032ad02"`,
);
});
});

0 comments on commit d80dbbf

Please sign in to comment.