-
Notifications
You must be signed in to change notification settings - Fork 191
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
feat: crude stable var implementation #4289
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -3,3 +3,4 @@ mod map; | |
mod public_state; | ||
mod set; | ||
mod singleton; | ||
mod stable_public_state; |
69 changes: 69 additions & 0 deletions
69
yarn-project/aztec-nr/aztec/src/state_vars/stable_public_state.nr
This file contains 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,69 @@ | ||
use crate::context::{Context}; | ||
use crate::oracle::{ | ||
storage::{storage_read, storage_write}, | ||
}; | ||
use crate::history::public_value_inclusion::prove_public_value_inclusion; | ||
use dep::std::option::Option; | ||
use dep::protocol_types::traits::{Deserialize, Serialize}; | ||
|
||
struct StablePublicState<T>{ | ||
context: Context, | ||
storage_slot: Field, | ||
} | ||
|
||
impl<T> StablePublicState<T> { | ||
pub fn new( | ||
// Note: Passing the contexts to new(...) just to have an interface compatible with a Map. | ||
context: Context, | ||
storage_slot: Field | ||
) -> Self { | ||
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); | ||
Self { | ||
context, | ||
storage_slot, | ||
} | ||
} | ||
|
||
// Intended to be only called once. | ||
pub fn initialize<T_SERIALIZED_LEN>(self, value: T) where T: Serialize<T_SERIALIZED_LEN> { | ||
assert(self.context.private.is_none(), "Public state wrties only supported in public functions"); | ||
// TODO: Must throw if the storage slot is not empty -> cannot allow overwriting | ||
// This is currently impractical, as public functions are never marked `is_contract_deployment` | ||
// in the `call_context`, only private functions will have this flag set. | ||
let fields = T::serialize(value); | ||
storage_write(self.storage_slot, fields); | ||
} | ||
|
||
pub fn read_public<T_SERIALIZED_LEN>(self) -> T where T: Deserialize<T_SERIALIZED_LEN> { | ||
assert(self.context.private.is_none(), "Public read only supported in public functions"); | ||
let fields = storage_read(self.storage_slot); | ||
T::deserialize(fields) | ||
} | ||
|
||
pub fn read_private<T_SERIALIZED_LEN>(self) -> T where T: Deserialize<T_SERIALIZED_LEN> { | ||
assert(self.context.public.is_none(), "Private read only supported in private functions"); | ||
let private_context = self.context.private.unwrap(); | ||
|
||
// Read the value from storage (using the public tree) | ||
let fields = storage_read(self.storage_slot); | ||
|
||
// TODO: The block_number here can be removed when using the current header in the membership proof. | ||
let block_number = private_context.get_header().global_variables.block_number; | ||
|
||
// Loop over the fields and prove their inclusion in the public tree | ||
for i in 0..fields.len() { | ||
// TODO: Update membership proofs to use current header (Requires #4179) | ||
// Currently executing unnecessary computation: | ||
// - a membership proof of the header(block_number) in the history | ||
// - a membership proof of the value in the public tree of the header | ||
prove_public_value_inclusion( | ||
fields[i], | ||
self.storage_slot + i, | ||
block_number as u32, | ||
(*private_context), | ||
) | ||
} | ||
T::deserialize(fields) | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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.
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.
Why not just expose the tree directly? Is it simply to get something to work before we decide to invest more time in this?
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.
Look at #4320, it is because there need to be more plumbing in place. We straight up don't have access to the data through the node right now (in private execution).