From 4c97fcd808cd9e940b63884f98845d552e50e48b Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Wed, 29 May 2024 13:09:03 +0200 Subject: [PATCH] Moved file out of src --- .../ethereum/circuits/lib/{src => }/StorageLayout.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) rename vlayer/ethereum/circuits/lib/{src => }/StorageLayout.md (64%) diff --git a/vlayer/ethereum/circuits/lib/src/StorageLayout.md b/vlayer/ethereum/circuits/lib/StorageLayout.md similarity index 64% rename from vlayer/ethereum/circuits/lib/src/StorageLayout.md rename to vlayer/ethereum/circuits/lib/StorageLayout.md index f0945359..7a8481d2 100644 --- a/vlayer/ethereum/circuits/lib/src/StorageLayout.md +++ b/vlayer/ethereum/circuits/lib/StorageLayout.md @@ -8,22 +8,24 @@ Contract storage is divided into 2256 **slots** - 32 bytes each. Such ### Statically-sized values -Statically-sized values are stored one by one in the order of their initialization. If two or more consecutive variables can fit into one slot, they are packed there together. If a variable can't fit with the previous variable, it is stored in the next slot. Variables that appear after an array or structure variables, always start a new slot. Similarly arrays and structures also always start a new storage slot. +Statically-sized values are stored one by one, starting at slot 0, in the order of their initialization in the contract. If two or more consecutive variables can fit into one slot, they are packed there together. If a variable can't fit with the previous variable, it is stored in the next slot. Variables that appear after an array or structure variables, always start a new slot. Similarly arrays and structures also always start a new storage slot. Variables of a structure are stored just as standalone variables. ### Dynamically-sized values -Dynamically-sized values can't be stored in the same as statically-sized values. Adding new elements to an existing dynamic array would demand values stored in succeeding slots to shift further away. +Dynamically-sized values can't be stored in the same way as statically-sized values. Adding new elements to an existing dynamic array would demand values stored in succeeding slots to shift further away. Instead, dynamically-sized values are stored at slots chosen by keccak hashing. This is where such an enormous number of storage slots is used. It prevents data from overlapping. **Slot position `p`** - the starting position of a structure that is calculated based on previously appearing in contract values - is used to calculate further the positions of data. #### Dynamic arrays -In case of dynamic arrays, position `p` contains the length of the array. Position of the first element is calculated by `keccak256(p)`. The next elements are stored just as statically-sized array's elements - one +In case of dynamic arrays, position `p` contains the length of the array. Position of the first element is calculated by `keccak256(p)`. The next elements are stored just as statically-sized array's elements - one after another. #### Mapping +Values of mapping are stored at slots: `keccak256(h(k).p)`, where `k` is a key in mapping and `h` is a function as follows: in case of value types `h` is a padding to 32 bytes, in case of strings and byte arrays `h(k) = k`. + ## Employment Understanding how contract storage works enables developer to make gas-efficient contracts. @@ -41,7 +43,7 @@ contract Contract{ } ``` -If they are initialized in this order, variable `b` wouldn't fit into the same slot as `a` (because `b` takes up all 32 bytes) and `c` also wouldn't fit with `b`. Because of that contract will use three slots. +If variables are initialized in this order, variable `b` wouldn't fit into the same slot as `a` (because `b` takes up all 32 bytes) and `c` also wouldn't fit with `b`. Because of that contract will use three slots. If the order is changed: @@ -53,7 +55,7 @@ contract Contract { } ``` -Now variables `a` and `c` can be packed into one slot and memory is saved since this contract uses only three slots/ +Now variables `a` and `c` can be packed into one slot and memory is saved since this contract uses only three slots. ### Efficient variables reading