1. 程式人生 > >ethereumjs/ethereumjs-vm-1-簡介

ethereumjs/ethereumjs-vm-1-簡介

https://github.com/ethereumjs/ethereumjs-vm

其實這就是怎麼自己使用該模組來生成一個類似geth客戶端的以太坊虛擬機器,然後進行各類區塊鏈操作

SYNOPSIS概要

Implements Ethereum's VM in Javascript.用Javascript實現以太坊虛擬機器

Fork Support分支支援

Starting with the v2.5.0 release we now support both Byzantium and Constantinople fork rules - with Byzantium

currently being the default (this will change in the future). See release notes for further details and have a look at the API docs on instructions how to instantiate the VM with the respective fork rules.

v2.5.0版本釋出開始,我們現在支援ByzantiumConstantinople兩種分支規則-目前Byzantium是預設值(未來會改變)。看 

release notes 瞭解更多版本細節並在說明中檢視 API docs文件,瞭解要如何根據相應的版本初始化虛擬機器

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).

如果你仍在尋找這個庫的Spurious Dragon相容版本,可以安裝最新的2.2.x系列

INSTALL

npm install ethereumjs-vm

 

USAGE

var VM = require('ethereumjs-vm')

//create a new VM instance
var vm = new VM()
var code = '7f4e616d65526567000000000000000000000000000000000000000000000000003055307f4e616d6552656700000000000000000000000000000000000000000000000000557f436f6e666967000000000000000000000000000000000000000000000000000073661005d2720d855f1d9976f88bb10c1a3398c77f5573661005d2720d855f1d9976f88bb10c1a3398c77f7f436f6e6669670000000000000000000000000000000000000000000000000000553360455560df806100c56000396000f3007f726567697374657200000000000000000000000000000000000000000000000060003514156053576020355415603257005b335415603e5760003354555b6020353360006000a233602035556020353355005b60007f756e72656769737465720000000000000000000000000000000000000000000060003514156082575033545b1560995733335460006000a2600033545560003355005b60007f6b696c6c00000000000000000000000000000000000000000000000000000000600035141560cb575060455433145b1560d25733ff5b6000355460005260206000f3'

vm.runCode({
  code: Buffer.from(code, 'hex'), // code needs to be a Buffer
  gasLimit: Buffer.from('ffffffff', 'hex')
}, function(err, results){
  console.log('returned: ' + results.return.toString('hex'));
})

Also more examples can be found here更多的例子可以在下面找到

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.

為了建立能夠在瀏覽器中單獨使用的情況,需要安裝browserify模組,然後檢視 run-transactions-simple example例子(可看本部落格ethereumjs-vm/examples/run-transactions-simple)。這將提供全域性變數EthVM 給你使用。生成的檔案將在./examples/run-transactions-simple/build.js

API

VM

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

有關VM例項化、公開API和發出事件的文件,可見生成的API docs  (可看本部落格ethereumjs/ethereumjs-vm-2-API文件)

StateManger狀態管理

The API for the StateManager is currently in Beta, separate documentation can be found here.StateManager的API目前還是Beta版本,其單獨的文件可見 here (可看本部落格ethereumjs/ethereumjs-vm-3-StateManager

The StateManager API has been largely reworked recently and the StateManager will be removed from the VM and provided as a separate package in a future v3.0.0 release, see release notes for the v2.5.0 VM release for further details.StateManager的API最近已經重新複用,並且StateManager將從VM中移除,在未來的v3.0.0版本中將作為單獨的包來提供,更過未來的細節可見v2.5.0版本的VM發行release notes

Internal Structure內部結構

The VM processes state changes at many levels.

VM在許多級別上流程狀態會變化

  • runBlockchain
    • for every block, runBlock 就是對鏈上的沒有區塊都執行runBlock方法
  • runBlock
    • for every tx, runTx  就是對區塊中的所有交易都執行runTx方法
    • pay miner and uncles  然後付款給礦工和叔塊
  • runTx
    • check sender balance 先檢視傳送者的餘額
    • check sender nonce 再檢查傳送者的nonce值
    • runCall  執行runCall方法
    • transfer gas charges  轉移收取的gas費用
  • runCall
    • checkpoint state  儲存檢查點狀態
    • transfer value  轉移值
    • load code  下載程式碼
    • runCode  執行runCode方法
    • materialize created contracts  實現建立的合約
    • revert or commit checkpoint  恢復或提交檢查點狀態
  • runCode
    • iterate over code 遍歷程式碼
    • run op codes 執行操作碼
    • track gas usage 追蹤gas的使用
  • OpFns
    • run individual op code 獨自執行操作碼
    • modify stack 修改堆疊
    • modify memory 修改記憶體
    • calculate fee 計算開銷

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

用於CREATE, CALL和CALLCODE呼叫的opFns將會回撥到 runCall

DEVELOPMENT開發

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

開發者文件-目前主要是測試和除錯資訊-可在here找到

Spurious Dragon