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

Nethereum.Contracts.EventExtensions.Sort() throws Value cannot be null. (Parameter 'source') #723

Closed
moccajoghurt opened this issue Oct 22, 2021 · 2 comments

Comments

@moccajoghurt
Copy link

moccajoghurt commented Oct 22, 2021

When you run Nethereums log processor for a few minutes, it sometimes crashes with the following exception: System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)

Here is a sample that you can run to reproduce the error:

using System;
using System.Numerics;
using System.Threading.Tasks;
using Nethereum.Signer;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;

public class BugExample {
    public async Task RunProcessor() {
        BigInteger blockStart = 13436170; 
        BigInteger blockEnd = 13441170;
        var url = "https://mainnet.infura.io/v3/"; // insert your own infura-url here
        var privateKey = "b5b1f848776e3af46b02501cc53da9e37c8f4c522245c27eabb95b9bde91277d"; // just a random key
        var account = new Account(privateKey, Chain.MainNet);
        var web3 = new Web3(account, url);
        var processor = web3.Processing.Logs.CreateProcessor(
            async log => {
                Console.WriteLine("Block: " + log.BlockNumber);
                await Task.Delay(500);
            });
        await processor.ExecuteAsync(blockEnd, default, blockStart);
    }
}

The error is caused inside Nethereum/src/Nethereum.Contracts/Extensions/EventExtensions.cs:

Here is the stack trace:

Value cannot be null. (Parameter 'source')
at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) 
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
at Nethereum.Contracts.EventExtensions.Sort(IEnumerable`1 logs) 
at Nethereum.BlockchainProcessing.LogProcessing.LogOrchestrator.ProcessAsync(BigInteger fromNumber, BigInteger toNumber, CancellationToken cancellationToken) 
at Nethereum.BlockchainProcessing.BlockchainProcessor.ExecuteAsync(BigInteger toBlockNumber, CancellationToken cancellationToken, Nullable`1 startAtBlockNumberIfNotProcessed) 
@moccajoghurt moccajoghurt changed the title Log processor throws: Value cannot be null. (Parameter 'source') EventExtensions.Sort() throws Value cannot be null. (Parameter 'source') Oct 22, 2021
@moccajoghurt moccajoghurt changed the title EventExtensions.Sort() throws Value cannot be null. (Parameter 'source') Nethereum.Contracts.EventExtensions.Sort() throws Value cannot be null. (Parameter 'source') Oct 22, 2021
@juanfranblanco
Copy link
Member

@moccajoghurt many thanks for this, do you know what specific block range you had the issue? If you use the blockchain progress storage, you should be able to identify the block, error and restart from there.

The log processor is designed to get a specific log, for example all the transfer events (for all contracts), so you may be encountering a range of blocks with no logs, and hence a null value, which is highly unlikely or you may have a bad response with infura. Also the processor has a retry process (in case of failure).

In anyway I am looking into it, as I had tested this crawling / logs for many blocks but this has never occurred.

@juanfranblanco
Copy link
Member

juanfranblanco commented Oct 22, 2021

@moccajoghurt Ok it seems that this is an intermittent issue with infura as I have reproduced the issue, in random blocks, in which we are not getting any logs as a response. This is an interesting scenario, as it is not expected, so the result is incorrect. As a side effect the logs when getting sorted, as there should be some are being rejected even if this is for 100 block range.

juanfranblanco added a commit that referenced this issue Oct 22, 2021
…ecuting actions #723

There seems to be an issue that Infura is returning null values, when it should be returning some values when processing logs, this implements a simple retry mechanism when null values are returned, although this adds an small retry overload (single request), it should not be an issue as block ranges are defaulted to 100.

Included is the resetting of retries, and error handling when calling actions, these should be handled already by users, but this does not updated the progress.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants