contracts icon indicating copy to clipboard operation
contracts copied to clipboard

Globally deduplicate *ReceiverSeen events

Open CodeSandwich opened this issue 2 years ago • 5 comments

The problem

When multiple users have identical drips or splits configurations, DripsReceiverSeen and SplitsReceiverSeen events are emitted for each of them, which wastes gas and causes noise in events.

The solution

The simplest approach is to add to storage a mapping(bytes32 => bool) seenHashes. Whenever a drips configuration is configured, check the value of seenHashes[configHash]. If it's true, do not emit the *ReceiverSeen events. If it's false, set it to true and emit the events.

The advantages

  • If a configuration contains just 2 or more items and it has been used before, gas is being saved. Not emitting the events saves more gas than costs reading the content of seenHashes.
  • In setSplits if a configuration has been seen before, we can assume that it's valid and we don't need to analyze it.
  • Additional data can be store in seenHashes. For splits it can be the sum of the weights, this way function Splits.splitResults doesn't need to accept currReceivers anymore and by extension neither does DripsHub.collectableAll.
  • Consumers of events don't need to perform any deduplication.

The disadvantages

  • If a configuration hasn't been seen before, it costs 22K gas to set the value of seenHashes. This cost may never be recouped if the configuration is short or not shared by many users.

CodeSandwich avatar Jun 03 '22 11:06 CodeSandwich