Skip to content

Commit

Permalink
fix: Use gas billed in block header building (#12101)
Browse files Browse the repository at this point in the history
Fixes #12089
  • Loading branch information
sirasistant authored Feb 19, 2025
1 parent 0852588 commit e3bc1a5
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions yarn-project/aztec.js/src/contract/get_gas_limits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe('getGasLimits', () => {

txSimulationResult.publicOutput!.gasUsed = {
totalGas: Gas.from({ daGas: 140, l2Gas: 280 }),
// Assume teardown gas limit of 20, 30
billedGas: Gas.from({ daGas: 150, l2Gas: 290 }),
teardownGas: Gas.from({ daGas: 10, l2Gas: 20 }),
publicGas: Gas.from({ daGas: 50, l2Gas: 200 }),
};
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuit-types/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export const mockSimulatedTx = async (seed = 1) => {
totalGas: makeGas(),
teardownGas: makeGas(),
publicGas: makeGas(),
billedGas: makeGas(),
},
);
return new TxSimulationResult(privateExecutionResult, tx.data, output);
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuit-types/src/test/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export async function makeBloatedProcessedTx({
totalGas: Gas.empty(),
teardownGas: Gas.empty(),
publicGas: Gas.empty(),
billedGas: Gas.empty(),
} satisfies GasUsed;

return makeProcessedTxFromTxWithPublicCalls(
Expand Down
9 changes: 7 additions & 2 deletions yarn-project/circuit-types/src/tx/gas_used.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { type Gas } from '@aztec/circuits.js';
export interface GasUsed {
/**
* Total gas used across both private and public executions.
* Note that this does not determine the transaction fee. The fee is calculated using `teardownGasLimits` from
* Note that this does not determine the transaction fee. The fee is calculated with billedGas, which uses `teardownGasLimits` from
* `GasSettings`, rather than actual teardown gas.
*/
totalGas: Gas;

/** Total gas used during public execution, including teardown gas */
/** Total gas used during public execution, including actual teardown gas */
publicGas: Gas;

/**
* The actual gas used in the teardown phase.
*/
teardownGas: Gas;

/**
* The gas billed for the transaction. This uses teardown gas limit instead of actual teardown gas.
*/
billedGas: Gas;
}
2 changes: 2 additions & 0 deletions yarn-project/circuit-types/src/tx/processed_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export async function makeProcessedTxFromPrivateOnlyTx(
);

const gasUsed = {
// Billed gas is the same as total gas since there is no teardown execution
totalGas: tx.data.gasUsed,
billedGas: tx.data.gasUsed,
teardownGas: Gas.empty(),
publicGas: Gas.empty(),
} satisfies GasUsed;
Expand Down
9 changes: 7 additions & 2 deletions yarn-project/circuit-types/src/tx/public_simulation_output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ export class PublicSimulationOutput {
constants: CombinedConstantData.schema,
txEffect: TxEffect.schema,
publicReturnValues: z.array(NestedProcessReturnValues.schema),
gasUsed: z.object({ totalGas: Gas.schema, teardownGas: Gas.schema, publicGas: Gas.schema }),
gasUsed: z.object({
totalGas: Gas.schema,
teardownGas: Gas.schema,
publicGas: Gas.schema,
billedGas: Gas.schema,
}),
})
.transform(
fields =>
Expand All @@ -81,7 +86,7 @@ export class PublicSimulationOutput {
CombinedConstantData.empty(),
TxEffect.empty(),
times(2, NestedProcessReturnValues.random),
{ teardownGas: Gas.random(), totalGas: Gas.random(), publicGas: Gas.random() },
{ teardownGas: Gas.random(), totalGas: Gas.random(), publicGas: Gas.random(), billedGas: Gas.random() },
);
}
}
1 change: 1 addition & 0 deletions yarn-project/circuit-types/src/tx/simulated_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class TxSimulationResult extends PrivateSimulationResult {
return (
this.publicOutput?.gasUsed ?? {
totalGas: this.publicInputs.gasUsed,
billedGas: this.publicInputs.gasUsed,
teardownGas: Gas.empty(),
publicGas: Gas.empty(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ export const buildHeaderAndBodyFromTxs = runInSpan(
const contentCommitment = new ContentCommitment(new Fr(numTxs), blobsHash, parityShaRoot, outHash);

const fees = body.txEffects.reduce((acc, tx) => acc.add(tx.transactionFee), Fr.ZERO);
const manaUsed = txs.reduce((acc, tx) => acc.add(new Fr(tx.gasUsed.totalGas.l2Gas)), Fr.ZERO);
const manaUsed = txs.reduce((acc, tx) => acc.add(new Fr(tx.gasUsed.billedGas.l2Gas)), Fr.ZERO);

const header = new BlockHeader(previousArchive, contentCommitment, stateReference, globalVariables, fees, manaUsed);

Expand Down
1 change: 1 addition & 0 deletions yarn-project/simulator/src/public/public_processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('public_processor', () => {
totalGas: Gas.empty(),
teardownGas: Gas.empty(),
publicGas: Gas.empty(),
billedGas: Gas.empty(),
},
revertCode: RevertCode.OK,
processedPhases: [],
Expand Down
15 changes: 15 additions & 0 deletions yarn-project/simulator/src/public/public_tx_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ describe('public_tx_simulator', () => {
const expectedTotalGas = privateGasUsed.add(expectedPublicGasUsed);
expect(txResult.gasUsed).toEqual({
totalGas: expectedTotalGas,
billedGas: expectedTotalGas,
teardownGas: Gas.empty(),
publicGas: expectedPublicGasUsed,
});
Expand Down Expand Up @@ -369,6 +370,7 @@ describe('public_tx_simulator', () => {
const expectedTotalGas = privateGasUsed.add(expectedPublicGasUsed);
expect(txResult.gasUsed).toEqual({
totalGas: expectedTotalGas,
billedGas: expectedTotalGas,
teardownGas: Gas.empty(),
publicGas: expectedPublicGasUsed,
});
Expand Down Expand Up @@ -403,8 +405,10 @@ describe('public_tx_simulator', () => {

const expectedTeardownGasUsed = enqueuedCallGasUsed;
const expectedTotalGas = privateGasUsed.add(expectedTeardownGasUsed);
const expectedBilledGas = privateGasUsed.add(teardownGasLimits);
expect(txResult.gasUsed).toEqual({
totalGas: expectedTotalGas,
billedGas: expectedBilledGas,
teardownGas: expectedTeardownGasUsed,
publicGas: expectedTeardownGasUsed,
});
Expand Down Expand Up @@ -443,8 +447,11 @@ describe('public_tx_simulator', () => {
const expectedPublicGasUsed = enqueuedCallGasUsed.mul(3); // 2 for setup and 1 for app logic.
const expectedTeardownGasUsed = enqueuedCallGasUsed;
const expectedTotalGas = privateGasUsed.add(expectedPublicGasUsed).add(expectedTeardownGasUsed);
const expectedBilledGas = privateGasUsed.add(expectedPublicGasUsed).add(teardownGasLimits);

expect(txResult.gasUsed).toEqual({
totalGas: expectedTotalGas,
billedGas: expectedBilledGas,
teardownGas: expectedTeardownGasUsed,
publicGas: expectedPublicGasUsed.add(expectedTeardownGasUsed),
});
Expand Down Expand Up @@ -593,8 +600,10 @@ describe('public_tx_simulator', () => {
const expectedAppLogicGas = enqueuedCallGasUsed.mul(2);
const expectedTeardownGasUsed = enqueuedCallGasUsed;
const expectedTotalGas = privateGasUsed.add(expectedSetupGas).add(expectedAppLogicGas).add(expectedTeardownGasUsed);
const expectedBilledGas = privateGasUsed.add(expectedSetupGas).add(expectedAppLogicGas).add(teardownGasLimits);
expect(txResult.gasUsed).toEqual({
totalGas: expectedTotalGas,
billedGas: expectedBilledGas,
teardownGas: expectedTeardownGasUsed,
publicGas: expectedTotalGas.sub(privateGasUsed),
});
Expand Down Expand Up @@ -675,8 +684,10 @@ describe('public_tx_simulator', () => {
const expectedAppLogicGas = enqueuedCallGasUsed.mul(2);
const expectedTeardownGasUsed = enqueuedCallGasUsed;
const expectedTotalGas = privateGasUsed.add(expectedSetupGas).add(expectedAppLogicGas).add(expectedTeardownGasUsed);
const expectedBilledGas = privateGasUsed.add(expectedSetupGas).add(expectedAppLogicGas).add(teardownGasLimits);
expect(txResult.gasUsed).toEqual({
totalGas: expectedTotalGas,
billedGas: expectedBilledGas,
teardownGas: expectedTeardownGasUsed,
publicGas: expectedTotalGas.sub(privateGasUsed),
});
Expand Down Expand Up @@ -759,8 +770,10 @@ describe('public_tx_simulator', () => {
const expectedAppLogicGas = enqueuedCallGasUsed.mul(2);
const expectedTeardownGasUsed = enqueuedCallGasUsed;
const expectedTotalGas = privateGasUsed.add(expectedSetupGas).add(expectedAppLogicGas).add(expectedTeardownGasUsed);
const expectedBilledGas = privateGasUsed.add(expectedSetupGas).add(expectedAppLogicGas).add(teardownGasLimits);
expect(txResult.gasUsed).toEqual({
totalGas: expectedTotalGas,
billedGas: expectedBilledGas,
teardownGas: expectedTeardownGasUsed,
publicGas: expectedTotalGas.sub(privateGasUsed),
});
Expand Down Expand Up @@ -900,8 +913,10 @@ describe('public_tx_simulator', () => {
const expectedPublicGasUsed = enqueuedCallGasUsed.mul(2); // 1 for setup and 1 for app logic.
const expectedTeardownGasUsed = enqueuedCallGasUsed;
const expectedTotalGas = privateGasUsed.add(expectedPublicGasUsed).add(expectedTeardownGasUsed);
const expectedBilledGas = privateGasUsed.add(expectedPublicGasUsed).add(teardownGasLimits);
expect(txResult.gasUsed).toEqual({
totalGas: expectedTotalGas,
billedGas: expectedBilledGas,
teardownGas: expectedTeardownGasUsed,
publicGas: expectedPublicGasUsed.add(expectedTeardownGasUsed),
});
Expand Down
1 change: 1 addition & 0 deletions yarn-project/simulator/src/public/public_tx_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class PublicTxSimulator {
totalGas: context.getActualGasUsed(),
teardownGas: context.teardownGasUsed,
publicGas: context.getActualPublicGasUsed(),
billedGas: context.getTotalGasUsed(),
},
revertCode,
revertReason: context.revertReason,
Expand Down

0 comments on commit e3bc1a5

Please sign in to comment.