web3j icon indicating copy to clipboard operation
web3j copied to clipboard

java.lang.NullPointerException in at org.web3j.protocol.core.filters.LogFilter.process(LogFilter.java:46) over {"code":-32000,"message":"exceed maximum block range: 5000"}}

Open paulvi opened this issue 5 years ago • 11 comments

java.lang.NullPointerException: null
	at org.web3j.protocol.core.filters.LogFilter.process(LogFilter.java:46)
	at org.web3j.protocol.core.filters.Filter.getInitialFilterLogs(Filter.java:119)
	at org.web3j.protocol.core.filters.Filter.run(Filter.java:69)
	at org.web3j.protocol.rx.JsonRpc2_0Rx.run(JsonRpc2_0Rx.java:89)
	at org.web3j.protocol.rx.JsonRpc2_0Rx.lambda$ethLogFlowable$2(JsonRpc2_0Rx.java:79)
	at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:71)
	at io.reactivex.Flowable.subscribe(Flowable.java:14935)
	at io.reactivex.Flowable.subscribe(Flowable.java:14872)
	at io.reactivex.Flowable.subscribe(Flowable.java:14791)

Code in question

for (EthLog.LogResult logResult : logResults) { https://github.com/web3j/web3j/blob/master/core/src/main/java/org/web3j/protocol/core/filters/LogFilter.java#L46

    @Override
    protected void process(List<EthLog.LogResult> logResults) {
        for (EthLog.LogResult logResult : logResults) {
            if (logResult instanceof EthLog.LogObject) {
                Log log = ((EthLog.LogObject) logResult).get();
                callback.onEvent(log);
            } else {
                throw new FilterException(
                        "Unexpected result type: " + logResult.get() + " required LogObject");
            }
        }
    }

Bug_title

java.lang.NullPointerException in at org.web3j.protocol.core.filters.LogFilter.process(LogFilter.java:46)

The should be check if result is null instead of Exception

Steps To Reproduce

Describe the exact steps or scenario to reproduce the issue.

Expected behavior

No exception

Actual behavior

A description of the actual behavior occurring.

Environment

Describe the environment in which the issue occurs

  • Web3j version: 4.8.4 and 4.8.7 give the same error
  • Java or Android version: Java 8
  • Operating System: macOS

Additional context

Add any other context about the problem here.

  • Logs
  • Sample code and/or code snippets
    private void createEventMonitor() {
        log.info("createEventMonitor() begin...");
        // contract from block
        EthFilter filter = new EthFilter(DefaultBlockParameter.valueOf(new BigInteger("7605105")),
                DefaultBlockParameterName.LATEST, hedgingStrategy.getController());

        //Disposable subscription
        subscription = web3j
                .ethLogFlowable(filter)
                .subscribe(
                        event -> {
            log.info("Withdraw event received:");
            log.info("  event data >>> {}", event.getData());
            log.info("  event topic >>> {}", event.getTopics().stream().collect(Collectors.joining()));
            log.info("  event address >>> {}", event.getAddress());
            log.info("  event txHash >>> {}", event.getTransactionHash());
        }, error -> {
            log.error("Event error: {}", error, error); //!
        });

        log.info("createEventMonitor() end.");
    }
  • Unit/integration tests to highlight the issue
  • etherscan references

paulvi avatar Aug 25 '21 08:08 paulvi

Asked on https://stackoverflow.com/questions/68919728/java-lang-nullpointerexception-at-org-web3j-protocol-core-filters-logfilter-proc

paulvi avatar Aug 25 '21 08:08 paulvi

Are you sure you are not using an RPC endpoint with a pool of servers that don't synchronize the filterIds ? I've experienced the same issue, and the root cause was that the eth_newFilter call reached a server, while the next eth_getFilterLogs didn't recognize the just created filterId.

alexdupre avatar Aug 27 '21 16:08 alexdupre

Thanks for sharing. Well, this is still TEST environment, and there maybe different instances running, but for sure they are not connected between each other to synchronize the filterIds.

By the way, how to get this filterId ? I have not seen any API for that.

~~I wonder, can it be geth node bug, when geth node giving the same ids?~~


P.S. I recalled, that I had issue, where Filter id got visible. But it was when connecting to geth node (server) was in fact failing.

It was raised on SO https://stackoverflow.com/questions/68933870/warn-org-web3j-protocol-core-filters-filter-reinstallfilter153-the-filter-has

2021-08-26 14:40:25,134 [pool-2-thread-1] WARN  org.web3j.protocol.core.filters.Filter.reinstallFilter(153) -The filter has not been found. Filter id: 330683721788227458774458119455366581270
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
2021-08-26 14:40:25,420 [pool-2-thread-1] ERROR org.web3j.protocol.core.filters.Filter.lambda$run$0(97) -Error sending request
org.web3j.protocol.core.filters.FilterException: Error sending request
    at org.web3j.protocol.core.filters.Filter.throwException(Filter.java:194)
    at org.web3j.protocol.core.filters.Filter.run(Filter.java:104)
    at org.web3j.protocol.core.filters.Filter.reinstallFilter(Filter.java:155)
    at org.web3j.protocol.core.filters.Filter.pollFilter(Filter.java:137)
    at org.web3j.protocol.core.filters.Filter.lambda$run$0(Filter.java:92)
    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:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.InterruptedIOException: interrupted
    at okio.Timeout.throwIfReached(Timeout.kt:98)
    at okio.OutputStreamSink.write(Okio.kt:53)
    at okio.AsyncTimeout$sink$1.write(AsyncTimeout.kt:103)
    at okio.RealBufferedSink.flush(RealBufferedSink.kt:247)
    at okhttp3.internal.http1.Http1ExchangeCodec.finishRequest(Http1ExchangeCodec.kt:158)
    at okhttp3.internal.connection.Exchange.finishRequest(Exchange.kt:90)

Alex, that was great lead,

paulvi avatar Aug 30 '21 07:08 paulvi

P.P.S.

My bad, at some point I disabled most of debug logging, and when I turned in on, I saw exact JSON RPC error

{"code":-32000,"message":"exceed maximum block range: 5000"}}

So this issue should actually be about catching low level error and giving exact JSON RPC error message in higher Web3j API

2021-08-30 15:37:37 DEBUG o.w.p.websocket.WebSocketService - Sending request: {"jsonrpc":"2.0","method":"eth_newFilter","params":[{"topics":[],"fromBlock":"earliest","toBlock":"latest","address":["0xF46a8985347f072eB7A743a8506F3cDF791560a5"]}],"id":1}
2021-08-30 15:37:37 DEBUG o.w.p.websocket.WebSocketClient - Received message {"jsonrpc":"2.0","id":1,"result":"0x62f7226ff50e7c8bfcafa5e4df32a668"}
 from server wss://ws-testnet.hecochain.com
2021-08-30 15:37:37 DEBUG o.w.p.websocket.WebSocketService - Sending request: {"jsonrpc":"2.0","method":"eth_getFilterLogs","params":["0x62f7226ff50e7c8bfcafa5e4df32a668"],"id":2}
2021-08-30 15:37:37 DEBUG o.w.p.websocket.WebSocketClient - Received message {"jsonrpc":"2.0","id":2,"error":{"code":-32000,"message":"exceed maximum block range: 5000"}}
 from server wss://ws-testnet.hecochain.com
2021-08-30 15:37:37 ERROR c.h.p.h.s.w.c.r.ContractEventSubscriber - Event error 1: {}
java.lang.NullPointerException: null
	at org.web3j.protocol.core.filters.LogFilter.process(LogFilter.java:46)
	at org.web3j.protocol.core.filters.Filter.getInitialFilterLogs(Filter.java:119)
	at org.web3j.protocol.core.filters.Filter.run(Filter.java:69)

paulvi avatar Aug 30 '21 07:08 paulvi

I'll push a PR soon that will show the error instead of hiding it with a NPE, since there may be multiple causes for the getInitialFilterLogs failure.

alexdupre avatar Aug 30 '21 11:08 alexdupre

https://github.com/web3j/web3j/pull/1495

alexdupre avatar Aug 30 '21 12:08 alexdupre

#1495 merged, is this issue good to close? @paulvi

yasseralaa avatar Sep 07 '21 11:09 yasseralaa

well, I believe this issue should be closed after next version release.

paulvi avatar Sep 08 '21 10:09 paulvi

This is not a bug in web3j, but a limitation of BSC. My practice is to check whether it exceeds 5000 before starting. If it exceeds 5000, use web3j first Ethgetlogs pull logs of previous periods

kutasms avatar Jan 20 '22 07:01 kutasms

Hi @paulvi, is this issue good to close?

andrii-kl avatar Jan 28 '22 13:01 andrii-kl

https://github.com/web3j/web3j/pull/1495

andrii-kl avatar Feb 16 '22 15:02 andrii-kl