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() {

     function stateChanger(uint a) {

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

I get the following output in the geth console:

> c.initNumbers({from:web3.eth.accounts[0],gas:4000000000});
> c.stateChanger(3, {from:web3.eth.accounts[0],gas:400000000});
> c.stateChanger(4, {from:web3.eth.accounts[0],gas:400000000});
> c.getNumber(0);
> c.getNumber(1);
> c.getNumber(3);

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


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

function getNumber(uint index) constant returns (uint) {
    if(index >= numbers.length) 
        return 0; 
        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.

  • 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
    Aug 1 '16 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. Aug 1 '16 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
    Aug 1 '16 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
    Aug 1 '16 at 17:11
  • 1
    Yes, I was using browser solidity. Thanks for the reply. I have to redesign some of my methods.
    – Sebi
    Aug 1 '16 at 18:00

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.