Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watchBlock using a wss transport not emitting latest block when emitOnBegin is enabled #2733

Closed
1 task done
Cygnusfear opened this issue Sep 17, 2024 · 1 comment
Closed
1 task done

Comments

@Cygnusfear
Copy link

Check existing issues

Viem Version

2.21.0

Current Behavior

We're running into a case with watchBlocks using a wss transport not properly emitting the latest block when emitOnBegin is enabled. We're noticing this on an L3 Arbitrum chain with low tx volume, Arbitrum only generates new block as new TX come in.
Blocks are emitted as soon as a new TX/Block is made.

Expected Behavior

emitOnBegin always emits the latest available block on begin
(similar to how http would, or wss transport using polling)

Steps To Reproduce

import {
  createPublicClient,
  type Client,
  http,
  type Chain,
  webSocket,
} from "viem";
import { arbitrumSepolia as _arbitrumSepolia } from "viem/chains";

export const testConduit = Object.assign(_arbitrumSepolia as Chain, {
  name: "Conduit Cloudlines",
  id: 573146821555,
  blockExplorers: {
    default: {
      name: "Conduit",
      url: "https://explorer-cloudlines-7gm4rnga08.t.conduit.xyz",
    },
  },
  rpcUrls: {
    default: {
      http: ["https://rpc-cloudlines-7gm4rnga08.t.conduit.xyz"],
      webSocket: ["wss://rpc-cloudlines-7gm4rnga08.t.conduit.xyz"],
    },
  },
});

async function main() {
  const publicClient = await createPublicClient({
    transport: webSocket(testConduit.rpcUrls.default.webSocket[0]),
  });

  const publicClient2 = await createPublicClient({
    transport: http(testConduit.rpcUrls.default.http[0]),
  });

  const lastWssBlock = await publicClient.getBlock();
  const lastHttpBlock = await publicClient2.getBlock();

  console.log("lastWssBlock", lastWssBlock);
  console.log("lastHttpBlock", lastHttpBlock);

  publicClient.watchBlocks({
    emitOnBegin: true,
    onBlock: async (block) => {
      console.log("publicClient1: ", block);
    },
    pollingInterval: 1000,
  });

  publicClient2.watchBlocks({
    emitOnBegin: true,
    onBlock: async (block) => {
      console.log("publicClient2:", block);
    },
    pollingInterval: 1000,
  });
}

main().catch((err) => {
  console.error("mainLoop Err", err);
});

Link to Minimal Reproducible Example

https://gist.github.com/ya7ya/322444102d4b926c289148bc98973010

Anything else?

No response

@jxom jxom closed this as completed in 312160a Sep 17, 2024
Copy link
Contributor

github-actions bot commented Oct 3, 2024

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Viem version. If you have any questions or comments you can create a new discussion thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant