在學(xué)習(xí)區(qū)塊鏈時(shí),我們按照某些文章的教程,使用 Browser-solidity 在 Go-Ethereum上進(jìn)行智能合約部署時(shí),可能會(huì)出現(xiàn)Error: exceeds block gas limit undefined
的報(bào)錯(cuò)信息,表示當(dāng)前合約所需的gas超過了區(qū)塊的最大gas惧眠。這可能是由于創(chuàng)世區(qū)塊的配置文件genesis.json
中的參數(shù)gasLimit
設(shè)置過小有關(guān),以下是可能的解決辦法于个。
查詢原因
這些文章中提供的創(chuàng)世區(qū)塊配置文件genesis.json
中的參數(shù)gasLimit
的值為0x2fefd8
氛魁,這是一個(gè)16進(jìn)制的數(shù)字,轉(zhuǎn)換為十進(jìn)制為3141592
厅篓。進(jìn)制在線轉(zhuǎn)換工具秀存。
我們使用此配置初始化,然后輸入miner.start()
挖礦羽氮,等待一段時(shí)間后輸入miner.stop()
停止挖礦或链。
先查看區(qū)塊數(shù):
> eth.blockNumber
132
隨便查看一個(gè)區(qū)塊的信息:
> 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: []
}
可以看到此區(qū)塊的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澳盐,已經(jīng)比區(qū)塊的gasLimit還高,部署時(shí)當(dāng)然會(huì)失敗令宿。這可能由于之前通過Browser-solidity編譯出來的gas普遍只有幾十萬叼耙,而現(xiàn)在已經(jīng)需要幾百萬的gas。
解決辦法
將創(chuàng)世區(qū)塊的配置文件genesis.json
中的參數(shù)gasLimit
設(shè)置為0xffffffff
(十進(jìn)制值為4294967295
)粒没。重新初始化筛婉,生成區(qū)塊,查看區(qū)塊信息癞松。
> 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: []
}
可以看到此區(qū)塊的gasLimit為4211854946爽撒。這時(shí)再部署就不會(huì)報(bào)錯(cuò)了。
遺留問題
我在網(wǎng)上搜索解決方法時(shí)响蓉,有看到類似的問題A problem about gas limit #3760匆浙,解答者表示可以使用--targetgaslimit
這個(gè)指令來修改區(qū)塊的gas limit,不過我試了很久一直提示指令錯(cuò)誤厕妖,也沒有搜索到這個(gè)指令應(yīng)該怎么用,我是這樣用的:
> eth.blockNumber
132
> --target 132 gaslimit 8888888
(anonymous): Line 1:10 Unexpected number (and 1 more errors)
提示錯(cuò)誤挑庶。有大神如果知道的話請留言告訴我言秸,謝謝!
已解決迎捺。使用--targetgaslimit value
就可以設(shè)置最低gas
限制举畸。value
傳gas
最低值,默認(rèn)4712388
凳枝。