以太坊 – 处理交易时的VM异常:缺乏气体

以太坊 – 处理交易时的VM异常:缺乏气体,第1张

概述我使用testrpc,web3 1.0和solidity来构建一个简单的Dapp,但我总是得到这个错误,我找不到有什么问题.请帮忙. 我的javascript文件: const Web3 = require('web3');const fs = require('fs');const web3 = new Web3(new Web3.providers.HttpProvider("http: 我使用testrpc,web3 1.0和solidity来构建一个简单的Dapp,但我总是得到这个错误,我找不到有什么问题.请帮忙.

我的javascript文件:

const Web3 = require('web3');const fs = require('fs');const web3 = new Web3(new Web3.provIDers.httpProvIDer("http://localhost:8545"));const code = fs.readfileSync('Voting.sol').toString();const solc = require('solc');const compiledCode = solc.compile(code);// deploy contractconst abIDeFinition = JsON.parse(compiledCode.contracts[':Voting'].interface);const VotingContract = new web3.eth.Contract(abIDeFinition);const byteCode = compiledCode.contracts[':Voting'].bytecode;const deployedContract = VotingContract.deploy({data: byteCode,arguments: [['a','b','c']]}).send({  from: '0x386fd5fbe3804f24b35477f06aa78a178ce021bd',gas: 4700000,gasPrice: '2000000000'},function(error,transactionHash) {}).on('error',function(error){}).on('transactionHash',function(transactionHash){}).on('receipt',function(receipt){   console.log(receipt.contractAddress);}).then(function(newContractInstance) {  newContractInstance.methods.getList().call({from: '0x386fd5fbe3804f24b35477f06aa78a178ce021bd'}).then(console.log);});

我的合同文件:

pragma solidity ^0.4.11;// We have to specify what version of compiler this code will compile withcontract Voting {  /* mapPing fIEld below is equivalent to an associative array or hash.  The key of the mapPing is candIDate name stored as type bytes32 and value is  an unsigned integer to store the Vote count  */  mapPing (bytes32 => uint8) public VotesReceived;  /* solidity doesn't let you pass in an array of strings in the constructor (yet).  We will use an array of bytes32 instead to store the List of candIDates  */  bytes32[] public candIDateList;  /* This is the constructor which will be called once when you  deploy the contract to the blockchain. When we deploy the contract,we will pass an array of candIDates who will be contesting in the election  */  function Voting(bytes32[] candIDatenames) {    candIDateList = candIDatenames;  }  function getList() returns (bytes32[]) {    return candIDateList;  }  // This function returns the total Votes a candIDate has received so far  function totalVotesFor(bytes32 candIDate) returns (uint8) {    require(valIDCandIDate(candIDate) == false);    return VotesReceived[candIDate];  }  // This function increments the Vote count for the specifIEd candIDate. This  // is equivalent to casting a Vote  function VoteForCandIDate(bytes32 candIDate) {    require(valIDCandIDate(candIDate) == false);    VotesReceived[candIDate] += 1;  }  function valIDCandIDate(bytes32 candIDate) returns (bool) {    for(uint i = 0; i < candIDateList.length; i++) {      if (candIDateList[i] == candIDate) {        return true;      }    }    return false;  }}

另外,我使用以下命令启动testrpc:

testrpc –account =“0xce2ddf7d4509856c2b7256d002c004db6e34eeb19b37cee04f7b493d2b89306d,2000000000000000000000000000000”

任何帮助,将不胜感激.

解决方法 你不应该使用gas来调用getter方法.请记住,从区块链中读取是免费的 – 写入数据需要花费金钱(气体),因为必须验证写入并达成共识.

因此,您的getter方法应使用常量属性标记,例如

function getList() constant returns (bytes32[]) {  return candIDateList;}

其次,您甚至不需要候选列表的getter,因为可以直接访问该属性,例如newContractInstance.candIDateList()

第三,你应该使用映射而不是数组,例如映射(bytes32 => bool)public valIDCandIDates,因为你的合同只关心候选者是否有效.你真的,真的不希望你的合同中有循环,因为你希望你的函数调用具有恒定的燃气成本.如果你使用循环,你将耗尽气体.

将上述所有内容放在一起就可以获得这样的合同

pragma solidity ^0.4.11;// We have to specify what version of compiler this code will compile withcontract Voting {  /* mapPing fIEld below is equivalent to an associative array or hash.  The key of the mapPing is candIDate name stored as type bytes32 and value is  an unsigned integer to store the Vote count  */  mapPing (bytes32 => uint8) public VotesReceived;  mapPing (bytes32 => bool) public valIDCandIDates;  /* This is the constructor which will be called once when you  deploy the contract to the blockchain. When we deploy the contract,we will pass an array of candIDates who will be contesting in the election  */  function Voting(bytes32[] candIDateList) {    for (uint i = 0; i < candIDateList.length; i++) {      valIDCandIDates[candIDateList[i]] = true;    }  }  // This function returns the total Votes a candIDate has received so far  function totalVotesFor(bytes32 candIDate) constant returns (uint8) {    return VotesReceived[candIDate];  }  // This function increments the Vote count for the specifIEd candIDate. This  // is equivalent to casting a Vote  function VoteForCandIDate(bytes32 candIDate) onlyForValIDCandIDate(candIDate) {    VotesReceived[candIDate] += 1;  }  function isValIDCandIDate(bytes32 candIDate) constant returns (bool)  {    return valIDCandIDates[candIDate];  }  modifIEr onlyForValIDCandIDate(bytes32 candIDate) {    require(isValIDCandIDate(candIDate));    _;  }}
总结

以上是内存溢出为你收集整理的以太坊 – 处理交易时的VM异常:缺乏气体全部内容,希望文章能够帮你解决以太坊 – 处理交易时的VM异常:缺乏气体所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1083341.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存