[Ethereum] Deploying and calling a contract works only with Ganache (not with Geth)

contract-invocationgo-ethereum

I deployed a HelloWorld contract using web3j and it worked awesome with Ganache. I was able to call my only function from the contract and it returns the desired result all the time.

When I deploy the contract to a private node created with geth, the transaction appears in pendingTransactions list and it returns the address of the deployed contract shortly after starting mining, but I cannot call the funciton. It always returns 0x (tested with both geth console and web3j). As you can see it is there.
enter image description here
After starting the miner you can observe that it is successfully deployed:
enter image description here

This is the code that deploys the contract (it works with Ganache):
enter image description here

The problem is that every time when I want to call a function it returns 0x just like the contract is not there. I checked and geth manages to find the specific contract:
enter image description here

EDIT:

I used 3 methods to call the contract: Java code using web3j, Geth console and Remix ide.

1) As you can see the contract is deployed(it appeared at pending transactions and was added to the blockchain after I started the miner. The idea is that the result of the method is still empty string not "helloWorld")

2) From Geth console it fails to cast the value to BigNumber. I read somewhere that this might be because the node is not synchronized and it doesn't know what value to return, so it returns empty value.
enter image description here

3) From Java using Web3j
enter image description here

It throws an exceptions because the value returned is 0x and it fails to cast it:

enter image description here

Genesis.json

{


"config": {
    "chainId": 3792,
    "homesteadBlock": 0,
    "eip155Block": 0,
    "eip158Block": 0
  },
  "difficulty": "2000",
  "gasLimit": "3141592000000",
  "alloc": {
    "ca56bdff4ab30b96b4f618e718b88c7a1721ecc3": {
      "balance": "300000000000000000000"
    }
  }
}

Best Answer

TLDR, but here is a fundamental difference between Ganache and Geth:

When Ganache starts, it automatically unlocks the accounts specified in the command-line.

For example:

ganache-cli
    --port=8545
    --gasPrice=20000000000
    --gasLimit=8000000
    --account=0x0000000000000000000000000000000000000000000000000000000000000001,1000000
    --account=0x0000000000000000000000000000000000000000000000000000000000000002,1000000

Will unlock the accounts with private keys 0x0...1 and 0x0...2 (2 accounts).

The default (if you don't specify anything) is 0x0...1 thru 0x0...a (10 accounts).

Geth doesn't do that, so you must unlock the accounts yourself prior to running any of your Truffle tests, by sending to Geth an "unlock account" command for each one of the accounts used in these tests.

Alternatively, you can sign each transaction with your private key before sending it to your Geth node. Other than for testing, this approach is actually the recommended one, because if somebody hacks your node then they can obtain your account credentials.

Related Topic