-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Return gas usage per phase from node tx simulation (#6255)
Allows clients set their total and teardown gas limits accordingly before submitting a tx to the network.
- Loading branch information
1 parent
ba618d5
commit fb58dfc
Showing
17 changed files
with
242 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,59 @@ | ||
import { Gas } from '@aztec/circuits.js'; | ||
|
||
import { mockSimulatedTx } from '../mocks.js'; | ||
import { PublicKernelType } from './processed_tx.js'; | ||
import { SimulatedTx } from './simulated_tx.js'; | ||
|
||
describe('simulated_tx', () => { | ||
it('convert to and from json', () => { | ||
const simulatedTx = mockSimulatedTx(); | ||
expect(SimulatedTx.fromJSON(simulatedTx.toJSON())).toEqual(simulatedTx); | ||
let simulatedTx: SimulatedTx; | ||
|
||
beforeEach(() => { | ||
simulatedTx = mockSimulatedTx(); | ||
}); | ||
|
||
describe('json', () => { | ||
it('convert to and from json', () => { | ||
expect(SimulatedTx.fromJSON(simulatedTx.toJSON())).toEqual(simulatedTx); | ||
}); | ||
|
||
it('convert undefined effects to and from json', () => { | ||
simulatedTx.privateReturnValues = undefined; | ||
simulatedTx.publicOutput = undefined; | ||
expect(SimulatedTx.fromJSON(simulatedTx.toJSON())).toEqual(simulatedTx); | ||
}); | ||
}); | ||
|
||
it('convert undefined effects to and from json', () => { | ||
const simulatedTx = mockSimulatedTx(); | ||
simulatedTx.privateReturnValues = undefined; | ||
simulatedTx.publicOutput = undefined; | ||
expect(SimulatedTx.fromJSON(simulatedTx.toJSON())).toEqual(simulatedTx); | ||
describe('getGasLimits', () => { | ||
beforeEach(() => { | ||
simulatedTx.tx.data.publicInputs.end.gasUsed = Gas.from({ daGas: 100, l2Gas: 200 }); | ||
simulatedTx.publicOutput!.gasUsed = { | ||
[PublicKernelType.SETUP]: Gas.from({ daGas: 10, l2Gas: 20 }), | ||
[PublicKernelType.APP_LOGIC]: Gas.from({ daGas: 20, l2Gas: 40 }), | ||
[PublicKernelType.TEARDOWN]: Gas.from({ daGas: 10, l2Gas: 20 }), | ||
}; | ||
}); | ||
|
||
it('returns gas limits from private gas usage only', () => { | ||
simulatedTx.publicOutput = undefined; | ||
// Should be 110 and 220 but oh floating point | ||
expect(simulatedTx.getGasLimits()).toEqual({ | ||
totalGas: Gas.from({ daGas: 111, l2Gas: 221 }), | ||
teardownGas: Gas.empty(), | ||
}); | ||
}); | ||
|
||
it('returns gas limits for private and public', () => { | ||
expect(simulatedTx.getGasLimits()).toEqual({ | ||
totalGas: Gas.from({ daGas: 154, l2Gas: 308 }), | ||
teardownGas: Gas.from({ daGas: 11, l2Gas: 22 }), | ||
}); | ||
}); | ||
|
||
it('pads gas limits', () => { | ||
expect(simulatedTx.getGasLimits(1)).toEqual({ | ||
totalGas: Gas.from({ daGas: 280, l2Gas: 560 }), | ||
teardownGas: Gas.from({ daGas: 20, l2Gas: 40 }), | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './array.js'; | ||
export * from './object.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { mapValues } from './object.js'; | ||
|
||
describe('mapValues', () => { | ||
it('should return a new object with mapped values', () => { | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
const fn = (value: number) => value * 2; | ||
|
||
const result = mapValues(obj, fn); | ||
|
||
expect(result).toEqual({ a: 2, b: 4, c: 6 }); | ||
}); | ||
|
||
it('should handle an empty object', () => { | ||
const obj = {}; | ||
const fn = (value: number) => value * 2; | ||
|
||
const result = mapValues(obj, fn); | ||
|
||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should handle different value types', () => { | ||
const obj = { a: 'hello', b: true, c: [1, 2, 3] }; | ||
const fn = (value: any) => typeof value; | ||
|
||
const result = mapValues(obj, fn); | ||
|
||
expect(result).toEqual({ a: 'string', b: 'boolean', c: 'object' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** Returns a new object with the same keys and where each value has been passed through the mapping function. */ | ||
export function mapValues<K extends string | number | symbol, T, U>( | ||
obj: Record<K, T>, | ||
fn: (value: T) => U, | ||
): Record<K, U>; | ||
export function mapValues<K extends string | number | symbol, T, U>( | ||
obj: Partial<Record<K, T>>, | ||
fn: (value: T) => U, | ||
): Partial<Record<K, U>>; | ||
export function mapValues<K extends string | number | symbol, T, U>( | ||
obj: Record<K, T>, | ||
fn: (value: T) => U, | ||
): Record<K, U> { | ||
const result: Record<K, U> = {} as Record<K, U>; | ||
for (const key in obj) { | ||
result[key] = fn(obj[key]); | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.