EELS Workshop Part 1

Ethereum Cat Herders · 10 Jul 2023 · 12:09

Sam Wilson presents a short hands-on tutorial on setting up the Ethereum Execution Layer Specifications (EELS) and writing a new EVM opcode. The walkthrough covers cloning the execution specs, creating a Python virtual environment, installing the project, and a tour of how each hard fork is a self-contained implementation. Sam then adds an example SUB10 instruction: stack handling, gas charging, the program counter update and registering the opcode in the interpreter. A second part is planned for writing the test.

Speakers

Topics

From Ethereum Cat Herders; community: ECH Institute.

Transcript

foreign welcome to a quick tutorial on setting up the ethereum execution layer specifications also known as eels and writing an opcode for the evm I'm your host Sam Wilson and uh yeah let's get started so we're going to start by just making a little directory to hold all of our stuff for this then we're going to clone the first project we need which is the ethereum execution specs so the execution specs is a fairly regular python project it's about using

talks and Pi test it runs on python 3. although if you do want to run the tests we are currently limited to python version 3.8 exactly we're working on changing the Restriction but for today you need python 3.8 um I don't have python 3.8 on this computer so unfortunately we won't be running the tests but I can still walk you through the the initial setup and writing the op code it's been a little while since I've worked on the specs so uh please bear with me a little bit okay so now that the specs is cloned Let's uh go in there

so the first thing you need to do when you're working on development with the specs is set up a virtual environment this helps keep your package environment clean if you're a regular python developer you're probably already very familiar with this so the way you do that is python3 Dash M remember to specify python3 then so this chooses the module then from the python standard library and then the second argument here is the directory that you'd like to create the virtual environment so let's go ahead and do that and then you activate the virtual environment with a DOT and activate perfect so now we're inside the virtual environment any packages we install here will be kept separate from

your system python packages so pip is the package manager for python projects so we're going to do a pip install Dash e which means install from the current directory well sorry it means install in editable mode and then the dot here means install from the current directory um so we're going to install the execution specs into our virtual environment it'll go and fetch all of the dependencies and get it all set up so we can do some development here perfect so let's uh take a quick tour here so at the very top level we have uh most of our project configuration files some

metadata documentation for hard Fork information then if we go into the source directory that's where most of the magic happens let me just clear my screen here okay so inside of this director we have the ethereum subfolder that's where the entire specification lives ethereum optimized right here that contains a optimized implementation written in Rust of our database system and we swap that in for the python implementation when we're syncing mainnet otherwise we'd absolutely never catch up to the most recent blocks and finally we have the ethereum respect

tools directory that contains most of the tooling that we use so there's Tools in there for creating a new hard Fork there's Tools in there for compiling our documentation and some extra lints and checks that and then tools for code quality that live in there So today we're most interested in the ethereum directory so we're going to jump into there and uh kind of a similar idea um give you a little bit of a tour so um in this director we have a mix of implementation details and hard Forks so all of the hard Forks here like Arrow Glacier uh Berlin Byzantium Constantinople they all live in in this

directory and then there are also implementation details like ethash uh ROP all of those things live here um we organize these specs as into these directories and each directory contains a complete implementation of ethereum for that hard Fork so if you were to go into say um Shanghai you'll find everything you need to implement Shanghai independent of the previous hard forks and that's that's true for all of the hard Forks in here so the reason we've taken this approach is to kind of um give developers and implementers a snapshot of ethereum exactly as it is

without any distractions so you're not going to find any like ifs or different Behavior depending on what block you're in once you get into uh let's say Shanghai um that is only Shanghai and that's great from an implementer point of view because you don't need to think about like oh you know this little piece of code is turned on this other piece of code is turned off once you're here everything in this folder is turned on and um yeah it makes it much easier to understand it's one of the big advantages eels has over say copying the get implementation so we're going to jump into the VM directory here that's where the evm is implemented so it's a fairly normal python

implementation of an interpreter um we're going to go into the instructions directory here because that's where obviously instructions are implemented um and then we're going to be adding an OP code sub 10. so sub 10 always subtracts 10 from the most recently pushed item on the stack we're going to pretend that's an arithmetic function so let's jump into the arithmetic file here um So currently our documentation is written in restructured text that is probably going to change to markdown so a lot more people are familiar with markdown but for now it is for structure text um we have our own custom integer types

like YouTube 56 u64 and those are all implemented in the base types module so if you get curious about what's available say you need something fancy for cryptography it'll probably be in in base types so let's go down here and take a look at the sub instructions that's where we're going to be basing our implementation off of so um there we go so the so the sub instruction here is divided up into several parts and every instruction that we Implement is divided into these same parts so first you have the doc string so the doc string just gives

documentation for the function following that you'll have the stack operations so all of the operations that modify the stack so popping inputs off uh the swaps all that are going to be implemented here in the first part of the instruction then we have the gas calculation and charge and gas that's the second section and then finally you have the actual implementation of the function and then most shopcodes will have a program counter modifier here um some will have a more complicated a program counter modification section especially the jump family of op codes um push also has you know a little bit more complicated because they have to account for the length of the immediate argument but everything else just has a PC Plus

equals one so let's copy our sub-implementation here and we will create our sub 10 implementation so sub 10 subtracts 10 from the top element of the stack and push the result back on the stack perfect so the evm object up here contains everything about the state of the evm um so we can see down here that the stack is contained within the cvmr object since this since sub 10 only takes one argument

we're only going to pop one item and we're going to call it input and then we're going to change the implementation here so result equals input minus 10. and then we push the result back on the stack and we increment the program counter by one so that's a pretty easy implementation um let's dive in a little bit to how the gas charging works um so let's say we want to change how much gas this this charges so if we jump to a definition here there we go so now we're at the definition so here you can see all the constants they're very similarly named if you're familiar with the yellow paper to the constants

defined there for the sub 10 let's say that it's a more expensive op code so we're going to select the gas high constant here and we'll go back to where we were and we'll change this to gas high the gas Scout oh and we have to import that as well there we go a little bit of formatting we have to do here so we do have tooling that'll do all the formatting for you fixture code make it look good the instructions for running that are all in the readme or rather the contributing.md file

um unfortunately I don't have python 3.8 so I can't show you that today but uh when I get to recording part two I will be able to show you so let's go back down to our sub 10. yeah so if we jump a little bit into the charge gas function here um it's pretty much as simple as you would expect and a lot of the specifications are written like this so we have our check here that just says if the evm does not have enough gas left raised out of gas error and then if it does have enough gas you subtract it from gas left and you continue on and that is you know a great example of how simple the specs are so yeah now we have our sub 10 instruction written and finished so

let's go add it to The Interpreter so we can do that by going uh in the init.pi function so this contains our module rather or package this contains all of the op codes that The Interpreter knows about so we're going to add it here after side extend and we're going to call it sub 10 equals 0x 0 C so that gives us the name to uh integer mapping and then we have to add it to uh this addictive callable functions Ops dot sub 10.

so the arithmetic functions sub 10 evm and there you go and now The Interpreter knows how to use the sub 10 function um that's pretty much it for today in the next installment of this you know incredibly short series we're going to talk about writing a test to prove that the sub 10 instruction works correctly um that's it for today and uh thanks a lot for for coming out talk to you soon

Related talks