From cad93fe405ef770028e9d6cdf02963365ca24492 Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Wed, 6 Dec 2023 19:38:29 -0700 Subject: [PATCH 1/4] Add transient hostios --- include/hostio.h | 24 ++++++++++++++++++++++-- include/storage.h | 23 ++++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/include/hostio.h b/include/hostio.h index 9718321..998bde1 100644 --- a/include/hostio.h +++ b/include/hostio.h @@ -36,7 +36,7 @@ VM_HOOK(account_codehash) void account_codehash(const uint8_t * address, uint8_t * 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); @@ -46,11 +46,31 @@ VM_HOOK(storage_load_bytes32) void storage_load_bytes32(const uint8_t * key, uin * 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); +/** + * Reads a 32-byte value from transient 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 transient storage at offset `key`, which will be `0` when not previously + * set. The semantics, then, are equivalent to that of the EVM's [`TLOAD`] opcode. + * + * [`TLOAD`]: https://www.evm.codes/#5c + */ +VM_HOOK(storage_transient_load_bytes32) void storage_transient_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 transient storage at offset `key`. Furthermore, refunds are tabulated exactly as in the + * EVM. The semantics, then, are equivalent to that of the EVM's [`TSTORE`] opcode. + * + * [`TSTORE`]: https://www.evm.codes/#5d + */ +VM_HOOK(storage_transient_store_bytes32) void storage_transient_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. diff --git a/include/storage.h b/include/storage.h index 3ab07a3..6d63273 100644 --- a/include/storage.h +++ b/include/storage.h @@ -23,7 +23,7 @@ extern "C" { /** * storage_load / store load or store a value from storage accordingly. - * + * * The value of the first "storage" pointer is not used. * generated headers provide: * a const storage pointer when working for a view-only function @@ -42,6 +42,27 @@ inline void storage_store(void *storage, const uint8_t *key, const uint8_t *valu storage_store_bytes32(key, value); } +/** + * storage_transient_load / store load or store a value from transient storage accordingly. + * + * The value of the first "storage" pointer is not used. + * generated headers provide: + * a const storage pointer when working for a view-only function + * a non const pointer for a mutating function + * no pointer (so don't call storage_transient_load) for a pure function + * + */ +inline void storage_transient_load(const void* storage, const uint8_t *key, uint8_t *dest) { + storage_transient_load_bytes32(key, dest); +} + +/** + * see documentation for storage_transient_load + */ +inline void storage_transient_store(void *storage, const uint8_t *key, const uint8_t *value) { + storage_transient_store_bytes32(key, value); +} + /** * calculate slot for a map with base slot "storage" to put "key" * If key requires padding it must be applied before calling this function From dd88168886e463ce867d2e60b6396b96c97ec770 Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Wed, 13 Dec 2023 20:04:34 -0700 Subject: [PATCH 2/4] Change transient hostio name --- include/hostio.h | 4 ++-- include/storage.h | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/hostio.h b/include/hostio.h index 998bde1..56346fd 100644 --- a/include/hostio.h +++ b/include/hostio.h @@ -59,7 +59,7 @@ VM_HOOK(storage_store_bytes32) void storage_store_bytes32(const uint8_t * key, c * * [`TLOAD`]: https://www.evm.codes/#5c */ -VM_HOOK(storage_transient_load_bytes32) void storage_transient_load_bytes32(const uint8_t * key, uint8_t * dest); +VM_HOOK(transient_load_bytes32) void transient_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 @@ -69,7 +69,7 @@ VM_HOOK(storage_transient_load_bytes32) void storage_transient_load_bytes32(cons * * [`TSTORE`]: https://www.evm.codes/#5d */ -VM_HOOK(storage_transient_store_bytes32) void storage_transient_store_bytes32(const uint8_t * key, const uint8_t * value); +VM_HOOK(transient_store_bytes32) void transient_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 diff --git a/include/storage.h b/include/storage.h index 6d63273..061f256 100644 --- a/include/storage.h +++ b/include/storage.h @@ -43,24 +43,24 @@ inline void storage_store(void *storage, const uint8_t *key, const uint8_t *valu } /** - * storage_transient_load / store load or store a value from transient storage accordingly. + * transient_load / store load or store a value from transient storage accordingly. * * The value of the first "storage" pointer is not used. * generated headers provide: * a const storage pointer when working for a view-only function * a non const pointer for a mutating function - * no pointer (so don't call storage_transient_load) for a pure function + * no pointer (so don't call transient_load) for a pure function * */ -inline void storage_transient_load(const void* storage, const uint8_t *key, uint8_t *dest) { - storage_transient_load_bytes32(key, dest); +inline void transient_load(const void* storage, const uint8_t *key, uint8_t *dest) { + transient_load_bytes32(key, dest); } /** - * see documentation for storage_transient_load + * see documentation for transient_load */ -inline void storage_transient_store(void *storage, const uint8_t *key, const uint8_t *value) { - storage_transient_store_bytes32(key, value); +inline void transient_store(void *storage, const uint8_t *key, const uint8_t *value) { + transient_store_bytes32(key, value); } /** From df30fbe564580907c55b63feab7dab5050149617 Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Wed, 13 Dec 2023 20:55:06 -0700 Subject: [PATCH 3/4] Link to EIP since transient opcodes aren't on evm.codes yet --- include/hostio.h | 4 ++-- include/storage.h | 21 --------------------- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/include/hostio.h b/include/hostio.h index 56346fd..9bdc14e 100644 --- a/include/hostio.h +++ b/include/hostio.h @@ -57,7 +57,7 @@ VM_HOOK(storage_store_bytes32) void storage_store_bytes32(const uint8_t * key, c * value stored in the EVM transient storage at offset `key`, which will be `0` when not previously * set. The semantics, then, are equivalent to that of the EVM's [`TLOAD`] opcode. * - * [`TLOAD`]: https://www.evm.codes/#5c + * [`TLOAD`]: https://eips.ethereum.org/EIPS/eip-1153 */ VM_HOOK(transient_load_bytes32) void transient_load_bytes32(const uint8_t * key, uint8_t * dest); @@ -67,7 +67,7 @@ VM_HOOK(transient_load_bytes32) void transient_load_bytes32(const uint8_t * key, * the EVM transient storage at offset `key`. Furthermore, refunds are tabulated exactly as in the * EVM. The semantics, then, are equivalent to that of the EVM's [`TSTORE`] opcode. * - * [`TSTORE`]: https://www.evm.codes/#5d + * [`TSTORE`]: https://eips.ethereum.org/EIPS/eip-1153 */ VM_HOOK(transient_store_bytes32) void transient_store_bytes32(const uint8_t * key, const uint8_t * value); diff --git a/include/storage.h b/include/storage.h index 061f256..ce9fe84 100644 --- a/include/storage.h +++ b/include/storage.h @@ -42,27 +42,6 @@ inline void storage_store(void *storage, const uint8_t *key, const uint8_t *valu storage_store_bytes32(key, value); } -/** - * transient_load / store load or store a value from transient storage accordingly. - * - * The value of the first "storage" pointer is not used. - * generated headers provide: - * a const storage pointer when working for a view-only function - * a non const pointer for a mutating function - * no pointer (so don't call transient_load) for a pure function - * - */ -inline void transient_load(const void* storage, const uint8_t *key, uint8_t *dest) { - transient_load_bytes32(key, dest); -} - -/** - * see documentation for transient_load - */ -inline void transient_store(void *storage, const uint8_t *key, const uint8_t *value) { - transient_store_bytes32(key, value); -} - /** * calculate slot for a map with base slot "storage" to put "key" * If key requires padding it must be applied before calling this function From 25572f729f149fc7ebb1216eb0b402ce78ad2b43 Mon Sep 17 00:00:00 2001 From: Joshua Colvin Date: Wed, 13 Dec 2023 20:58:50 -0700 Subject: [PATCH 4/4] Preserve spaces in PR --- include/hostio.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/hostio.h b/include/hostio.h index 9bdc14e..deb5375 100644 --- a/include/hostio.h +++ b/include/hostio.h @@ -36,7 +36,7 @@ VM_HOOK(account_codehash) void account_codehash(const uint8_t * address, uint8_t * 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); @@ -46,7 +46,7 @@ VM_HOOK(storage_load_bytes32) void storage_load_bytes32(const uint8_t * key, uin * 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);