Skip to content
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

fix: use simulation to estimate gas used (WIP) #11211

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion yarn-project/archiver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"lodash.omit": "^4.5.0",
"tsc-watch": "^6.0.0",
"tslib": "^2.5.0",
"viem": "^2.7.15",
"viem": "2.22.8",
"ws": "^8.13.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-faucet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"koa": "^2.14.2",
"koa-bodyparser": "^4.4.1",
"koa-router": "^12.0.0",
"viem": "^2.7.15",
"viem": "2.22.8",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"@aztec/types": "workspace:^",
"axios": "^1.7.2",
"tslib": "^2.4.0",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"commander": "^12.1.0",
"koa": "^2.14.2",
"koa-router": "^12.0.0",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"files": [
"dest",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/bb-prover/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"jest-mock-extended": "^3.0.3",
"ts-node": "^10.9.1",
"typescript": "^5.0.4",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"files": [
"dest",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuit-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"lodash.omit": "^4.5.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"files": [
"dest",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"solc": "^0.8.27",
"source-map-support": "^0.5.21",
"tslib": "^2.4.0",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"devDependencies": {
"@aztec/accounts": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"tslib": "^2.4.0",
"typescript": "^5.0.4",
"util": "^0.12.5",
"viem": "^2.7.15",
"viem": "2.22.8",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"zod": "^3.23.8"
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/epoch-cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"get-port": "^7.1.0",
"jest-mock-extended": "^3.0.7",
"tslib": "^2.4.0",
"viem": "^2.7.15",
"viem": "2.22.8",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@viem/anvil": "^0.0.10",
"dotenv": "^16.0.3",
"tslib": "^2.4.0",
"viem": "^2.7.15",
"viem": "2.22.8",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/ethereum/src/contracts/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { type L1ReaderConfig } from '../l1_reader.js';
export class RollupContract {
private readonly rollup: GetContractReturnType<typeof RollupAbi, PublicClient<HttpTransport, Chain>>;

constructor(public readonly client: PublicClient, address: Hex) {
constructor(public readonly client: PublicClient<HttpTransport, Chain>, address: Hex) {
this.rollup = getContract({ address, abi: RollupAbi, client });
}

Expand Down
39 changes: 39 additions & 0 deletions yarn-project/ethereum/src/l1_tx_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import { sleep } from '@aztec/foundation/sleep';
import {
type Account,
type Address,
type BlockOverrides,
type Chain,
type GetTransactionReturnType,
type Hex,
type HttpTransport,
MethodNotFoundRpcError,
MethodNotSupportedRpcError,
type PublicClient,
type TransactionReceipt,
type WalletClient,
Expand Down Expand Up @@ -563,4 +566,40 @@ export class L1TxUtils {

return withBuffer;
}

public async simulateGasUsed(
request: L1TxRequest,
_gasConfig?: L1TxUtilsConfig,
blockOverrides: BlockOverrides<bigint, number> = {},
): Promise<bigint> {
const gasConfig = { ...this.config, ..._gasConfig };
const gasPrice = await this.getGasPrice(gasConfig, false);
try {
const result = await this.publicClient.simulate({
validation: true,
blocks: [
{
blockOverrides,
calls: [
{
from: this.walletClient.account.address,
to: request.to!,
data: request.data,
maxFeePerGas: gasPrice.maxFeePerGas,
maxPriorityFeePerGas: gasPrice.maxPriorityFeePerGas,
},
],
},
],
});
return result[0].calls[0].gasUsed;
} catch (err) {
if (err instanceof MethodNotFoundRpcError || err instanceof MethodNotSupportedRpcError) {
// Node doesn't support simulation, return -1n gas estimate
this.logger?.error('Node does not support simulation API');
return -1n;
}
throw err;
}
}
}
2 changes: 1 addition & 1 deletion yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"supertest": "^6.3.3",
"ts-node": "^10.9.1",
"typescript": "^5.0.4",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"files": [
"dest",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/p2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"ts-node": "^10.9.1",
"typescript": "^5.0.4",
"uint8arrays": "^5.0.3",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"files": [
"dest",
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/p2p/src/mem_pools/attestation_pool/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { makeHeader } from '@aztec/circuits.js/testing';
import { type Secp256k1Signer } from '@aztec/foundation/crypto';
import { Fr } from '@aztec/foundation/fields';

import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { type LocalAccount, generatePrivateKey, privateKeyToAccount } from 'viem/accounts';

/** Generate Account
*
* Create a random signer
* @returns A random viem signer
*/
export const generateAccount = () => {
export const generateAccount = (): LocalAccount => {
const privateKey = generatePrivateKey();
return privateKeyToAccount(privateKey);
};
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/proof-verifier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@aztec/foundation": "workspace:^",
"@aztec/noir-protocol-circuits-types": "workspace:^",
"@aztec/telemetry-client": "workspace:^",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/prover-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@aztec/world-state": "workspace:^",
"source-map-support": "^0.5.21",
"tslib": "^2.4.0",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/prover-node/src/bond/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
type GetContractReturnType,
type HttpTransport,
type PrivateKeyAccount,
type PublicActions,
type PublicClient,
type PublicActions, // type PublicClient,
type PublicRpcSchema,
type WalletActions,
type WalletClient,
type WalletRpcSchema,
} from 'viem';

Expand All @@ -21,7 +21,7 @@ import { EscrowContract } from './escrow-contract.js';
import { TokenContract } from './token-contract.js';

export async function createBondManager(
rollupContract: GetContractReturnType<typeof RollupAbi, PublicClient>,
rollupContract: GetContractReturnType<typeof RollupAbi, WalletClient<HttpTransport, Chain, PrivateKeyAccount>>,
client: Client<
HttpTransport,
Chain,
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/pxe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"lodash.omit": "^4.5.0",
"sha3": "^2.1.4",
"tslib": "^2.4.0",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"devDependencies": {
"@aztec/noir-contracts.js": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/sequencer-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"lodash.chunk": "^4.2.0",
"lodash.pick": "^4.4.0",
"tslib": "^2.4.0",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"devDependencies": {
"@aztec/archiver": "workspace:^",
Expand Down
72 changes: 66 additions & 6 deletions yarn-project/sequencer-client/src/publisher/l1-publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,32 @@ export class L1Publisher {
// we will fail estimation in the case where we are simulating for the
// first ethereum block within our slot (as current time is not in the
// slot yet).
const gasGuesstimate = blobEvaluationGas + L1Publisher.PROPOSE_GAS_GUESS;
// const gasGuesstimate = blobEvaluationGas + L1Publisher.PROPOSE_GAS_GUESS;

// const timestamp = BigInt((await this.publicClient.getBlock()).timestamp + this.ethereumSlotDuration);

// const simulationResult = await this.publicClient.simulate({
// validation: true,
// blocks: [
// {
// blockOverrides: {
// time: timestamp,
// },
// calls: [
// {
// from: this.account.address,
// to: this.rollupContract.address,
// data: encodeFunctionData({
// abi: this.rollupContract.abi,
// functionName: 'validateBlobs',
// args: [Blob.getEthBlobEvaluationInputs(encodedData.blobs)],
// }),
// },
// ],
// },
// ],
// });

const attestations = encodedData.attestations
? encodedData.attestations.map(attest => attest.toViemSignature())
: [];
Expand All @@ -1007,7 +1032,7 @@ export class L1Publisher {
Blob.getEthBlobEvaluationInputs(encodedData.blobs),
] as const;

return { args, gas: gasGuesstimate };
return { args, blobEvaluationGas };
}

private getSubmitEpochProofArgs(args: {
Expand Down Expand Up @@ -1049,19 +1074,36 @@ export class L1Publisher {
}
try {
const kzg = Blob.getViemKzgInstance();
const { args, gas } = await this.prepareProposeTx(encodedData);
const { args, blobEvaluationGas } = await this.prepareProposeTx(encodedData);
const data = encodeFunctionData({
abi: this.rollupContract.abi,
functionName: 'propose',
args,
});
const time = BigInt((await this.publicClient.getBlock()).timestamp + this.ethereumSlotDuration);
let simulationResult = await this.l1TxUtils.simulateGasUsed(
{
to: this.rollupContract.address,
data,
},
undefined,
{
time,
},
);

if (simulationResult === -1n) {
this.log.warn('Node does not support simulation API. Using gas guesstimate');
simulationResult = L1Publisher.PROPOSE_GAS_GUESS;
}

const result = await this.l1TxUtils.sendAndMonitorTransaction(
{
to: this.rollupContract.address,
data,
},
{
fixedGas: gas,
fixedGas: simulationResult + blobEvaluationGas,
...opts,
},
{
Expand Down Expand Up @@ -1092,19 +1134,37 @@ export class L1Publisher {
}
try {
const kzg = Blob.getViemKzgInstance();
const { args, gas } = await this.prepareProposeTx(encodedData);
const { args, blobEvaluationGas } = await this.prepareProposeTx(encodedData);
const data = encodeFunctionData({
abi: this.rollupContract.abi,
functionName: 'proposeAndClaim',
args: [...args, quote.toViemArgs()],
});
const time = BigInt((await this.publicClient.getBlock()).timestamp + this.ethereumSlotDuration);

let simulationResult = await this.l1TxUtils.simulateGasUsed(
{
to: this.rollupContract.address,
data,
},
undefined,
{
time,
},
);

if (simulationResult === -1n) {
this.log.warn('Node does not support simulation API. Using gas guesstimate');
simulationResult = L1Publisher.PROPOSE_GAS_GUESS;
}

const result = await this.l1TxUtils.sendAndMonitorTransaction(
{
to: this.rollupContract.address,
data,
},
{
fixedGas: gas,
fixedGas: simulationResult + blobEvaluationGas,
...opts,
},
{
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"lodash.merge": "^4.6.2",
"ts-node": "^10.9.1",
"typescript": "^5.0.4",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"files": [
"dest",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/validator-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"koa": "^2.14.2",
"koa-router": "^12.0.0",
"tslib": "^2.4.0",
"viem": "^2.7.15"
"viem": "2.22.8"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
Expand Down
Loading
Loading