8

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 ?

2
  • where is contract mortal?
    – Shomari
    Commented Dec 28, 2017 at 11:40
  • 1
    I updated by question Commented Dec 29, 2017 at 7:52

3 Answers 3

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.

6
  • I see it is executing, it maybe a bug in remix as you pointed out. Thankyou :) Commented Dec 29, 2017 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.
    – user19510
    Commented Dec 29, 2017 at 14:15
  • @smarx, yes I see your point, as the string type appears to be unbound
    – Shomari
    Commented Jan 1, 2018 at 1:24
  • @smarx So if string is unbounded, what is the best alternative for storing string data ? Commented Jan 1, 2018 at 7:47
  • 1
    @AhsanJamal You can use bytes32 or any number of bytes in place of string. Just make sure that the bytes can support necessary information. You can then easily convert the bytes to string easily. ethereum.stackexchange.com/questions/2519/… Commented Oct 7, 2018 at 10:43
8

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.

7
  • 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.
    – user19510
    Commented Dec 28, 2017 at 13:22
  • Just to confirm, I see the same infinite gas warnings as OP when using Solidity 0.4.19 on Remix.
    – user19510
    Commented Dec 28, 2017 at 15:32
  • Even if I change the type from string to Bytes32, I am still getting infinite gas for these functions. Commented Dec 29, 2017 at 7:51
  • Switching to bytes32 gets rid of the warning for me.
    – user19510
    Commented Dec 29, 2017 at 14:25
  • Why is this being downvoted? Seems reasonable enough Commented Jan 11, 2018 at 9:07
1

It is because you are using string which does not has a particular size i.e., it is unbound data type. You can use bytes32 in place of string to limit the cost of gas on execution code because solidity considers it a 32 bit literal.

1
  • That said, is it true people should avoid using string at any cost, since it will always return infinite gas usage.
    – Maxareo
    Commented Mar 25, 2021 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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