-
Notifications
You must be signed in to change notification settings - Fork 7
/
hostio.h
369 lines (333 loc) · 14.4 KB
/
hostio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright 2022-2023, Offchain Labs, Inc.
// For licensing, see https://github.com/stylus-sdk-c/blob/stylus/licenses/COPYRIGHT.md
#ifndef STYLUS_HOSTIO_H
#define STYLUS_HOSTIO_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define VM_HOOK(name) extern __attribute__((import_module("vm_hooks"), import_name(#name)))
/**
* Gets the ETH balance in wei of the account at the given address.
* The semantics are equivalent to that of the EVM’s [`BALANCE`] opcode.
*
* [`BALANCE`]: https://www.evm.codes/#31
*/
VM_HOOK(account_balance) void account_balance(const uint8_t * address, uint8_t * dest);
/**
* Gets the code hash of the account at the given address. The semantics are equivalent
* to that of the EVM's [`EXT_CODEHASH`] opcode. Note that the code hash of an account without
* code will be the empty hash
* `keccak("") = c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470`.
*
* [`EXT_CODEHASH`]: https://www.evm.codes/#3F
*/
VM_HOOK(account_codehash) void account_codehash(const uint8_t * address, uint8_t * dest);
/**
* Reads a 32-byte value from permanent storage. Stylus's storage format is identical to
* that of the EVM. This means that, under the hood, this hostio is accessing the 32-byte
* value stored in the EVM state trie at offset `key`, which will be `0` when not previously
* set. The semantics, then, are equivalent to that of the EVM's [`SLOAD`] opcode.
*
* [`SLOAD`]: https://www.evm.codes/#54
*/
VM_HOOK(storage_load_bytes32) void storage_load_bytes32(const uint8_t * key, uint8_t * dest);
/**
* Stores a 32-byte value to permanent storage. Stylus's storage format is identical to that
* of the EVM. This means that, under the hood, this hostio is storing a 32-byte value into
* the EVM state trie at offset `key`. Furthermore, refunds are tabulated exactly as in the
* EVM. The semantics, then, are equivalent to that of the EVM's [`SSTORE`] opcode.
*
* [`SSTORE`]: https://www.evm.codes/#55
*/
VM_HOOK(storage_store_bytes32) void storage_store_bytes32(const uint8_t * key, const uint8_t * value);
/**
* Gets the basefee of the current block. The semantics are equivalent to that of the EVM's
* [`BASEFEE`] opcode.
*
* [`BASEFEE`]: https://www.evm.codes/#48
*/
VM_HOOK(block_basefee) void block_basefee(uint8_t * basefee);
/**
* Gets the unique chain identifier of the Arbitrum chain. The semantics are equivalent to
* that of the EVM's [`CHAIN_ID`] opcode.
*
* [`CHAIN_ID`]: https://www.evm.codes/#46
*/
VM_HOOK(chainid) uint64_t chainid();
/**
* Gets the coinbase of the current block, which on Arbitrum chains is the L1 batch poster's
* address. This differs from Ethereum where the validator including the transaction
* determines the coinbase.
*/
VM_HOOK(block_coinbase) void block_coinbase(uint8_t * coinbase);
/**
* Gets the gas limit of the current block. The semantics are equivalent to that of the EVM's
* [`GAS_LIMIT`] opcode. Note that as of the time of this writing, `evm.codes` incorrectly
* implies that the opcode returns the gas limit of the current transaction. When in doubt,
* consult [`The Ethereum Yellow Paper`].
*
* [`GAS_LIMIT`]: https://www.evm.codes/#45
* [`The Ethereum Yellow Paper`]: https://ethereum.github.io/yellowpaper/paper.pdf
*/
VM_HOOK(block_gas_limit) uint64_t block_gas_limit();
/**
* Gets a bounded estimate of the L1 block number at which the Sequencer sequenced the
* transaction. See [`Block Numbers and Time`] for more information on how this value is
* determined.
*
* [`Block Numbers and Time`]: https://developer.arbitrum.io/time
*/
VM_HOOK(block_number) uint64_t block_number();
/**
* Gets a bounded estimate of the Unix timestamp at which the Sequencer sequenced the
* transaction. See [`Block Numbers and Time`] for more information on how this value is
* determined.
*
* [`Block Numbers and Time`]: https://developer.arbitrum.io/time
*/
VM_HOOK(block_timestamp) uint64_t block_timestamp();
/**
* Calls the contract at the given address with options for passing value and to limit the
* amount of gas supplied. The return status indicates whether the call succeeded, and is
* nonzero on failure.
*
* In both cases `return_data_len` will store the length of the result, the bytes of which can
* be read via the `read_return_data` hostio. The bytes are not returned directly so that the
* programmer can potentially save gas by choosing which subset of the return result they'd
* like to copy.
*
* The semantics are equivalent to that of the EVM's [`CALL`] opcode, including callvalue
* stipends and the 63/64 gas rule. This means that supplying the `u64::MAX` gas can be used
* to send as much as possible.
*
* [`CALL`]: https://www.evm.codes/#f1
*/
VM_HOOK(call_contract) uint8_t call_contract(
const uint8_t * contract,
const uint8_t * calldata,
const size_t calldata_len,
const uint8_t * value,
const uint64_t gas,
size_t * return_data_len
);
/**
* Gets the address of the current program. The semantics are equivalent to that of the EVM's
* [`ADDRESS`] opcode.
*
* [`ADDRESS`]: https://www.evm.codes/#30
*/
VM_HOOK(contract_address) void contract_address(uint8_t * address);
/**
* Deploys a new contract using the init code provided, which the EVM executes to construct
* the code of the newly deployed contract. The init code must be written in EVM bytecode, but
* the code it deploys can be that of a Stylus program. The code returned will be treated as
* WASM if it begins with the EOF-inspired header `0xEFF000`. Otherwise the code will be
* interpreted as that of a traditional EVM-style contract. See [`Deploying Stylus Programs`]
* for more information on writing init code.
*
* On success, this hostio returns the address of the newly created account whose address is
* a function of the sender and nonce. On failure the address will be `0`, `return_data_len`
* will store the length of the revert data, the bytes of which can be read via the
* `read_return_data` hostio. The semantics are equivalent to that of the EVM's [`CREATE`]
* opcode, which notably includes the exact address returned.
*
* [`Deploying Stylus Programs`]: https://developer.arbitrum.io/TODO
* [`CREATE`]: https://www.evm.codes/#f0
*/
VM_HOOK(create1) void create1(
const uint8_t * code,
const size_t code_len,
const uint8_t * endowment,
uint8_t * contract,
size_t * revert_data_len
);
/**
* Deploys a new contract using the init code provided, which the EVM executes to construct
* the code of the newly deployed contract. The init code must be written in EVM bytecode, but
* the code it deploys can be that of a Stylus program. The code returned will be treated as
* WASM if it begins with the EOF-inspired header `0xEFF000`. Otherwise the code will be
* interpreted as that of a traditional EVM-style contract. See [`Deploying Stylus Programs`]
* for more information on writing init code.
*
* On success, this hostio returns the address of the newly created account whose address is a
* function of the sender, salt, and init code. On failure the address will be `0`,
* `return_data_len` will store the length of the revert data, the bytes of which can be read
* via the `read_return_data` hostio. The semantics are equivalent to that of the EVM's
* `[CREATE2`] opcode, which notably includes the exact address returned.
*
* [`Deploying Stylus Programs`]: https://developer.arbitrum.io/TODO
* [`CREATE2`]: https://www.evm.codes/#f5
*/
VM_HOOK(create2) void create2(
const uint8_t * code,
const size_t code_len,
const uint8_t * endowment,
const uint8_t * salt,
uint8_t * contract,
size_t * revert_data_len
);
/**
* Delegate calls the contract at the given address, with the option to limit the amount of
* gas supplied. The return status indicates whether the call succeeded, and is nonzero on
* failure.
*
* In both cases `return_data_len` will store the length of the result, the bytes of which
* can be read via the `read_return_data` hostio. The bytes are not returned directly so that
* the programmer can potentially save gas by choosing which subset of the return result
* they'd like to copy.
*
* The semantics are equivalent to that of the EVM's [`DELEGATE_CALL`] opcode, including the
* 63/64 gas rule. This means that supplying `u64::MAX` gas can be used to send as much as
* possible.
*
* [`DELEGATE_CALL`]: https://www.evm.codes/#F4
*/
VM_HOOK(delegate_call_contract) uint8_t delegate_call_contract(
const uint8_t * contract,
const uint8_t * calldata,
const size_t calldata_len,
const uint64_t gas,
size_t * return_data_len
);
/**
* Emits an EVM log with the given number of topics and data, the first bytes of which should
* be the 32-byte-aligned topic data. The semantics are equivalent to that of the EVM's
* [`LOG0`], [`LOG1`], [`LOG2`], [`LOG3`], and [`LOG4`] opcodes based on the number of topics
* specified. Requesting more than `4` topics will induce a revert.
*
* [`LOG0`]: https://www.evm.codes/#a0
* [`LOG1`]: https://www.evm.codes/#a1
* [`LOG2`]: https://www.evm.codes/#a2
* [`LOG3`]: https://www.evm.codes/#a3
* [`LOG4`]: https://www.evm.codes/#a4
*/
VM_HOOK(emit_log) void emit_log(uint8_t * data, size_t len, size_t topics);
/**
* Gets the amount of gas left after paying for the cost of this hostio. The semantics are
* equivalent to that of the EVM's [`GAS`] opcode.
*
* [`GAS`]: https://www.evm.codes/#5a
*/
VM_HOOK(evm_gas_left) uint64_t evm_gas_left();
/**
* Gets the amount of ink remaining after paying for the cost of this hostio. The semantics
* are equivalent to that of the EVM's [`GAS`] opcode, except the units are in ink. See
* [`Ink and Gas`] for more information on Stylus's compute pricing.
*
* [`GAS`]: https://www.evm.codes/#5a
* [`Ink and Gas`]: https://developer.arbitrum.io/TODO
*/
VM_HOOK(evm_ink_left) uint64_t evm_ink_left();
/**
* The `ENTRYPOINT` macro handles importing this hostio, which is required if the
* program's memory grows. Otherwise compilation through the `ArbWasm` precompile will revert.
* Internally the Stylus VM forces calls to this hostio whenever new WASM pages are allocated.
* Calls made voluntarily will unproductively consume gas.
*/
VM_HOOK(memory_grow) void memory_grow(const uint16_t pages);
/**
* Gets the address of the account that called the program. For normal L2-to-L2 transactions
* the semantics are equivalent to that of the EVM's [`CALLER`] opcode, including in cases
* arising from [`DELEGATE_CALL`].
*
* For L1-to-L2 retryable ticket transactions, the top-level sender's address will be aliased.
* See [`Retryable Ticket Address Aliasing`] for more information on how this works.
*
* [`CALLER`]: https://www.evm.codes/#33
* [`DELEGATE_CALL`]: https://www.evm.codes/#f4
* [`Retryable Ticket Address Aliasing`]: https://developer.arbitrum.io/arbos/l1-to-l2-messaging#address-aliasing
*/
VM_HOOK(msg_sender) void msg_sender(const uint8_t * sender);
/**
* Get the ETH value in wei sent to the program. The semantics are equivalent to that of the
* EVM's [`CALLVALUE`] opcode.
*
* [`CALLVALUE`]: https://www.evm.codes/#34
*/
VM_HOOK(msg_value) void msg_value(const uint8_t * value);
/**
* Efficiently computes the [`keccak256`] hash of the given preimage.
* The semantics are equivalent to that of the EVM's [`SHA3`] opcode.
*
* [`keccak256`]: https://en.wikipedia.org/wiki/SHA-3
* [`SHA3`]: https://www.evm.codes/#20
*/
VM_HOOK(native_keccak256) void native_keccak256(const uint8_t * bytes, size_t len, uint8_t * output);
/**
* Reads the program calldata. The semantics are equivalent to that of the EVM's
* [`CALLDATA_COPY`] opcode when requesting the entirety of the current call's calldata.
*
* [`CALLDATA_COPY`]: https://www.evm.codes/#37
*/
VM_HOOK(read_args) void read_args(const uint8_t * data);
/**
* Copies the bytes of the last EVM call or deployment return result. Reverts if out of
* bounds. The semantics are equivalent to that of the EVM's [`RETURN_DATA_COPY`] opcode.
*
* [`RETURN_DATA_COPY`]: https://www.evm.codes/#3e
*/
VM_HOOK(read_return_data) size_t read_return_data(uint8_t * dest, size_t offset, size_t size);
/**
* Writes the final return data. If not called before the program exists, the return data will
* be 0 bytes long. Note that this hostio does not cause the program to exit, which happens
* naturally when [`user_entrypoint`] returns.
*/
VM_HOOK(write_result) void write_result(const uint8_t * data, size_t len);
/**
* Returns the length of the last EVM call or deployment return result, or `0` if neither have
* happened during the program's execution. The semantics are equivalent to that of the EVM's
* [`RETURN_DATA_SIZE`] opcode.
*
* [`RETURN_DATA_SIZE`]: https://www.evm.codes/#3d
*/
VM_HOOK(return_data_size) size_t return_data_size();
/**
* Static calls the contract at the given address, with the option to limit the amount of gas
* supplied. The return status indicates whether the call succeeded, and is nonzero on
* failure.
*
* In both cases `return_data_len` will store the length of the result, the bytes of which can
* be read via the `read_return_data` hostio. The bytes are not returned directly so that the
* programmer can potentially save gas by choosing which subset of the return result they'd
* like to copy.
*
* The semantics are equivalent to that of the EVM's [`STATIC_CALL`] opcode, including the
* 63/64 gas rule. This means that supplying `u64::MAX` gas can be used to send as much as
* possible.
*
* [`STATIC_CALL`]: https://www.evm.codes/#FA
*/
VM_HOOK(static_call_contract) uint8_t static_call_contract(
const uint8_t * contract,
const uint8_t * calldata,
const size_t calldata_len,
const uint64_t gas,
size_t * return_data_len
);
/**
* Gets the gas price in wei per gas, which on Arbitrum chains equals the basefee. The
* semantics are equivalent to that of the EVM's [`GAS_PRICE`] opcode.
*
* [`GAS_PRICE`]: https://www.evm.codes/#3A
*/
VM_HOOK(tx_gas_price) void tx_gas_price(uint8_t * gas_price);
/**
* Gets the price of ink in evm gas basis points. See [`Ink and Gas`] for more information on
* Stylus's compute-pricing model.
*
* [`Ink and Gas`]: https://developer.arbitrum.io/TODO
*/
VM_HOOK(tx_ink_price) uint64_t tx_ink_price();
/**
* Gets the top-level sender of the transaction. The semantics are equivalent to that of the
* EVM's [`ORIGIN`] opcode.
*
* [`ORIGIN`]: https://www.evm.codes/#32
*/
VM_HOOK(tx_origin) void tx_origin(uint8_t * origin);
#ifdef __cplusplus
}
#endif
#endif