3

In order to have a list of the instances of the same contracts created, one method used is to emit an event for each instance created.

To query the events(or called logs), we can use web3.eth.getPastLogs, but the dilemma here is that what values to use for its parameter fromBlock and toBlock.

Hence the question: is it scalable to use fromBlock: 0, toBlock: latest?

Also noted that, we could also simply store instances of the same contracts in a array, which would cost more storage space and don't work well with deletions of instances.

1
  • AFAIK nodes are indexing events (more precisely log topics). So from:0 shouldn’t be a problem. The performance will mainly depend on the number of events. I didn’t check this. But it should be easy to verify by picking an older erc20 and fetching all Transfer events from:0.
    – ivicaa
    Commented Oct 7, 2018 at 17:48

1 Answer 1

3

You should check these:

Is searching data stored in event logs prohibitively slow?

More specifically, what is the time complexity of eth_getLogs?

In worst case, where every block contains log matching your query it is 0(n). But it's rarely a case. Bloom filters utilize probability of false positives, so the more sophisticated your filter is (more topics it has), the faster you will get your results.

How does Ethereum make use of bloom filters?

When a block is generated or verified, the address of any logging contract, and all the indexed fields from the logs generated by executing those transactions are added to a bloom filter, which is included in the block header. The actual logs are not included in the block data, to save space.

When an application wants to find all the log entries from a given contract, or with specific indexed fields (or both), the node can quickly scan over the header of each block, checking the bloom filter to see if it may contain relevant logs. If it does, the node re-executes the transactions from that block, regenerating the logs, and returning the relevant ones to the application.

About the bloom filter:

A Bloom filter is a space-efficient probabilistic data structure [...] that is used to test whether an element is a member of a set. False positive matches are possible, but false negatives are not.

For the case you want to check this in the code, this is a good starting point: https://github.com/ethereum/go-ethereum/blob/master/eth/filters/filter.go

Also interesting: What is the capacity of the logsBloom?

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.