EVM Stateful Precompiles | Andrea Vargas - Ava Labs
Ethereum Denver·Wed, Oct 9, 2024, 12:00 AM
Unlock the Power of EVM Stateful Precompiles: Discover how these precompiles can enhance smart contract functionality by maintaining state across transactions. APPLICATIONS FOR ETHDenver 2024: #YearOfTheSpork ARE OPEN APPLY NOW https://ethdenver.com/apply
Transcript
VM stateful pre compiles uh saying in other words is just how to enable new functionality at your Virtual Machine level for your own blockchain to be customized to specific use cases so before we start and probably up can someone help me with the yeah there we go okay so before we start is great to have like a common background on what a blockchain virtual machines are also a snet which is a concept um in where avalan you can launch your own chains and this will be important in the con in the context of of stateful pre- compiles because in order to have a pre-compile you will need to have your own chain or make the whole network to accept your pre compile we'll get into that in a minute but how we can see blockchains is like a replicated State machine which means that you you start with one state call it for instance um the different balances that you may have the different uh accounts that you have active right now whatever the state of your blockchain means and it goes to another state through um a Trans State transition function that is being registered in your virtual machine so the virtual machines Define this Logic on how we are going to transition from one state to another in the context of blockchain this is a block so each time you add a new block you change from one state to another um so you can think of virtual machines to be some sort like for those that are familiar to to oriented objectoriented programming you can think of these as classes and instances so in the same way that you have um objects and classes sorry in the same way that you have a virtual machine you can have an instance of that virtual machine which is a blockchain so let's say you you have the evm which handles all the generic um logic of the of the virtual machine and then you have the instance of ethereum mayet the the instance of the Avalanche C chain and you can have as well a subnet evm which enables you to create subnets or your custom chains uh powered by the evm so when we're talking about customizing the evm uh first we need to abstract what the evm is in the different layer of of how blockchain works so we have the case of ethereum where you can have the network level or the consensus level and then we have the execution level so the execution level is going to tell us like what are we going to change the consensus level is going to make sure every note has the same execution um the same execution step so we all have the same state and at the very top we have the solidity or application layer where we are deploying as a developer you mainly interact with this layer but you can also interact with the different layers um behind that and when we see for instance the avalan blockchain you can have the peer-to-peer the consensus and as well you have the execution level where you can launch different subnets each of them having their own execution environment or their own virtual machine and on top of that you can uh at the application Level do can do whatever you want you can have some evm where you writing solidity some Wason VM that you can write in rust or whatever you can have some uh virtual machine that uses move or or you can even have like custom virtual machines uh but for this Workshop we're only focusing in this specific part so it's not like building an application what we're doing today is just modifying The Interpreter of the evm so you can have specific functions Associated to execution environment so basic of the evm so in order to go from one state to another state you can have different kind of interactions so you can have an external Loan account interacting with another external Loan account such as as sending just balance of of the Native token right you can have interactions between contracts and contracts calling other contracts as well the thing is um for for this specific case we will be focusing on contracts um and one thing to to have in mind is even though the evm is tour in complete you cannot do or it's going to be super hard to start doing uh complex computations and it's going to be expensive so it's easy to create just simple computation uh assign some memory storage for creating uh different tokens managing balances that kind of thing but when you are trying to create like linear regressions and machine learning models like even more complex computation it's going to be hard and it's going to be expensive so for instance uh if if we one it's not only the the computational thing it's also when you're building in solidity you might face some limitations for instance the lack of libraries and this is something we're going to be solving with pre compiles so for instance one thing that you can use this for and something that that pre-compile is activated actually in ethereum for is for cryptography you is never good idea to write your own algorithm for cryptography so one good thing and something that is already battle tested is probably not out in solidity so one thing that you can think of how you are going to build your application is not going to the application Level but to the Virtual Machine level and so the pre-compile you can see it like like go code wrap it in an solidity interface so this is going to make it even more efficient solidity is actually um a language that has a lot of flaws and so and go is a little bit more battle tested has a lot of of libraries so if we can see like the different um benefits of why doing this is some a lot of has to do with performance it's performance uh you can use existent libraries which is really nice when you want to create a specific um things for let's say serero knowledge proofs you don't need to go and reinvent the wheel on solidity but you can just take what it's already built on go and just implement in your pre compile uh it will give you more security you will have uh more advanced functionality and it's going to be great so another thing is like for interoperability to verify data on communication Primitives this is something that we actually use as a the precompile to communicate different chains that they are not evm so if we have some way to decode and en encode data from one chain to another we will be able to start communicating to chain that don't doesn't even have the same execution invol environment um such as the evm so uh that is actually what happened with avalan war messaging if anyone want wants to go deeper into that um but for now just think of the pre compile as this um grapper on on some go implementation something to have in mind as well is that that pre- compiles are are not at the bite code level you don't have any bite code Associated to this you will have you will be modifying the specific code of the virtual machine so think of it as as a shortcut to execute a specific functions of your virtual machine um so an important thing is how this work is whenever a pre-compile is called this is deploying some a specific address we will see an example um in the next slide but it gets deploy a specific address and whenever this address is is being called by by any application the the evm will know it's a pre-compile and will not load any Vite code um in contrast if it's not a pre-compile then it loads a Vite code and interprets the the the bite code so for instance this is the interface that a pre-compiled contract not a stateful pre-compiled yet just a pre-compile contract is going to use and something also important is that you can customize the gas that is being consumed by this pre-compile why because this doesn't follow the rules of the op codes kind of uh charging of the gap as you do with simple applications on solidity but you can specify how much it's going to cost you to call let's say a a cryptographic function on the evm this will make it as as well more efficient and then you have the execution uh part of it so this is how for instance uh shatu 56 looks like it's going to be deployed at this specific address this is a reserved address this is why it looks so cool uh so so weird because it's not like um like created in normal way but they have some space of addresses reserved for these pre- compiles and this is how you interact with it you just need to import the interface and in order to to start interacting it's just like a normal interaction with any with like any other solidity contract it just will execute not in the smart contract itself on solidity but in the go implementation of it so how you create one and this is a g code where you can I don't know if you can see it really clear but the G code you actually import some Crypt shat to 56 in the in the project which is a go library and you can start implementing over here it is the implementation so that is how you start doing prec compiles um as that just computation but when we come to to stateful pre compiles this stateful pre-compile actually injects a state um it it can actually inject um or have some State access to to the to the database that we're working with um so it needs to implement some other structure it has the Run function with has this pre-compile accessible State this is really important and is really um dangerous as well because you are giving access to the state of the blockchain to your virtual machine so this is why you need an a network upgrade each time you want to implement these kind of of pre compiles either you need a network upgrade or you need your own chain and this is why this starts taking a little bit more um adoption once we can start launching our own chains instead of trying to customize the the general purpose chain so as these uh as the other one this one also have has some gas supplied and some remaining gas after the execution of the of the pre-compile and when we see this element of the pre-compile accessible State you can see that this is this gets the state database and this this is the part where you will be able to start changing the different key values on your database so yeah behind that all that is just key value pairs where you can uh each key refers of an address of a contract and the value you're you're going to be changing and it Al all hashes in the value in the value uh side of the key value purse um so what kind of different pre compiles can we see that Mak sense stateful pre compiles uh because these now is not only computation but you can also start um writing into State you can say for instance um I want an allow list for who can transact in my network making this the chain suitable for permission use cases or more private use cases instead of blocking or or having the allow list at the application Level you can say like I don't want anyone want to deploy I don't know a Marketplace or an nft on a institutional chain that doesn't have anything to do so you can leave that everyone like let's say everyone can participate but not everyone can deploy a contract on the Chain so that way you can restrict how the blog space going to be used for your specific application another thing that we can have is native Mentor this is really nice when we're trying to build cross chain applications especially when we are trying to use some native token that represents another native token in another chain or any other token so for instance if we are U talking about the subnet that use that wants to use I don't know usdc as a native token as a native gas you can actually set the native minor of the of the of the Native token to be the bridge contract of course this has some implications on how this is going to um you need to make sure that contract is audited and in order to to actually um mean those tokens natively correctly also another thing that you can do is creating a smart contract or a pre-compile that handles all the fee configuration and this is great for instance when you are um want to let's say reduce the gas fee or the gas or the gas price that you are paying a minimum guys price uh on each transaction uh before this precompile you will need to go for a n Network upgrade but if you activate this pre-compile you can say that a contract is going to handle it of course these contracts will have an admin and you can do it however you want but this allows you to customize the the execution layer of your blockchain without need needing to go um at an application Level um so all this happens and how you uh configure this kind of precom polies on your Genesis block the Genesis block keeps uh track of where is this going to be activated because some pre- compiles could be activated at the very beginning some others need to be activated as an upgrade so if you're going to activate that as um in the very beginning you will need a pre-compile you you specify the address and you will be able to uh start interacting with the solidity interface as I just showed um and if you want to go deeper because this is not like a super long talk um to see how you can deploy your from pre- compiles this is actually go go code uh we have in here an academy that you can check out uh so basically it will allow we we created something that we call the pre-compile VM this pre-compile VM allows you to only focus into the pre-compile part you don't have to it abstracts all the complexity of the subnet evm or the evm as a whole and it will allow you to only deploy this so basically if you want to see how the steps look like of course we're not doing it today but you just need to clone the repo and you create a solidity interface this solidity interface is how you will be interacting with it from another contract and this solidity interface is just like as simple as any interface that you have seen in solidity before but once you have the solidity interface you can compile that and then you can create um we have this generate pre-compile sh that is super helpful for you to create all the boiler plate con uh code that you will need in order to generate a the this um this pre compile and now that now you need just to to fill out the logic of how your pre compile is going to work there is where you start putting the the functions or the libraries for from go um either the shat 256 or different um cryptographic functions different kind of Logics that you can put there I I've seen a lot of hackathon projects including uh functionalities for serero knowledge proofs um so yeah you can import there a thing uh the thing you're going to do and then you need to specify when you're building a virtual machine you need to tell these virtual machine what plugins or what pre- compiles are you activating so you just modify one file set the the reserver you're going to be using to activate and you comp complete the module. go which is a file where you specify the address that you're going to deploy too so you just need to make sure you're not using reserved addresses for another thing or you cannot have two pre-compiled with the same address Associated to them because if not it's going to fail the build you just build the virtual machine and start a network um so yeah that's it like it you can run a network as well just test it out see how it works and we do have in in the academy that I just show someone miss it here is the QR um you will find different exercises from doing um computational pre-compile just to generate different cryptographic function functions up to creating allow list or anything that just start toching the state of your virtual machine um so thank you so much everyone and if anyone has any any doubt we can happy to talk after I I go step down thank you so much Andrea uh everyone uh if you want to learn more about the topic don't hesitate to consult the academy they're here for here they're here for that and they'll be more than happy happy to give you more information about um the shortcuts on evm thank you so much Andrea so now for our next talk we will have morat adinis the founder of prev who will join us and he will be here to present uh pre-confirmation on mainnet no yeah there's a mixup I'm happy present my topic I've got a uh
Automatic transcript — names and jargon may be misspelled.