zeta-chain / node

ZetaChain’s blockchain node and an observer validator client
https://zetachain.com
MIT License
167 stars 109 forks source link

Hashes from newHeads and eth_getBlockByNumber are different #3050

Closed KirillPamPam closed 1 month ago

KirillPamPam commented 1 month ago

Describe the Bug We noticed the differences in the hashes between the blocks via the newHeads subscription and blocks via eth_getBlockByNumber at the same height. The blocks via eth_getBlockByNumber are equals to the ones in https://explorer.zetachain.com/ but the newHeads blocks seem to be forked.

To Reproduce There is the script to reproduce this bug.

const wsUrl = "your_ws_url";
const rpcUrl = "your_http_url";
const WebSocket = require('ws');

function wsNewHeads(wsUrl, cb) {
    const ws = new WebSocket(wsUrl);
    ws.onopen = () => {
        console.log("ws connected");
        ws.send(JSON.stringify({
            jsonrpc: "2.0",
            method: "eth_subscribe",
            params: ["newHeads"],
            id: 1
        }));
    };
    ws.onclose = () => {
        console.log("ws closed");
    };
    let isFirst = true;
    ws.onmessage = (event) => {
        if (isFirst) {
            isFirst = false;
            return;
        }
        const data = JSON.parse(event.data);
        cb(data);
    };
}

async function rpcGetBlock(rpcUrl, blockNum) {
    const response = await fetch(rpcUrl, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            jsonrpc: "2.0",
            method: "eth_getBlockByNumber",
            params: [blockNum, false],
            id: 1
        })
    });
    return await response.json();
}

async function main() {
    wsNewHeads(wsUrl, async (data) => {
        const block = await rpcGetBlock(rpcUrl, data.params.result.number);
        console.log(data.params.result)
        console.log(block)
    });
}

main();

Expected Behavior The hashes from the newHeads sub and eth_getBlockByNumber must be equal to each other.

Screenshots

Environment (please complete the following information):

skosito commented 1 month ago

fixed with https://github.com/zeta-chain/node/pull/3047

Thanks @KirillPamPam for reporting!

KirillPamPam commented 1 month ago

@skosito Awesome! Will it be included in the next release?