5

I am testing out solidity in remix ide using simple contracts. This is the contract that I wrote:

 contract mortal {

address owner;

function mortal() {
    owner = msg.sender;
}

function kill(){
    if(msg.sender == owner){
        selfdestruct(owner);
    }
   }
}

contract Hello is mortal
{

string public message;

function Hello(){
    message = 'This is the initial Message';
    }

function getMessage() public constant returns(string){
    return message;
    }

function setNewMessage(string newMessage) public payable {
    message = newMessage;   
    }
} 

On looking at details I am getting infinite gas estimates for each of these functions.

{
"Creation": {
    "codeDepositCost": "243200",
    "executionCost": "infinite",
    "totalCost": "infinite"
},
"External": {
    "getMessage()": "infinite",
    "kill()": "30636",
    "message()": "infinite",
    "setNewMessage(string)": "infinite"
}
}

Please elaborate why am I getting infinite gasEstimates for these functions and how can I avoid this mistake ?

  • where is contract mortal? – nyusternie Dec 28 '17 at 11:40
  • 1
    I updated by question – Ahsan Jamal Dec 29 '17 at 7:52
3

The contract compiles just fine:

pragma solidity ^0.4.15;

contract mortal {
    address owner;

    function mortal() public {
        owner = msg.sender;
    }

    function kill() public {
        if (msg.sender == owner) {
            selfdestruct(owner);
        }
    }
}

contract Hello is mortal {
    string public message;

    function Hello() public {
        message = 'This is the initial Message';
    }

    function getMessage() public constant returns(string) {
        return message;
    }

    function setNewMessage(string newMessage) public payable {
        message = newMessage;   
    }
} 

but then the warnings appear...

enter image description here

but it submits/confirms on Ropsten just fine..

enter image description here

and the method calls work fine too..

enter image description here

This may just be a bug in the compiler's static analysis.

  • I see it is executing, it maybe a bug in remix as you pointed out. Thankyou :) – Ahsan Jamal Dec 29 '17 at 13:09
  • 2
    The warning is just that. It doesn't mean that the contract won't work; it just means that the gas usage is potentially unbounded. I think that the warning is correct. – smarx Dec 29 '17 at 14:15
  • @smarx, yes I see your point, as the string type appears to be unbound – nyusternie Jan 1 '18 at 1:24
  • @smarx So if string is unbounded, what is the best alternative for storing string data ? – Ahsan Jamal Jan 1 '18 at 7:47
  • I'm not sure I understand the question. What's wrong with just using string? If you want to limit the length of the string users can supply, you could check the length. (E.g. require(newMessage.length <= 20);) – smarx Jan 1 '18 at 10:56
6

I suspect this is because the cost of those functions is indeed unbounded. Strings can be of any length, so setNewMessage() needs to store an unbounded amount of data, and getMessage() needs to read an unbounded amount of data.

If you want to avoid that warning, you'd have to use a data type with a fixed upper bound on its size.

  • I don't know what an "out of gas limit" is (maybe the infinite gas warning being discussed?) but please do add an answer if you believe you have a solution. – smarx Dec 28 '17 at 13:22
  • Just to confirm, I see the same infinite gas warnings as OP when using Solidity 0.4.19 on Remix. – smarx Dec 28 '17 at 15:32
  • Even if I change the type from string to Bytes32, I am still getting infinite gas for these functions. – Ahsan Jamal Dec 29 '17 at 7:51
  • Switching to bytes32 gets rid of the warning for me. – smarx Dec 29 '17 at 14:25
  • Why is this being downvoted? Seems reasonable enough – Vincent Van Den Berghe Jan 11 '18 at 9:07

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.