1

I am using web3 (installed from npm, version 1.0.0-beta.34) to write a nodejs script that should invoke the method update() defined in a contract I deployed with Truffle on my private ethereum network.

I can succesfully invoke constant methods of the said contract, but when i try to call send on another method, the callback returns the tx hash but i cannot get the receipt, after a very long time waiting for the script to succeed (or fail) i get

error Error: Failed to check for transaction receipt: {}

For reference here's my simple contract

pragma solidity ^0.4.17;

 contract MappingExample {
    mapping(address => uint) public balances;
    uint8 public test = 0;

    function getBalances() public view returns(uint){
            return balances[msg.sender];
    }

    function update() public {
            test = test + 1;
            balances[msg.sender] = test;
    }

    constructor() public {
            test = 0;
    }
 }

And here's the js code I wrote to interact with the contract

const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider('http://xxxx:8545'))

const contractAbi = [...]
const account1 = '0xaaaaaaaaaaaaaaaaaa'
const contractAddress = '0xcccccccccccccc'

const contractInstance = new web3.eth.Contract(contractAbi, 
    contractAddress, {
      from: account1,
      gasPrice: '100000000000'
    })

web3.eth.personal.unlockAccount(account1, 'passphrase', 36000)
.then(response => {

    console.log('Account unlocked', response)

    contractInstance.methods.update()
      .send({ from: account1}, function (error, result) {
        console.log('Error', error)
        console.log('Result', result)

        // Now we get the balance
        contractInstance.methods.getBalances().call({ from: account1 }, 
        function (error, result) {
           console.log('Error', error)
           console.log('Result', result)
        })

      })
       .on('transactionHash', (hash) => {
        console.log('hash ' + hash)
       })
      .on('receipt', (receipt) => {
         console.log('receipt ' + receipt)
       })
       .on('confirmation', (confirmationNumber, receipt) => {
          console.log('confirmation ' + confirmationNumber + receipt)
       })
       .on('error', (error) => {
          console.log('error ' + error)
       })
   })

The output is

Account unlocked true
Error null
Result 0x64d057977bd27ad6248590fb245af3fa2409407da00ccda086dbc0c8994cafe7
hash 0x64d057977bd27ad6248590fb245af3fa2409407da00ccda086dbc0c8994cafe7
Error null
Result 0 
// Here it hangs for a very long time and then prints:
error Error: Failed to check for transaction receipt:
{}

Where is the mistake?

More information:

  • Running Geth 1.8.7-stable on all nodes

  • The script is launched on my laptop (macOS)

  • The target node is a Geth 1.8.7 node on which i created some accounts using the personal api.

  • The contract address is taken from "truffle deploy --reset" output

  • The "sender" address is a test account i created (with Geth) on the node I am pointing web3 to.

UPDATE with more information

  • The network has 4 non mining nodes and 2 mining nodes

  • The web3 script is pointing at a non-mining node

  • It looks like transactions are just staying "pending" indefinetly. If I run eth.getBlock("pending", true).transactions I can see all the transactions I tried.

UPDATE WITH PROGRESS I was able to deploy my contract successfully and to make transactions against it by running a new node on my laptop with the --mine flag on. It might be that geth (or the ethereum spec itself) requires at least two nodes actively mining on the network in order to correctly handle contract transactions, i've found some comments on the internet saying this but I cannot provide official confirmation yet .

  • How is the private network set up? Just a guess, but perhaps no blocks are getting mined. – user19510 May 9 '18 at 14:34
  • Forgot to mention that :) I have 4 nodes that are not mining (no --mine flag) and 2 nodes that are mining (started with --mine flag) . I'm a newbie but i'm pretty sure the 2 miners are mining since the geth output shows output like the following: INFO [05-09|15:28:20] Successfully sealed new block number=2568 hash=d5251b…19e3d0 INFO [05-09|15:28:20] 🔗 block reached canonical chain number=2563 hash=7d9045…00dff4 INFO [05-09|15:28:20] 🔨 mined potential block number=2568 hash=d5251b…19e3d0 INFO [05-09|15:28:20] Commit new mining work and so on – fat May 9 '18 at 14:36
  • Have you tried simple balance transfers? Just to confirm no transactions are working (as opposed to something specific to sending transactions to this contract). – user19510 May 9 '18 at 14:37
  • yes, using the geth console though... could it make a difference? i'll try a simple transfer with web3 asap – fat May 9 '18 at 14:38
  • I was able to create a transaction using the web3 nodejs api BUT i noticed something weird, the transaction is accepted but it remains pending. Using a visualization dashboard i can see that the network has >10 pending transactions and they stay pending even after several blocks get mined... the number of pending transactions keep growing as i try to invoke the contract's method or when i try to do transactions using web3js. I can see that these transactions are pending running "eth.getBlock("pending", true).transactions" in the geth console – fat May 9 '18 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.