使用Geth部署的私有網(wǎng)絡(luò)痪寻。
然后在使用truffle migrate
部署智能合約的時候娄昆,出現(xiàn)了如下錯誤:
Running migration: 1_initial_migration.js
Deploying Migrations...
... undefined
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: exceeds block gas limit
...
有可能是兩個問題引起的:
- 可能給了transaction太高的gaslimit浸船,尤其是高于block gas limit昭齐。所以要么降低transaction的gaslimit要么提高block gas limit瞎惫。
降低truffle的transaction gaslimit:
在truffle.js配置項中添加了from和gas的參數(shù):
from: "address-account",
gas: 4612388
提高block的gaslimit:
在geth啟動時添加--targetgaslimit '9000000000000'
的配置項盐股。
其中驮审,針對合約和交易的gas限制鲫寄,有一個說明:
- 交易的大小限制并不是一個固定數(shù),截至到16.2.7號限制大概是 780KB(大約300w gas)
- 交易和區(qū)塊的大小都沒有固定的限制疯淫,它可以自動伸縮地来,但是這并不是意味著完全沒有限制,當(dāng)前單個區(qū)塊最多的gas使用量為3,141,592 gas熙掺,理論上來講未斑,你可以創(chuàng)建一個可以消耗單個區(qū)塊所有g(shù)as的,很大的單筆交易
genache-cli
之前一直用genache的客戶端币绩,完美兼容蜡秽。用到原生的geth,就多少會有點問題缆镣。后來發(fā)現(xiàn)還有個genache-cli終端程序芽突。下來試一下。
其和Ganache一樣董瞻,都是Truffle框架中的一個開發(fā)工具寞蚌,其實就是Ganache的一個命令行版本田巴。其使用ethereumjs來模擬了所有ethereum應(yīng)用的行為。同時也包含了RPC功能挟秤,可以用來部署自己的私有網(wǎng)絡(luò)壹哺。
安裝
使用npm安裝:
npm install -g ganache-cli
使用方法
命令格式:
$ ganache-cli <options>
官方給的各個配置參數(shù)說明(稍后翻譯,先放原文):
-a or --accounts: Specify the number of accounts to generate at startup.
-e or --defaultBalanceEther: Amount of ether to assign each test account. Default is 100.
-b or --blockTime: Specify blockTime in seconds for automatic mining. Default is 0 and no auto-mining.
-d or --deterministic: Generate deterministic addresses based on a pre-defined mnemonic.
-n or --secure: Lock available accounts by default (good for third party transaction signing)
-m or --mnemonic: Use a specific HD wallet mnemonic to generate initial addresses.
-p or --port: Port number to listen on. Defaults to 8545.
-h or --hostname: Hostname to listen on. Defaults to Node's server.listen() default.
-s or --seed: Use arbitrary data to generate the HD wallet mnemonic to be used.
-g or --gasPrice: Use a custom Gas Price (defaults to 20000000000)
-l or --gasLimit: Use a custom Gas Limit (defaults to 90000)
-f or --fork: Fork from another currently running Ethereum client at a given block. Input should be the HTTP location and port of the other client, e.g. http://localhost:8545. You can optionally specify the block to fork from using an @ sign: http://localhost:8545@1599200.
-i or --networkId: Specify the network id the ganache-cli will use to identify itself (defaults to the current time or the network id of the forked blockchain if configured)
--db: Specify a path to a directory to save the chain database. If a database already exists, ganache-cli will initialize that chain instead of creating a new one.
--debug: Output VM opcodes for debugging
--mem: Output ganache-cli memory usage statistics. This replaces normal output.
--noVMErrorsOnRPCResponse: Do not transmit transaction failures as RPC errors. Enable this flag for error reporting behaviour which is compatible with other clients such as geth and Parity.
作為node_module
其直接可以作為nodejs的組件艘刚,直接提供web3js的provider管宵,然后與eth網(wǎng)絡(luò)進行交互。
作為Web3的provider:
var ganache = require("ganache-cli");
web3.setProvider(ganache.provider());
作為一個http server(提供服務(wù)):
var ganache = require("ganache-cli");
var server = ganache.server();
server.listen(port, function(err, blockchain) {...});