0

I have minimized the following solidity code generated in remix to demonstrate the issue. This code warns the test001.regester function requires infinite gas which obviously by the code cannot be.

Could some one determine what the issue really is?

I have tried several versions of calling sequence of the remote call, and all have the same issue. (Other versions are shown commented out in the function code below.)

pragma solidity ^0.4.17;

contract test002 {

    address[] AddressList;

    event ItemAdded(address, address);


    function getItemCount() public view returns(uint) {return(AddressList.length);}
    function getItem(uint index) public view returns(address) {return(AddressList[index]);}

    function addItem(address inItem) public {
        AddressList[AddressList.length] = inItem;
        ItemAdded(this, inItem);
    }

}

contract test001 {

    address AA;

    event Creation(address, address);

    function test001() public {
        AA = new test002();

        Creation(address(this), AA);
    }

    function register() public {
        // option 1
//        test002(AA).addItem(address(this));

        // option 2        
//        test002 A = test002(AA);
//        A.addItem(this);

        // option 3
        test002 A = test002(AA);
        A.addItem(address(this));
    }



}
1

I believe this is just a limitation of the Solidity compiler. Any call to another contract triggers this warning, since Solidity doesn't know how much gas the other contract will require.

It's technically possible for the compiler to recognize here via static analysis that the address you're using always comes from new test002() in the constructor, but that's actually quite a difficult inference to make. The Solidity compiler isn't that smart, so it gives up and says you're calling another contract and so the required gas cannot be determined at compile time.

It's safe to just ignore the warning.

EDIT

Addressing the error you mentioned in the comment.

This line always tries to write past the end of an array:

AddressList[AddressList.length] = inItem;

You either need to extend the length of array first, like so:

AddressList.length += 1
AddressList[AddressList.length - 1] = inItem;

or (better) just use push:

AddressList.push(inItem);
|improve this answer|||||
  • When creating the contract(s) in Remix, the call to register fails due to the following error with no ability to debug. "transact to test001.register errored: gas required exceeds allowance or always failing transaction" – Les Feb 10 '18 at 19:13
  • Are you sure? For me, it fails with "transact to test001.register errored: VM error: revert." Oh, just saw your comment edit. Yes, that error is similar. Neither has to do with the infinite gas warning. – user19510 Feb 10 '18 at 19:15
  • See my edit for the cause of that error. – user19510 Feb 10 '18 at 19:17
  • Thank you very much! That seems to resolve this issue for this example! I will now go back and attempt to apply this fix to the original code and see what happens. – Les Feb 10 '18 at 19:25

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.