Coder Social home page Coder Social logo

code-chunks's Introduction

EIP-2926: Chunk-Based Code Merkleization Example

This repository contains:

  • merkleize.py: based on a script shared by @vbuterin. This script contains the following functions:
    • chunkify: Takes a bytecode and return chunks of 32 bytes each.
    • treefy: takes 32 byte chunks and the total byte code length, and returns a binary tree containing all the chunks.
    • next_power_of_2: This function is used to calculate the total number of leafs in the binary tree. For example:
      • For 2 chunks, next power of 2 is: 2, the binary tree will contain 2 leafs in total.
      • For 3 chunks, next power of 2 is: 4, the binary tree will contain 4 leafs in total.
      • For 5 chunks, next power of 2 is: 8, the binary tree will contain 8 leafs in total.
      • For 6 chunks, next power of 2 is: 8, the binary tree will contain 8 leafs in total.
      • For 9 chunks, next power of 2 is: 16, the binary tree will contain 16 leafs in total.
      • For 513 chunks, next power of 2 is: 1024, the binary tree will contain 1024 leafs in total.
    • sha3_sha256: Uses hashlib.sha3_256() to hash the data.
  • treeviz.py: Contains a function generate_tree, which receives the tree generated by merkleize.py and generates a Graphviz tree visualization.
  • examples.py: this script makes use of merkleize.py and treeviz.py, in order to generate the trees corresponding to the contracts in the bytecodes directory, this directory contains some examples of contracts displayed on etherscan.io:
    • BAT
    • LINK
    • MKR
    • OMG
    • USDC
    • LEND
    • WBTC
    • YFI
    • USDT

Executing

  • Install Graphviz.
  • Install the graphviz python library:
pip install graphviz

After that we can execute the example.py script which will generate the SVG files in the render/ directory.

Results

The result contains a Binary tree containing all the chunks, after the tree is generated, a root is calculated based on the chunks binary tree root (0-index element) and the metadata:

The metadata corresponds to the total bytecode length as a Big Endian 32 bit number.

The leafs of the tree corresponds to hash(chunk + code_start). Except for the empty (zero) leafs which are generated to fill the binary tree.

If you move your mouse over a leaf it will show you the chunk + code_start hash, or the empty value (zeros):

These are the results after executing the script (click on the image to see the full size):

BAT

  • Total chunks: 102
  • Total leafs: 128
  • Total nodes: 256

LINK

  • Total chunks: 98
  • Total leafs: 128
  • Total nodes: 256

MKR

  • Total chunks: 105
  • Total leafs: 128
  • Total nodes: 256

OMG

  • Total chunks: 122
  • Total leafs: 128
  • Total nodes: 256

USDC

  • Total chunks: 68
  • Total leafs: 128
  • Total nodes: 256

DAI

  • Total chunks: 247
  • Total leafs: 256
  • Total nodes: 512

LEND

  • Total chunks: 215
  • Total leafs: 256
  • Total nodes: 512

WBTC

  • Total chunks: 143
  • Total leafs: 256
  • Total nodes: 512

YFI

  • Total chunks: 200
  • Total leafs: 256
  • Total nodes: 512

USDT

  • Total chunks: 513
  • Total leafs: 1024
  • Total nodes: 2048

Click here to see the tree

code-chunks's People

Contributors

hugo-dc avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

developerfred

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.