以太坊学习路线——(二、上)搭建testrpc测试链
生活随笔
收集整理的這篇文章主要介紹了
以太坊学习路线——(二、上)搭建testrpc测试链
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這篇博客演示的基本操作系統環境是CentOS 7,參考書籍:以太坊開發實戰——以太坊關鍵技術與案例分析 第五章(吳壽鶴、馮翔、劉濤、周廣益?? 著)。首先請自行搜索安裝并更新nodejs到較高版本。
一.搭建testrpc
1.安裝
$ npm install -g ethereumjs-testrpc2.使用
[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) 0x49ab097da3c5089764e0d5c4117f950263274c0bPrivate Keys ================== (0) 3a309c5637d8779d966b4f20dcadb2aa8af94b61e0c68a233227110ad62e761b (1) 15423d15bb286bcb536326e7a8fcb4506348ea18fe9efe709d88713fc8d6ed70 (2) 9bfb02707ea25e1bdb76f1c278413d328cb6dd83ef9f7347f90a42dc60569548 (3) 1a56cd45227ebb3f0c880511949af0546940fa2a83d285af280346c48b95ffe9 (4) 5f4784161667cb5933105813d8a39aae81598188504d94258dd56e5b683dbdea (5) 49a86b8b069b27c472507164ec337238644fcff99f7be28daf7690f96a5d27fe (6) d97d4f64ec195b8da6600c325700b4bc54ba018cf1f29a6aaf806c35591dcfc4 (7) 7e7c4d034163c364ff082e412eac6165df3f42a86571ab064fe534c2392aa851 (8) 88be5fbe48cd11a2b4da75ef808370919c3c1c0229bbbdd8fdd3c655ecd7dbc0 (9) 7b1af953c39e4b9f09c57bbb34c2e113c745f9d2dc5b37cf982ce411be4ceab1HD 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 --helptestrpc: Fast Ethereum RPC client for testing and developmentFull docs: https://github.com/ethereumjs/testrpcUsage: 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:
- --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 .?
總結
以上是生活随笔為你收集整理的以太坊学习路线——(二、上)搭建testrpc测试链的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eclipse解决中文乱码
- 下一篇: Android各应用市场包名