Skip to content

Commit 11278e4

Browse files
committed
refactor(state): remove DynRegion associated type
1 parent 0f488ae commit 11278e4

File tree

4 files changed

+18
-799
lines changed

4 files changed

+18
-799
lines changed

src/riscv/lib/src/state_backend.rs

Lines changed: 17 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@
3636
//! [Prove]: octez_riscv_data::mode::Prove
3737
3838
mod elems;
39-
pub mod normal_backend;
4039
pub mod proof_backend;
4140
pub(crate) mod proof_layout;
4241
mod region;
4342
pub mod verify_backend;
4443

45-
use bincode::enc::Encoder;
46-
use bincode::error::EncodeError;
4744
pub use elems::*;
4845
use octez_riscv_data::components::atom::AtomMode;
4946
use octez_riscv_data::components::atom::CloneAtomMode;
@@ -52,137 +49,30 @@ use octez_riscv_data::components::data_space::CloneDataSpaceMode;
5249
use octez_riscv_data::components::data_space::DataSpaceMode;
5350
use octez_riscv_data::components::data_space::EncodeDataSpaceMode;
5451
use octez_riscv_data::mode::Mode;
55-
use octez_riscv_data::serialisation::elem::Elem;
5652
pub use proof_layout::*;
53+
use trait_set::trait_set;
5754

58-
/// Manager of the state backend storage
59-
pub trait ManagerBase: Mode + Sized {
60-
/// Dynamic region represents a fixed-sized byte vector that has been allocated in the state storage
61-
type DynRegion;
62-
}
63-
64-
/// Manager with allocation capabilities
65-
///
66-
/// Any `ManagerAlloc` inherently has read & write capabilities,
67-
/// since the manager creates the values on the first allocation.
68-
pub trait ManagerAlloc: ManagerRead + ManagerWrite {
69-
/// Allocate a dynamic region in the state storage.
70-
fn allocate_dyn_region(len: usize) -> Self::DynRegion;
71-
}
72-
73-
/// Manager with read capabilities
74-
pub trait ManagerRead: ManagerBase + AtomMode + DataSpaceMode {
75-
/// Read the length of the dynamic region in bytes.
76-
fn dyn_region_len(region: &Self::DynRegion) -> usize;
77-
78-
/// Read an element in the region. `address` is in bytes.
79-
///
80-
/// # Safety
81-
///
82-
/// The caller must ensure the access is within bounds.
83-
///
84-
/// ```text
85-
/// address + E:STORED <= region.len()
86-
/// ```
87-
unsafe fn dyn_region_read<E: Elem>(region: &Self::DynRegion, address: usize) -> E;
88-
89-
/// Read elements from the region. `address` is in bytes.
90-
///
91-
/// # Panics
92-
///
93-
/// Panics if the read would go out of bounds.
94-
fn dyn_region_read_all<E: Elem>(region: &Self::DynRegion, address: usize, values: &mut [E]) {
95-
if values.is_empty() {
96-
return;
97-
}
98-
99-
assert!(
100-
values
101-
.len()
102-
.checked_mul(E::STORED_SIZE.get())
103-
.expect("Total length should not overflow")
104-
.checked_add(address)
105-
.expect("End address should not overflow")
106-
<= Self::dyn_region_len(region),
107-
);
108-
109-
for (i, value) in values.iter_mut().enumerate() {
110-
// SAFETY: The assertion above ensures all reads are within bounds.
111-
unsafe {
112-
*value = Self::dyn_region_read::<E>(
113-
region,
114-
E::STORED_SIZE.get().wrapping_mul(i).wrapping_add(address),
115-
)
116-
};
117-
}
118-
}
119-
}
55+
trait_set! {
56+
/// Manager of the state backend storage
57+
pub trait ManagerBase = Mode + Sized;
12058

121-
/// Manager with write capabilities
122-
pub trait ManagerWrite: ManagerBase + AtomMode + DataSpaceMode {
123-
/// Update an element in the region. `address` is in bytes.
124-
///
125-
/// # Safety
59+
/// Manager with allocation capabilities
12660
///
127-
/// The caller must ensure the access is within bounds.
128-
///
129-
/// ```text
130-
/// address + E:STORED <= region.len()
131-
/// ```
132-
unsafe fn dyn_region_write<E: Elem>(region: &mut Self::DynRegion, address: usize, value: E);
61+
/// Any `ManagerAlloc` inherently has read & write capabilities,
62+
/// since the manager creates the values on the first allocation.
63+
pub trait ManagerAlloc = AtomMode + DataSpaceMode;
13364

134-
/// Update multiple elements in the region. `address` is in bytes.
135-
///
136-
/// # Panics
137-
///
138-
/// Panics if the write would go out of bounds.
139-
fn dyn_region_write_all<E: Elem + Copy>(
140-
region: &mut Self::DynRegion,
141-
address: usize,
142-
values: &[E],
143-
) where
144-
Self: ManagerRead,
145-
{
146-
if values.is_empty() {
147-
return;
148-
}
65+
/// Manager with read capabilities
66+
pub trait ManagerRead = AtomMode + DataSpaceMode;
14967

150-
assert!(
151-
values
152-
.len()
153-
.checked_mul(E::STORED_SIZE.get())
154-
.expect("Total length should not overflow")
155-
.checked_add(address)
156-
.expect("End address should not overflow")
157-
<= Self::dyn_region_len(region)
158-
);
159-
160-
for (i, value) in values.iter().enumerate() {
161-
// SAFETY: The assertion above ensures all writes are within bounds.
162-
unsafe {
163-
Self::dyn_region_write::<E>(
164-
region,
165-
E::STORED_SIZE.get().wrapping_mul(i).wrapping_add(address),
166-
*value,
167-
);
168-
}
169-
}
170-
}
171-
}
68+
/// Manager with write capabilities
69+
pub trait ManagerWrite = AtomMode + DataSpaceMode;
17270

173-
/// Manager with the ability to serialise regions
174-
pub trait ManagerSerialise: ManagerRead + EncodeAtomMode + EncodeDataSpaceMode {
175-
/// Serialise the contents of the dynamic region.
176-
fn serialise_dyn_region<E: Encoder>(
177-
region: &Self::DynRegion,
178-
encoder: E,
179-
) -> Result<(), EncodeError>;
180-
}
71+
/// Manager with the ability to serialise regions
72+
pub trait ManagerSerialise = EncodeAtomMode + EncodeDataSpaceMode;
18173

182-
/// Manager with the ability to clone regions
183-
pub trait ManagerClone: ManagerBase + CloneAtomMode + CloneDataSpaceMode {
184-
/// Clone the dynamic region.
185-
fn clone_dyn_region(region: &Self::DynRegion) -> Self::DynRegion;
74+
/// Manager with the ability to clone regions
75+
pub trait ManagerClone = CloneAtomMode + CloneDataSpaceMode;
18676
}
18777

18878
#[cfg(test)]
@@ -240,6 +130,7 @@ mod tests {
240130
use octez_riscv_data::hash::PartialHash;
241131
use octez_riscv_data::merkle_proof::FromProof;
242132
use octez_riscv_data::merkle_tree::MerkleTree;
133+
use octez_riscv_data::serialisation::elem::Elem;
243134
use rand::RngCore;
244135

245136
use super::*;

src/riscv/lib/src/state_backend/normal_backend.rs

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)