gas问题out of gas的解决

时间:2021-07-20 23:33:32

1.昨天遇见了还是以前遇见的问题,就是发现有些函数就是不能用web3调用,然后怎么弄都写不到数组上,但是今天终于将它解决了
web3的学习:
https://github.com/ethereum/wiki/wiki/JavaScript-API

今天又重新想要跑一下cryptopunks
当想要在私有链配置自己的合约的时候,有时你会很残酷地发现它告诉你out of gas,这个时候你就要去查看你的初始块配置的时候的gasLimit是不是设的比较小了,或者是你部署合约的时候gas给的是不是太小
重新又创建了一个私有链local4,这一次它的gaslimit设置为8个f,果然就没有再出现过gas方面的问题了

但是在调用函数的时候我又出现了gas方面的问题

我发现这个问题的原因竟然是因为我调用的时候没有设定使用的gas数,在remix的compile-details-gasestimates中我们是可以查看到每个函数大概要用到的gas的数量的,在下面的错误的transaction信息中,我们可以看见,gas的估计基本上都是90000,我想啊,这可能是自定义的每一个函数使用的最大值,当你的函数运行超过这个值以后呢,可能运行就不成功了,因为没gas了,所以之前的调用才会出错
然后之后我就去测了一个没有设置gas但是成功了的transaction,然后发现它的gas值处果然是90000,这个值是该交易的sender给的gas数

> eth.getTransaction('0x4650e6c26f9ad2b850b66ad0e2b6836f4e6ad1760453d45c9d5d870c312cc227')
{
blockHash: "0x40afdacfe0d91bac521ae4ff924b00952191da1d201180c31e6b368b83f25f06",
blockNumber: 132,
from: "0x0c585ae7e2e15ffd3e04e82c0e4adf5bad5a94b0",
gas: 90000,
gasPrice: 18000000000,
hash: "0x4650e6c26f9ad2b850b66ad0e2b6836f4e6ad1760453d45c9d5d870c312cc227",
input: "0x3ccfd60b",
nonce: 13,
r: "0x8af9e5b6be338538c2862bb36516052c9782a819644acea94cff243cbc2dab82",
s: "0x63e5f39801860647e82ebd736fa1c32dc00fb2d53b58f55fac87c46e87bf3501",
to: "0xafddc17c7963bd81598defbe28b43230a4bdb719",
transactionIndex: 0,
v: "0x41",
value: 0
}

然后再去查看一个自己设置了gas的transaction的gas值,我们可以很明确地看见这里的gas值就是我自己设置的300000

> eth.getTransaction('0x97a1ea29b42e0c7edee7fcfb742920b18c2732ee1a3e33ff2371a5208a13ca1e')
{
blockHash: "0xe1c7fe5cd751935f98a6fc0d3b65eab9ac4b1f4b5dcc81bbdcaf258aa40f4f26",
blockNumber: 131,
from: "0x0c585ae7e2e15ffd3e04e82c0e4adf5bad5a94b0",
gas: 300000,
gasPrice: 18000000000,
hash: "0x97a1ea29b42e0c7edee7fcfb742920b18c2732ee1a3e33ff2371a5208a13ca1e",
input: "0x23165b7500000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000258",
nonce: 12,//交易的发起者在之前进行过的交易数量。
r: "0x771a755550958a5863d8ca5b56784d62fa449afb724fd2efd28bd36a38dc183a",
s: "0x2fc49b6f4303fe4beb96f70921ca79100ac3a991206132429aa4e42e45590bad",
to: "0xafddc17c7963bd81598defbe28b43230a4bdb719",
transactionIndex: 0,//该交易在区块中的索引
v: "0x41",
value: 0
}

Web3调用语句:

instance.acceptBidForPunk(6,600,{from:web3.eth.accounts[1]});
> eth.getTransactionReceipt('0x2cab683a0c84d917210652f18f80161e8141893497e45cd09609525d537ad86e')
{
blockHash: "0x15d1f2d37f4647419dc583d1af1dd47d183e8c21041254b07e9f8ea7ff81f496",
blockNumber: 128,
contractAddress: null,
cumulativeGasUsed: 90000,
from: "0x0c585ae7e2e15ffd3e04e82c0e4adf5bad5a94b0",
gasUsed: 90000,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
status: "0x0",//没成功
to: "0xafddc17c7963bd81598defbe28b43230a4bdb719",
transactionHash: "0x2cab683a0c84d917210652f18f80161e8141893497e45cd09609525d537ad86e",
transactionIndex: 0
}

从这个返回结果我们可以看出这笔交易没能成功写到区块上

本来这个函数的作用是接受这个投标bid,然后下面来个数组的信息就应该变为false等,但是可以看见下面的输出还是没有改变
console.log(instance.punksOfferedForSale.call(6));
console.log(instance.punkBids.call(6));

[ true,
BigNumber { s: 1, e: 0, c: [ 6 ] },
'0x0c585ae7e2e15ffd3e04e82c0e4adf5bad5a94b0',
BigNumber { s: 1, e: 2, c: [ 600 ] },
'0x0000000000000000000000000000000000000000' ]
wanghuideMacBook-Pro:new wanghui$ node new1.js
wanghuideMacBook-Pro:new wanghui$ node new1.js
[ true,
BigNumber { s: 1, e: 0, c: [ 6 ] },
'0xa79be9d7cb24c4b43f5e89198f25a96f7a5f191f',
BigNumber { s: 1, e: 2, c: [ 620 ] } ]

Web3调用语句,加上gas后:
instance.acceptBidForPunk(6,600,{from:web3.eth.accounts[1],gas:300000});

> eth.getTransactionReceipt('0x97a1ea29b42e0c7edee7fcfb742920b18c2732ee1a3e33ff2371a5208a13ca1e')
{
blockHash: "0xe1c7fe5cd751935f98a6fc0d3b65eab9ac4b1f4b5dcc81bbdcaf258aa40f4f26",
blockNumber: 131,
contractAddress: null,
cumulativeGasUsed: 56074,
from: "0x0c585ae7e2e15ffd3e04e82c0e4adf5bad5a94b0",
gasUsed: 56074,
logs: [{
address: "0xafddc17c7963bd81598defbe28b43230a4bdb719",
blockHash: "0xe1c7fe5cd751935f98a6fc0d3b65eab9ac4b1f4b5dcc81bbdcaf258aa40f4f26",
blockNumber: 131,
data: "0x0000000000000000000000000000000000000000000000000000000000000001",
logIndex: 0,
removed: false,
topics: ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x0000000000000000000000000c585ae7e2e15ffd3e04e82c0e4adf5bad5a94b0", "0x000000000000000000000000a79be9d7cb24c4b43f5e89198f25a96f7a5f191f"],
transactionHash: "0x97a1ea29b42e0c7edee7fcfb742920b18c2732ee1a3e33ff2371a5208a13ca1e",
transactionIndex: 0
}, {
address: "0xafddc17c7963bd81598defbe28b43230a4bdb719",
blockHash: "0xe1c7fe5cd751935f98a6fc0d3b65eab9ac4b1f4b5dcc81bbdcaf258aa40f4f26",
blockNumber: 131,
data: "0x0000000000000000000000000000000000000000000000000000000000000000",
logIndex: 1,
removed: false,
topics: ["0x58e5d5a525e3b40bc15abaa38b5882678db1ee68befd2f60bafe3a7fd06db9e3", "0x0000000000000000000000000000000000000000000000000000000000000006", "0x0000000000000000000000000c585ae7e2e15ffd3e04e82c0e4adf5bad5a94b0", "0x0000000000000000000000000000000000000000000000000000000000000000"],
transactionHash: "0x97a1ea29b42e0c7edee7fcfb742920b18c2732ee1a3e33ff2371a5208a13ca1e",
transactionIndex: 0
}],
logsBloom: "0x00000000004000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000400000000000000000000000000000100010000000008000000000000000000000000000000001000000000000000020000000000000000000800000000000000000000000010000000000000000408000000001000400000000000000000000008000000800000000000000000000000000000000000000000000000000000000000000000080000000000000002000000000000000000000000000000000000000000000000000020000000000080000000000000000000000000000000000000000000002000000000",
status: "0x1",
to: "0xafddc17c7963bd81598defbe28b43230a4bdb719",
transactionHash: "0x97a1ea29b42e0c7edee7fcfb742920b18c2732ee1a3e33ff2371a5208a13ca1e",
transactionIndex: 0
}

然后就发现这个函数的调用成功了,但是很奇怪的一点,就是我们可以从gasUsed: 56074看出,真正使用到的gas其实是小于90000的,但是为什么还是失败,之后看见在解释吧
数组信息变为:
console.log(instance.punksOfferedForSale.call(6));
console.log(instance.punkBids.call(6));

[ false,
BigNumber { s: 1, e: 0, c: [ 6 ] },
'0xa79be9d7cb24c4b43f5e89198f25a96f7a5f191f',
BigNumber { s: 1, e: 0, c: [ 0 ] },
'0x0000000000000000000000000000000000000000' ]
[ false,
BigNumber { s: 1, e: 0, c: [ 6 ] },
'0x0000000000000000000000000000000000000000',
BigNumber { s: 1, e: 0, c: [ 0 ] } ]

余额变为
console.log(instance.pendingWithdrawals.call(web3.eth.accounts[1]));

BigNumber { s: 1, e: 2, c: [ 620 ] }

当然,这上面只是十分死板地将gas的值给定死了,如果想要比较自动得到使用的gas值的话,是可以使用web3的estimate来做的
或者是你可以先调用一个函数,但是这个时候你还不要进行挖矿,然后此时交易的区块的状态就是pending,查看其gaslimit
web3.eth.getBlock("pending").gasLimit
4712388

然后在gas处就可以写这个值了

gas问题out of gas的解决的更多相关文章

  1. solidity 合约单元测试报错 org.fisco.bcos.web3j.protocol.exceptions.TransactionException: Transaction has failed with status: 0x16. Gas used: 1163650. (not-enough gas?)

    org.fisco.bcos.web3j.protocol.exceptions.TransactionException: Transaction has failed with status: 0 ...

  2. ADs系列之通用数据解析服务GAS(即将开源)

    面对成百上千的生产系统用户操作数据接入落地,你是否厌倦了每次机械编写打包解包的代码?对一次性接入多个数据的时候,还要对不同人联调,费时费力,你是否还会手忙脚乱,忙中不断出错?是否当数据出问题了,用的时 ...

  3. Self-organizing Maps及其改进算法Neural gas聚类在异常进程事件识别可行性初探

    catalogue . SOM简介 . SOM模型在应用中的设计细节 . SOM功能分析 . Self-Organizing Maps with TensorFlow . SOM在异常进程事件中自动分 ...

  4. 区块链扩容方案之Gas值限制

    区块链扩容一直是区块链团队的重点研究方向.因为比特币对区块大小的设定是固定的,而且中本聪将最初大小值限定为1M,但随着交易量的增加,网络拥堵情况也愈渐严重,最终也导致了比特币的分叉. 区别于比特币固定 ...

  5. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  6. [LeetCode] Gas Station 加油站问题

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  7. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  8. 20. Candy && Gas Station

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  9. Gas Station

    Description: There are N gas stations along a circular route, where the amount of gas at station i i ...

随机推荐

  1. c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast

    c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast  [版权声明]转载请注明出处 http://www.cnblogs.c ...

  2. python中使用chrome进行自动化测试,浏览器变量设置

  3. struts2的 result 通配符 OGNL

    result: 1). result 是 action 节点的子节点 2). result 代表 action 方法执行后, 可能去的一个目的地 3). 一个 action 节点可以配置多个 resu ...

  4. PHP 安全

    作为PHP程序员,特别是新手,对于互联网的险恶总是知道的太少,对于外部的入侵有很多时候是素手无策的,他们根本不知道黑客是如何入侵的.提交入侵.上传漏洞.sql 注入.跨脚本攻击等等.作为最基本的防范你 ...

  5. detain ssh server 设置

    ssh connection refused 处理方法 一般这种情况是 opens server 没安装 或 没启动 检查 openssh 是否安装 su 登录root账号,安装 openssh se ...

  6. OD: Universal Shellcode

    本节讲如果开发通用的 Shellcode. Shellcode 的组织 shellcode 的组织对成功地 exploit 很重要. 送入缓冲区的数据包括: . 填充物.一般用 0x90 (NOP) ...

  7. c语言中的字符数组与字符串

    1.字符数组的定义与初始化 字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素. char str[10]={ 'I',' ','a','m',' ',‘h’,'a','p','p','y ...

  8. VS2013中使用QT插件后每次重新编译问题

    环境 系统:win7 64位旗舰版 软件:VS2013.QT5.5.1-32位.Qt5 Visual Studio Add-in1.2.4 概述 使用QT Visual Studio插件打开pro项目 ...

  9. forfiles命令批处理删除过期文件

    命令格式: forfiles.exe /p "D:\备份" /s /m *.zip /d -7 /c "cmd /c del @path" /p:指定目录 /s ...

  10. 【译】.NET 跨平台界面框架和为什么你首先要考虑再三

    现在用 C# 来开发跨平台应用已经有很成熟的方案,即共用非界面代码,而每个操作系统搭配特定的用户界面代码.这个方案的好处是可以直接使用操作系统原生的控件和第三方控件,还能够和操作系统深度集成. 这里的 ...