Solidity

solidity 動態結構數組

  • September 29, 2018

當我用solidity開發我的智能合約時,我遇到了一個問題。下面是我的可靠程式碼。

pragma solidity ^0.4.0;

contract RegisterContract{

event setNewUser(bytes32 name,address etherAddr, address contractAddr,uint now);
address owner;
struct User{
   bytes32 name;
   address etherAddr;
   address contractAddr;
}
User[] private users;

constructor() public{
   owner = msg.sender;
}
modifier checkOwner(){
   require(msg.sender == owner);
   _;
}
function getOwner() public view returns (address){
   return owner;
}
// for a new user create a contract 
function registerUser(bytes32 name,address etherAddr, address contractAddr) public checkOwner{
   User memory newUser;
   newUser.name = name;
   newUser.etherAddr = etherAddr;
   newUser.contractAddr = contractAddr;
   users.push(newUser);
   emit setNewUser(name,etherAddr,contractAddr,now);
}
// 
function setAddress(bytes32 name,address etherAddr, address contractAddr) public checkOwner{
   for(uint8 i=0;i<users.length;i++){
       if(users[i].name==name){
           users[i].etherAddr=etherAddr;
           users[i].contractAddr=contractAddr;
       }
   }
}
// when systems assess all user
function getUsers() public checkOwner view returns (bytes32[],address[],address[]) {
   bytes32[] memory names= new bytes32[](users.length);
   address[] memory etherAddr = new address[](users.length);
   address[] memory contractAddr = new address[](users.length);
   for(uint8 i=0;i<users.length;i++){
       names[i]= users[i].name;
       etherAddr[i] = users[i].etherAddr;
       contractAddr[i] = users[i].contractAddr;
   }
   return (names,etherAddr,contractAddr);
}
//for a user who import contract 
function getContractAddress(address etherAddr) public checkOwner view returns (bytes32,address) {
   for(uint8 i=0;i<users.length;i++){
       if(users[i].etherAddr==etherAddr){
           return (users[i].name,users[i].contractAddr);
       }
   }
}
}

問題是當我想呼叫registerUser以及當我使用 web3.js 呼叫**registerContract.methods.registerUser(name,etheraddress,contractaddress)**它在 geth 控制台上引發錯誤。錯誤是

錯誤:交易已經恢復由EVM:{“blockHash”:“0x45fe755c8c1f600108b55a12fa3bdf59dac0fe76d39883f23d15b2f9603d868d”,“blockNumber”:22339,“contractAddress”:空,“cumulativeGasUsed”:90000,“從”:“0x5869c2317ce2df31cb1269d8028e9062ff470749”,“gasUsed”:90000 , “logsBloom”: “0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000“,”狀態“:假的,”要“:”0xc249fa432a1c659e7aa4ad57e24e405215461afa“,”transactionHash“:”0xb52e0fabe160070597bd40192658b6f84779d52d4b295b39295d381eb0856f2d“,”transactionIndex“:0,”事件“:{}}{} }{} }

當我處理 struct User 和數組推送時有什麼問題嗎?或者有什麼我在開發時沒有意識到的問題?

我的猜測是您將氣體限制設置得太低 - 嘗試將其從 90000 增加到 1000000 並查看交易是否通過。

推理是,如果它由於以下原因而失敗:

require(msg.sender == owner);

您會看到該函式消耗的氣體較少(因為這是執行的第一個程式碼,並且 require 將退還未使用的氣體)。您使用了 90,000 的全部 gas 配額這一事實意味著您要麼遇到了一個斷言(你沒有),要麼你的 gas 用完了。

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