Skip to content

Commit

Permalink
fix(avm-simulator): fix env getters (#6357)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed May 13, 2024
1 parent 7ad4179 commit 485fe40
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 101 deletions.
26 changes: 13 additions & 13 deletions docs/docs/protocol-specs/public-vm/gen/_instruction-set.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ Get the address of the currently executing l2 contract
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.address`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/ADDRESS.png)](/img/protocol-specs/public-vm/bit-formats/ADDRESS.png)
Expand All @@ -818,7 +818,7 @@ Get the _storage_ address of the currently executing context
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.storageAddress`
- **Details**: The storage address is used for public storage accesses.
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/STORAGEADDRESS.png)](/img/protocol-specs/public-vm/bit-formats/STORAGEADDRESS.png)
Expand All @@ -835,7 +835,7 @@ Get the address of the sender (caller of the current context)
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.sender`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/SENDER.png)](/img/protocol-specs/public-vm/bit-formats/SENDER.png)
Expand All @@ -852,7 +852,7 @@ Get the fee to be paid per "L2 gas" - constant for entire transaction
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.feePerL2Gas`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/FEEPERL2GAS.png)](/img/protocol-specs/public-vm/bit-formats/FEEPERL2GAS.png)
Expand All @@ -869,7 +869,7 @@ Get the fee to be paid per "DA gas" - constant for entire transaction
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.feePerDaGas`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/FEEPERDAGAS.png)](/img/protocol-specs/public-vm/bit-formats/FEEPERDAGAS.png)
Expand All @@ -886,7 +886,7 @@ Get the computed transaction fee during teardown phase, zero otherwise
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.transactionFee`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56


Expand All @@ -903,7 +903,7 @@ Get how many contract calls deep the current call context is
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.contractCallDepth`
- **Details**: Note: security issues with EVM's tx.origin can be resolved by asserting `calldepth == 0`.
- **Tag updates**: `T[dstOffset] = u8`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/CONTRACTCALLDEPTH.png)](/img/protocol-specs/public-vm/bit-formats/CONTRACTCALLDEPTH.png)
Expand All @@ -920,7 +920,7 @@ Get this rollup's L1 chain ID
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.globals.chainId`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/CHAINID.png)](/img/protocol-specs/public-vm/bit-formats/CHAINID.png)
Expand All @@ -937,7 +937,7 @@ Get this rollup's L2 version ID
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.globals.version`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/VERSION.png)](/img/protocol-specs/public-vm/bit-formats/VERSION.png)
Expand All @@ -954,7 +954,7 @@ Get this L2 block's number
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.globals.blocknumber`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/BLOCKNUMBER.png)](/img/protocol-specs/public-vm/bit-formats/BLOCKNUMBER.png)
Expand Down Expand Up @@ -988,7 +988,7 @@ Get the block's beneficiary address
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.globals.coinbase`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/COINBASE.png)](/img/protocol-specs/public-vm/bit-formats/COINBASE.png)
Expand All @@ -1005,7 +1005,7 @@ Total amount of "L2 gas" that a block can consume
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.globals.l2GasLimit`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/BLOCKL2GASLIMIT.png)](/img/protocol-specs/public-vm/bit-formats/BLOCKL2GASLIMIT.png)
Expand All @@ -1022,7 +1022,7 @@ Total amount of "DA gas" that a block can consume
- **Args**:
- **dstOffset**: memory offset specifying where to store operation's result
- **Expression**: `M[dstOffset] = context.environment.globals.daGasLimit`
- **Tag updates**: `T[dstOffset] = u32`
- **Tag updates**: `T[dstOffset] = field`
- **Bit-size**: 56

[![](/img/protocol-specs/public-vm/bit-formats/BLOCKDAGASLIMIT.png)](/img/protocol-specs/public-vm/bit-formats/BLOCKDAGASLIMIT.png)
Expand Down
36 changes: 19 additions & 17 deletions docs/src/preprocess/InstructionSet/InstructionSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ const INSTRUCTION_SET_RAW = [
Summary: "Get the address of the currently executing l2 contract",
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "storageaddress",
Expand All @@ -529,7 +529,7 @@ const INSTRUCTION_SET_RAW = [
Summary: "Get the _storage_ address of the currently executing context",
Details: "The storage address is used for public storage accesses.",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "sender",
Expand All @@ -547,7 +547,7 @@ const INSTRUCTION_SET_RAW = [
Summary: "Get the address of the sender (caller of the current context)",
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "feeperl2gas",
Expand All @@ -566,7 +566,7 @@ const INSTRUCTION_SET_RAW = [
'Get the fee to be paid per "L2 gas" - constant for entire transaction',
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "feeperdagas",
Expand All @@ -585,7 +585,7 @@ const INSTRUCTION_SET_RAW = [
'Get the fee to be paid per "DA gas" - constant for entire transaction',
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "transactionfee",
Expand All @@ -604,7 +604,7 @@ const INSTRUCTION_SET_RAW = [
"Get the computed transaction fee during teardown phase, zero otherwise",
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "contractcalldepth",
Expand All @@ -623,7 +623,7 @@ const INSTRUCTION_SET_RAW = [
Details:
"Note: security issues with EVM's tx.origin can be resolved by asserting `calldepth == 0`.",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u8`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "chainid",
Expand All @@ -641,7 +641,7 @@ const INSTRUCTION_SET_RAW = [
Summary: "Get this rollup's L1 chain ID",
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "version",
Expand All @@ -659,7 +659,7 @@ const INSTRUCTION_SET_RAW = [
Summary: "Get this rollup's L2 version ID",
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "blocknumber",
Expand All @@ -677,7 +677,7 @@ const INSTRUCTION_SET_RAW = [
Summary: "Get this L2 block's number",
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "timestamp",
Expand Down Expand Up @@ -713,7 +713,7 @@ const INSTRUCTION_SET_RAW = [
Summary: "Get the block's beneficiary address",
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "blockl2gaslimit",
Expand All @@ -731,7 +731,7 @@ const INSTRUCTION_SET_RAW = [
Summary: 'Total amount of "L2 gas" that a block can consume',
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "blockdagaslimit",
Expand All @@ -749,7 +749,7 @@ const INSTRUCTION_SET_RAW = [
Summary: 'Total amount of "DA gas" that a block can consume',
Details: "",
"Tag checks": "",
"Tag updates": "`T[dstOffset] = u32`",
"Tag updates": "`T[dstOffset] = field`",
},
{
id: "calldatacopy",
Expand Down Expand Up @@ -1549,7 +1549,8 @@ halt
},
{
name: "dstOffset",
description: "memory offset specifying where the first limb of the radix-conversion result is stored.",
description:
"memory offset specifying where the first limb of the radix-conversion result is stored.",
},
{
name: "radix",
Expand All @@ -1562,14 +1563,15 @@ halt
description: "the number of limbs the word will be converted into.",
type: "u32",
mode: "immediate",
}
},
],

Expression: `TBD: Storage of limbs and if T[dstOffset] is constrained to U8`,
Summary: "Convert a word to an array of limbs in little-endian radix form",
Details: "The limbs will be stored in a contiguous memory block starting at `dstOffset`.",
Details:
"The limbs will be stored in a contiguous memory block starting at `dstOffset`.",
"Tag checks": "`T[srcOffset] == field`",
}
},
];
const INSTRUCTION_SET = INSTRUCTION_SET_RAW.map((instr) => {
instr["Bit-size"] = instructionSize(instr);
Expand Down
1 change: 1 addition & 0 deletions noir-projects/aztec-nr/aztec/src/context/avm_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl AvmContext {
pub fn fee_per_da_gas(self) -> Field {
fee_per_da_gas()
}

/**
* Emit a log with the given event selector and message.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Fr } from '@aztec/foundation/fields';

import { TypeTag } from '../avm_memory_types.js';
import { initContext, initExecutionEnvironment, initGlobalVariables } from '../fixtures/index.js';
import {
Address,
Expand Down Expand Up @@ -49,18 +50,19 @@ describe.each([

await instruction.execute(context);

expect(context.machineState.memory.getTag(0)).toBe(TypeTag.FIELD);
const actual = context.machineState.memory.get(0).toFr();
expect(actual).toEqual(value);
});
});

type GlobalsInstruction = typeof ChainId | typeof Version | typeof BlockNumber | typeof Timestamp;
describe.each([
[ChainId, 'chainId'],
[Version, 'version'],
[BlockNumber, 'blockNumber'],
[Timestamp, 'timestamp'],
])('Global Variables', (clsValue: GlobalsInstruction, key: string) => {
[ChainId, 'chainId', TypeTag.FIELD],
[Version, 'version', TypeTag.FIELD],
[BlockNumber, 'blockNumber', TypeTag.FIELD],
[Timestamp, 'timestamp', TypeTag.UINT64],
])('Global Variables', (clsValue: GlobalsInstruction, key: string, tag: TypeTag) => {
it(`${clsValue.name} should (de)serialize correctly`, () => {
const buf = Buffer.from([
clsValue.opcode, // opcode
Expand All @@ -81,6 +83,7 @@ describe.each([

await instruction.execute(context);

expect(context.machineState.memory.getTag(0)).toBe(tag);
const actual = context.machineState.memory.get(0).toFr();
expect(actual).toEqual(value);
});
Expand Down
Loading

0 comments on commit 485fe40

Please sign in to comment.