0

Why does the attached contract always throw an invalid opcode when I execute test() and verify() (in that order)?

pragma solidity ^0.4.10;

contract Parent
{

    uint value;

    function isValueOne() public constant returns (bool) {
        return value == 1;
    }

}


contract Child is Parent
{

    uint public value;

    function test() public {
        value = 1;
    }

    function verify() {
        require(isValueOne());
    }
}

From my understanding, this should never be the case. I've read the docs on inheritance and I couldn't find out what's going on. After moving isValueOne to Child everything works.

I've tested this code on testrpc and remix with the same results.

0

You code is failing for a few reasons.

1- Function verify() is not marked as constant. It should be marked as constant given that it is reading data and not writing. (Fixing this will eliminate the error you are getting).

2- Even when this error is fixed, the contract will not work as intended because you are declaring uint public value in the Child which shadows the declaration of the variable with the same name/type in Parent.

Here's how your code should look for it to work:

pragma solidity ^0.4.10;

contract Parent
{

    uint public value;

    function isValueOne() public constant returns (bool) {
        return (value == 1);
    }

}


contract Child is Parent
{

    function test() public {
        value = 1;
    }

    function verify() constant returns (bool) {
        require(isValueOne());
        return true;
    }
}
  • Thanks for the quick answer and it works. I've read somewhere that constant doesn't do anything currently, what would cause this fail here? This should definitely be documented. – user8754979 Oct 10 '17 at 21:08
  • The compiler won't enforce adding it, but you do have to add it in order to tell it to use eth_call function instead of eth_sendTransaction. – pabloruiz55 Oct 10 '17 at 21:13
  • Given that I change something in verify() and update a property, therefore removing constant, this code will fail. I don't think that's right – user8754979 Oct 10 '17 at 21:14
  • You are not currently modifying any state variable in verify(). – pabloruiz55 Oct 10 '17 at 21:23
  • For reference, the problem was that I redefined value in Child. The compiler should throw an error in this case, this issue is already opened on github github.com/ethereum/solidity/issues/2563 (I was able to find this after being reminded of shadowing.) – user8754979 Oct 10 '17 at 21:37

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.