ethereumjs-vm/examples/run-transactions-simple

https://github.com/ethereumjs/ethereumjs-vm/tree/master/examples/run-transactions-simple

prerequisite先决条件

$ npm install -g browserify http-server

然后还要将package.json中的模块安装了:

npm install

Instruction

Run command

$ browserify index.js -o bundle.js

运行成功后生成:

Then host this folder in a web server

$ http-server

终端:

userdeMacBook-Pro:run-transactions-simple user$ http-server
Starting up http-server, serving ./
Available on:
  http://127.0.0.1:8080
  http://192.168.1.102:8080
Hit CTRL-C to stop the server
[Sat Dec 08 2018 11:24:45 GMT+0800 (中国标准时间)] "GET /" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15"
[Sat Dec 08 2018 11:24:45 GMT+0800 (中国标准时间)] "GET /bundle.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15"

open http://localhost:8080 in a browser and check the result in web console.

页面运行情况:

扫描二维码关注公众号,回复: 4475283 查看本文章

查看代码到底做了什么:

index.js

/*
 * Example - Running code on an ethereum-vm
 *
 *
 * To run this example in the browser, bundle this file
 * with browserify using `browserify index.js -o bundle.js`
 * and then load this folder onto a HTTP WebServer (e.g.
 * using node-static or `python -mSimpleHTTPServer`).
 */
var Buffer = require('safe-buffer').Buffer // use for Node.js <4.5.0
var VM = require('../../index.js')

// create a new VM instance
var vm = new VM()

var code = '7f4e616d65526567000000000000000000000000000000000000000000000000003055307f4e616d6552656700000000000000000000000000000000000000000000000000557f436f6e666967000000000000000000000000000000000000000000000000000073661005d2720d855f1d9976f88bb10c1a3398c77f5573661005d2720d855f1d9976f88bb10c1a3398c77f7f436f6e6669670000000000000000000000000000000000000000000000000000553360455560df806100c56000396000f3007f726567697374657200000000000000000000000000000000000000000000000060003514156053576020355415603257005b335415603e5760003354555b6020353360006000a233602035556020353355005b60007f756e72656769737465720000000000000000000000000000000000000000000060003514156082575033545b1560995733335460006000a2600033545560003355005b60007f6b696c6c00000000000000000000000000000000000000000000000000000000600035141560cb575060455433145b1560d25733ff5b6000355460005260206000f3'

vm.on('step', function (data) {
  console.log(data.opcode.name)
})

vm.runCode({
  code: Buffer.from(code, 'hex'),
  gasLimit: Buffer.from('ffffffff', 'hex')
}, function (err, results) {
  console.log('returned: ' + results.return.toString('hex'))
  console.log('gasUsed: ' + results.gasUsed.toString())
  console.log(err)
})

其实就是在vm上运行了一段代码

index.html

<script src='/bundle.js'></script>

猜你喜欢

转载自www.cnblogs.com/wanghui-garcia/p/10087210.html