Coder Social home page Coder Social logo

dvm's Introduction

Stack machine

Typescript implementation Ethereum Virtual Machine (EVM)

Stack-based virtual machine with an ephemeral memory byte-array and persistent key-value storage. Elements on the stack are 32-byte words, and all keys and values in storage are 32 bytes. There are over 100 opcodes

Blockchain integration

Implementation Blockchain Context

interface IContext {
    // CanTransfer returns whether the account contains
    // sufficient ether to transfer the value
    canTransfer(storage: IStorage, address: Buffer, amount: bigint): boolean;
    // Transfer transfers ether from one account to the other
    transfer(storage: IStorage, sender: Buffer, recipient: Buffer, amount: bigint): void;
    // Message information
    origin: Buffer;   // Provides information for ORIGIN
    gasPrice: bigint; // Provides information for GASPRICE

    // Block information
    coinbase: Buffer;    // Provides information for COINBASE
    gasLimit: bigint;    // Provides information for GASLIMIT
    blockNumber: bigint; // Provides information for NUMBER
    time: bigint;        // Provides information for TIME
    difficulty: bigint;  // Provides information for DIFFICULTY
}

Implementation Blockchain Storage

interface IStorage {
    createAccount(address: Buffer): void;

    subBalance(address: Buffer, value: bigint): void;
    getBalance(address: Buffer): bigint;
    addBalance(address: Buffer, value: bigint): void;

    getNonce(address: Buffer): bigint;
    setNonce(address: Buffer, value: bigint): void;

    getCode(address: Buffer): Buffer;
    setCode(address: Buffer, code: Buffer): void;
    getCodeSize(address: Buffer): bigint;
    getCodeHash(address: Buffer): Buffer;
    
    getValue(address: Buffer, key: Buffer): Buffer;
    setValue(address: Buffer, key: Buffer, value: Buffer): void;

    snapshot(): number;
    revertToSnapshot(value: number): void;

    // Exist reports whether the given account exists in state.
    // Notably this should also return true for suicided accounts.
    exist(address: Buffer): boolean;

    // Empty returns whether the given account is empty. Empty
    // is defined according to EIP161 (balance = nonce = code = 0).
    empty(address: Buffer): boolean;

    suicide(address: Buffer): void;
    hasSuicided(address: Buffer): boolean;

    addRefund(gas: bigint): void;
    subRefund(gas: bigint): void;
    getRefund(): bigint;

    addLog(log: Log): void;
    logs(): Array<Log>;

Implementation Blockchain Config

type Config = {
    debug: boolean;
    params?: Params;
    precompiles?: {
        [byte: number]: IPrecompiledContract
    };
    bigIntToAddress(b: bigint): Buffer;
    createAddress(address: Buffer, nonce: bigint): Buffer;
    createAddressWithSalt(address: Buffer, solt: bigint, code: Buffer): Buffer;
    getBlockHash(n: number): Buffer;
};

Also you can add your Precompiled Contract, and change the cost of gas

Run

    const storage = new Storage();
    const context = new Context();
    const vm = new VM(context, storage, config);
    const gasPool = new GasPool(context.gasLimit);

    const snapshot = storage.snapshot();

    const result = await vm.applyMessage(message, gasPool);

    if (result.error) {
        storage.revertToSnapshot(snapshot);
    }

Tests

npm run test

dvm's People

Contributors

user-dob avatar

Watchers

 avatar

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.