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

[6.0] Fix semantics of AvlTree.insert & new AvlTree.insertOrUpdate method #1038

Merged
merged 9 commits into from
Jan 9, 2025

Conversation

kushti
Copy link
Member

@kushti kushti commented Dec 2, 2024

close #908

In this PR, semantics of AvlTree.insert fixed, and new AvlTree.insertOrUpdate method added

@kushti kushti added this to the 6.0.0 milestone Dec 3, 2024
@kushti kushti requested a review from aslesarenko December 16, 2024 21:31
Copy link
Member

@aslesarenko aslesarenko left a comment

Choose a reason for hiding this comment

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

LGTM, but see comments.

*
* @param key key to look up
* @param value value to check it was inserted or updated
* @return Success(Some(value)), Success(None), or Failure
Copy link
Member

Choose a reason for hiding this comment

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

Make sense to describe each case of returned result.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

def insertOrUpdate(
entries: Coll[(Coll[Byte], Coll[Byte])],
proof: Coll[Byte]): Option[AvlTree] = {
if (!tree.isInsertAllowed || !tree.isUpdateAllowed) {
Copy link
Member

Choose a reason for hiding this comment

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

Both flags are required, however for each given tree it will either be insert or update (but not both).
Is it possible to make it more flexible and require the flag only if the corresponding operation is happening for a given tree.

Copy link
Member Author

Choose a reason for hiding this comment

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

" for each given tree it will either be insert or update (but not both)" - no, it could be insert for one key and update for another

@@ -146,7 +146,7 @@ class CErgoTreeEvaluator(
val insertRes = bv.performInsert(key.toArray, value.toArray)
// TODO v6.0: throwing exception is not consistent with update semantics
// however it preserves v4.0 semantics (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/908)
Copy link
Member

Choose a reason for hiding this comment

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

todo can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

operations.forall { case (key, value) =>
var res = true
// the cost of tree update is O(bv.treeHeight)
addSeqCost(UpdateAvlTree_Info, nItems) { () =>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
addSeqCost(UpdateAvlTree_Info, nItems) { () =>
// Here (and in the previous methods) the cost is not properly approximated.
// When the tree is small (or empty), but there are many `operations`, the treeHeight will grow on every iteration.
// So should the cost on every iteration.
addSeqCost(UpdateAvlTree_Info, nItems) { () =>

@kushti kushti merged commit f1c1287 into v6.0.0 Jan 9, 2025
4 checks passed
@kushti kushti deleted the i908 branch January 9, 2025 12:40
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