GithubHelp home page GithubHelp logo

merklefruit / evm-from-scratch Goto Github PK

View Code? Open in Web Editor NEW
23.0 2.0 2.0 356 KB

Implementing the Ethereum Virtual Machine from scratch & documenting the process

JavaScript 7.25% TypeScript 92.75%
ethereum evm

evm-from-scratch's Introduction

EVM From Scratch Challenge

I implemented the Ethereum Virtual Machine from scratch in Typescript. The only external dependency is the ethereum-cryptography package used for the keccak256 hash function.

Introduction

The EVM is the core of the Ethereum protocol. It is a stack-based virtual machine that executes bytecode and updates the glogbal state according to the rules described in the Ethereum Yellow Paper. The EVM is responsible for executing smart contracts and is what makes Ethereum a "World Computer".

What is this challenge about?

W1nt3r.eth is the creator of the EVM From Scratch challenge, which consists in a series of 116 tests that need to be passed sequentially in order to have a working EVM implementation. The challenge is a great way to learn about the EVM and how it works. I highly recommend it if you want to really understand what is going on under the hood of Ethereum smart contracts.

Progress Log

  • Day 0: Research of relevant learning material & tools to get started
  • Day 1: Gathering more resources & reading Mastering Ethereum chapter 13
  • Day 2: Setting up the EVM-from-scratch challenge & EVM class
  • Day 3: Reading the yellow paper & EVM inception (EVM inside EVM)
  • Day 4: Stack & memory implementation & first Opcodes
  • Day 5: PUSH, POP, SUB Opcodes, MachineState context struct
  • Day 6: Most Arithmetic, Comparison, Bitwise operations & JUMP Opcodes
  • Day 7: Memory structure & related Opcodes
  • Day 8: TxData, globalState, Block data & related Opcodes
  • Day 9: More Environmental Opcodes & CALLDATALOAD. "Officially" started the challenge!
  • Day 10: CALLDATASIZE, CALLDATACOPY, CODESIZE, CODECOPY Opcodes
  • Day 11: EXTCODESIZE, EXTCODECOPY, SELFBALANCE Opcodes
  • Day 12: Research & study on the Storage / data layer of the Ethereum protocol
  • Day 13: Simple Storage implementation, SSTORE, SLOAD, RETURN, REVERT Opcodes
  • Day 14: Upgraded test file & refactored code, added GAS, LOG Opcodes
  • Day 15: Major EVM class refactoring & started CALL Opcode
  • Day 16: Final CALL implementation & RETURNDATASIZE, RETURNDATACOPY Opcodes
  • Day 17: Opcode runners refactoring, DELEGATECALL, STATICCALL Opcodes
  • Day 18: CREATE, SELFDESTRUCT Opcodes. Challenge completed!

Running the EVM locally

Get started by cloning the repo and installing the node dependencies:

git clone https://github.com/nicolas-racchi/evm-from-scratch.git
cd evm-from-scratch/evm-from-scratch-challenge
yarn

I added a command-line script to allow running the EVM with a simple command. If you want to run simple bytecode input (without passing any transaction data or block data), you can use the following command:

yarn start:bytecode <your-bytecode-input>

# example:
yarn start:bytecode 604260005260206000F3

If you want to provide additional runtime data, you can use the input.json file in the root directory as a template for the expected input format. You can then run the file content with the following command:

yarn start:file input.json

The program will output the step-by-step execution in a file under the evm-from-scratch-challenge/logs directory.

Running tests

This command will run all challenge-related tests:

yarn test

Credits

evm-from-scratch's People

Contributors

merklefruit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

andiivanov ae-0h

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.