openzeppelin-contracts icon indicating copy to clipboard operation
openzeppelin-contracts copied to clipboard

Refactor the Checkpoint library, with new key/value balances and new lookup mechanisms

Open Amxx opened this issue 3 years ago • 1 comments

This PR includes quite a few things:

  • Procedural generation of the Checkpoint library, automating the support for various key/value lengths
    • Checkpoints.Checkpoint224: key is uint32 and value is uint224. Usecase: history checkpoints for voting
    • Checkpoints.Checkpoint160: key is uint96 and value is uint160. Usecase: Address sequence (for NFT batch minting)
  • For each Checkpoint type, functions availables are:
    • latest()
    • push(key, value)
    • lowerLookup(key): returns the c.value of the first checkpoint for which key <= c.key (or 0 if no such checkpoint exists)
    • upperLookup(key): returns the c.value of the last checkpoint for which key >= c.key (or 0 if no such checkpoint exists)
    • upperLookupRecent(key): same as upperLookup but optimised to look in recent checkpoints.
  • Checkpoint.History now uses the Checkpoints.Checkpoint224 structure, with block.number for keys.
    • ~~getAtBlock now uses upperLookupRecent, optimizing for recent value lookup~~ → delayed to further PR

Fixes #3585

PR Checklist

  • [x] Tests
  • [ ] Documentation
  • [ ] Changelog entry

Amxx avatar Jul 28 '22 13:07 Amxx

Uses https://github.com/ethereum/solidity/issues/9117#issuecomment-1198003153 to skip checks

Amxx avatar Jul 29 '22 08:07 Amxx