1

Use Case:
I am implementing a deposit system where I will get a block as it is mined and parse transactions to check for a deposit of Ether or a specific token. Getting the same block twice will result in same transactions parsed twice and that eventually might result in wrong credits etc.

To listen to blocks, I have implemented two scenarios but facing the same issue in both of them.

Scenario 1:
web3 version 0.19.0
Node Version 8.12.0
Ethereum Node: Geth Rinkeby Node Running on AWS

I use web3.eth.filter to listen to the latest block hashes and print the latest block info. For some blocks, I get the same block number twice.

var filter = web3.eth.filter('latest');
filter.watch(function(error, result) {
  if (!error) {
    web3.eth.getBlock(result, true, function(error, data) {
      if(!error)
      {
        console.log('Block: '+data.number+' Transactions Count: ',data.transactions.length);
      }
    });
  }
});

Output:

/* Notice the Block Number 3205025 Fired Twice with the same txn count */
Block:  3205023 Transactions: 39
Block:  3205024 Transactions: 24
Block:  3205024 Transactions: 24
Block:  3205025 Transactions: 17

Scenario 2:
web3 version 1.0.0 beta 36
Node Version 8.12.0
Ethereum Node Node: Infura Rinkeby Websocket

I used web3.eth.subscribe to subscribe to latest block headers and as a result, I get exactly same block headers emitted more than once from the emitter.

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://rinkeby.infura.io/ws'))

const subscription = web3.eth.subscribe('newBlockHeaders');

subscription.subscribe((error, result) => {
  if (error) console.log(error)
})
.on('data', async (block) => {
  console.log(block.number);
});

Output:

3215743
3215743
3215744
3215744
3215744
3215744

Am I doing anything wrong or is it the usual behavior or is there any way to handle the duplication of the exact same block.

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.