以太坊学习路线——(二、上)搭建testrpc测试链

这篇博客演示的基本操作系统环境是CentOS 7,参考书籍:以太坊开发实战——以太坊关键技术与案例分析 第五章(吴寿鹤、冯翔、刘涛、周广益   著)。首先请自行搜索安装并更新nodejs到较高版本。


一.搭建testrpc

1.安装

$ npm install -g ethereumjs-testrpc

2.使用

[root@localhost geth]# testrpc
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)

Available Accounts
==================
(0) 0x623053f6e32ea1740ab791fae0e2e402b367e501
(1) 0xc8f4672e8e988df4d5f6ee9c1322eba005583267
(2) 0x242e2ad79192f5f28c13d6d9ee57976ef346d63f
(3) 0x88304656b1031469af4412df4b2de8b2c7759385
(4) 0x55b0d4de3900b00c7ac1c5ede911a832c3f0ec91
(5) 0xaf767e3d3da4b8bcba5ca9978d5ca4a7bdf6532f
(6) 0x7ce1fd31c404682eba0d7fd33d8635c47ee2cc4b
(7) 0x40023a7f603fd4fc9675c1670b249d054ae33eef
(8) 0xa723cea532564ded55ad8e6256fb016d25cf18b5
(9) 0x49ab097da3c5089764e0d5c4117f950263274c0b

Private Keys
==================
(0) 3a309c5637d8779d966b4f20dcadb2aa8af94b61e0c68a233227110ad62e761b
(1) 15423d15bb286bcb536326e7a8fcb4506348ea18fe9efe709d88713fc8d6ed70
(2) 9bfb02707ea25e1bdb76f1c278413d328cb6dd83ef9f7347f90a42dc60569548
(3) 1a56cd45227ebb3f0c880511949af0546940fa2a83d285af280346c48b95ffe9
(4) 5f4784161667cb5933105813d8a39aae81598188504d94258dd56e5b683dbdea
(5) 49a86b8b069b27c472507164ec337238644fcff99f7be28daf7690f96a5d27fe
(6) d97d4f64ec195b8da6600c325700b4bc54ba018cf1f29a6aaf806c35591dcfc4
(7) 7e7c4d034163c364ff082e412eac6165df3f42a86571ab064fe534c2392aa851
(8) 88be5fbe48cd11a2b4da75ef808370919c3c1c0229bbbdd8fdd3c655ecd7dbc0
(9) 7b1af953c39e4b9f09c57bbb34c2e113c745f9d2dc5b37cf982ce411be4ceab1

HD Wallet
==================
Mnemonic:      penalty silk correct trial good drama action arctic drip rebel weather level
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545

官方参数说明文档:

[root@localhost gduyt]# testrpc --help

testrpc: Fast Ethereum RPC client for testing and development
  Full docs: https://github.com/ethereumjs/testrpc

Usage: testrpc [options]
  options:
  --port/-p <port to bind to, default 8545>
  --host/-h <host to bind to, default 0.0.0.0>
  --fork/-f <url>   (Fork from another currently running Ethereum client at a given block)

  --db <db path>   (directory to save chain db)
  --seed <seed value for PRNG, default random>
  --deterministic/-d     (uses fixed seed)
  --mnemonic/-m <mnemonic>
  --accounts/-a <number of accounts to generate at startup>
  --acctKeys <path to file>  (saves generated accounts and private keys as JSON object in specified file)
  --secure/-s   (Lock accounts by default)
  --unlock <accounts>   (Comma-separated list of accounts or indices to unlock)

  --blocktime/-b <block time in seconds>
  --networkId/-i <network id> (default current time)
  --gasPrice/-g <gas price>   (default 20000000000)
  --gasLimit/-l <gas limit>   (default 90000)

  --debug       (Output VM opcodes for debugging)
  --verbose/-v
  --mem         (Only show memory output, not tx history)

  --help / -?    (this output)
  • --accounts或-a :指定在启动时生成多少个账户。eg:testrpc -a 1
  • --blocktime或-b :设置每隔多少秒产生一个区块,用于自动挖矿,默认0,不会自动挖矿。eg:testrpc -b 1
  • --deterministic或-d:产生一个基于之前助记符的确定地址,你不需要关心 之前定义的助记符是什么,如果你有使用了testrpc -d,那么每次启动testrpc都会使用同一个助记符。eg:testrpc -a 1 -d
  • --secure或-s:默认锁上所有的账户(有利于第三方交易)。
  • --mnemonic或-m:使用一个指定的分层确定钱包助记符(HD wallet mnemonic)来生成初始的地址。eg:
[root@localhost gduyt]# testrpc -a 1 -m "this is mine"
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
……
HD Wallet
==================
Mnemonic:      this is mine
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545
​
  • --port或-p:用于监听的端口。默认8545。eg:testrpc -a 1 -d -p 8888
  • --host/-h:用于设置监听的域名。默认是localhost。
  • --seed:用于任意的数据生成确定钱包助记符(HD wallet mnemonic)。种子是一串由随机生成器生成的随机数。这串随机数可以用来生成钱包中不同的账户的公私钥对。eg:testrpc -a 1 --seed “11”
  • --gasPrice或-g:自定义gas价格(默认20000000000)
  • --gasLimit或-l:自定义gas额限(默认90000)。
  • --fork或-f <url>:从另一个当前正在运行着的以太坊客户端所给的区块编号开始分叉。参数值应该是另一个客户端的HTTP访问地址和端口号,你可以使用“@”号来指定要分叉的区块编号:http://localhost:8545@1599200。eg:testrpc -a 1 -p 8888 -f http://localhost:8545@5。
  • --networkId或-i:指定网络id,testrpc将用来识别它自己(默认当前时间,或如果配置了分叉网络的话,那会使用分叉网络的id)。
  • --db <db path>:指定一个保存区块数据的目录路径。如果目录中已经存在了一个数据库了,testrpc会初始化那个链,而不再创建一个新链了。
  • --debug:输出用于debug的虚拟机操作码(opcodes)
  • --mem:输出testrpc的内存使用统计。这会替换正常输出。
  • --account:设置--account=…(注意account后面没有‘s’)使用私钥和他们的余额来生成初始账户。基本使用方法:testrpc --account=“<privatekey>,balance”。(注意:私钥有64个字符,并且必须是以0x开头的十六进制的字符串。余额可以是一个整数,也可以是一个0x开头的十六进制的值,单位是wei。当使用--account时,testrpc不会为你创建HD wallet)。
  • --unlock <accounts>:可以申明--unlock…多次,在解锁账户时即可以向--unlock…传入公钥地址也可以传入账户的索引号。当结合-secure使用时,--unlock会覆盖指定账户的锁定状态。eg:testrpc --secure --unlock “0x1212…” --unlock “0xsdn…”   也可以通过指定账户的索引来解锁账户,eg:restrpc --unlock 0 --unlock 1

3.类(library)

testrpc也可以作为一个类库使用。

(1).当作一个Web3 provider:

var Testrpc = require("ethereumjs-testrpc");
web3.setProvider(TestRPC.provider());

(2).当作一个普通的http服务:

var TestRPC = require("ethereumjs-testrpc");
var server = TestRPC.server();
server.listen(port, function(err, blockchain) {...});

.provider().server()都会返回一个对象,通过这个返回的对象你可以设置testrpc的行为,对象中的参数是可选的,可用参数如下:

  • "accounts":数组对象,数组中每个对象需要有个banance的键,键对应的值用16进制表示。同样secretKey键也必须要指明,它表示账户的私钥。如果没有指明secretKey键,那么会自动生成一个。
  •  
  • "debug": boolean - 输出虚拟机的操作码用于debug。
  • "logger": Object - 对象,就像console,实现一个log()方法。
  • "mnemonic": 使用一个指定的分层确定钱包助记符(HD wallet mnemonic)来生成初始地址。
  • "port": 作为一个服务时,监听的端口。
  • "seed": 使用一个任意数据,用来生成要被使用的分层确定钱包助记符。
  • "total_accounts": number - 启动时生成多少各个账户。
  • "fork": string - 和命令行选项中的--fork一样。
  • "network_id": integer - 和命令行选项中的--network_id一样。
  • "time": Date - 第一个区块开始的日期,使用这个功能,配合evm_increaseTime方法一起测试有时间依赖性的代码。
  • "locked": boolean - 默认情况下,账户是否是被锁的。
  • "unlocked_accounts": Array - 要被解锁的地址或索引的数据。
  • "db_path": String - 指定保存链数据库的目录路径。如果数据库已存在,那么testrpc将初始化该链,而不是创建新链。
  • "account_keys_path": String - 指定要保存帐户和私钥以供测试的文件。

 4.Testrpc中已实现的方法

  • bzz_hive (stub)
  • bzz_info (stub)
  • debug_traceTransaction
  • eth_accounts
  • eth_blockNumber
  • eth_call
  • eth_coinbase
  • eth_estimateGas
  • eth_gasPrice
  • eth_getBalance
  • eth_getBlockByNumber
  • eth_getBlockByHash
  • eth_getBlockTransactionCountByHash
  • eth_getBlockTransactionCountByNumber
  • eth_getCode (only supports block number “latest”)
  • eth_getCompilers
  • eth_getFilterChanges
  • eth_getFilterLogs
  • eth_getLogs
  • eth_getStorageAt
  • eth_getTransactionByHash
  • eth_getTransactionByBlockHashAndIndex
  • eth_getTransactionByBlockNumberAndIndex
  • eth_getTransactionCount
  • eth_getTransactionReceipt
  • eth_hashrate
  • eth_mining
  • eth_newBlockFilter
  • eth_newFilter (includes log/event filters)
  • eth_protocolVersion
  • eth_sendTransaction
  • eth_sendRawTransaction
  • eth_sign
  • eth_syncing
  • eth_uninstallFilter
  • net_listening
  • net_peerCount
  • net_version
  • miner_start
  • miner_stop
  • personal_listAccounts
  • personal_lockAccount
  • personal_newAccount
  • personal_unlockAccount
  • personal_sendTransaction
  • shh_version
  • rpc_modules
  • web3_clientVersion
  • web3_sha3

还有一些非标准的方法,在原始的RPC规范中没有的:

  • evm_snapshot : Snapshot the state of the blockchain at the current block. Takes no parameters. Returns the integer id of the snapshot created.
  • evm_revert : Revert the state of the blockchain to a previous snapshot. Takes a single parameter, which is the snapshot id to revert to. If no snapshot id is passed it will revert to the latest snapshot. Returns true.
  • evm_increaseTime : Jump forward in time. Takes one parameter, which is the amount of time to increase in seconds. Returns the total time adjustment, in seconds.
  • evm_mine : Force a block to be mined. Takes no parameters. Mines a block independent of whether or not mining is started or stopped.

不支持的方法eth_compileSolidity: 如果你希望用JavaScript编译solidity,请参阅solcJS项目。

5.Docker

开始使用Docker映像的最简单方法是:

docker run -d -p 8545:8545 ethereumjs/testrpc:latest

要通过Docker将选项传递给testrpc,只需将参数添加到run命令:

docker run -d -p 8545:8545 ethereumjs/testrpc:latest -a 10 --debug

要从源代码构建Docker容器: 

git clone https://github.com/ethereumjs/testrpc.git && cd testrpc
docker build -t ethereumjs/testrpc .

猜你喜欢

转载自blog.csdn.net/GDUYT_gduyt/article/details/88914286