1

I thought that I had an error similar to this one, that the default account was not set (I was getting method undefined errors for all methods called in my contract).I've set it both in the geth console

eth.defaultAccount = eth.accounts[0]

as well as the js code using geth:

web3 = new Web3(new Web3.providers.HttpProvider(`http://${gethServer.host}:${gethServer.port}`));
/*
    Strange bug. Need to specify the account the transactions are made from.
*/
web3.eth.defaultAccount = web3.eth.accounts[0];

When calling the methods of this contract:

contract C {

     uint[] numbers;

     function initNumbers() {
         numbers.push(1);
         numbers.push(2);
     }

     function stateChanger(uint a) {
         numbers.push(a);
     }

     function getNumber(uint index) returns (uint) {
         if(index >= numbers.length)
            return 0;
         else
            return numbers[index];
     }
}

I get the following output in the geth console:

> c.initNumbers({from:web3.eth.accounts[0],gas:4000000000});
"0x76a1882128d64d94a7bad34e656cd83d57d2a1aefc51b99ba5893f8521a1a79e"
> c.stateChanger(3, {from:web3.eth.accounts[0],gas:400000000});
"0x1b7d5ef5d19a553af756790796a094d2ba213454ab056b7696e2f18e636d8ffc"
> c.stateChanger(4, {from:web3.eth.accounts[0],gas:400000000});
"0xd342a49323b7280e1954a9d33ac9bc457a3015768d5059801d2edf1d16585b9b"
> c.getNumber(0);
"0x31295a2a7ea20cee58e6a144039ae46b835110583b6d1b9517228627eb50999c"
> c.getNumber(1);
"0xce4dba582f37c72d58c767a2b9371a9a8ddbc1e63bc6c96eb5f61cc779168f64"
> c.getNumber(3);
"0x065bd993c4c8e86b2046a53f626619020ad550958dca955a40c839bbc9a1d0c7"

Why does geth return transaction hashes instead of integers as defined by the contract interface? Is it broken?

1 Answer 1

1

Add constant before returns (unit) and it will work as expected:

function getNumber(uint index) constant returns (uint) {
    if(index >= numbers.length) 
        return 0; 
    else 
        return numbers[index]; 
}

The reason why you get a transaction hash is because geth is executing your command as a transaction that changes the blockchain.

5
  • Thanks. But what happens when you do try to change the block chain? I have an example that adds an address to an array of addresses and returns true if it did or false if the address is already in the array. When I run the contract I get only the tx hash. Should I use the constant keyword even if the method alters the block chain?
    – Sebi
    Commented Aug 1, 2016 at 13:28
  • You cannot use the constant keyword for methods that will change the blockchain state. If you want to return a status, you will have to design your contract to save the state and use a constant function to retrieve this saved state. See also Reading values from a contract: When do I need transactions? - there is an example inc() - there can be multiple transactions of this method in a single block so it does not make sense to get a return status from the state-changing method. Commented Aug 1, 2016 at 13:33
  • But is it possible to return a bool value in a method that changes the state of the block chain? I've tried with and without the constant keyword. Without I get only the tx hash. And with the word I get true but the value is not stored in the block chain as expected. I know this worked before. I'm not sure what happened to geth.
    – Sebi
    Commented Aug 1, 2016 at 13:54
  • 1
    @Sebi Not possible in the way you're expecting because the transaction may never get mined. You need to use an Event. Maybe you were using some testing tool instead of Geth.
    – eth
    Commented Aug 1, 2016 at 17:11
  • 1
    Yes, I was using browser solidity. Thanks for the reply. I have to redesign some of my methods.
    – Sebi
    Commented Aug 1, 2016 at 18:00

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.