-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: using
WithHash<T>
in SharedMutable
+ fixing slot allocation (
#11716) With the introduction of `WithHash<T>` it made sense to use it in `SharedMutable` as well. Given that I was dealing with storage I decided to tackle other `SharedMutable` related issues. Fixes #5492 Fixes #5491 --------- Co-authored-by: Nicolás Venturo <nicolas.venturo@gmail.com>
- Loading branch information
Showing
10 changed files
with
199 additions
and
272 deletions.
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
55 changes: 55 additions & 0 deletions
55
noir-projects/aztec-nr/aztec/src/state_vars/shared_mutable/shared_mutable_values.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,55 @@ | ||
use crate::state_vars::shared_mutable::{ | ||
scheduled_delay_change::{SCHEDULED_DELAY_CHANGE_PCKD_LEN, ScheduledDelayChange}, | ||
scheduled_value_change::ScheduledValueChange, | ||
}; | ||
use crate::utils::array; | ||
use dep::protocol_types::{traits::Packable, utils::arrays::array_concat}; | ||
use std::meta::derive; | ||
|
||
/// SharedMutableValues is just a wrapper around ScheduledValueChange and ScheduledDelayChange that then allows us | ||
/// to wrap both of these values in WithHash. WithHash allows for efficient read of values in private. | ||
/// | ||
/// Note that the WithHash optimization does not work in public (due to there being no unconstrained). But we also want | ||
/// to be able to read the values efficiently in public and we want to be able to read each value separately. For that | ||
/// reason we expose `get_delay_change_storage_slot` and `get_value_change_storage_slot` which point to the correct | ||
/// location in the storage. This is "hacky" as we pack and store the values together but there is no way around it. | ||
#[derive(Eq)] | ||
pub(crate) struct SharedMutableValues<T, let INITIAL_DELAY: u32> { | ||
svc: ScheduledValueChange<T>, | ||
sdc: ScheduledDelayChange<INITIAL_DELAY>, | ||
} | ||
|
||
impl<T, let INITIAL_DELAY: u32> SharedMutableValues<T, INITIAL_DELAY> { | ||
pub(crate) fn new( | ||
svc: ScheduledValueChange<T>, | ||
sdc: ScheduledDelayChange<INITIAL_DELAY>, | ||
) -> Self { | ||
SharedMutableValues { svc, sdc } | ||
} | ||
|
||
pub fn get_delay_change_storage_slot(shared_mutable_storage_slot: Field) -> Field { | ||
shared_mutable_storage_slot | ||
} | ||
|
||
pub fn get_value_change_storage_slot(shared_mutable_storage_slot: Field) -> Field { | ||
shared_mutable_storage_slot + (SCHEDULED_DELAY_CHANGE_PCKD_LEN as Field) | ||
} | ||
} | ||
|
||
// We pack to `2 * N + 1 + SCHEDULED_DELAY_CHANGE_PCKD_LEN` fields because ScheduledValueChange contains T twice | ||
// + 1 field for block_of_change + SCHEDULED_DELAY_CHANGE_PCKD_LEN fields for ScheduledDelayChange | ||
impl<T, let INITIAL_DELAY: u32, let N: u32> Packable<2 * N + 1 + SCHEDULED_DELAY_CHANGE_PCKD_LEN> for SharedMutableValues<T, INITIAL_DELAY> | ||
where | ||
T: Packable<N>, | ||
{ | ||
fn pack(self) -> [Field; 2 * N + 1 + SCHEDULED_DELAY_CHANGE_PCKD_LEN] { | ||
array_concat(self.sdc.pack(), self.svc.pack()) | ||
} | ||
|
||
fn unpack(fields: [Field; 2 * N + 1 + SCHEDULED_DELAY_CHANGE_PCKD_LEN]) -> Self { | ||
SharedMutableValues { | ||
sdc: Packable::unpack(array::subarray(fields, 0)), | ||
svc: Packable::unpack(array::subarray(fields, SCHEDULED_DELAY_CHANGE_PCKD_LEN)), | ||
} | ||
} | ||
} |
Oops, something went wrong.