美文网首页我爱编程
以太坊开发学习笔记 - truffle migrate

以太坊开发学习笔记 - truffle migrate

作者: kamiSDY | 来源:发表于2018-04-12 19:07 被阅读0次

使用Geth部署的私有网络。
然后在使用truffle migrate部署智能合约的时候,出现了如下错误:

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的参数:

from: "address-account",
gas: 4612388

提高block的gaslimit:
在geth启动时添加--targetgaslimit '9000000000000'的配置项。

其中,针对合约和交易的gas限制,有一个说明:

  • 交易的大小限制并不是一个固定数,截至到16.2.7号限制大概是 780KB(大约300w gas)
  • 交易和区块的大小都没有固定的限制,它可以自动伸缩,但是这并不是意味着完全没有限制,当前单个区块最多的gas使用量为3,141,592 gas,理论上来讲,你可以创建一个可以消耗单个区块所有gas的,很大的单笔交易

genache-cli

之前一直用genache的客户端,完美兼容。用到原生的geth,就多少会有点问题。后来发现还有个genache-cli终端程序。下来试一下。
其和Ganache一样,都是Truffle框架中的一个开发工具,其实就是Ganache的一个命令行版本。其使用ethereumjs来模拟了所有ethereum应用的行为。同时也包含了RPC功能,可以用来部署自己的私有网络。

安装

使用npm安装:
npm install -g ganache-cli

使用方法

命令格式:
$ ganache-cli <options>

官方给的各个配置参数说明(稍后翻译,先放原文):

-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网络进行交互。
作为Web3的provider:

var ganache = require("ganache-cli");
web3.setProvider(ganache.provider());

作为一个http server(提供服务):

var ganache = require("ganache-cli");
var server = ganache.server();
server.listen(port, function(err, blockchain) {...});

参考链接

相关文章

网友评论

    本文标题:以太坊开发学习笔记 - truffle migrate

    本文链接:https://www.haomeiwen.com/subject/nrpfkftx.html