9

I'm experimenting with Solidity Browser

Here is my contract code:

contract Test {

    mapping (address => uint256) weismap;

    function bet(uint vote) payable returns (uint256 weis) {
        if (msg.value==0) throw;
        weismap[msg.sender]= msg.value;
        return  weismap[msg.sender];
    }

    function test()  returns (uint myVote)  {
           return 1;
    }
}

If I set as transaction value 1

I set as transaction value at 1

,create the contract and call the function bet with 1 as parameter

the function is run successfully.

If after that, I call the test() function I get

VM Exception: invalid JUMP

Any idea, about what's wrong?

eth
  • 85,679
  • 53
  • 285
  • 406
Gyonder
  • 295
  • 2
  • 7

1 Answers1

5

You need to add the constant keyword to the test() function like:

pragma solidity ^0.4.0;
contract Test {
    mapping (address => uint256) weismap;

    function bet(uint vote) payable returns (uint256 weis) {
        if (msg.value==0) 
            throw;
        weismap[msg.sender]= msg.value;
        return  weismap[msg.sender];
    }

    function test() constant returns (uint myVote)  {
        return 1;
    }
}

Calling bet(...) with the value 2. Note that I'm sending 2 ethers as well with the transaction:

enter image description here

Now I call test() AND I've left the value 2. This call to test() fails as the constant function is being called as a transaction, not a read of the values off the blockchain:

enter image description here

If I call test() AND I've set value to blank, this call to test() succeeds as the constant function is being called as a read of the values off the blockchain:

enter image description here

BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193
  • Sorry, but it fails. Have you tried it out yourself?Does it work? – Gyonder Sep 12 '16 at 15:01
  • Yup. Tried it in browser-solidity with local geth 1.4.11-stable. Answer updated with more details. – BokkyPooBah Sep 12 '16 at 15:24
  • Yes but you didn't call bet. Could you try this please? First you set the transaction value. Then call bet with let's say 2. Then test. Does it work? Thanks – Gyonder Sep 12 '16 at 15:44