Skip to content

Improves safety & readability on transaction root derivation logic#2668

Merged
vicsn merged 13 commits intofeat/program-updatabilityfrom
feat/improve-tree-logic
Apr 29, 2025
Merged

Improves safety & readability on transaction root derivation logic#2668
vicsn merged 13 commits intofeat/program-updatabilityfrom
feat/improve-tree-logic

Conversation

@howardwu
Copy link
Member

@howardwu howardwu commented Mar 28, 2025

Motivation

This PR improves safety and readability upon PR 2577.

Related PRs

@howardwu howardwu changed the title Improve tx root derivation logic Improves tx root derivation logic Mar 28, 2025
@howardwu howardwu changed the title Improves tx root derivation logic Improves safety & readability on transaction root derivation logic Mar 28, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the safety and readability of transaction root derivation by refactoring how transaction trees are constructed and by renaming ambiguous function names. Key changes include:

  • Removing the explicit fee index and updating the transaction tree construction logic to use an optional fee reference.
  • Renaming Deployment::len to Deployment::num_functions for clarity.
  • Updating comments in serialization and transaction-related modules to emphasize recomputation of ID values and prevent malleability.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
ledger/block/src/transactions/rejected/mod.rs Refactored transaction ID computation by removing the fee index and using fee.as_ref.
ledger/block/src/transaction/serialize.rs Updated comments regarding exclusion of deployment/execution IDs from serialization.
ledger/block/src/transaction/mod.rs Adjusted transaction tree derivation to pass an optional fee instead of an explicit fee index.
ledger/block/src/transaction/merkle.rs Reimplemented transaction_tree to incorporate fee as an Option and updated error message checks.
ledger/block/src/transaction/deployment/mod.rs Renamed Deployment::len to Deployment::num_functions to reflect function count.
ledger/block/src/transaction/bytes.rs Updated comments to align with new transaction recomputation behavior.
console/program/src/state_path/configuration/mod.rs Reordered type alias definitions for deployment and execution trees for clarity.
Comments suppressed due to low confidence (1)

ledger/block/src/transaction/merkle.rs:120

  • The error message uses ambiguous formatting with '{fee_index}' and '{}' placeholders. Consider refactoring it (e.g., using format!()) to ensure that the fee index value is correctly inserted into the message.
ensure!(fee_index < Self::MAX_TRANSITIONS, "The fee index ('{fee_index}') in the transaction tree must be less than {}", Self::MAX_TRANSITIONS);

@howardwu howardwu marked this pull request as ready for review March 28, 2025 20:00
let fee_index = deployment_or_execution_tree.number_of_leaves();
// Ensure the fee index is within the Merkle tree size.
ensure!(
fee_index < Self::MAX_TRANSITIONS,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to bound it based on the transaction type, then MAX_TRANSITIONS is relevant only for executions. MAX_FUNCTIONS is the deployment related limit.

Right now we keep them all at 2^5, but if any are changed in the future, this check may not be correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

As this method could contain either a deployment tree OR an execution tree, should I check both <= MAX_FUNCTIONS and < MAX_TRANSITIONS?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably a good idea to do both checks.

Alternatively, because we always know what type of tree we are passing into this function, we can do a conditional check (using a boolean or const generic)

Copy link
Collaborator

@d0cd d0cd Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want the check number_of_leaves < Self::MAX_{TRANSITIONS/FUNCTIONS} do also be run when there is no fee for added safety?

Do Self::deployment_tree and Self::execution_tree need similar checks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raychu86 - added check here: 05f30a5

@d0cd - this is not necessary. The tree is correct by construction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussing offline with @d0cd, we've identified additional deployment size checks that should be added into the tree checks.

See bfa667f for the updates.

Copy link
Collaborator

@vicsn vicsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, modulo Ray's and my open comments

Copy link
Collaborator

@d0cd d0cd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good with the overall changes! Left a question on additional safety checks.

@d0cd d0cd mentioned this pull request Mar 28, 2025
24 tasks
@howardwu
Copy link
Member Author

This PR is ready for re-review and merging if LGTM'ed.

@vicsn vicsn requested review from d0cd and raychu86 April 21, 2025 18:41
Signed-off-by: vicsn <victor.s.nicolaas@protonmail.com>
@vicsn vicsn merged commit bcaf360 into feat/program-updatability Apr 29, 2025
2 checks passed
@vicsn vicsn deleted the feat/improve-tree-logic branch April 29, 2025 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants