-
Notifications
You must be signed in to change notification settings - Fork 600
refactor!: ephemeral arrays #22162
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
Open
mverzilli
wants to merge
11
commits into
merge-train/fairies
Choose a base branch
from
martin/volatile-arrays
base: merge-train/fairies
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
refactor!: ephemeral arrays #22162
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2769578
initial exploration
mverzilli 8839209
replace fetch_pending_tagged_logs impl with volatile arrays
mverzilli 0b5a2a8
volatile -> ephemeral
mverzilli be3ff2c
use ephemeral for notes and events validation and storage
mverzilli 7ae06bf
port message context resolution to ephemeral arrays
mverzilli 952813a
port pending partial notes request arrays to ephemeral
mverzilli 5e031a8
simplify signatures thanks to ephemeral arrays
mverzilli d5eac0b
minutiae
mverzilli 6642a5b
merge from fairies
mverzilli 50073ec
some initial tests of ephemeral array isolation
mverzilli 578e874
minutiae
mverzilli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,320 @@ | ||
| use crate::oracle::ephemeral; | ||
| use crate::protocol::traits::{Deserialize, Serialize}; | ||
|
|
||
| /// A dynamically sized array that exists only during a single contract call frame. | ||
| /// | ||
| /// Ephemeral arrays are backed by in-memory storage on the PXE side rather than a persistent database. Each contract | ||
| /// call frame gets its own isolated slot space of ephemeral arrays. Child simulations cannot see the parent's | ||
| /// ephemeral arrays, and vice versa. | ||
| /// | ||
| /// Each logical array operation (push, pop, get, etc.) is a single oracle call, making ephemeral arrays significantly | ||
| /// cheaper than capsule arrays and more appropriate for transient data that is never supposed to be persisted anyway. | ||
| /// | ||
| /// ## Use Cases | ||
| /// | ||
| /// Ephemeral arrays are designed for transient communication between PXE (TypeScript) and contracts (Noir) during | ||
| /// simulation, for example, note validation requests or event validation responses. | ||
| /// | ||
| /// For data that needs to persist across simulations, contract calls, etc, use | ||
| /// [`CapsuleArray`](crate::capsules::CapsuleArray) instead. | ||
| pub struct EphemeralArray<T> { | ||
| pub base_slot: Field, | ||
| } | ||
|
|
||
| impl<T> EphemeralArray<T> { | ||
| /// Creates an ephemeral array at the given base slot. | ||
| /// | ||
| /// Multiple ephemeral arrays can coexist within the same call frame by using different base slots. | ||
| pub unconstrained fn at(base_slot: Field) -> Self { | ||
| Self { base_slot } | ||
| } | ||
|
|
||
| /// Returns the number of elements stored in the array. | ||
| pub unconstrained fn len(self) -> u32 { | ||
| ephemeral::len_oracle(self.base_slot) | ||
| } | ||
|
|
||
| /// Stores a value at the end of the array. | ||
| pub unconstrained fn push(self, value: T) | ||
| where | ||
| T: Serialize, | ||
| { | ||
| let serialized = value.serialize(); | ||
| let _ = ephemeral::push_oracle(self.base_slot, serialized); | ||
| } | ||
|
|
||
| /// Removes and returns the last element. Panics if the array is empty. | ||
| pub unconstrained fn pop(self) -> T | ||
| where | ||
| T: Deserialize, | ||
| { | ||
| let serialized = ephemeral::pop_oracle(self.base_slot); | ||
| Deserialize::deserialize(serialized) | ||
| } | ||
|
|
||
| /// Retrieves the value stored at `index`. Panics if the index is out of bounds. | ||
| pub unconstrained fn get(self, index: u32) -> T | ||
| where | ||
| T: Deserialize, | ||
| { | ||
| let serialized = ephemeral::get_oracle(self.base_slot, index); | ||
| Deserialize::deserialize(serialized) | ||
| } | ||
|
|
||
| /// Overwrites the value stored at `index`. Panics if the index is out of bounds. | ||
| pub unconstrained fn set(self, index: u32, value: T) | ||
| where | ||
| T: Serialize, | ||
| { | ||
| let serialized = value.serialize(); | ||
| ephemeral::set_oracle(self.base_slot, index, serialized); | ||
| } | ||
|
|
||
| /// Removes the element at `index`, shifting subsequent elements backward. Panics if out of bounds. | ||
| pub unconstrained fn remove(self, index: u32) { | ||
| ephemeral::remove_oracle(self.base_slot, index); | ||
| } | ||
|
|
||
| /// Removes all elements from the array. | ||
| pub unconstrained fn clear(self) { | ||
| ephemeral::clear_oracle(self.base_slot); | ||
| } | ||
|
|
||
| /// Calls a function on each element of the array. | ||
| /// | ||
| /// The function `f` is called once with each array value and its corresponding index. Iteration proceeds | ||
| /// backwards so that it is safe to remove the current element (and only the current element) inside the | ||
| /// callback. | ||
| /// | ||
| /// It is **not** safe to push new elements from inside the callback. | ||
| pub unconstrained fn for_each<Env>(self, f: unconstrained fn[Env](u32, T) -> ()) | ||
| where | ||
| T: Deserialize, | ||
| { | ||
| let mut i = self.len(); | ||
| while i > 0 { | ||
| i -= 1; | ||
| f(i, self.get(i)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| mod test { | ||
| use crate::test::helpers::test_environment::TestEnvironment; | ||
| use crate::test::mocks::MockStruct; | ||
| use super::EphemeralArray; | ||
|
|
||
| global SLOT: Field = 1230; | ||
| global OTHER_SLOT: Field = 5670; | ||
|
|
||
| #[test] | ||
| unconstrained fn empty_array() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array: EphemeralArray<Field> = EphemeralArray::at(SLOT); | ||
| assert_eq(array.len(), 0); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| unconstrained fn empty_array_read() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
| let _: Field = array.get(0); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| unconstrained fn empty_array_pop() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
| let _: Field = array.pop(); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn array_push() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
| array.push(5); | ||
|
|
||
| assert_eq(array.len(), 1); | ||
| assert_eq(array.get(0), 5); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| unconstrained fn read_past_len() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
| array.push(5); | ||
|
|
||
| let _ = array.get(1); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn array_pop() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
| array.push(5); | ||
| array.push(10); | ||
|
|
||
| let popped: Field = array.pop(); | ||
| assert_eq(popped, 10); | ||
| assert_eq(array.len(), 1); | ||
| assert_eq(array.get(0), 5); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn array_set() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
| array.push(5); | ||
| array.set(0, 99); | ||
| assert_eq(array.get(0), 99); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn array_remove_last() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
|
|
||
| array.push(5); | ||
| array.remove(0); | ||
|
|
||
| assert_eq(array.len(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn array_remove_some() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
|
|
||
| array.push(7); | ||
| array.push(8); | ||
| array.push(9); | ||
|
|
||
| assert_eq(array.len(), 3); | ||
|
|
||
| array.remove(1); | ||
|
|
||
| assert_eq(array.len(), 2); | ||
| assert_eq(array.get(0), 7); | ||
| assert_eq(array.get(1), 9); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn array_remove_all() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
|
|
||
| array.push(7); | ||
| array.push(8); | ||
| array.push(9); | ||
|
|
||
| array.remove(1); | ||
| array.remove(1); | ||
| array.remove(0); | ||
|
|
||
| assert_eq(array.len(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn for_each_called_with_all_elements() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
|
|
||
| array.push(4); | ||
| array.push(5); | ||
| array.push(6); | ||
|
|
||
| let called_with = &mut BoundedVec::<(u32, Field), 3>::new(); | ||
| array.for_each(|index, value| { called_with.push((index, value)); }); | ||
|
|
||
| assert_eq(called_with.len(), 3); | ||
| assert(called_with.any(|(index, value)| (index == 0) & (value == 4))); | ||
| assert(called_with.any(|(index, value)| (index == 1) & (value == 5))); | ||
| assert(called_with.any(|(index, value)| (index == 2) & (value == 6))); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn for_each_remove_some() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
|
|
||
| array.push(4); | ||
| array.push(5); | ||
| array.push(6); | ||
|
|
||
| array.for_each(|index, _| { | ||
| if index == 1 { | ||
| array.remove(index); | ||
| } | ||
| }); | ||
|
|
||
| assert_eq(array.len(), 2); | ||
| assert_eq(array.get(0), 4); | ||
| assert_eq(array.get(1), 6); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn for_each_remove_all() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array = EphemeralArray::at(SLOT); | ||
|
|
||
| array.push(4); | ||
| array.push(5); | ||
| array.push(6); | ||
|
|
||
| array.for_each(|index, _| { array.remove(index); }); | ||
|
|
||
| assert_eq(array.len(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn different_slots_are_isolated() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array_a = EphemeralArray::at(SLOT); | ||
| let array_b = EphemeralArray::at(OTHER_SLOT); | ||
|
|
||
| array_a.push(10); | ||
| array_a.push(20); | ||
| array_b.push(99); | ||
|
|
||
| assert_eq(array_a.len(), 2); | ||
| assert_eq(array_a.get(0), 10); | ||
| assert_eq(array_a.get(1), 20); | ||
|
|
||
| assert_eq(array_b.len(), 1); | ||
| assert_eq(array_b.get(0), 99); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn works_with_multi_field_type() { | ||
| let _ = TestEnvironment::new(); | ||
|
|
||
| let array: EphemeralArray<MockStruct> = EphemeralArray::at(SLOT); | ||
|
|
||
| let a = MockStruct::new(5, 6); | ||
| let b = MockStruct::new(7, 8); | ||
| array.push(a); | ||
| array.push(b); | ||
|
|
||
| assert_eq(array.len(), 2); | ||
| assert_eq(array.get(0), a); | ||
| assert_eq(array.get(1), b); | ||
|
|
||
| let popped: MockStruct = array.pop(); | ||
| assert_eq(popped, b); | ||
| assert_eq(array.len(), 1); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they are useful for more than that, but I want to grow a bit more confidence on the API and oracle design before encouraging people to use it happily