0

when I develop my smart contract with solidity, I encounter a problem. And Below is my solidity code.

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);
        }
    }
}
}

The question is when I want to call registerUser and when I using web3.js calling registerContract.methods.registerUser(name,etheraddress,contractaddress) It throws an error on geth console. The error is

Error: Transaction has been reverted by the EVM: { "blockHash": "0x45fe755c8c1f600108b55a12fa3bdf59dac0fe76d39883f23d15b2f9603d868d", "blockNumber": 22339, "contractAddress": null, "cumulativeGasUsed": 90000, "from": "0x5869c2317ce2df31cb1269d8028e9062ff470749", "gasUsed": 90000, "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status": false, "to": "0xc249fa432a1c659e7aa4ad57e24e405215461afa", "transactionHash": "0xb52e0fabe160070597bd40192658b6f84779d52d4b295b39295d381eb0856f2d", "transactionIndex": 0, "events": {} }

Is it anything wrong when I addressed struct User and array push? Or there is any problems which I was unaware of at the time I developed?

  • 2
    Have you checked if the msg.sender is the owner? It might be because of checkOwner() modifier. Maybe you are sending the transaction from a user other than the owner. – vhie Sep 28 '18 at 8:52
  • It would be better to share your execution code, coz contract looks ok(I've checked via Remix, it works) – Aquila Sep 28 '18 at 9:35
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – Richard Horrocks Sep 28 '18 at 13:09
  • @vhie yes, I have checked it. I also run successfully in remix, but I don't know why I cannot run normally in my own environment – Hermes Keng Sep 29 '18 at 9:16
  • This is admittedly not about the OP's question. I think it bears mentioning that this approach will simply fail when there are too many users. The problem is rooted in the data structure and manifests in the unbounded for loops which is a well-known anti-pattern. – Rob Hitchens - B9lab Sep 29 '18 at 19:58
0

My guess is that you are setting the gas limit too low - try increasing it from 90000 to 1000000 and see if the transaction goes through.

Reasoning is that if it was failing due to:
require(msg.sender == owner);
you'd see less gas being consumed by the function (since this is the first code that is executed, and require will refund unused gas). The fact that you are using your entire gas allowance of 90,000 implies that either you're hitting an assert (which you don't have) or you're running out of gas.

  • Thanks, the problem really is in gas, I found my code only call Mycontract.methods.Mymethod().send() but I don't give any parameter to point how much gas should be consume. btw, do you know if I don't set up gas limit, what is the default gas limit without any given specific gas ? – Hermes Keng Oct 1 '18 at 6:38
  • For truffle the default gas limit can be defined in truffle.js: – Adam Dossa Oct 1 '18 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.