-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(avm): Set gas allowance in public calls
- Loading branch information
1 parent
255ca72
commit 0e28cdc
Showing
13 changed files
with
188 additions
and
61 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
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,15 @@ | ||
struct GasOpts { | ||
l1_gas: Option<Field>, | ||
l2_gas: Option<Field>, | ||
da_gas: Option<Field>, | ||
} | ||
|
||
impl GasOpts { | ||
fn empty() -> Self { | ||
GasOpts { l1_gas: Option::none(), l2_gas: Option::none(), da_gas: Option::none() } | ||
} | ||
|
||
fn new(l1_gas: Field, l2_gas: Field, da_gas: Field) -> Self { | ||
GasOpts { l1_gas: Option::some(l1_gas), l2_gas: Option::some(l2_gas), da_gas: Option::some(da_gas) } | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
yarn-project/simulator/src/avm/opcodes/context_getters.test.ts
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,32 @@ | ||
import { initContext, initMachineState } from '../fixtures/index.js'; | ||
import { DAGasLeft, L1GasLeft, L2GasLeft } from './context_getters.js'; | ||
|
||
describe.each([ | ||
[L1GasLeft, 'l1GasLeft'], | ||
[L2GasLeft, 'l2GasLeft'], | ||
[DAGasLeft, 'daGasLeft'], | ||
] as const)('Context getter instructions for machine state', (clsValue, key) => { | ||
it(`${clsValue.name} should (de)serialize correctly`, () => { | ||
const buf = Buffer.from([ | ||
clsValue.opcode, // opcode | ||
0x01, // indirect | ||
...Buffer.from('12345678', 'hex'), // dstOffset | ||
]); | ||
const inst = new clsValue(/*indirect=*/ 0x01, /*dstOffset=*/ 0x12345678); | ||
|
||
expect(clsValue.deserialize(buf)).toEqual(inst); | ||
expect(inst.serialize()).toEqual(buf); | ||
}); | ||
|
||
it(`${clsValue.name} should read '${key}' correctly`, async () => { | ||
const value = 123456; | ||
const instruction = new clsValue(/*indirect=*/ 0, /*dstOffset=*/ 0); | ||
const context = initContext({ machineState: initMachineState({ [key]: value }) }); | ||
|
||
await instruction.execute(context); | ||
|
||
const actual = context.machineState.memory.get(0).toNumber(); | ||
const expected = key === 'l2GasLeft' ? value - 110 : value; // l2gascost decreases when it's executed | ||
expect(actual).toEqual(expected); | ||
}); | ||
}); |
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,31 @@ | ||
import type { AvmContext } from '../avm_context.js'; | ||
import { type MemoryValue, Uint32 } from '../avm_memory_types.js'; | ||
import { Opcode } from '../serialization/instruction_serialization.js'; | ||
import { GetterInstruction } from './instruction_impl.js'; | ||
|
||
export class L2GasLeft extends GetterInstruction { | ||
static type: string = 'L2GASLEFT'; | ||
static readonly opcode: Opcode = Opcode.L2GASLEFT; | ||
|
||
protected getValue(context: AvmContext): MemoryValue { | ||
return new Uint32(context.machineState.l2GasLeft); | ||
} | ||
} | ||
|
||
export class L1GasLeft extends GetterInstruction { | ||
static type: string = 'L1GASLEFT'; | ||
static readonly opcode: Opcode = Opcode.L1GASLEFT; | ||
|
||
protected getValue(context: AvmContext): MemoryValue { | ||
return new Uint32(context.machineState.l1GasLeft); | ||
} | ||
} | ||
|
||
export class DAGasLeft extends GetterInstruction { | ||
static type: string = 'DAGASLEFT'; | ||
static readonly opcode: Opcode = Opcode.DAGASLEFT; | ||
|
||
protected getValue(context: AvmContext): MemoryValue { | ||
return new Uint32(context.machineState.daGasLeft); | ||
} | ||
} |
Oops, something went wrong.