Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more functions for adding nodes to MastForest #1412

Merged
merged 7 commits into from
Jul 27, 2024

Conversation

sergerad
Copy link
Contributor

Add methods to MastForest to allow adding nodes with fewer LOC.

Follow on from #1404 which added similar methods to MastForestBuilder.

As per comment.

@sergerad
Copy link
Contributor Author

@bobbinth - following on from previous PR.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks great! Thank you! I left one comment inline about how we can make validation even more robust.

CHANGELOG.md Outdated Show resolved Hide resolved
Comment on lines 80 to 91
pub fn add_join(
&mut self,
left_child: MastNodeId,
right_child: MastNodeId,
) -> Result<MastNodeId, MastForestError> {
self.add_node(MastNode::new_join(left_child, right_child, self))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

One potentially nice thing to validate here would be that left_child and right_child have smaller values than the newly created node ID. We could create a new error variant in MastForestError for this.

This is also relevant for add_split(), add_loop(), add_call(), and add_syscall() methods below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

LMK if you would like the error string to be more descriptive and contain references to the type of node and other IDs.

core/src/mast/mod.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

All looks good! Thank you!

@sergerad sergerad marked this pull request as draft July 25, 2024 19:48
@sergerad
Copy link
Contributor Author

@bobbinth I added tests in this commit.

Just want to double check you weren't expecting that logic to be captured by my changes - because what happens instead is that the overflow causes a panic. Understood you may not have intended my logic to avoid this overflow behaviour, just wanted to check.

thread 'mast::serialization::tests::mast_forest_invalid_node_id' panicked at core/src/mast/node/join_node.rs:34:46:
index out of bounds: the len is 2 but the index is 3

I have put the PR into draft to avoid merging the currently failing test.

@sergerad sergerad requested a review from bobbinth July 25, 2024 20:02
@bobbinth
Copy link
Contributor

Just want to double check you weren't expecting that logic to be captured by my changes - because what happens instead is that the overflow causes a panic. Understood you may not have intended my logic to avoid this overflow behaviour, just wanted to check.

thread 'mast::serialization::tests::mast_forest_invalid_node_id' panicked at core/src/mast/node/join_node.rs:34:46:
index out of bounds: the len is 2 but the index is 3

I have put the PR into draft to avoid merging the currently failing test.

Ah - good catch!

I think we should fix this and there are two was to do it:

  1. Change node constructors to return a Result (e.g., JoinNode::new() would return an error if the children are not in the forest).
  2. Make a preemptive checks in MastForest::add_join() etc. methods. For example, it could look something like this:
/// Adds a join node to the forest, and returns the [`MastNodeId`] associated with it.
pub fn add_join(
    &mut self,
    left_child: MastNodeId,
    right_child: MastNodeId,
) -> Result<MastNodeId, MastForestError> {
    if left_child >= self.nodes.len() {
        Err(MastForestError::InvalidNodeId(left_child))
    } else if right_child >= self.nodes.len() {
        Err(MastForestError::InvalidNodeId(right_child))
    } else {
        self.add_node(MastNode::new_join(left_child, right_child, self))
    }
}

The first option may be a more intrusive change, but the second option would require duplication of logic (i.e., we'd need something similar in MastForestBuilder::esnure_join()). So, I'm a bit on the fence here, but leaning a bit toward the first option.

@sergerad sergerad marked this pull request as ready for review July 26, 2024 09:07
@sergerad
Copy link
Contributor Author

@bobbinth I have implemented option 1 as you suggested. LMK if anything needs to be changed.

I preferred option 1 on the basis that it felt more in line with the "parse, don't validate" idiom by enforcing the invariant in the node constructors.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left just one non-blocking comment inline.

core/src/mast/serialization/info.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

All looks good! Thank you again!

@bobbinth bobbinth merged commit 74f1c2f into 0xPolygonMiden:next Jul 27, 2024
9 checks passed
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.

2 participants