mina icon indicating copy to clipboard operation
mina copied to clipboard

Memoize transaction related hashes

Open georgeee opened this issue 5 months ago • 0 comments

Some hashes that are computed during staged ledger application are dependent only on a transaction itself. These hashes could be precomputed before staged ledger application, to minimize time of block creation.

Main change revolves around introducing and using the following two types:

type zkapp_precomputed_t =
    { all_account_updates :
        ( Account_update.t
        , Zkapp_command.Digest.Account_update.t
        , Zkapp_command.Digest.Forest.t )
        Zkapp_command.Call_forest.t
    ; init_account_update_result : get_next_account_update_result
    ; tx_commitment_on_start : Inputs.Transaction_commitment.t
    ; full_commitment_on_start : Inputs.Transaction_commitment.t
    ; lookup_actions_hash : Actions.t -> Field.t option
    }

type precomputed_t =
    { memo_hash : Random_oracle.Digest.t
    ; derived_token_ids : Token_id.t Account_id.Map.t
    ; zkapp_precomputed : zkapp_precomputed_t option
    }

Type precomputed_t contains data that is dependent only on a transaction (and not on the ledger to which it is to be applied). It contains some hashes either in direct form (as memo_hash) or indirect form (as all_account_updates, for computation of which some hashing is used deep inside the function call).

TODO this PR lacks a change to block producer to keep precomputed_t in transaction pool and use for block creation.

As measured by mina ledger test apply benchmark, this PR improves performance of staged ledger diff application by some 27% (measured together with #15784, #15980, #16055).

Explain how you tested your changes:

  • [x] Ran a benchmark
  • [ ] Ran CI/nightly tests

Checklist:

  • [x] Dependency versions are unchanged
    • Notify Velocity team if dependencies must change in CI
  • [ ] Modified the current draft of release notes with details on what is completed or incomplete within this project
  • [x] Document code purpose, how to use it
    • Mention expected invariants, implicit constraints
  • [x] Tests were added for the new behavior
    • Document test purpose, significance of failures
    • Test names should reflect their purpose
  • [ ] All tests pass (CI will check this if you didn't)
  • [x] Serialized types are in stable-versioned modules
  • [x] Does this close issues?
  • Closes #14752

georgeee avatar Sep 16 '24 13:09 georgeee