(三)區塊鏈開發之以太坊TestRPC測試鏈搭建

1、安裝

brew install node

npm install -g ethereumjs-testrpc

2、使用

testrpc <options>

options:

-a或-accounts。指定啓動時生成多個多個賬戶

➜  ~ testrpc -a 1
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)

Available Accounts
==================
(0) 0x566ae79bc4998ca7d35da3dbd46dfdb44582fa78

Private Keys
==================
(0) bd3da9fe19e9090db7e3bf21d54b5772689ac7ede8fff5c4eb5bdb89dfe22daf

HD Wallet
==================
Mnemonic:      boring memory robust crop jump play polar leaf fetch sound make arrow
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545

-b或--blocktime。設置每個多少秒生成一個區塊,用於自動挖礦。默認0,不會自動挖礦。

testrpc -b 1
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)

Available Accounts
==================
(0) 0x67ff216743647ea9b7205ff0267da451d63a7dcd
(1) 0x450f2896c47c6e8763b6d389c40166584d0ced40
(2) 0x09d58801bc4175425bf23cf42c645c250ac91645
(3) 0x7383fa0f1997fdcec3ff651d9957d374e8ffb226
(4) 0x1f16340cbc8a644079e3bae3138d2071f278b6e7
(5) 0x84c9517725d67c652265f3ec78fe9476916e2367
(6) 0xeffbf37de26be67be6a71fd1064ce62f6bc501f6
(7) 0xa13cc62fa4dd7efc52e796fa6a982f6e4c603307
(8) 0x05b70cb3505590adb1940abde54c530b555a4dbc
(9) 0x454dec238cb8d07ffd32a5b2256ec02102941d85

Private Keys
==================
(0) 9fa86667fec95e1c9cfa7ff48fe8957a73fc1522f9d0c7a75f0ce6992be83086
(1) 104476884731317f65abc25ae8352fb3856d96917d618466bbbf1d0f8be3c25e
(2) e9b99526c6cdfee041ea4161044d7ffcdf9f4dc0d7f4ccfb25c72c45d6970a2a
(3) 2dbc3f0e106f7377059b75819e81d0bb8d655c56eb6cd992c0752025e1b64789
(4) 7c194ed334848597fc83ac3c6c80c9cfd3127297a2efe7f09d779c38fcf16a7c
(5) 0530a0e82ff5d6a216b6f1b6c42cdbca8895a886dbeaa5944dac60ca62b58fb3
(6) 84c812e3327c07f1716b7a993e2b96c9e3cd77894a9bb4f6e313f4ba8432783a
(7) b663ce4d3542a14656fb096c27ec449a9c4e5a72d20071e3d3594cadf9787617
(8) 140989ed8dc4b508c138a9368da31eb5dfa0b1cbb4479e50de48287766d4cd35
(9) 856708d8b84d8fc952daca9095cfb7d31a40a1a9c9c7670fe0855266b4052b8e

HD Wallet
==================
Mnemonic:      paper pride pencil treat dilemma sniff august reward immense idea matrix peasant
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545

-d或--deterministic。產生一個基於之前定義的助記詞符的確定地址。你不需要關心之前定義的助記詞符是什麼,如果你使用了testrpc -d ,那麼你每次啓動testrpc都會使用同一個助記詞符

➜  ~ testrpc -a 1 -d
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)

Available Accounts
==================
(0) 0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1

Private Keys
==================
(0) 4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d

HD Wallet
==================
Mnemonic:      myth like bonus scare over problem client lizard pioneer submit female collect
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545

-n或--secure默認鎖上所有可用的賬戶(有利於第三方的交易)

-m或--mnemonic使用一個指定的分層確定錢包助記詞符(HD wallet mnemonic)來生成初始的地址

➜  ~ testrpc -a 1 -m "squirrel shield mass midnight aim ocean sudden brave bounce swamp attract tower"
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)

Available Accounts
==================
(0) 0xd4ce85753a9245ec87f9ddb4f10feef237a8b922

Private Keys
==================
(0) 3b5d26302347447b47ae251c00102e99cf6d44dc3bb48adf0b0a0be152ee988e

HD Wallet
==================
Mnemonic:      squirrel shield mass midnight aim ocean sudden brave bounce swamp attract tower
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545

-p或--port。用於監聽的端口。默認8545

➜  ~ testrpc -a 1 -d -p 9999
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)

Available Accounts
==================
(0) 0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1

Private Keys
==================
(0) 4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d

HD Wallet
==================
Mnemonic:      myth like bonus scare over problem client lizard pioneer submit female collect
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:9999

-h 或--hostname。用於設置監聽的域名,默認是localhost

-s 或--seed 使用任意的數據生成分層確定錢包助記詞符。種子是一串由隨機數生成器生成的隨機數。這串隨機數可以用力啊生成錢包中不同賬戶的公私鑰對

➜  ~ testrpc -a 1 -s '11'
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)

Available Accounts
==================
(0) 0x542d03ebb5f091bf2b08b5766ed896b62310d92a

Private Keys
==================
(0) c2ad48afbef7564d65589afce4c6e0e02e902ca46a79d259add7a31e5f5a1cda

HD Wallet
==================
Mnemonic:      donor curve veteran risk blind morning jewel sock quarter rescue security biology
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545

-g或--gasPrice 自定義gas價格(默認20000000000)

-l或--gasLimit 自定義gas限額(默認0x47E7C4)

-f 或--fork。從另一個當前正在運行着的以太坊客戶端所給的區塊編號開始分叉。參數值應該是另一個客戶端的HTTP訪問地址和端口號,如:http://localhost:8545.你可以使用@來指定區塊編號:http://localhost:8545@1223131

testrpc -a 1 -p 9999 -f http://localhost:8545@16

-i或--network-id。指定網絡id,TestRPC將用來識別它自己(默認當前時間,或如果配置了分叉網路的話,那麼使用分叉網絡的id)

--db指定一個保存區塊數據的目錄路徑。如果目錄中已經存在一個數據庫了,TestRPC會初始化那個鏈,而不再創建一個新鏈了。

--debug 輸出用於debug的虛擬機操作碼(opcodes)

--mem。輸出TestRPC的內存使用統計。這會替換正常的輸出

特殊options

--account 設置--account=···(注意account後面沒有‘s’)使用私鑰和他們的餘額來生成初始賬號

testrpc --account="<privatekey>,balance"

testrpc --account="0x590f47c8d2aac75460c08cd761fa81559e2053ce608f9f4fb68c27a73cb86379,1000"  --account="0x590f47c8d2aac75460c08cd761fa81559e2053ce608f9f4fb68c27a73cb86379,1000"

-u或--unlock 申明--unlock···多次,在解鎖賬戶時既可以想--unlock···傳入公鑰地址也可以傳入賬戶的索引號。當結合-secure使用時--unlock會覆蓋指定賬戶的鎖定狀態

testrpc --secure --unlock “0x1234...” --unlock “0x1234...”

也可以指定索引值來解鎖賬戶:

testrpc --secure -u  0 -u 1

3類(Library)

testrpc也可以作爲一個類庫使用

(1)當做一個Web3 provider

var TestPRC = require("ethereumjs-testrpc");

web3.setProvider(TestPRC.provider());

(2)當做一個普通的http服務

var TestPRC = require("ethereumjs-testrpc");

var server = TestRPC.server();

server.listen(port,function(err,blockchain){...});

.provider()和.server()都會返回一個對象,通過這個返回的對象你可以設置testrpc的行爲,對象中的參數可選的,可用的參數如下:

accounts。數組對象,每個對象需要有個balance的鍵,鍵對應的值用十六進制表示,通用secreKey鍵也必須要指明,它表示賬戶的私鑰。如果沒有指名secretkey就會自動生成一個

const web3= require(‘web3’)

const TestRPC = require(‘ethereumjs-testrpc’)

const web3 = new Web3()

//設置參數

web3.setProvider(TestRPC.provider({
port:9999

accounts[

          {balance,'0x12'},

          {balance,'0x12'}

]

}))

web3.eth.getAccounts(err,result) =>{

if (err){console.log(err)

console.log(result)

}

}

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章