Strengthen token delegations
The consistency of a delegate's stake with respect to its delegators' balances is a critical property for Tenderbake, as it must enforce the stake balances invariants: tzip!172 (comment 677243929)
The fact that the consistency of a delegate's stake with respect to its delegators' balances is enforced at multiple locations makes code maintenance and reviews more cumbersome.
These locations include crediting a contract, spending from a contract, freezing fees, punishing a delegate, etc.; as well as the delegation management which is performed by Delegate_storage.set.
Design
With the transfer function of the Token module, it becomes possible to
locate stake movements inside the token module so that the associated invariants are enforced in a single module.
Tasks
-
Move references to
Stake_storage.add_stakeandStake_storage.remove_stakerelated to transfers and delegations in theTokenmodule -
Remove redundant representations: (1) remove
Token.DelegateBalanceconstructor as it becomes redundant with the constructorToken.Contract, and (2) unify the typesReceipt.balanceandToken.[source | container | sink] -
Reduce the number of calls to
add_stakeandremove_stakedepending on the nature of transfers (e.g. no stake updating needed when the source and the sink share the same delegate)