Data structures for a Verkle powered EVM
ETHCC 7 · 11 Jul 2024 · 23:38
Hadrien Croubois of OpenZeppelin explains how the coming Verkle tree upgrade changes smart contract storage costs. In Verkle, he says, storage slots are grouped in extensions of 256, so keeping related data close together becomes cheaper, while mappings and array lengths scatter it. His fix: combine ERC-721 owner and approval mappings into one struct, which on a Verkle testnet cut transfer gas from about 55,000 to 49,000, with figures not final. He compares three priority queue implementations and suggests compilers such as Solidity's could adapt storage layout, noting this would break upgradeability unless opt-in.
Speakers
Topics
From ETHCC 7, hosted by Ethereum France.
Transcript
Okay, hello everyone. Thank you for coming here on that very last day, very last afternoon of the conference. Please bear in mind we are going to go into something technical, but I think it'd be interesting for for the developers among you. We're going to talk about data structure for a vertical power DVM. Uh most of you know that the ver the verge is coming and that will have an effect on how you write your contract, or at least it should have an effect. So very quickly, I'm working for Open Zeppelin and our thesis is that we are all here building the and the next big economy, a trillion dollar economy. Uh but we at OpenZapin are not building it.
You are and our objective is to build you give you the right tools and the right knowledge to to do that fast and with low risks. Uh yeah, this is a later version of the contract. Yeah, you know that I guess. Uh right now, all our contract, yours, mine, everything, uh, use storage. I mean, most of them use storage, like your ERC20 store balances, your 721 store ownership of the of the NFTs, that kind of things. And this is all stored in a very big state tree that you may or may not know, like how it's designed.
But right now, what's happening is that you've got a build world state tree that goes from the root to the account, and the accounts could be anything, could be EOA, could be contract. Those are basically the same object. But if it's a contract, then it has a code hash and a storage route, and this storage route is another tree where where the storage of the contract is. And that's how self-destruct worked or used to work is by removing this storage tree. We'll see that in vertical, it doesn't work. That's why the self-destruct was was deprecated or at least very much nerfed. And in each each account has its own state tree.
So you know that the storage of an account won't overlap with the storage of another just because they are under different nodes here. Uh it's also like every node here is somehow identical or equivalent in the sense that whether you access this one or that one, the the access costs are basically the same. Uh, and and accesses uh are expensive in terms of storage, so that's why you need to warm up your storage, and that's why it's quite expensive to do the first write of the first suite. But when it's warmed up, then all that is in cache, and then you can do things way cheaper. Uh with the verge, this is all going to change.
Forget about those two layers, one on below the other. Like you only get one big vertical tree that's designed a bit like that. And basically, the way you put data in this in this new vertical tree is that basically you take the address of your contract, you take the last, the first 31 bytes of of your of your of your map. So basically the first 31 bytes of your position in this, and you hash that together, and that will go from the root to what I would call an extension. And then the very last byte of this offset of this position is basically where you go into this extension.
And so these slots are gathered by groups of 256. And so you see that basically the same tree, like all the extensions can belong to basically any account. Like in the very worst case scenario, you could have a collision and two contracts would write to the same place, but that's that's very unlucky. But it's also like in this tree, you don't know by definition which uh which extensions belong to which account, so that's why we don't want to do self-destrict anymore. And so you have this this bucket, this extension, and you know that right now, that at that point, like all uh items are not identical, in the sense that if you have two uh offsets that are close to one another, like they are in the same, they only change in the last in the last byte, then you know they're in the same extension, and they're basically in the same stuff that you're going to cache.
But if you access in very different locations, then they're in different extensions. And the thing is that this tree, because it's a vertical tree, uses a specific hash that is super expensive for nodes to compute, so they're going to charge you for that. And so instead of like paying for warming up each slot individually, you're going to have to pay for warming up the entire extension. Uh, and this is where like there is a fundamental change with what we had before is that like data locality becomes important in in your in your cost. So data locality, basically you don't really see it uh unless you go low level.
But what Solidity and Viper do is that there is this grammar that they start at slot zero to put your stuff and then if you have variable one after the other, they will just add n to move and that's how it works for state variable and things like that. But whenever you have an array or a mapping they need to go to somewhere in the in their space where basically it's free to they are free to do whatever they want. And that's how why they use hashes. And so this hash is like hey I want to go from this position to a random one and that's how they derive array. And you have a similar mechanism for mapping solidity and viper just differ on the order but the logic is is is pretty similar.
And so the result is that your storage particularly when you use mapping and most people use mapping for for balances and things like that gets scattered all over the space and that's not very efficient with with vertical. So for example in the case of a mapping like if you have a mapping at at position zero then depending on the key you are jumping all over the place. And those pseudo pseudo random location are great but like yeah they're not in the same extension. So if you update key 42 and key zero well those are two extensions that you need to update. And if you have two mappings that's that's even worse.
And there is also arrays arrays are pretty good because there is good data locality. However the length of the array is stored at the like the fundamental slot of the array and then you hash to go to slot zero. So whenever you want to access a slot in array you first have to check that your index is within the length and so that means you need to load the length then the item and those are two different extensions. Also, like if you're not really lucky like this, you know that hashing this fundamental position will give you something that ends in F6. And so that means that you can put 10 items in your array before like item number 11, so index 10, goes to the to the next extension.
So while extensions are long, like they contain 256 slots, if you're not that lucky, uh you would have poor data locality at the beginning. Then all following 256 items are in the same same extension, but maybe that's that's a bit of a waste. If you are if your array is not very long, uh we can do better than that. Uh so what you can do as a developer is you cannot do much, but there is some stuff you can do. The first thing is saying like, yeah, if you have multiple mappings in your contract that have the same key pattern, instead of having one mapping that is going from address to balance, and another mapping that is from address to delegate, and another mapping that is from address to permission, well, maybe this balance, delegate and permission, you can put them in the same structure.
That way, if for any reason you need to access two of these values in the same transaction, well, hopefully they will be in the same in the same extension. And so you save a lot on warming up storage. Uh like if you're unlucky, maybe the limit of an extension is in the middle of the error of the structure, so that's not that good, but same problem as with the array before. If you're if you're pointing to something that is at the beginning of an extension, you know you have 256 slots. And that's, I mean, it works if the keys are the same. So you it doesn't work if you want to put balances and approvals for RC20 because they have different access patterns, but it works for some stuff. For example, if you look at ERC721, so one of the main contracts that's been used in in the past years, uh, you have this mapping from token to address that is the owner, and you also have um a mapping from token to address that is a token approval, because you can approve individual tokens.
And whenever there is a transfer, this approval gets reset because you don't want to approve a token, transfer it, and then use the approval that was set before. So this has to be reset, or if it's zero, you don't reset it, but you still need to check it's zero. So basically, both of these mappings are accessed at the same time when you do a transfer. And so the idea of the diff is to say, like, let's build a structure that is token details that contain the two, and we remove these two mappings here and here, and instead we have a new mapping that is token ID to token details. And hopefully, like the token details, which is a small structure, both elements will be in the same extension.
You have just one chance in 356 that it's not the case. And so we did I did that on the on the vertical test nets, and basically those are two pages of the of the I don't know what the name is one of the explorer that the vertical test net provide that is very cool because it shows you the witness, it's a witness explorer. Show you the witness of the of the proof that happens in Virkle. And what you see is that this is like the normal ERC20, sorry, ERC721, the one you can take right now from the library, and this is the packed one, the one with with this diff that was applied. And you can see that the the amount of gas went down from 55,000 to 49,000.
So this amount of gas is subject to change. The vertical numbers are not finalized exactly, but this gives you a trend that you can save gas by just reordering your your mapping and reordering your data. There is no logic change whatsoever, uh, but it's it's it's basically the same thing. And if you if you go to the link, everything will be available for you to check code the links. You will see that those are the accounts and the and the slot, and you'll see that here the slots are very different, and here the slots are basically the same ones. That's why it's more efficient. Another thing you can do is use more arrays and potentially custom arrays.
Uh there is something that we've been working on at Open Zeppelin that is a priority queue. People have asked us for that. And our first uh implementation of a priority queue used mapping. And it's pretty nice because we have the tree on one side and the item on the other. So when we reorder the tree, we just update this mapping and the nodes stay put. When you pop something, uh you have to reorder a bit the tree, but there is only one item to pop, you don't have to reorder the item at the same time. Uh and so it's pretty efficient on normal uh EVM, the current EVM. But then we did an implementation with an array that instead of having two mapping as a single array, so hopefully better data locality, and a structure on top of that.
And we use some some tooling to get accesses that doesn't check the length, otherwise it's it's it's a bit more expensive, but yeah, because here there is no length check anyway. Uh and and so this is a bit different because because the data is combined, the value the the item part and the tree part are in the same structure. When you do something like a pop, for example, you have way more reordering to do because you want to make sure that you're popping both the tree item and and the value item. So basically, this structure requires more data accesses. That's what you have to understand.
And this is a variant that is similar, but we also like use some logic to put the length at the same at the beginning of the data instead of in the other slot. So again, it's a bit complex for now, but everything will be linked. So if you want to check it out later, you can. And then I took those three implementations of the priority queue, and I basically pushed 10 values, the same values in all cases, to the implementation on the vertical test net, and then I popped the smaller value of the 10. And this is what we get. So basically, we see that the mapping operation uh is cheaper, it's more expensive than the other because we have this bad data locality by the mapping construction compared to the array construction.
And we see that this is particularly noticeable in the pop operation, which is kind of funny because these two implementations here, they do more storage accesses than the blue one. If you were to do the same experiment on the current EVM, you would see that for the POP, the blue is actually cheaper. So right now the blue is cheaper on mainnet, but in a few months it will be way more expensive. So that's one of the things where you you may prepare for vertical even if it's a bit worse today. And again, all these numbers are available. Here I'm just comparing the two implementation on the vertical.
I'm not comparing them to the implementation on the on the current uh network because the vertical trees are not the costs are not finalized. But this would be an interesting discussion for when we have a new test net. Uh so yeah, what we ultimately need is, I mean, it's nice to do all that and have all this assembly to help you, but what you ultimately want is you want the compiler to take care of that for you. So here are some clues of what the compiler could do. One of the things that could happen is that in the grammar, instead of using hashes, you could use the same hashes and and and just clear out the very last byte, bit, no, yeah, byte. That way you are sure that whenever you jump to a mapping alternative array, you are at the beginning of an extension and you have some some some room to work with.
And that's pretty easy. It's something we we do in our namespace uh definition, but could be done at the compiler level. Uh what you can do also, and that's very easy, Ivan, as well. Like you can do it in in assembly yourself, but the compiler could do it for you, is put the length of the array at as as the very first element of the array instead of putting it in another location. Like for Solidity, it was a no-brainer that all slots were identical. So let's just put the length somewhere in the data somewhere else. We vergul this assumption breaks, and so the compiler could reorganize what they do. And the very last thing that I think would be interesting, uh, and that's something that is very difficult to do as a user, so maybe the compiler can help you with that, is that when you have multiple mappings like this, like in ERC721, it's easy to combine the two, like we saw how to do it with a structure.
But then you have ERC721 enumerable that extends on ERC721, and that includes two more mappings that have the same key pattern. So basically, you would want these two to be not only in this in the same struct, but also in a struct that is the same as this one. So it would be nice if at the language level you had a way to say, oh, by the way, this mapping uh it's it's an extension of the previous one. So just like here there is a struct, here there is a struct, and put that at the end of this one, like combine the two and use the same storage. Uh, I'm not sure exactly how what it would look like. It's not very hard to imagine how it would work under the hood.
Uh we just need Solidity to go in that direction. Uh the real issue with all those three approaches, uh, particularly the first two, is that it completely changed uh the way Solidity represents storage. And Solidity has always represented storage the same way since the very early ages. And that's how we have uh contract upgradability that works because when you go to a new compiler version, if you describe your storage the same way, you get the same representation under the hood. This completely breaks that.
So this breaks upgradability of contract. It's a bit of a pain, so it would have to be opt-in, but uh I seriously think that the compiler needs to evolve to be more efficient in the context of vertical. And if the Solidity compiler doesn't evolve in that direction, well, maybe we need another language and another another compiler for the vertical EVM. So thank you very much. You have links to the slides. You have links to the source code if you want to run it and see the transaction. Because if you get the link to the slide, then you obviously get all the other links for free.
Yeah, thank you. So do you have any idea or have you ever considered um a customized data storage and to to replace um mpt over code tree to um to do to deal with a specific um uh data storage or uh data storage operations and context the specific context of data storage operations uh to release them um um to to use more optimized um uh on uh optimize the performance.
I mean that I I'm not sure I fully understood you mean a different storage for presenting trees like in the in the storage of contracts and yeah uh I'm such as um uh you have you you have a smart contracts and OS2 the um uh um operators are uh are uh uh are binary uh uh heaps and so uh do you have an idea to to to do to redesign the uh storage uh the data storage structure so so this is an implementation of a binary heap that we have so binary heaps because of the way they are designed are are very compact in the sense that you can put them in an array so if you want to store a binary in the storage of your contract that's that's really easy to optimize I think it that's not the main concern. Yeah I got okay yeah uh yeah the main concern to me like and binary heaps are not very frequently uh implemented in contract like we don't have an implementation edit in contracts even though like the project is six years old so I my main concern would more be like like solidity developers are used to using mappings for stuff that cannot be easily replaced with arrays like balances.
That's that's I think where the main concern is and I don't have a solution if any one of you has an idea of how to replace uh this kind of of of mappings here that the does with something that has a better data quality like you are a workstore for the next five years I think any other question? Thanks for the talk. Um will you guys provide libraries for vertical trees as you did for Merkle trees? So it's different.
Like we have a library for Merkle trees that allows you to verify a proof. The library that verifies proof doesn't rely on anything being stored on chain except the root. Basically the the Merkle tree that you are trying to prove is stored of chain somewhere. There is a representation of it. And what you want to prove is that a particular leaf is in the tree and you provide a proof for that. And everything is handled in memory in the stack. So that's not going to become less efficient or anything like that. Now the other question is like uh do are we going to provide uh proving mechanism for new data structures? People asked us like, do you provide like storage like libraries for doing storage proofs which like are Merkle but are not supported by your Merkle library?
Uh I'm not exactly sure if we will in in the sense of a vocal tree, proving that something is in a vocal tree is is very different in the sense that the construction of vertical means that the proof is uh a vector commitment basically. So it's it's the verification is very different from um from verifying a Merkle tree by doing successive hashes and rebuilding the the path. So uh I mean depends on the demand. If users demand it, we'll definitely consider it. I'm wondering whether the the EVM itself will not provide it through some kind of of pre-compile or something like that, because it's like it's it's big it be it's becoming part of like verifying the state of of the of the blockchain.
So like maybe the the node clients will provide this functionality, and if they don't, then it's just a matter of implementing the circuit and making it available depending on demand yeah we may do it. Okay thank you thank you uh we don't have time for other questions so thank you again for your question and for your talk give me a lot of data give me a reason give me a reason give me a reason give me a certain give me a lot of data
Related talks
- Ethereum in 30 minutes
- J1 RF 01 Rick Dudley what I don t like about Ethereum
- Ethereum in 25 Minutes, Version MMXVII
- Ethereum Forks And Their Impact On Smart Contract Security
- Ethereum Singapore | Vitalik Buterin on State of Ethereum Ecosystem
- Ethereum Execution Layer Specifications (EELS)
- From PGAs to 1559 to ETH2 — How Everything (and Nothing) Changes With Gas Fees - Matt Cutler
- The Future of EOF: Layer 1, Layer 2, and Beyond! by Danno Ferrin | Devcon SEA
- This Year in Ethereum - Evan Van Ness