Skip to content

Commit 4cfb427

Browse files
authored
docs(yellowpaper): finish AVM Context definitions (AztecProtocol#3709)
Finishing AVM Context definitions and initialization (just for initial message calls - not nested ones).
1 parent 912df7e commit 4cfb427

File tree

1 file changed

+84
-2
lines changed
  • yellow-paper/docs/public-vm

1 file changed

+84
-2
lines changed

yellow-paper/docs/public-vm/avm.md

+84-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ A contract's public bytecode is a series of execution instructions for the AVM.
3838
Many terms and definitions here are borrowed from the [Ethereum Yellow Paper](https://ethereum.github.io/yellowpaper/paper.pdf).
3939
:::
4040

41-
An "Execution Context" includes the information necessary to trigger AVM execution along with the state maintained by the AVM throughout execution:
41+
An "Execution Context" includes the information necessary to initiate AVM execution along with the state maintained by the AVM throughout execution:
4242
```
4343
AVMContext {
4444
environment: ExecutionEnvironment,
@@ -51,12 +51,13 @@ AVMContext {
5151

5252
The first two entries, "Execution Environment" and "Machine State", share the same lifecycle. They contain information pertaining to a single message call and are initialized prior to the start of a call's execution.
5353

54-
> When a nested message call is made, a new environment and machine state are initialized by the caller. In other words, a nested message call has its own environment and machine state, although their initialization will be partially derived from the caller's context.
54+
> When a nested message call is made, a new environment and machine state are initialized by the caller. In other words, a nested message call has its own environment and machine state which are _partially_ derived from the caller's context.
5555
5656
The "Execution Environment" is fully specified by a message call's execution agent and remains constant throughout a call's execution.
5757
```
5858
ExecutionEnvironment {
5959
address,
60+
storageAddress,
6061
origin,
6162
l1GasPrice,
6263
l2GasPrice,
@@ -68,6 +69,7 @@ ExecutionEnvironment {
6869
globalVariables: PublicGlobalVariables,
6970
messageCallDepth,
7071
isStaticCall,
72+
isDelegateCall,
7173
}
7274
```
7375

@@ -77,6 +79,86 @@ MachineState {
7779
l1GasLeft,
7880
l2GasLeft,
7981
pc,
82+
memory: offset => value, // uninitialized at start
83+
}
84+
```
85+
86+
"World State" contains persistable VM state. If a message call succeeds, its world state updates are applied to the calling context (whether that be a parent call's context or the transaction context). If a message call fails, its world state updates are rejected by its caller. When a _transaction_ succeeds, its world state updates persist into future transactions.
87+
```
88+
WorldState {
89+
publicStorage: (address, slot) => value, // read/write
90+
noteHashes: (address, index) => noteHash, // read & append only
91+
nullifiers: (address, index) => nullifier, // read & append only
92+
l1l2messageHashes: (address, key) => messageHash, // read only
93+
contracts: (address) => bytecode, // read only
94+
}
95+
```
96+
97+
> Note: the notation `key => value` describes a mapping from `key` to `value`.
98+
99+
> Note: each member of the world state is implemented as an independent merkle tree with different properties.
100+
101+
The "Accrued Substate", as coined in the [Ethereum Yellow Paper](https://ethereum.github.io/yellowpaper/paper), contains information that is accrued throughout transaction execution to be "acted upon immediately following the transaction." These are append-only arrays containing state that is not relevant to other calls or transactions. Similar to world state, if a message call succeeds, its substate is appended to its calling context, but if it fails its substate is dropped by its caller.
102+
```
103+
AccruedSubstate {
104+
logs: [], // append-only
105+
l2toL1Messages: [], // append-only
106+
}
107+
```
108+
109+
Finally, when a message call halts, it sets the context's "Message Call Results" to communicate results to the caller.
110+
```
111+
MessageCallResults {
112+
reverted: boolean,
113+
output: [] | undefined,
114+
}
115+
```
116+
117+
### Context initialization for initial call
118+
This section outlines AVM context initialization specifically for a **public execution request's initial message call** (_i.e._ not a nested message call). Context initialization for nested message calls will be explained in a later section.
119+
120+
When AVM execution is initiated for a public execution request, the AVM context is initialized as follows:
121+
```
122+
context = AVMContext {
123+
environment: INITIAL_EXECUTION_ENVIRONMENT,
124+
machineState: INITIAL_MACHINE_STATE,
125+
accruedSubstate: empty,
126+
worldState: <latest world state>,
127+
results: INITIAL_MESSAGE_CALL_RESULTS,
128+
}
129+
```
130+
> Note: Since world state persists between transactions, the latest state is injected into a new AVM context.
131+
132+
Given a `PublicCallRequest` and its parent `TxRequest`, these above-listed "`INITIAL_*`" entries are defined as follows:
133+
```
134+
INITIAL_EXECUTION_ENVIRONMENT = ExecutionEnvironment {
135+
address: PublicCallRequest.contractAddress,
136+
storageAddress: PublicCallRequest.CallContext.storageContractAddress,
137+
origin: TxRequest.origin,
138+
l1GasPrice: TxRequest.l1GasPrice,
139+
l2GasPrice: TxRequest.l2GasPrice,
140+
calldata: PublicCallRequest.args,
141+
sender: PublicCallRequest.CallContext.msgSender,
142+
portal: PublicCallRequest.CallContext.portalContractAddress,
143+
bytecode: worldState.contracts[PublicCallRequest.contractAddress],
144+
blockHeader: <latest block header>,
145+
globalVariables: <latest global variable values>
146+
messageCallDepth: 0,
147+
isStaticCall: PublicCallRequest.CallContext.isStaticCall,
148+
isDelegateCall: PublicCallRequest.CallContext.isDelegateCall,
149+
}
150+
151+
INITIAL_MACHINE_STATE = MachineState {
152+
l1GasLeft: TxRequest.l1GasLimit,
153+
l2GasLeft: TxRequest.l2GasLimit,
154+
pc: 0,
80155
memory: uninitialized,
81156
}
157+
158+
INITIAL_MESSAGE_CALL_RESULTS = MessageCallResults {
159+
reverted: false,
160+
output: undefined,
161+
}
82162
```
163+
164+
> Note: unlike memory in the Ethereum Virtual Machine, uninitialized memory in the AVM is not readable! A memory cell must be written (and therefore [type-tagged](./state-model#types-and-tagged-memory)) before it can be read.

0 commit comments

Comments
 (0)