Web3.js: web3.eth.subscribe(“newBlockHeaders”) fires twice upon new blocks

Created on 14 Nov 2018  ·  9Comments  ·  Source: ChainSafe/web3.js

web3.eth.subscribe("newBlockHeaders", async (error, event) => {
    if (!error) {
        console.log(event.number)
        return;
    }
    console.log(error);
});

console.log results:

949
949
950
950
951
951
952
952
help wanted

Most helpful comment

I can't reproduce the original problem posted here, but the duplicates seem to come from calling .subscribe() twice. At the time of writing for v1.0.0-beta.44, the working code looks like:

const Web3 = require('web3');

/* ganache-cli ws */
const web3 = new Web3(new Web3.providers.WebsocketProvider('http://127.0.0.1:8545'));

/* This creates and event emitter linked to eth_subscribe */
const subscription = web3.eth.subscribe('newBlockHeaders');

/* This exposes the events from the subscription, synchronously */
subscription.on('data', async (block, error) => {
  console.log(block.number,  await web3.eth.getAccounts() );
});

Calling subscription.subscribe('newBlockHeaders', () => {}) creates a second subscription to the node, which also triggers the data event.

All 9 comments

if (_.isFunction(this.requestManager.provider.on)) {
            _ethereumCall.subscribe('newBlockHeaders', checkConfirmation.bind(null, existingReceipt, false));
        } else {
            intervalId = setInterval(checkConfirmation.bind(null, existingReceipt, true), 1000);
        }

As per the code, for section not defined with a .on , it keeps calling itself in 1000ms. Most probably that is a reason for the output you are getting.

web3.eth
        .subscribe("newBlockHeaders")
        .on("data", async (error, event) => {
            console.log(event.number);
        });

I'm not sure if I understand you correctly but I've changed to this and still receiving duplicated blocks?

web3.eth.subscribe("newBlockHeaders", (error, event) => {
    if (!error) {
        console.log(event.number)
        return;
    }
    console.log(error);
});

Have you tested it without async because it doesn't need one there? :)

I can't reproduce the original problem posted here, but the duplicates seem to come from calling .subscribe() twice. At the time of writing for v1.0.0-beta.44, the working code looks like:

const Web3 = require('web3');

/* ganache-cli ws */
const web3 = new Web3(new Web3.providers.WebsocketProvider('http://127.0.0.1:8545'));

/* This creates and event emitter linked to eth_subscribe */
const subscription = web3.eth.subscribe('newBlockHeaders');

/* This exposes the events from the subscription, synchronously */
subscription.on('data', async (block, error) => {
  console.log(block.number,  await web3.eth.getAccounts() );
});

Calling subscription.subscribe('newBlockHeaders', () => {}) creates a second subscription to the node, which also triggers the data event.

Closed due inactivity of the creator.

I'm facing exactly the same issue with a minimal code example.

let blockSubscription = web3.eth.subscribe('newBlockHeaders')

blockSubscription.subscribe((error, result) => {
    if (error) {
        console.log("Error subscribing to event", error)
        process.exit()
    }
}).on('data', blockHeader => {
    if (!blockHeader || !blockHeader.number)
        return

    console.log("Block "+blockHeader.number)
})

Output:

Block 6663458
Block 6663458
Block 6663459
Block 6663459
Block 6663460
Block 6663460

Should I open a new issue or this one could be reopened?

@jesobreira can you get the block number and the block hash?

Looks like you've faced network reorgs -), it is correct behaviour. compare their hashes. these blocks are different. and even they may have different histories (parentHash), and even deeper -)

Was this page helpful?
0 / 5 - 0 ratings