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 for further details and have a look at the on instructions how to instantiate the VM with the respective fork rules.
从v2.5.0
版本发布开始,我们现在支持Byzantium
和Constantinople
两种分支规则-目前Byzantium
是默认值(未来会改变)。看 了解更多版本细节并在说明中查看 文档,了解要如何根据相应的版本初始化虚拟机
If you are still looking for a compatible version of this library install the latest of the 2.2.x
series (see ).
如果你仍在寻找这个库的兼容版本,可以安装最新的2.2.x系列
INSTALL
npm install ethereumjs-vm
USAGE
var VM = require('ethereumjs-vm')//create a new VM instancevar 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 . This will give you a global variable EthVM
to use. The generated file will be at ./examples/run-transactions-simple/build.js
.
为了创建能够在浏览器中单独使用的情况,需要安装browserify模块,然后查看
例子(可看本博客)。这将提供全局变量EthVM
给你使用。生成的文件将在./examples/run-transactions-simple/build.js
API
VM
For documentation on VM
instantiation, exposed API and emitted events
see generated .
有关VM实例化、公开API和发出事件的文档,可见生成的API docs (可看本博客)
StateManger状态管理
The API for the StateManager
is currently in Beta
, separate documentation can be found .StateManager的API目前还是
Beta版本,其单独的文档可见
here (可看本博客)
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 for the v2.5.0
VM release for further details.StateManager
的API最近已经重新复用,并且StateManager
将从VM中移除,在未来的v3.0.0
版本中将作为单独的包来提供,更过未来的细节可见v2.5.0
版本的VM发行
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 .
开发者文档-目前主要是测试和调试信息-可在找到