Proto: split `delegate_storage.ml`
While trying to understand the delegation and staking code, I found the file delegate_storage.ml
a bit harsh to grasp. It is a huge file: ~1000 lines. I tried to split the file in multiple files, with separate concerns to guide my understanding. Just like there is already smaller files like delegate_activation_storage.ml
responsible for only one or two keys in the store (for instance delegate_activation_storage.ml
manages Storage.Contract.Delegate_last_cycle_before_deactivation
and Storage.Contract.Inactive_delegate
).
Is there any interest for such a refactoring?
Here is the proposed split:
-
delegate_storage.ml
Storage.Delegates
Storage.Contract.Frozen_deposits_limit
-
delegate_missed_endorsements_storage.ml
Storage.Contract.Missed_endorsement
-
delegate_slashed_deposits_storage.ml
Storage.Slashed_deposits
-
delegate_sampler.ml
Storage.Delegate_sampler_state
-
delegate_cycles.ml
-
cycle_end
andinit_first_cycles
-
The MR also contains eight related minor refactoring, which simplify some interfaces:
-
Proto: move
set_active
intoStake_storage
It merges
Delegate_table_storage.set_active
intoStake_storage.set_active
. The function inDelegate_table_storage
, do not require access to the part ofStorage
from whichDelegate_table_storage
is responsible. Merging the two function simplify the interfaces and avoid having three distinctset_active
functions. -
Proto: Rename
Delegate.check_delegate
intoDelegate.check
Just a renaming.
-
Proto: simplify
Delegate.registered
The function
Contract_delegate_storage.registered
is based on invariant ensured byDelegate_table_storage
(i.e. a delegate is self delegated), better to move the function. Then, we simplify the function implementation by requiring only one read access into the context. -
Proto: remove
freeze_deposits_do_not_call_except_for_migration
This function is only called once in the whole protocol code. I suggest to inline it for avoiding incorrect usage. Besides, the name is a bit misleading, the function is only used while migrating from genesis, which is more a chain initialization that a protocol migration.
-
Proto: merge
Seed.cycle_end
intoDelegate.cycle_end
It is to enforce a little bit more the following comment in
seed_storage.ml
:(* NB: the clearing of past seeds is done elsewhere by the caller *)
. -
Proto: introduce
Alpha_context.Contract.Delegate
Move the functions
Alpha_context.Delegate.{init,set,find}
intoAlpha_context.Contract.Delegate
. These functions basically handle the field 'delegate' of a contract. They do not handle storage related to a given delegate, like other function inAlpha_context.Delegate
. -
Prote: rename
Delegate.delegate_participation_info
intoDelegate.participation_info
Just a renaming.
-
Proto/Delegate: remove dead code
Remove the duplicate function
Stake.delegate_pubkey
(also available asDelegate.pubkey
)