Baking_nonces: Fix for getting blocks of previous cycle

What

Solve a FIXME from lib_delegate/baking_nonces.ml, specifically improve the clarity of the algorithm used in blocks_from_previous_cycle.

Why

FIXMEs are scary 😞

How

From the author's understanding, we have the following situation (before FIXME) :

Indexes :        first                               last                                   level
Chain   : [  ...  <-  []  <- []  <-  ...  <-  []  <-  []  <-  []  <- ...  <-  []  <-  []  <-  []  ]
                  |____________________________________|                                      ^
                             previous cycle                                                  HEAD

The goal of blocks_from_previous_cycle is to get the blocks from first to last. For that, we set length = level - first.

Previously, the approach was to:

  • calculate length = level - first
  • retrieve the blocks from first to level
  • if level is the same as last (i.e. we are exactly at the end of a cycle), we got all the "previous_cycle" blocks precisely
  • otherwise, we cut the blocks from last to level (using drop_n)

Now, we simply get the last - first blocks from the last block, without needing to drop blocks. Note that we need to calculate the distance between last and first, because this is not necessarily the blocks_per_cycle constant, as this constant can be changed, and the first cycle having this change will have its predecessor have a different value, so the invariant won't hold anymore.

Manually testing the MR

dune exec tezt/tests/main.exe -- --file nonce_seed_revelation.ml

Checklist

  • Document the interface of any function added or modified (see the coding guidelines)
  • Document any change to the user interface, including configuration parameters (see node configuration)
  • Provide automatic testing (see the testing guide).
  • For new features and bug fixes, add an item in the appropriate changelog (docs/protocols/alpha.rst for the protocol and the environment, CHANGES.rst at the root of the repository for everything else).
  • Select suitable reviewers using the Reviewers field below.
  • Select as Assignee the next person who should take action on that MR
Edited by Gabriel Moise

Merge request reports

Loading