coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.59k stars 1.32k forks source link

Duplicate signatures recieved by event listener #2514

Open 0xMukesh opened 1 year ago

0xMukesh commented 1 year ago

Hey! I'm using the addEventListener function (exported by @coral-xyz/anchor package) to subscribe to a certain anchor event. On logging the transaction signatures, it shows that the event listener is receiving duplicate transaction signatures. I'm accesing the transaction signature via the 3rd parameter of the callback function parameter of addEventListener.

program.addEventListener("Event", async (event, slot, signature) => {
  console.log(signature);
}) 

image

The issue is quite random, sometimes the event listener duplicate transaction signature while the other times it doesn't. What might be the cause of the issue? Is the RPC which I'm using or something else?

tanmaymunjal commented 1 year ago

Hey @0xMukesh, thanks for raising the issue

I am not 100% sure what might be the exact cause behind this issue, though one thing that might be happening is that the 64-byte ed25519 signature is based on the instructions and a recent (latest) block hash and not on on-chain data in the AccountInfo Sruct.

Hence if you are sending multiple identical transactions over the same period of time, there may be all signed in the same block hash. That might be what is causing the issue, though not 100% sure.

0xMukesh commented 1 year ago

hey there! the issue was resolved in anchor's discord server. the event listener captures the events emitted during transaction simulation, it could be easily avoided via checking if transaction signature (3rd parameter of the callback function) is equal to an zeroed transaction signature.

program.addEventListener("eventName", (event, slot, signature) => {
    if (signature === '1111111111111111111111111111111111111111111111111111111111111111') return;
  // logic goes here
})

solution provided in anchor's discord server: https://discord.com/channels/889577356681945098/1108166211436544130/1112419817727737879

im not sure regarding the internals of the websocket which publishes/sends the events to the event listener and it sends duplicate "legit" events few times but the solution provided works as a perfect workaround.

tanmaymunjal commented 1 year ago

Oh, that's nice. Though not sure if that works for a single user who is making multiple parallel yet identical requests. It is an anti-pattern though, contracts should be so that you don't need to do that, but still, an interesting use case that seems to create issues with the existing events architecture. Will look in more detail into why this is occurring but yeah thanks for clarifying.

0xMukesh commented 1 year ago

the workaround works perfectly even when a single user makes parallel yet identical transactions, two transactions can't be completely "identical" as they differ in signature. ive tried looking in internals of connection.OnLogs function (exported by @solana/web3.js) which is used by anchor's addEventListener function and RPC's code under solana-labs/solana repository, but couldn't find any such piece of code which could be an issue of duplicate events.