-
Notifications
You must be signed in to change notification settings - Fork 265
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
Constrain timestamp in global variables #948
Changes from 11 commits
0c75c6a
05f5a18
00e80de
c5fcdf8
0c90689
f5f5077
8065cb7
0726370
a1b2f79
03d6b08
058b86d
fffc126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -267,12 +267,11 @@ describe('L1Publisher integration', () => { | |
await makeBloatedProcessedTx(128 * i + 96), | ||
await makeBloatedProcessedTx(128 * i + 128), | ||
]; | ||
// @todo @LHerskind fix time. | ||
const globalVariables = new GlobalVariables( | ||
new Fr(config.chainId), | ||
new Fr(config.version), | ||
new Fr(1 + i), | ||
Fr.ZERO, | ||
new Fr(await rollup.read.lastBlockTs()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we want the ts of the last block here? Do we not want the current timestamp? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ts of the last block is the minimum value that will work. With the current timestamp, you have the issue of dealing with anvil not progressing time as "outside" so this was just an easy way to pick a meaningful value. |
||
); | ||
const [block] = await builder.buildL2Block(globalVariables, txs, l1ToL2Messages); | ||
|
||
|
@@ -363,7 +362,7 @@ describe('L1Publisher integration', () => { | |
new Fr(config.chainId), | ||
new Fr(config.version), | ||
new Fr(1 + i), | ||
Fr.ZERO, | ||
new Fr(await rollup.read.lastBlockTs()), | ||
); | ||
const [block] = await builder.buildL2Block(globalVariables, txs, l1ToL2Messages); | ||
|
||
|
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { EthAddress } from '@aztec/circuits.js'; | ||
|
||
/** | ||
* Configuration of the L1GlobalReader. | ||
*/ | ||
export interface GlobalReaderConfig { | ||
/** | ||
* Rollup contract address. | ||
*/ | ||
rollupContract: EthAddress; | ||
/** | ||
* The RPC Url of the ethereum host. | ||
*/ | ||
rpcUrl: string; | ||
/** | ||
* The API key of the ethereum host. | ||
*/ | ||
apiKey?: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this on sequencer config already? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, so it is just to not require the entire config to be passed to the builder. If you look at the other configs, you will see similar with values that are "shared" between them, so the full config makes up the union of them. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Fr, GlobalVariables } from '@aztec/circuits.js'; | ||
import { createDebugLogger } from '@aztec/foundation/log'; | ||
|
||
/** | ||
* Reads values from L1 state that is used for the global values. | ||
*/ | ||
export interface L1GlobalReader { | ||
getLastTimestamp(): Promise<bigint>; | ||
getVersion(): Promise<bigint>; | ||
getChainId(): Promise<bigint>; | ||
} | ||
|
||
/** | ||
* Builds global variables from L1 state. | ||
*/ | ||
export interface GlobalVariableBuilder { | ||
buildGlobalVariables(blockNumber: Fr): Promise<GlobalVariables>; | ||
} | ||
|
||
/** | ||
* Simple implementation of a builder that uses the minimum time possible for the global variables. | ||
*/ | ||
export class SimpleGlobalVariableBuilder implements GlobalVariableBuilder { | ||
private log = createDebugLogger('aztec:sequencer:simple_global_variable_builder'); | ||
constructor(private readonly reader: L1GlobalReader) {} | ||
|
||
/** | ||
* Simple builder of global variables that use the minimum time possible. | ||
* @param blockNumber - The block number to build global variables for. | ||
* @returns The global variables for the given block number. | ||
*/ | ||
public async buildGlobalVariables(blockNumber: Fr): Promise<GlobalVariables> { | ||
const lastTimestamp = new Fr(await this.reader.getLastTimestamp()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ibid There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above, also hinted in the comments above the function. |
||
const version = new Fr(await this.reader.getVersion()); | ||
const chainId = new Fr(await this.reader.getChainId()); | ||
|
||
this.log( | ||
`Built global variables for block ${blockNumber}: (${chainId}, ${version}, ${blockNumber}, ${lastTimestamp})`, | ||
); | ||
|
||
return new GlobalVariables(chainId, version, blockNumber, lastTimestamp); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { GlobalReaderConfig } from './config.js'; | ||
import { GlobalVariableBuilder, SimpleGlobalVariableBuilder } from './global_builder.js'; | ||
import { ViemReader } from './viem-reader.js'; | ||
|
||
export { SimpleGlobalVariableBuilder } from './global_builder.js'; | ||
export { GlobalReaderConfig } from './config.js'; | ||
|
||
/** | ||
* Returns a new instance of the global variable builder. | ||
* @param config - Configuration to initialize the builder. | ||
* @returns A new instance of the global variable builder. | ||
*/ | ||
export function getGlobalVariableBuilder(config: GlobalReaderConfig): GlobalVariableBuilder { | ||
return new SimpleGlobalVariableBuilder(new ViemReader(config)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { createEthereumChain } from '@aztec/ethereum'; | ||
import { RollupAbi } from '@aztec/l1-artifacts'; | ||
import { | ||
GetContractReturnType, | ||
PublicClient, | ||
HttpTransport, | ||
createPublicClient, | ||
http, | ||
getContract, | ||
getAddress, | ||
} from 'viem'; | ||
import * as chains from 'viem/chains'; | ||
import { GlobalReaderConfig } from './config.js'; | ||
import { L1GlobalReader } from './global_builder.js'; | ||
|
||
/** | ||
* Reads values from L1 state using viem. | ||
*/ | ||
export class ViemReader implements L1GlobalReader { | ||
private rollupContract: GetContractReturnType<typeof RollupAbi, PublicClient<HttpTransport, chains.Chain>>; | ||
private publicClient: PublicClient<HttpTransport, chains.Chain>; | ||
|
||
constructor(config: GlobalReaderConfig) { | ||
const { rpcUrl, apiKey, rollupContract: rollupContractAddress } = config; | ||
|
||
const chain = createEthereumChain(rpcUrl, apiKey); | ||
|
||
this.publicClient = createPublicClient({ | ||
chain: chain.chainInfo, | ||
transport: http(chain.rpcUrl), | ||
}); | ||
|
||
this.rollupContract = getContract({ | ||
address: getAddress(rollupContractAddress.toString()), | ||
abi: RollupAbi, | ||
publicClient: this.publicClient, | ||
}); | ||
} | ||
|
||
/** | ||
* Fetches the last timestamp that a block was processed by the contract. | ||
* @returns The last timestamp that a block was processed by the contract. | ||
*/ | ||
public async getLastTimestamp(): Promise<bigint> { | ||
return BigInt(await this.rollupContract.read.lastBlockTs()); | ||
} | ||
|
||
/** | ||
* Fetches the version of the rollup contract. | ||
* @returns The version of the rollup contract. | ||
*/ | ||
public async getVersion(): Promise<bigint> { | ||
return BigInt(await this.rollupContract.read.VERSION()); | ||
} | ||
|
||
/** | ||
* Gets the chain id. | ||
* @returns The chain id. | ||
*/ | ||
public async getChainId(): Promise<bigint> { | ||
return await Promise.resolve(BigInt(this.publicClient.chain.id)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a relevant issue for us to come back to this later or is the plan to hash this out now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have not created an issue for it. Will add it to #824.