以太坊常见问题和错误

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

在学习区块链时,我们按照某些文章的教程,使用 Browser-solidity 在 Go-Ethereum上进行智能合约部署时,可能会出现Error: exceeds block gas limit undefined的报错信息,表示当前合约所需的gas超过了区块的最大gas。这可能是由于创世区块的配置文件genesis.json中的参数gasLimit设置过小有关,以下是可能的解决办法。

查询原因

这些文章中提供的创世区块配置文件genesis.json中的参数gasLimit的值为0x2fefd8,这是一个16进制的数字,转换为十进制为3141592。进制在线转换工具。

我们使用此配置初始化,然后输入miner.start()挖矿,等待一段时间后输入miner.stop()停止挖矿。

先查看区块数:

> eth.blockNumber
132

随便查看一个区块的信息:

> eth.getBlock(132)
{
  difficulty: 138787,
  extraData: "0xd783010801846765746886676f312e31308664617277696e",
  gasLimit: 3573388,
  gasUsed: 0,
  hash: "0x6f7d84c21967eafe67f13f6d7abc2143ddee006e93999c890990aad63e9a4fc5",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xf2e4cf8222e79b579543a5708ff0dfd1e6e37cfe",
  mixHash: "0x76d1f5e34735baa2491664025fadaa8376780df3e67125eff281736a27d57e8f",
  nonce: "0x090925fd3caf31cd",
  number: 132,
  parentHash: "0x92462eaf6fbbd80445724d5fde82794e3c0af56d348b2edfb7ce1e46ecefae39",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 536,
  stateRoot: "0xfa9ea92f0ad0545079e4f0bed40a08632a24c2693ac57b6b2242ae122b6356b0",
  timestamp: 1519481595,
  totalDifficulty: 17930276,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

可以看到此区块的gasLimit为3573388

然后再通过Browser-solidity编写智能合约,然后编译生成web3代码:

var testcontractContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"add","outputs":[{"name":"b","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]);
var testcontract = testcontractContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000f57600080fd5b60b68061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631003e2d2146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b600080606483019050809150509190505600a165627a7a723058208f5727572e0f6113593417a5211692c99ed69158e62764f4e3f76c0c5846afc30029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

可以看到此合约所需的gas为4700000,已经比区块的gasLimit还高,部署时当然会失败。这可能由于之前通过Browser-solidity编译出来的gas普遍只有几十万,而现在已经需要几百万的gas。

解决办法

将创世区块的配置文件genesis.json中的参数gasLimit设置为0xffffffff(十进制值为4294967295)。重新初始化,生成区块,查看区块信息。

> eth.blockNumber
20
> eth.getBlock(20)
{
  difficulty: 132288,
  extraData: "0xd783010801846765746886676f312e31308664617277696e",
  gasLimit: 4211854946,
  gasUsed: 0,
  hash: "0x98a230aa2d7aaee1b92732488aff3571d9569b44ee2c52bd2a47cc115649ba1d",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
  mixHash: "0xef93a1be29bbabcf7e6a9d2031b2b76b224547c77aa4f5a6d5edaa1ed538e428",
  nonce: "0x756c86cc86699021",
  number: 20,
  parentHash: "0x62dacb00b038677688e70da047f5074038caa8ac16d161118a06b8a053789bda",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 536,
  stateRoot: "0xc74d4094304b408ac107955a721c1a45393687672cf1ab40761c1bf37cc5efff",
  timestamp: 1519531846,
  totalDifficulty: 2764672,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

可以看到此区块的gasLimit为4211854946。这时再部署就不会报错了。

遗留问题

我在网上搜索解决方法时,有看到类似的问题A problem about gas limit #3760,解答者表示可以使用--targetgaslimit这个指令来修改区块的gas limit,不过我试了很久一直提示指令错误,也没有搜索到这个指令应该怎么用,我是这样用的:

> eth.blockNumber
132
> --target 132 gaslimit 8888888
(anonymous): Line 1:10 Unexpected number (and 1 more errors)

提示错误。应该使用--targetgaslimit value就可以设置最低gas限制。value传gas最低值,默认4712388。

原文转载自:https://www.jianshu.com/p/f030338a0c31