@0yi0/ethereumjs-vm 中文文档教程

发布于 5年前 浏览 74 项目主页 更新于 3年前

SYNOPSIS

NPM 包构建状态覆盖状态Gitter

js-standard-style

在 Javascript 中实现以太坊的虚拟机。

Fork Support

VM 当前支持以下硬分叉规则:

  • Byzantium
  • Constantinople
  • Petersburg (default)
  • Istanbul (DRAFT)

如果您仍在寻找 Spurious Dragon 该库的兼容版本安装最新的 2.2.x 系列(参见 Changelog)。

Istanbul Harfork Support

v4.0.0 版本中,我们开始添加 EIP 的实现 候选人或被接受加入 Istanbul 硬分叉。 你可以 使用 istanbul hardfork 选项激活初步的 Istanbul VM 旗帜。

当前支持的 Istanbul EIP:

  • EIP-1108 (Candidate): alt_bn128 Gas Cost Reductions, see PR #540

请注意,这是高度实验性的,仅用于实验目的, 由于 Istanbul 范围尚未最终确定,大多数 EIP 仍处于 DRAFT 阶段 状态并可能会进行更新和更改。

最终的Istanbul VM 将随着主要版本的升级发布到 v5.0.0v6.0.0

查看相应的问题以关注讨论和当前状态 伊斯坦布尔规划

INSTALL

npm install ethereumjs-vm

USAGE

const BN = require('bn.js')
var VM = require('ethereumjs-vm').default

// Create a new VM instance
// For explicity setting the HF use e.g. `new VM({ hardfork: 'petersburg' })`
const vm = new VM()

const STOP = '00'
const ADD = '01'
const PUSH1 = '60'

// Note that numbers added are hex values, so '20' would be '32' as decimal e.g.
const code = [PUSH1, '03', PUSH1, '05', ADD, STOP]

vm.on('step', function(data) {
  console.log(`Opcode: ${data.opcode.name}\tStack: ${data.stack}`)
})

vm.runCode({
  code: Buffer.from(code.join(''), 'hex'),
  gasLimit: new BN(0xffff),
})
  .then(results => {
    console.log('Returned : ' + results.returnValue.toString('hex'))
    console.log('gasUsed  : ' + results.gasUsed.toString())
  })
  .catch(err => console.log('Error    : ' + err))

Example

该项目包含以下示例:

  1. ./examples/run-blockchain: Loads tests data, including accounts and blocks, and runs all of them in the VM.
  2. ./examples/run-code-browser: Show how to use this library in a browser.
  3. ./examples/run-solidity-contract: Compiles a Solidity contract, and calls constant and non-constant functions.
  4. ./examples/run-transactions-complete: Runs a contract-deployment transaction and then calls one of its functions.
  5. ./examples/decode-opcodes: Decodes a binary EVM program into its opcodes.

所有示例都有自己的 README.md 解释如何运行它们。

BROWSER

要在浏览器中独立使用,请安装 browserify 并检查 run-transactions-simple example。 这将为您提供一个全局变量 EthVM 供您使用。 生成的文件位于 ./examples/run-transactions-simple/build.js

API

VM

有关 VM 实例化、公开的 API 和发出的 events 的文档,请参阅生成的 API 文档

StateManger

StateManager 的 API 目前处于 Beta 阶段,可以在此处 找到单独的文档,另请参阅 v2.5.0 VM 中的发行说明发布有关 StateManager 重写的详细信息。

Internal Structure

VM 在多个级别处理状态更改。

  • runBlockchain
  • for every block, runBlock
  • runBlock
  • for every tx, runTx
  • pay miner and uncles
  • runTx
  • check sender balance
  • check sender nonce
  • runCall
  • transfer gas charges
  • runCall
  • checkpoint state
  • transfer value
  • load code
  • runCode
  • materialize created contracts
  • revert or commit checkpoint
  • runCode
  • iterate over code
  • run op codes
  • track gas usage
  • OpFns
  • run individual op code
  • modify stack
  • modify memory
  • calculate fee

CREATECALLCALLCODE 的 opFns 回调到 runCall

DEVELOPMENT

可以在此处找到开发人员文档(目前主要包含有关测试和调试的信息)。

EthereumJS

请参阅我们的组织文档,了解 EthereumJS 的介绍以及有关当前标准和最佳实践的信息。

如果您想加入工作或改进库,请查看我们的贡献指南

LICENSE

MPL-2.0

SYNOPSIS

NPM PackageBuild StatusCoverage StatusGitter

js-standard-style

Implements Ethereum's VM in Javascript.

Fork Support

The VM currently supports the following hardfork rules:

  • Byzantium
  • Constantinople
  • Petersburg (default)
  • Istanbul (DRAFT)

If you are still looking for a Spurious Dragon compatible version of this library install the latest of the 2.2.x series (see Changelog).

Istanbul Harfork Support

With the v4.0.0 release we are starting to add implementations of EIPs being candidates or accepted for inclusion within the Istanbul hardfork. You can activate a preliminary Istanbul VM by using the istanbul hardfork option flag.

Currently supported Istanbul EIPs:

  • EIP-1108 (Candidate): alt_bn128 Gas Cost Reductions, see PR #540

Note that this is highly experimental and solely meant for experimental purposes, since Istanbul scope is not yet finalized and most EIPs are still in a DRAFT state and likely subject to updates and changes.

A final Istanbul VM will be released along a major version bump to likely v5.0.0 or v6.0.0.

Have a look at the corresponding issue to follow the discussion and current state on Istanbul planning.

INSTALL

npm install ethereumjs-vm

USAGE

const BN = require('bn.js')
var VM = require('ethereumjs-vm').default

// Create a new VM instance
// For explicity setting the HF use e.g. `new VM({ hardfork: 'petersburg' })`
const vm = new VM()

const STOP = '00'
const ADD = '01'
const PUSH1 = '60'

// Note that numbers added are hex values, so '20' would be '32' as decimal e.g.
const code = [PUSH1, '03', PUSH1, '05', ADD, STOP]

vm.on('step', function(data) {
  console.log(`Opcode: ${data.opcode.name}\tStack: ${data.stack}`)
})

vm.runCode({
  code: Buffer.from(code.join(''), 'hex'),
  gasLimit: new BN(0xffff),
})
  .then(results => {
    console.log('Returned : ' + results.returnValue.toString('hex'))
    console.log('gasUsed  : ' + results.gasUsed.toString())
  })
  .catch(err => console.log('Error    : ' + err))

Example

This projects contain the following examples:

  1. ./examples/run-blockchain: Loads tests data, including accounts and blocks, and runs all of them in the VM.
  2. ./examples/run-code-browser: Show how to use this library in a browser.
  3. ./examples/run-solidity-contract: Compiles a Solidity contract, and calls constant and non-constant functions.
  4. ./examples/run-transactions-complete: Runs a contract-deployment transaction and then calls one of its functions.
  5. ./examples/decode-opcodes: Decodes a binary EVM program into its opcodes.

All of the examples have their own README.md explaining how to run them.

BROWSER

To build for standalone use in the browser, install browserify and check run-transactions-simple example. This will give you a global variable EthVM to use. The generated file will be at ./examples/run-transactions-simple/build.js.

API

VM

For documentation on VM instantiation, exposed API and emitted events see generated API docs.

StateManger

The API for the StateManager is currently in Beta, separate documentation can be found here, see also release notes from the v2.5.0 VM release for details on the StateManager rewrite.

Internal Structure

The VM processes state changes at many levels.

  • runBlockchain
  • for every block, runBlock
  • runBlock
  • for every tx, runTx
  • pay miner and uncles
  • runTx
  • check sender balance
  • check sender nonce
  • runCall
  • transfer gas charges
  • runCall
  • checkpoint state
  • transfer value
  • load code
  • runCode
  • materialize created contracts
  • revert or commit checkpoint
  • runCode
  • iterate over code
  • run op codes
  • track gas usage
  • OpFns
  • run individual op code
  • modify stack
  • modify memory
  • calculate fee

The opFns for CREATE, CALL, and CALLCODE call back up to runCall.

DEVELOPMENT

Developer documentation - currently mainly with information on testing and debugging - can be found here.

EthereumJS

See our organizational documentation for an introduction to EthereumJS as well as information on current standards and best practices.

If you want to join for work or do improvements on the libraries have a look at our contribution guidelines.

LICENSE

MPL-2.0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文