5

When calling a state mutating function on the chain, the TX receipt object is returned. In Solidity Browser, under the Javascript VM, a return value is returned if one is set.

Why does the Javascript VM behave in this confusing way?

For example, the following contract contains a state mutation function which returns a value.

contract foo {

    uint public bar;

    function set(uint newbar) public returns (bool)
    {
        bar = newbar;
        return true;
    }
}

In the Javascript VM, the value is shown as returned... enter image description here

This is contrary to the behaviour of an on chain contract which returns the TX recipt. IOW returns have no purpose in a state mutating function (or am I missing something really fundamental?) enter image description here

The Javascript VM's return behaviour can lead to a false sense of functionality by which an author (like me, who should have known better) then writes contracts depending upon such return values but which have absoutly no functionality on chain...

Another example that works in VM but breaks on chain is when trying to interact with a 'new' contract...

contract foo {
    uint public number = 42;
    function set(uint num)
    {
        number = num;
    }
}

contract fooFactory {
    foo public kungFoo;
    function newFoo()
    {
        kungFoo = new foo();
        kungFoo.set(1); // This call will throw on chain yet works in VM
    }
}
3

browser-solidity on Github is described as "Browser-Only Solidity IDE and Runtime Environment". Returning values from non-constant functions is a helpful IDE feature, similar to instant execution without having to wait for a block to be mined. A purpose of the Javascript VM it uses is to make development and debugging easier and faster.

A downside is that the environment is not persisted, but browser-solidity can be attached directly to a node like Geth (Web3 Provider option below), or an "Injected Web3" such as Mist or MetaMask, where an actual blockchain can be used:

enter image description here

|improve this answer|||||
  • 1
    I don't understand how being allowed to return a value in JS VM non-constant functions makes for 'faster' debugging when it actually introduces practices that are inherently buggy, especially for inter-contract expectations. – o0ragman0o Dec 27 '16 at 5:06
  • I'm not following and is it possible to give an example of what you've observed (or edit the question)? – eth Dec 27 '16 at 19:49

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.