• gas问题out of gas的解决



    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处就可以写这个值了

  • 相关阅读:
    Django中的CSRF
    CentOS 7上快速安装saltstack
    Django中的Form和ModelForm
    Linux下git使用
    django 登录、注册
    django 知识点小结
    django 博客搭建
    django models中字段
    django models中的class meta
    Django Queryset增加manager
  • 原文地址:https://www.cnblogs.com/wanghui-garcia/p/9507548.html
Copyright © 2020-2023  润新知