Exploring the EVM+ paradigm with Arbitrum Stylus

ETHCC 7 · 10 Jul 2024 · 38:20

Daniel Ivanov, an R&D engineer at LimeChain, explains Arbitrum Stylus, an execution environment that runs WebAssembly (Wasm) programs alongside the EVM. He describes how contracts written in Rust, C or Go share addresses, ABIs and storage with Solidity contracts, so the two can call each other. He argues that Stylus makes pure computation and memory use far cheaper, but gives little benefit for storage-heavy ERC-20 logic. A live demo deploys a Rust ERC-20 and calls an Ed25519 signature verifier, which he says costs about 288k gas against roughly one million in the EVM. At the time Stylus was on testnet.

Speakers

Topics

From ETHCC 7, hosted by Ethereum France.

Transcript

Test. Okay. So hi everyone. My name is Daniel Ivanov, and I'm part of uh Lime Chain, which is a Web3 consultancy and development company in the blockchain space, and I'm an RD engineer there. For the last several months, we have been diving into Arbitrum Stylus, and I'm going to share with you more information on what we found out and uh what's good about it. So just to give you an overview of uh what we're going to talk about today, and after that, we're going to do a quick um presentation or a quick benchmarking, live benchmarking.

So, first of all, I would like to go over the foundational principles that make Stylus um something that's worth talking about in the first place and why it achieves the values that it um basically um presents to the community. And the other thing is doing the benchmark. Now just to start off, um arbitrum stylus is uh execution environment and execution environment which is similar to the EVM but different. So uh you can write smart contracts in it, uh they're called programs.

Um and they are executed, those programs are executed similarly to the EVM. However, the actual virtual machine that's being used is not the regular Ethereum virtual machine that you have been used to, but it's rather uh a new virtual machine called Wasm virtual machine. And WA WASM by itself is not developed by Arbitrum. Uh it's um a technology that's being developed for the web um ecosystem, it's just that it nicely fits within the blockchain space as well. Now, as I mentioned, Stylus is an uh an adapted Wasm execution environment.

It's developed by the Arbitrum score team of Chain Labs. It's not yet on mainnet, it's in testnet, uh, but still we can uh play around with it and see what the experience is, start building stuff, etc. Uh the team already did um an audit and um they've deployed it on the testnet Sepolia, Arbitrum Sepolia testnet. Um so to start off by saying that we already had Wasm execution environments before, we already seen them. Uh for example, Cosm Wasm is something that existed in the Cosmos ecosystem, and we already already have Wasm-based execution environments in, for example, the Polkadot ecosystem, uh where the runtimes of the Polkadot uh parachains or the actual relay is a Wasm execution environment.

So, what's the big deal of of Stylus and why it is worth talking about it in the at all right now? Now I would like to make the case first of all that in the Cosm Wasm uh situation or the uh Polkadot ecosystem, there was no concept of interoperability between the EVM and the Wasm execution environment. So in the CosWASM case, you had a module that's completely isolated within the let's say EVM module that a Cosmos node runs.

In this case, if you have a program written in let's say WASM and deployed it on a Cosmos node, it was it was not able to interact in any way with the smart contracts on the EVM virtual on the Ethereum virtual machine. And that's basically splitting the chain in two doesn't make any sense or doesn't take use of the interoperability or the things that are written in the EVM. So it was um it was not that much of a success. Now what the stylus theme did is something that's may seem similar, but it's completely different.

Here we have an overview architecture of it's extremely simplified architecture of a node where you can see that user requests are coming from the left side, it hits it, they hit the RPC endpoint, and um we got we we get to the point where we have this EVM plus. I think that the team uh at Arbitrum uh renamed it to multi VM. Uh so previously we had EVM plus, now it's uh multi VM component, and this multi-VM component um actually allows for multiple execution environments to exist in in the Arbitrum node. Now normally we have only the EVM there.

The EVM receives the transaction, it starts executing it. If it needs to, it accesses the precompiles. Precompiles are code that is written outside of the Ethereum virtual machine, which might be extremely useful for smart contracts to use, but it's extremely inefficient to do this logic on an EVM bytecode. For example, doing uh EC recovery, so recovering who signed this message is done through a precompile. So in this case, we have the EVM that accesses the precompiles, and if you think about it, it accesses the storage layer as well to quote something from the storage if it needs to vote values from a near C2 any mapping, for example, etc.

So normal nodes work without the red box there, without the red box, which is the stylus WASM box. And that's how normal uh Ethrium nodes work. Now, what Stylus did was or the Arbitrum team did was they introduced the second box, which is the red box, which is the uh Stylus virtual machine. And not only that, um those arrows that are marked with red are the most important thing that the Stylus team did. Uh let's start with with the first one that connects the EVM and the Stylus um virtual machine.

Whenever you interact with uh contract, let's say that you have a contract and you want to interact with another contract. Whenever you call the second contract, there is an interceptor that is part of the EVM plus module that checks whether the contract that you are targeting is uh Ethereum-based contract or stylus-based program. Depending on the uh the the thing that you target, the EVM plus module might intercept this and spawn the stylus virtual machine to proceed the execution on the stylus side. To be able to know, depending on the thing that you interact with within the let's say Ethereum transaction, to know whether you have to switch between the two VMs.

That's the first thing. That smart contract has a representation in the state, similarly to how Ethereum smart contracts have representation in the state. Now the ABI, which is the in public interface of smart contracts, is the same for stylus as well.

So when you write your stylus programs, you have several functions that you want to expose to be external. So those external functions are encoded or represented the same way that you would represent an Ethereum smart contract. This makes it again easy for other smart contracts or even external tooling, simply talking with the arbitrum node to be able to interact with the stylus program. So that's on the uh interaction between the EVM and the stylus.

There's there's full interoperability between smart contracts on the EVM side and the stylus side. All of that is done through an interface that connects the Ethereum execution environment with the persistence layer.

So when you want to vote the balance of an address in an ERC20 contract, under the hood, you do an S-load operation that accesses the storage. Similarly, for stylus, uh, when you want to vote something for from the storage, you do the same thing. You vote it from the storage, you vote it for from the same storage. So the storage that's being used for stylus program is part of the arbitrum state that's similar to the Ethereum virtual machine state. It's just that the interface that you need to use to get there is adapted for stylus. So back to the question why we why it's worth talking about it initially.

The first reason is that there's full interoperability between stylus and and and the EVM. That's that's one thing. Um not to mention the fact that when you have stylus uh or wasm-based execution environments, you're not restricted to let's say compiling from Solidity, Viper, etc. You cannot compile from that, but you can compile from Go, you can compile from Rust, C, etc. That's that's an enabler, a huge enabler that allows you to tap into existing libraries written in Rust, C, Go, etc., and use them in your smart contracts on Arbitrum. Now, as I mentioned, reason number one is interoperability.

It's possible because you have the program and the smart contract identifiers being equivalent, so that you have the same concept of addresses, the interface of how you interact with the stylus program, which is the function identifiers, they are the same as well. So you have the same concept of an ABI. The third thing that I already mentioned is the intercontract communication, which is the interceptor that figures out whether you're talking with a WASM-based program or you're talking with uh an Ethereum-based smart contract. Whenever you call something external to your contract, if it's required, the context switches to the execution being a stylus one.

And the last thing, the persistence layer which is shared between the two virtual machines. Now, okay, we have a virtual so far. We've identified a new type of virtual machine that allows you to write contracts in other languages, and it's completely interoperable with the existing the existing Ethereum already deployed Ethereum smart contracts on Arbitrum. That's that's already a valuable thing, obviously, but that's not the only thing that let's say Arbitrum uh the Arbitrum team uh went for it. And the second thing is the following.

On the left side, um I would assume that you know what the case for the EVM execution is. So let's let's go over it quickly. So we start off with a smart contract written in Solidity. Um you're already familiar with that. You write your Solidity code, and what you do is you use a Solidity compiler to get to an EVM bytecode. And you can see it in the yellow box on the left. So from the smart contract, you get to the EVM bytecode. That EVM bytecode is not directly executed by the processor in any way.

First of all, you deploy that EVM bytecode on the chain, and when you deploy it, you basically send it to an RPC endpoint, which is a node implemented in let's say Go or Rust, and in the general case of an Ethereum node. And this node stores the bytecode in its state. Whenever this contract is being called upon, the bytecode is loaded from state, and the following thing happens, which is the important thing. The Go code that you see there in the blue box, andor the Rust code, has the implementation of the Ethereum virtual machine, which starts to read from the bytecode of that smart contract.

Starts to parse every bytecode from it that it reads one instruction, one instruction after another. And this execution is called an interpreted execution, which means that the code interprets the bytecode one uh one uh operation code after another, and that's not the let's say the lowest level that you go. If you think about it, all of this execution within the Rust or the Go Ethereum node is actually compiled through the Rust or Go uh compiler to a binary, and this is the actual thing that's being executed by processors. So when you compile the Ethereum node to a binary, that's the thing that you pass to the to let's say the processor to to execute with native speeds.

So in the EVM side, you have some indirection interpretation of the Ethereum bytecode um and not directly executing the EVM bytecode by the CPU it's interpreted uh it it has a let's say an order of magnitude uh less of a speed compared to native execution uh you cannot if you think about it if you have a let's say addition of two numbers in Solidity and you execute them in an EVM node it would be on order of magnitude slower than if you sum two numbers in let's say Rust code that's executed natively and that's a rough approximation. On the right side we have the wasm situation where we again start with a wasm program and in this case we compile it through the styler style was compiler to a wasm binary.

Now you might have heard that wasm uh compilers produce a wasm bytecode but actually it is closer to a binary than a than a bytecode. So in this case when you compile something with the stylus compiler it is directly being executed by the processor through a thin layer of a virtual machine called the wasm virtual machine and this is the big reason why we have this this the following benefit actually in the Ethereum world uh if you see those subcodes let's say addition multiplier subtraction etc all those subcodes as I mentioned they are interpreted meaning that they're not directly executed by the processor so reason number two is that WASM is simply way faster uh and it's not faster because the let's say uh EVM is slow by swore because the people writing the EVM were not that good at writing it or constructing it.

It is slower due to the fundamental problem of not being executed natively by the processor. And wasm is is what is faster due to that. So we can see the following big things with with Wasm. Compute operations, and by what I mean by WASM is actually stylus here. So compute operations are from 10 to 100 times cheaper. So they're executing 10 to 100 times faster. You can add or you can process pure computations from 10 to 100 times faster.

And if you do that, then the resource consumption is again 10 to 100 times slower, then the actual gas, which is an approximation of the resource consumption, is lower. This means that if you write pure computations in stylus, you get this extreme benefit of doing the same work but way cheaper in terms of gas costs. That's the same for memory as well. They are 100 to 500 times cheaper because they're purely executed faster, therefore, gas consumption is way slower, uh way lower.

Now you have situations where the stylus program does not do pure computations or consumption of memory. We have you have situations, let's say if we think about an Ear C20 contract, the majority of the gas consumption comes from wading from state or updating state in some way. When you transfer balances or when you add approvals, you do safe transfer, etc. The majority of the gas comes from loading or storing to state. This means that in theory, and that's the way it's in practice, you might you will not get a huge benefit in writing a stylus-based ERC20 contract compared to writing it in Ethereum in DVM.

So you have to pick the right things to write in Stylus, which are pure computations. So let's let's go over what um how does a stylus let me zoom in a little bit. How does a stylus um program look like? And I'm going to start with the easiest example that all of you might be aware of, or actually probably will be aware of. So what I have on the left side is the stylus SDK written in Rust.

That's developed by the uh off-chain labs team, and they are releasing it as a software development kit for writing stylus programs. It helps you with compiling, deployment, checking was on byte codes, etc. And I'm going to go over an Ear C20 contract example, which might not yield um gas benefits, but then I'm going to go over an implementation of uh ED uh 25519 um signature verification, which is something that's not present as a precompile in DVM. And if you want to implement it in the EVM, it's quite costly.

Uh, but if you have it as a stylus uh program, it's way cheaper. So just to go over the syntax and so that you can um be aware of how program uh how does a program look like you have here uh a struct that's um holding the uh name, symbol and decimals uh of of the ERC20 contract. Those you can think of as the immutable constant constants that you set when you deploy an ERC20 contract, and this uh trait um defines those three properties that must be defined when you deploy the ERC20 um contract. The other thing that you can see is the uh declaration of what will be in your storage.

So in this case, we see that we have the ERC20 that has the normal or the familiar things that you um would see in an ERC20 contract. It has a mapping of the balances, it has a mapping of the allowances and the total supply. Um that's that's the definition of the storage. After that, you have the um declaration of the events that you will be using. And keep in mind that all those structs are macros provided by alloy, which is external project not related to uh to stylus in in any way, it's just that they provide a very good declaration or let's say structure definition uh for solidity types that you want to see uh that you want to use purely out of the EVM. Let's say that you have something a backend or something that interacts with smart smart contracts, those macros generate uh proper events or structures so that you can interact easily with your um EVM based contracts.

After that, we have a definition for the errors that we might throw, and actually so far we haven't seen any implementation of the logic, and now we're getting into that. We're implementing the ERC20 um struct where we define an internal function for the transfer. You can know you notice that all of this is a normal Rust code. You have uh mutable reference to the self, you have the address uh that's going to send the amount the uh the receiver and the value. This is an internal function, and the logic here is something that represents a normal change of the balances, which is not publicly which is not external.

Then we have the minting, which creates uh a value out of out of thin air, we have the burn, and all of those functions you can think of it as private functions of the RC20 uh struct. Now we are getting to the public stuff, and you can see that it's tagged by the external tag here. When we have this, we define the ERC20 um interface, the following functions to be external, and in this case, we have the name, symbol, decimals, total supply, balance of, and we have the public functions like transfer and transfer from. Now you can see that we have here the events.

This is something that's provided by the Stywas SDK. You can emit EVM based walks through that. And that's how an ERC20 contract looks like in Stylus. And I'm going to show you what is the actual um experience when you want to deploy this. So here we have the cargo stylus tooling, which is allows you to check the bytecode, the your code base for constrictions on WASM size, for actual deployment requests, for activation requests, etc. Now, this private key is a demo private key, so do not worry about it.

So what I'm what I have here now is an arbitrum neither vocal node running on my machine. It's a couple of um steps. Um it runs uh a couple of Docker images. Um so I have a container running a sequencer, and if I click on the deploy here or do the deploy, I'm providing the endpoint, which is the local host, and I'm providing the private key that deploys it. Now the thing that I actually missed showing you is the not only the implementation of the ERC20 contract, but actually defining the entry point.

So in a stylus program, you define an entry point, which is the thing that you want to deploy, or the contract or the program that you want to deploy. And that's how the stylus tooling figures out okay, this is the thing that I need to compile, this is the thing that I need to check against, this is the thing that I'm going to deploy. And here are the actual um implementations of the internal functions. So you you here you have the stylus test token that extends or inherits the ERC20 token that we saw. If you remember the mint functions were private, uh here we are exposing them because they are part of the external tag here.

Um it deployed the code at this specific address, and this is the deployment transaction hash. The thing with stylus programs is that when you deploy something, you need to explicitly activate it with the second transaction, so to say. So the tooling automatically calls the activation for you, but in theory, um you need to do a second transaction to activate it. Now I want to show something that's more explicit or valuable purely to basically do stuff in in Stylus, and that's the um Edwards verification. Uh the Edwards verification is basically it's not widely used um cryptographic scheme in the Ethereum ecosystem because we're used to using ECDCA uh keys in addresses, but it is widely used in the um let's say traditional web 2 ecosystem.

And um here we have a verifier of ED255519 uh signatures. You can notice here that I'm actually uh importing a random, not so random, but uh implementation of ED25519 verification scheme that's written by the Rust community, it has nothing to do with web3 in any way. It's just that it's an OSTD compact implementation of this verification, and I'm purely importing it like a normal Rust program would do.

When I import it, I I define my um external uh function, which is the verification, and I don't have anything in so I here's my entry point. This is the contract. I don't have any I don't have any storage in this, purely a whole contract that has a verify function, and um in order for me to demonstrate it uh better, I I did this verification function state modifying. And it's emitting walks. Now when you call verify, you provide uh the message that you have signed, the signature, and the public key.

And when you call when you call the function, you get this you get to this implementation, which is basically pub public key and signature, which is being created from the slice, is done uh is is actually a logic from from the Rust crate. Then you have the public key verify, which is doing the verification of the signature, and after that, when we have the result, we emit a lock of the verification result, basically whether it's success or or not success. And I'm going to show you how it looks like here.

So two things worth consider uh worth talking about. You can see that actually you might not be able to see it that that's good, but um, I'm using cast, which is another project. Um it's basically a tooling from okay from from Foundry. You can use cast to interact with the stylus programs because they are fully interoperable with the EVM programs. Uh you are doing a normal call so you so you do cast send to the RPC endpoint, you provide a gas limit, you have the private key that executes this, and then we have the address of the program. This is the address of the program which I previously deployed.

Here is the the function that I'm calling, uh verify, etc. etc. Here are the um this is the message, the second is the um signature, and lastly is the public key. And when I when I submit it, you can see that I'm using 288k worth of gas, and I have one lock which if you remember had a single Boolean property, success true or false. In this case, it ends up in with a one, meaning that the signature verification was successful, and if I let's say remove uh change the public key and um modify modify it from five to four, the data here and ends with zero, meaning that the verification is unsuccessful.

So we have uh a simple test showing that the verification works from for proper values, but it doesn't work for improper values. And the the important thing to know here is that this costs 280k gas in the EVM implementation did this this costs around one million gas. So this is a use case that shows okay. If you do this in in stylus, you have the gas cost benefit of doing the same logic, but it's way cheaper. So the takeaway here is if you want to do purely computationally intensive operations and not that storage of the operations, think about using stylus for arbitrum related projects.

Um if there are any questions. Thank you. Uh just to make uh things clear. When you're talking about native code, do you mean some standard existing was a machine with like a head-of-time compiler or something? Or I just like opened a description of the project say something like where the native code when the native code actually appear and like do we have a sort of a dedicated node for uh some um uh specific uh contract logic or so or what?

Yes. Um thank you for the question. Uh so when I refer to native execution, I mean the fact that when the stylus VM is working on the node and it's requested to execute a specific stylus program, that stylus program, the code that not the code, the binary that represents this stylus program is compiled ahead of time and it's compiled to a binary that's very close to let's say you compiling Rust code. Uh and Rust code compiles to a binary.

So when you compile the stylus program, you compile it to a binary, and when you instruct the stylus VM to execute that binary, it executes it. You you can think of it as a very little overhead compared to simply executing Rust code, for example. Um so if I understand correctly, um when compiling uh Solidity uh directly or EVM compatible called uh on a stylus-based uh machine, uh it costs uh way more gas, right? So it it doesn't matter whether you're the deploy you're deploying it on Arbitrum or other Ethereum nodes, if you deploy or if you compile Solidity code, you're compiling to EVM bytecode.

Yeah, and that's executed by interpretation by Go or Rust nodes, which is slower, and that's why it costs more gas. Um and so what what would be the the motivation to write even Solidity code on the stylus-based uh chain instead of directly was it only for like uh retro compatibility purposes? That's a good question. In an ideal case, the Wasm Stylus uh virtual machine is optimized enough so that in all cases you're at least in all cases, you're at least the it costs the same gas as the Ethereum uh implementation. However, um in the Ethereum implementation, uh, since we have an a big ecosystem of implementations, let's say for ERC20 contracts, which are gas optimized, etc.

We have people that have worked for years to shave off some little gas from every little detail of Ear C20, for example, logic. Whereas in the Stylus virtual machine, because it's so new, there are plenty of optimizations that you can do on the compilation side so that you produce a binary, oopsor, a binary that actually costs the same as DVM. Right now it's not the case. If you write an Ear C20 contract on Stylus, it will be more expensive simply due to the fact that you haven't uh had shaved off those compiler inefficiencies yet. But in the coming years, and if the team continues to focus on on the stylus compiler, they will get it down.

Perfect, thank you. Yeah, I guess we are out of time, so thanks a lot, and uh yeah. Thank you.

Related talks