Truffle

TesRPC 上的氣體不足錯誤

  • November 17, 2018

嘿,所以我一直在搞亂松露,當我在某個點之後測試命令時,我不斷收到氣體不足的錯誤。即使我解除安裝並重新安裝 testrpc,問題仍然存在。

對於程式碼格式問題,我深表歉意。

請記住,我想做的只是在我的測試環境中擁有無限的氣體。

這是我的程式碼`

 contract Organization {



struct Proposal {
  uint code;
  uint amount;
 string description;
 uint numberOfVotes;
 string name;
}

Proposal[] public proposals;
uint public numberOfProposals;


event ProposalAdded(uint code, uint amount, string description, int 
numberOfVotes , string name);


function addProposal(uint amount, string description , string name) 
returns (uint) {

proposals.push(Proposal(numberOfProposals,amount,description,0,name));
ProposalAdded(numberOfProposals,amount,description, 0, name);
numberOfProposals++;
return numberOfProposals;

}



function proposalExists (uint code) returns (bool) {
  for(uint i = 0; i < proposals.length; i++) {
    if (proposals[i].code == code) {
       return true;
     }
   }
    return false;
}

function numOfProposals() returns (uint){
   return numberOfProposals;
 }

function getProposalName(uint index) returns (string){
   return proposals[index].name;
}

function getProposalDescription(uint index) returns (string){
   return proposals[index].description;
}

function getProposalIndex(string name) returns (uint){
 for(uint i = 0; i < proposals.length; i++) {
     bytes memory a = bytes(proposals[i].name);
     bytes memory b = bytes(name);
   if (a.length == b.length) {
       return i;
   }
 }
 return 1000000;
}

function voteFotProposal(uint index) {
   proposals[index].numberOfVotes++;
 }

function getProposalVotesIndex(uint index) {
   proposals[index].numberOfVotes;
 }


struct Memmber {
 uint id;
 string name;
}

struct Commitee {
 uint id;
 string name;
 string missionStatement;
 uint balance;
 string [] memmbers;
}

event MemmberAdded(uint id, string name);
event CommiteeCreated(string name,string missionStatement );

string [] public memmbersArray;
string[] public comitees;
uint public numOfComitees = 0;
uint public numOfTotalMemmbers = 0;
Commitee [] fullComitees;




 function convertMemmberStrings (bytes32 [] values) internal returns 
 (string []){

       for(uint i=0;i<values.length;i++){
           MemmberAdded(numOfTotalMemmbers, 
           bytes32ToString(values[i]));
           memmbersArray.push(bytes32ToString(values[i]));
           numOfTotalMemmbers++;
   }
   return memmbersArray;
}


function addCommitee 
(string name, string missionStatement , uint funds, bytes32 [] values)
returns (string)
{
 comitees.push(name);
 CommiteeCreated(name,missionStatement );
 fullComitees.push(Commitee(numOfComitees, name, missionStatement, funds, convertMemmberStrings(values)));
 numOfComitees++;
 return name;
}

function getComitees (uint index) returns (string){
 return comitees[index];
}

function numberOfCommitees () returns (uint){
 return numOfComitees;
}

function bytes32ToString(bytes32 x) constant returns (string) {
   bytes memory bytesString = new bytes(32);
   uint charCount = 0;
   for (uint j = 0; j < 32; j++) {
     byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
     if (char != 0) {
       bytesString[charCount] = char;
       charCount++;
    }
}
bytes memory bytesStringTrimmed = new bytes(charCount);
for (j = 0; j < charCount; j++) {
   bytesStringTrimmed[j] = bytesString[j];
}
return string(bytesStringTrimmed);
}

function uintToBytes(uint v) constant returns (bytes32 ret) {
   if (v == 0) {
     ret = '0';
  }
  else {
   while (v > 0) {
       ret = bytes32(uint(ret) / (2 ** 8));
       ret |= bytes32(((v % 10) + 48) * 2 ** (8 * 31));
       v /= 10;
   }
}
return ret;
}


}

`

您可以通過啟動具有更高限制的 testrpc 來增加塊的氣體限制

testrpc -l 4500000000000

https://github.com/trufflesuite/ganache-cli/releases/tag/v3.0.0

TestRPC 3.0.0 重大更改:交易的預設氣體限制現在是 90000 氣體,而不是完整的塊氣體限制。

為了避免這些新的氣體不足錯誤,您現在可以將更高的氣體限製作為參數傳遞給 web3:

web3.eth.sendTransaction({..., gas: 3141592}) // choose your own gas limit suitable for you

引用自:https://ethereum.stackexchange.com/questions/18097