1

When I have two indexed inputs in an emitted event, the Ethereum logs index the pair or just each one separately?

I mean, if I want to filter event logs using two topics, then that get's the information directly from an index of the pair, or first an intersection of the two topics results is computed?

Example:

event MyEvent(address indexed account, uint256 indexed selectedNumber);

When I want to filter by account and selectedNumber, then the events for the pair are indexed already and directly returned, or the Node has to get the indexed events for account first, the ones for selectedNumber second, and intersect the result?

I'm curious to understand if they are optimizing storage or query speed.

1 Answers1

0

The number of indexed topics "only" specifies how many items is going to be stored in the resulting array.

  • The first item is always the event signature. A keccak256 hash of a flattened event declaration string, in your case MyEvent(address,uint256).
  • Following up to 3 indexed topics (ABI encoded), each of them is one item of the array.
  • And the last item concatenates all unindexed topics (again ABI encoded).

Specifics of how exactly are the resulting arrays stored, filtered, and retrieved, depend on each node client software.

This answer describes how they are stored in go-ethereum (currently the most used client software). As I understand it, all event logs available to the node are stored in some kind of a hashmap where the keys are the event log topics, and the values are the Log type items. I'm assuming that it's somehow possible to filter on the keys level, not having to loop through all values all the time, but this is at the edge of my knowledge, so I'm not sure how exactly that works.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100