Skip to content

Conversation

@bart-linera
Copy link
Contributor

@bart-linera bart-linera commented Nov 18, 2025

Motivation

The derived implementation of View::context currently returns just the context of the first sub-view of the view. This means that if a view A has a sub-view B, and view B has a sub-view C, when A calls context() on B and assumes that its own base key is eg. a byte shorter than what it gets as a result, it will in fact get a context with C's base key, which may lead to wrong results if B also appends some bytes to the base key before passing the context on to C (which happens in hashable wrappers, for example!).

Proposal

Make the implementation of context() in all views correctly trim the base key contained in the context before returning the value.

This unfortunately introduces the necessity of cloning the context and returning it by value instead of by reference.

An alternative (but somewhat more complicated) solution would be to require all views to store their inner contexts, and make the View derive macro require a context field in all structs for which View is derived.

Test Plan

CI will catch regressions.

This was also manually confirmed to fix a bug that surfaced in #4977.

Release Plan

  • Nothing to do / These changes follow the usual release cycle.
  • This bug may be causing some issues on the testnet, but the fix cannot be safely backported without a complex migration.

Links

Copy link
Contributor

@afck afck left a comment

Choose a reason for hiding this comment

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

Great catch! Looks good to me, but let's wait for at least one more approval, ideally by someone more familiar with the storage internals.

@deuszx
Copy link
Contributor

deuszx commented Nov 18, 2025

Ideally we'd also have a test proving that this was necessary but maybe that's too complicated to automate?

@bart-linera
Copy link
Contributor Author

Ideally we'd also have a test proving that this was necessary but maybe that's too complicated to automate?

I can try. It should be relatively easy to create a dummy view type in a test module that would exhibit this issue.

@bart-linera bart-linera force-pushed the fix-context-base-keys branch from 6030c6d to ff114a2 Compare November 18, 2025 13:22
@bart-linera
Copy link
Contributor Author

@deuszx I added a test and verified that it was failing before the changes from this PR.

Copy link
Contributor

@ma2bd ma2bd left a comment

Choose a reason for hiding this comment

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

nice catch!

@bart-linera bart-linera added this pull request to the merge queue Nov 18, 2025
Merged via the queue into linera-io:main with commit 5cb0efb Nov 18, 2025
34 checks passed
@bart-linera bart-linera deleted the fix-context-base-keys branch November 18, 2025 15:59
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