MinaProtocol / mina

Mina is a cryptocurrency protocol with a constant size blockchain, improving scaling while maintaining decentralization and security.
https://minaprotocol.com
Apache License 2.0
1.99k stars 528 forks source link

Add type level validation for User_commands #2535

Closed wu-s-john closed 2 years ago

wu-s-john commented 5 years ago

Right now, we represent unvalidated and validated User_commands as User_command.t and User_command.With_valid_signature.t.

It would be useful to have this be represented as a GADT. So, it would look something like this:

module User_command = struct
   type 'signature t = ....
    'signature = _ Truth.t
end

This would make the staged_ledger_diff code less verbose. It would avoid having extra modules that represent a level of validation. For example, it can remove the need for having the module, With_valid_signatures_and_proofs.

The staged_ledger_diff type can be represented as something like:

module Staged_ledger_diff = struct
   type ('signature, 'proof) t = ....
   constraint 'signature = _ Truth.t
   constraint 'proof = _ Truth.t
end

Having this can ultimately make the Staged_ledger code more expressive and concise. There are some functions in the module that have similar flow structures, but are created differently to handle fully validated and unvalidated staged_ledger_diffss.

This refactoring could combine functions like apply and apply_diff_unchecked into one function.

psteckler commented 5 years ago

Apparently, we can't use a GADT directly with bin_io. I see the error message:

 GADTs are not supported by bin_prot

I do see the hack that we have for the transition frontier persistence, invoking %bin_type_class.

But also:

GADTs are not supported by comparelib

from deriving compare.

psteckler commented 5 years ago

If we did use a GADT, the type would have a parameter to distinguish validated/unvalidated signatures. That doesn't play well with the versioning system as it is now.

enolan commented 5 years ago

Do we have any reason to (de)serialize commands that we don't know are valid?

shimkiv commented 2 years ago

Closing this issue as it is stale for a long time.