question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Some watched events randomly don't show up

See original GitHub issue

I’m using await contract.Method().watch({resourceNode: 'solidityNode'}, (err, res) => {...}); used on a few different methods.

It seems that some of the events do not trigger the function at all and simply go missing, meaning that the Dapp has an incomplete view of the state updates. This is a real nuisance.

I had the same events being listened to on both a Node.js instance and on some webpages using TronLink, and I discovered that the events that go missing are different for each instance (one page showed all the events). This means that the problem is not with the contract invocations or the events themselves but something to do with the code that retrieves the events.

Looking at the code it only seems to check for events every 3 seconds and only gets events for the latest block, so it is possible that it could be completely missing blocks. The getEvents function should check all blocks since the last time it was called, not just the latest block, and even if the interval is set for 20 seconds (simulating some sort of processing delay) it should still pick up all the events for the last few blocks to ensure that none are missed.

It is entirely possible that in a web page processing the UI could delay the getEvents function considerably, so that it gets called every 3 seconds cannot be relied upon. Same in the server, if there are a bunch of events to process from one block that involve heavy processing each, then the next block will be missed, which is not acceptable.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
UnforgivenZZZcommented, Jun 10, 2020

@forhas we all left tron so maybe don’t expected those issue to be fixed

1reaction
ColonelJcommented, Sep 23, 2019

In case it might help anyone, this is the code I’m using instead of using contract.Method().watch to discover the events due to this problem (seems to work reliably for recent events).

(Note that the 60 seconds on the first line is to allow for the fact that the newly loaded events will have timestamps in the past, and when onlyConfirmed is on, this gap will include the time it takes for the transaction to confirm on the solidity node. Even without onlyConfirmed there should still be at least 3 seconds because of waiting for the interval to trigger and this is also the maximum time since the previous block was mined.)

    let last_event_timestamp = Date.now() - 60000;
    let processing = false;
    setInterval(async () => {
        if (processing) return;
        processing = true;
        try {
            let events = [];
            let more_events =
                await tronWeb.getEventResult(CONTRACT_ADDRESS, {
                    onlyConfirmed: true,
                    sinceTimestamp: last_event_timestamp + 1,
                    sort: 'block_timestamp'
                });
            while (more_events.length
                   && more_events[more_events.length - 1].fingerprint) {
                events = events.concat(more_events);
                more_events =
                    await tronWeb.getEventResult(CONTRACT_ADDRESS, {
                        onlyConfirmed: true,
                        previousLastEventFingerprint:
                            more_events[more_events.length - 1].fingerprint,
                        sort: 'block_timestamp'
                    });
            }
            events = events.concat(more_events);
            if (events.length) {
                last_event_timestamp = events[events.length - 1].timestamp;
                for (let i = 0; i < events.length; ++i) {
                    if (events[i].name == 'Method') {
                        /*...*/
                    } else if (events[i].name == /*...*/) {
                        /*...*/
                    /*...*/
                    } else {
                        console.error('Unrecognized event name:', events[i].name);
                    }
                }
            }
        } catch (err) {
            console.error('Error getting events:', err);
        }
        processing = false;
    }, 3000);

This also has the advantage that it only does one request for all possible events that can be emitted by the contract. If you wanted you could add the eventName parameter to the options for getEventResult so you wouldn’t have to check the name.

Maybe the contract.Method().watch functionality could use logic similar to this to achieve the desired effect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Stop Your TV From Randomly Turning Itself On or Off
1. Unplug Your TV (and Plug It Into the Wall) ... As with all technology, try turning it off and on again before...
Read more >
List of most-watched television broadcasts - Wikipedia
This page lists the television broadcasts which had the most viewers within individual ... Many events are watched simultaneously around the world, and...
Read more >
Why does a random number appear on your TV screen when ...
So a random number appears on TV which is different for different set top boxes. This keeps them from getting pirated. So if...
Read more >
Symptoms - Schizophrenia - NHS
Read about symptoms of schizophrenia, including hallucinations, delusions, confused thoughts and changes in behaviour.
Read more >
Event Monitor | Johns Hopkins Medicine
Holter monitors record continuously, usually for about 24 to 48 hours. An event monitor does not record continuously. Instead, it records when you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found