eenagy
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Building a Blockchain from Scratch NOTE: work in progress, submitted as unfinished, for the finished article please check the linked source. This will be left as it was submitted. Motivations for Building a Blockchain from Scratch: - Understanding - Explaining to non-technical people why blockchain development takes so much time (abstraction layers) - Explaining to technical people ## Planned parts of this series - [PART 1 - Building the vm](https://hackmd.io/JsOVthlQSEKiEXcNPEwK9A#Part-1---Building-the-VM) - PART 2 - Building the extended vm (storage, transactions) - PART 3 - Consensus algorithm component - PART 4 - Compiler to translate to bytecode that can run on vm - PART 5 - Peer to peer compononent for transactions and block propagations - PART 6 - Client component to interact with the chain - PART 7 - Transaction explorer - PART 8 - Wallet primitives as library - PART 9 - Blockchain principles and characteristics, how well done (opennnes, decentralization, global, immutability, finality, etc) ## Part 1 - Building the VM The VM (Virtual Machine) component in blockchain systems. Its main purpose is to execute the code that triggers **state transitions** within the blockchain. It acts as a computational engine, responsible for determining whether the code can run successfully or not. Think of the VM as a machine that enforces the rules and restrictions of the blockchain. When code is executed by the VM, it checks if the code is allowed to run based on predefined conditions. If the code complies with the rules, the state of the blockchain is updated accordingly, reflecting the changes caused by the code execution. However, if the code violates any of the predefined rules or runs out of gas (meaning it exceeds the allocated computational resources), the VM halts the execution. In such cases, the code may revert, meaning the changes made to the state are undone, and an error or exceptional condition is triggered. For this part, the VM will the following attributes: - single threaded (no time slice) - no scheduling - deterministic machine, (no random value, finite operations through the amount of gas available) - atomic (either fails or succeeds) - stack-based architecture (word size of 256 bits) - immutable program code, loaded with the bytecode of the smart contract to be executed - volatile memory - set of environment variables and data ![Diagram adapted from Ethereum EVM illustrated(opens in a new tab)↗ The Ethereum state transition](https://hackmd.io/_uploads/HyyGTunv2.png) Note: In this context will be meaning Extended VM and not Ethereum VM. (Though it is building an Ethereum like virtual machine) ### Example of what we are trying to build to write a simple interpreter that does simple arithmetic and bitwise logic operations is pretty straightforward main.rs ``` rust enum Bytecode { // Define the bytecode instructions Add, Sub, Mul, Div, Push(i32), Halt, } fn interpreter(bytecode: Vec<Bytecode>, gas_limit: u32) { let mut stack: Vec<i32> = Vec::new(); let mut pc: usize = 0; let mut gas_left = gas_limit; while pc < bytecode.len() && gas_left > 0 { match bytecode[pc] { Bytecode::Add => { let b = stack.pop().unwrap(); let a = stack.pop().unwrap(); stack.push(a + b); gas_left -= 1; } Bytecode::Sub => { let b = stack.pop().unwrap(); let a = stack.pop().unwrap(); stack.push(a - b); gas_left -= 1; } Bytecode::Mul => { let b = stack.pop().unwrap(); let a = stack.pop().unwrap(); stack.push(a * b); gas_left -= 1; } Bytecode::Div => { let b = stack.pop().unwrap(); let a = stack.pop().unwrap(); stack.push(a / b); gas_left -= 1; } Bytecode::Push(value) => { stack.push(value); gas_left -= 1; } Bytecode::Halt => { break; } } pc += 1; } if gas_left == 0 { println!("Out of gas!"); } else if let Some(result) = stack.pop() { println!("Result: {}", result); } else { println!("Stack is empty!"); } } fn compile(bytecode_str: &str) -> Vec<Bytecode> { let bytecode = bytecode_str .split(',') .map(|opcode| match opcode.trim() { "ADD" => Bytecode::Add, "SUB" => Bytecode::Sub, "MUL" => Bytecode::Mul, "DIV" => Bytecode::Div, "HALT" => Bytecode::Halt, inst => { let value = inst.replace("PUSH ", ""); if let Ok(num) = value.parse::<i32>() { Bytecode::Push(num) } else { panic!("Invalid bytecode instruction: {}", value); } } }) .collect(); bytecode } fn main() { // Example bytecode as a string let bytecode_str = "PUSH 5, PUSH 3, ADD, HALT"; let gas_limit = 100; let bytecode = compile(bytecode_str); interpreter(bytecode, gas_limit); } ``` In this example we wrote a machine that allows you do do arithmetic operations: addition, sub, divisions, multiply without stack overflow or underflow in a manner that each operation is metered through the gas. This minimal machine does not have - minimal error handling - required instructions set - control flow operations, we cannot branch or call code with this yet - there is no blockchain specific functions - though it's deterministic, it's very minimal ### Supported Operations Our blockchain is going to support the following operations. We go through and add support for each group as needed. The VM instruction set offers most of the operations you might expect, including: - Arithmetic and bitwise logic operations - Execution context inquiries - Stack, memory access - Control flow operations - Logging, calling, and other operators #### Arithmetic operations - ADD //Add the top two stack items - MUL //Multiply the top two stack items - SUB //Subtract the top two stack items - DIV //Integer division - SDIV //Signed integer division - MOD //Modulo (remainder) operation - SMOD //Signed modulo operation - **ADDMOD** //Addition modulo any number - **MULMOD** //Multiplication modulo any number - EXP //Exponential operation - SIGNEXTEND //Extend the length of a two's complement signed integer - **KECCAK256** // compute the hash of a block of memory The operations: ADD, MUL, SUB, DIV, SDIV, MOD, SMOD common stack operations that most people are familiar, the only difference is that is these operations performed on 256 bit stack. ADDMOD, MULMOD, KECCACK256 operations to support cryptograpy. #### System operations - **LOGx** - **CREATE** - **CALL** - **CALLCODE** - **RETURN** - **STATICCALL** - **REVERT** - **INVALID** - **SELFDESTRUCT** #### Environmental operations - **GAS** - **ADDRESS** - **BALANCE** - **ORIGIN** - **CALLER** - **CALLVALUE** - **CALLDATALOAD** - **CALLDATASIZE** - **CALLDATACOPY** - **CODESIZE** - **CODECOPY** - **GASPRICE** - **EXTCODECOPY** - **EXTCODESIZE** - **RETURNDATASIZE** - **RETURNDATACOPY** #### Block operations - **BLOCKHASH** - **COINBASE** - **TIMESTAMP** - **NUMBER** - **DIFFICULTY** - **GASLIMIT** #### Logic operations - LT //Less-than comparison - GT //Greater-than comparison - SLT //Signed less-than comparison - SGT //Signed greater-than comparison - EQ //Equality comparison - ISZERO - AND //Bitwise AND operation - OR //Bitwise OR operation - XOR //Bitwise XOR operation - BYTE //Retrieve a single byte from a full-width 256-bit word Common vm logic operations. Please check the source code for implementations details. #### Stack, memory operations - POP //Remove the top item from the stack - MLOAD //Load a word from memory - MSTORE //Save a word to memory - MSTORE8 //Save byte to memory - SLOAD // Load a word from storage - SSTORE // Save a word to storage - MSIZE // Get the size of the active memory in bytes - PUSHx // Place x byte item on the stack, where x cab be any integer from 1 to 32 - DUPx // Duplicate x-th stack item, where x cab e any integer from 1 to 16 - SWAPx // Exchange 1st and (x+1)-th item, where x can be any integer These are also common vm operations. #### Process flow operations - STOP - JUMP - JUMPI - PC - JUMPDEST Common process flow operations. Check source code. ### Plan To implement the above operations we need to introduce and refactor our minimal applications and add the following - Stack: we need to introduce a Stack data structure with 256bit of word size - Memory: we need to abstract the memory and define what it is - Instructions: we have a long list of instruction set to support, abstracting them would be nice - Contract: volatile ROM for accessing contract code - Access to the world state, account/contract storage, and execution environment variables - we are not going into opcodes, the current implementation still going to use string based compilation - Add control flow ready interpreter, current doesn't allow jumps #### interpreter the looop #### instruction calling Operations will fall into the following categories stack operations ```rust= pub fn operation( pc: &mut u64, interpreter: &mut interpreter::Interpreter, scope: &mut interpreter::ScopeContext, ) -> Result<Vec<u8>, Error> { let x = match scope.stack.pop() { Some(value) => value, None => return Err(Error::StackUnderflow), }; let y = match scope.stack.peek() { Some(value) => value, None => return Err(Error::StackUnderflow), }; if let Some(result) = do_something_with_operands(x, y) { *y = result; Ok(Vec::new()) } else { Err(Error::Overflow) } } ``` memory operation interpreterer.statedb operation (accessing world state) scope.contract scope.memory other parts that use the ScopeContext #### gas In this implementation gas is static for all call, so this is not providing the right economic security model, and it's gameable. We update the gas implemenation in another part of this series. #### context - stack - memory - contract #### extended vm context the following operation will access the extended context #### contract ### Implementation TODO ### Notes about VM - security considerations when it comes to vm implementation - Stack-based architecthure to hybrid register based ones - how to handle EIPs (practice) - how to handle backward and forward compatibility - supporting different chains and their specific instructions sets - constant gas vs dynamic gas - error handling, error codes

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully