web3j icon indicating copy to clipboard operation
web3j copied to clipboard

eth_getFilterChanges should rebuild the filter if the filter is invalid

Open iikirilov opened this issue 8 years ago • 20 comments

This affects observables that poll for new data from the node using eth_getFilterChanges. Currently, if an exception is thrown the observable tries again, which is fine if there was some SocketException. However, if the node was to be restarted, the filter becomes invalid but web3j continues to poll with the invalid filter indefinitely.

The Filter class is throwing the exception.

Regarding the following exception:

15:26:26.205 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - --> POST <MYSECRETNODE> http/1.1
15:26:26.205 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
15:26:26.205 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 109
15:26:26.205 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - 
15:26:26.205 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - {"jsonrpc":"2.0","method":"eth_getFilterChanges","params":["0x3ca672eecb7329eaac5d2ee0c55a6f46"],"id":828471}
15:26:26.205 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - --> END POST (109-byte body)
15:26:26.348 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - <-- 200 OK <MYSECRETNODE> (143ms)
15:26:26.348 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json
15:26:26.348 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - Date: Tue, 16 Jan 2018 15:26:26 GMT
15:26:26.348 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 83
15:26:26.348 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - 
15:26:26.348 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - {"jsonrpc":"2.0","id":828471,"error":{"code":-32000,"message":"filter not found"}}
15:26:26.348 [pool-2-thread-2] DEBUG org.web3j.protocol.http.HttpService - <-- END HTTP (83-byte body)
15:26:26.348 [pool-2-thread-2] ERROR o.w.protocol.core.filters.Filter - Error sending request
org.web3j.protocol.core.filters.FilterException: Invalid request: Unknown Error
    at org.web3j.protocol.core.filters.Filter.throwException(Filter.java:154)
    at org.web3j.protocol.core.filters.Filter.pollFilter(Filter.java:113)
    at org.web3j.protocol.core.filters.Filter.lambda$run$18(Filter.java:75)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)

This issue comes as a result of fixing #279

iikirilov avatar Feb 08 '18 11:02 iikirilov

I recently encountered the same problem when I had to update and restart my geth node. Is there any way to catch this exception from the filter and make a new filter?

Daantie avatar Feb 21 '18 15:02 Daantie

What you describe is what needs to be implemented @Daantie

iikirilov avatar Feb 23 '18 12:02 iikirilov

The same problem. Did somebody find solution?

upers avatar Mar 14 '18 19:03 upers

Currently you need to rebuild the observable if this happens. We would welcome a PR to rebuild the filter in Web3j so users don't have to worry about this.

iikirilov avatar Mar 15 '18 10:03 iikirilov

I face this issue regularly, and it is very annoying. As this happens internally, inside the polling of web3j itself, the user doesn’t even has the easy way to learn that something bad happened, and that they need to (try) to rebuild the observable.

But actually, this issue opens a huge can of worms. Sorry to say, but the existing logic of filter polling is unreliable and needs major hardening (which would hopefully fix this #359 issue itself). Let me explain:

At the moment, the polling itself happens within core/filters/Filter.java. scheduledExecutorService.scheduleAtFixedRate invokes pollFilter(); which does web3j.ethGetFilterChanges(filterId).send();. On any issue (the ones I face were java.net.SocketTimeoutException: timeout and java.net.SocketException: Socket closed, but there may be any others), this code just grumbles in the logs and skips to the next iteration of ethGetFilterChanges. The problem is when the faulty iteration succeeded in sending the eth_getFilterChanges command to Ethereum node (and the node ran it), but failed in transmitting the node response (for some network failure). This will mean that the filter changes that failed to be delivered on the problematic iteration, are gone forever. The next iteration will report only the notifications after them, but not them anymore.

My opinion is that, if something failed during the filter polling (especially due to the network failures), we cannot just ignore it; the poller needs to know about the last successfully delivered data. So if the poller fails, it has to 1. rebuild the filter (because there could have been a Ethereum node restart), 2. reread the logs and make sure to replay everything that hasn’t get delivered during eth_getFilterChanges response transmission.

Until this, the whole filter logic should be considered unreliable and prone to lose the events.

amyodov avatar Mar 25 '18 13:03 amyodov

@amyodov agreed, however it is still possible to perform this "hardening" by the user. Either way, this should be handled by web3j, but it is not on my priority list at this time. If someone from the community could pick up this issue and propose a PR we can move the discussion forward.

iikirilov avatar Mar 26 '18 13:03 iikirilov

Hello. I made some fix in my web3j fork library. Now when we got error during polling ethereum filter. Exception will submited to Subscriber#exception method and we can handle it how we want. It's works for me. I know that it's solution isn't perfect, but it's enough for my project. Here link on my fork: https://github.com/upers/web3j

upers avatar Mar 26 '18 14:03 upers

Hi , I am using the following BlockObservable , and i am facing the same error . Here is the code

Web3j web3j = Web3j.build(new HttpService(envConfiguration.getEthereumServerUrl())); blockSubscription = web3j.blockObservable(false).subscribe( blockObserver);

` private static Subscriber blockObserver = new Subscriber() {

	@Override
	public void onCompleted() {
		LOGGER.info("complete event called");
	}

	@Override
	public void onError(Throwable e) {
		LOGGER.info("error listening block");
	}

	@Override
	public void onNext(EthBlock t) {
		LOGGER.info("block fetched");
	}
};`

Now in the above code , the onError function gets called only initially , that is if geth is not running on the BlockObservable initialisation , but if i restart geth , i face the ABOVE mentioned issue.

How will i rebuild the Observable ? I even updated the dependency to 3.4.0 . But i am stuck.

amitOodles avatar May 29 '18 12:05 amitOodles

I had a similar problem. I had to rewrite web3j a little so that it would throw an exception into the method onError action. Here link on my commit: https://github.com/upers/web3j/commit/f1869d7f75a53535dbe4dc83c4588a52f8fee62d

upers avatar Jun 02 '18 18:06 upers

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


This issue now has a funding of 250.0 DAI (250.0 USD @ $1.0/DAI) attached to it.

gitcoinbot avatar Jun 22 '18 02:06 gitcoinbot

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


Work has been started.

These users each claimed they can complete the work by 7 months, 3 weeks ago. Please review their action plans below:

1) upers has been approved to start work.

Got error code from RPC. If code -32000 then resubscribe.

Learn more on the Gitcoin Issue Details page.

gitcoinbot avatar Jun 23 '18 09:06 gitcoinbot

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


Work for 250.0 DAI (250.0 USD @ $1.0/DAI) has been submitted by:

  1. @upers
  2. @upers
  3. @upers

@ceresstation please take a look at the submitted work:

  • PR by @upers
  • PR by @upers
  • PR by @upers

gitcoinbot avatar Jun 23 '18 11:06 gitcoinbot

Hey there @upers, PRs look great overall but there are a couple of small issues pointed out by @conor10, happy to distribute the funds once those are done :)

spm32 avatar Jun 25 '18 02:06 spm32

Done. https://github.com/web3j/web3j/pull/614

upers avatar Jun 26 '18 06:06 upers

@ceresstation and @conor10, have you had chance to review latest from @upers? Would love to pay him out for the work :)

mkosowsk avatar Jun 29 '18 18:06 mkosowsk

My commit was merge to master. https://github.com/web3j/web3j/pull/614

upers avatar Jul 03 '18 16:07 upers

@upers Just saw that upers, sorry for the delay here, you're paid out 🎉

spm32 avatar Jul 05 '18 20:07 spm32

@ceresstation Thanks, but I still don't see tokens on my address: 0x96c892cd40d2fae76fd0e65fe3da471abe16cf84

upers avatar Jul 07 '18 16:07 upers

@upers ah it was an issue with the gas price I set given network congestion, you should receive it soon

spm32 avatar Jul 08 '18 10:07 spm32

Fixed in https://github.com/web3j/web3j/pull/614.

conor10 avatar Jul 09 '18 08:07 conor10