diff --git a/.gitignore b/.gitignore index 709b92e1c..7880254dd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ debug/ target/ target-tarpaulin/ target-custom/ +venv/ # These are backup files generated by rustfmt **/*.rs.bk diff --git a/sdk/src/common/system.rs b/sdk/src/common/system.rs index 643168140..bbba8f893 100644 --- a/sdk/src/common/system.rs +++ b/sdk/src/common/system.rs @@ -5,20 +5,22 @@ use rkyv::Deserialize; use { crate::common::merkle::merkleize, crate::common::types::{CanonicalOrderedTemporalHints, CrossProgramCall, Poseidon2Hash}, - crate::core::constants::DIGEST_BYTES, + // crate::core::constants::DIGEST_BYTES, crate::core::ecall::{ - call_tape_read, event_tape_read, ioread_private, ioread_public, self_prog_id_tape_read, + call_tape_read, + event_tape_read, + // ioread_private, ioread_public, self_prog_id_tape_read, }, core::ptr::slice_from_raw_parts, - std::collections::BTreeSet, + // std::collections::BTreeSet, }; #[cfg(not(target_os = "mozakvm"))] use {core::cell::RefCell, std::rc::Rc}; -use crate::common::traits::{Call, CallArgument, CallReturn, EventEmit}; +use crate::common::traits::{Call, EventEmit}; use crate::common::types::{ - CallTapeType, Event, EventTapeType, PrivateInputTapeType, ProgramIdentifier, - PublicInputTapeType, SystemTape, + CallTapeType, Event, EventTapeType, PrivateInputTapeType, PublicInputTapeType, RoleIdentifier, + SystemTape, }; /// `SYSTEM_TAPE` is a global singleton for interacting with @@ -63,39 +65,47 @@ pub(crate) static mut SYSTEM_TAPE: Lazy = Lazy::new(|| { // pre-populated data elements #[cfg(target_os = "mozakvm")] { - let mut self_prog_id_bytes = [0; DIGEST_BYTES]; - self_prog_id_tape_read(self_prog_id_bytes.as_mut_ptr()); - let self_prog_id = ProgramIdentifier(Poseidon2Hash::from(self_prog_id_bytes)); + type _x = PrivateInputTapeType; + type _y = PublicInputTapeType; + // TODO: Fix + // let mut self_prog_id_bytes = [0; DIGEST_BYTES]; + // self_prog_id_tape_read(self_prog_id_bytes.as_mut_ptr()); // Implement + // self_role_id_tape_read let self_prog_id = + // ProgramIdentifier(Poseidon2Hash::from(self_prog_id_bytes)); - let call_tape = populate_call_tape(self_prog_id); - let event_tape = populate_event_tape(self_prog_id); + // let call_tape = populate_call_tape(self_prog_id); + // let event_tape = populate_event_tape(self_prog_id); - let mut size_hint_bytes = [0; 4]; + // let mut size_hint_bytes = [0; 4]; - ioread_public(size_hint_bytes.as_mut_ptr(), 4); - let size_hint: usize = u32::from_le_bytes(size_hint_bytes).try_into().unwrap(); - let public_input_tape = PublicInputTapeType::with_size_hint(size_hint); + // ioread_public(size_hint_bytes.as_mut_ptr(), 4); + // let size_hint: usize = + // u32::from_le_bytes(size_hint_bytes).try_into().unwrap(); + // let public_input_tape = PublicInputTapeType::with_size_hint(size_hint); - ioread_private(size_hint_bytes.as_mut_ptr(), 4); - let size_hint: usize = u32::from_le_bytes(size_hint_bytes).try_into().unwrap(); - let private_input_tape = PrivateInputTapeType::with_size_hint(size_hint); + // ioread_private(size_hint_bytes.as_mut_ptr(), 4); + // let size_hint: usize = + // u32::from_le_bytes(size_hint_bytes).try_into().unwrap(); + // let private_input_tape = PrivateInputTapeType::with_size_hint(size_hint); - SystemTape { - private_input_tape, - public_input_tape, - call_tape, - event_tape, - } + // SystemTape { + // private_input_tape, + // public_input_tape, + // call_tape, + // event_tape, + // } + SystemTape::default() } }); #[cfg(target_os = "mozakvm")] +#[allow(warnings)] /// Populates a `MozakVM` [`CallTapeType`] via ECALLs. /// /// At this point, the [`CrossProgramCall`] messages are still rkyv-serialized, /// and must be deserialized at the point of consumption. Only the `callee`s are /// deserialized for persistence of the `cast_list`. -fn populate_call_tape(self_prog_id: ProgramIdentifier) -> CallTapeType { +fn populate_call_tape(self_role_id: RoleIdentifier) -> CallTapeType { let mut len_bytes = [0; 4]; call_tape_read(len_bytes.as_mut_ptr(), 4); let len: usize = u32::from_le_bytes(len_bytes).try_into().unwrap(); @@ -106,31 +116,32 @@ fn populate_call_tape(self_prog_id: ProgramIdentifier) -> CallTapeType { rkyv::access_unchecked::>(&*slice_from_raw_parts(buf.as_ptr(), len)) }; - let cast_list: Vec = archived_cpc_messages - .iter() - .map(|m| { - m.callee - .deserialize(Strategy::<_, Panic>::wrap(&mut ())) - .unwrap() - }) - .collect::>() - .into_iter() - .collect(); + // let cast_list: Vec = archived_cpc_messages + // .iter() + // .map(|m| { + // m.callee + // .deserialize(Strategy::<_, Panic>::wrap(&mut ())) + // .unwrap() + // }) + // .collect::>() + // .into_iter() + // .collect(); CallTapeType { - cast_list, - self_prog_id, + cast_list: Vec::default(), + self_role_id, reader: Some(archived_cpc_messages), index: 0, } } #[cfg(target_os = "mozakvm")] +#[allow(warnings)] /// Populates a `MozakVM` [`EventTapeType`] via ECALLs. /// /// At this point, the vector of [`CanonicalOrderedTemporalHints`] are still /// rkyv-serialized, and must be deserialized at the point of consumption. -fn populate_event_tape(self_prog_id: ProgramIdentifier) -> EventTapeType { +fn populate_event_tape(self_role_id: RoleIdentifier) -> EventTapeType { let mut len_bytes = [0; 4]; event_tape_read(len_bytes.as_mut_ptr(), 4); let len: usize = u32::from_le_bytes(len_bytes).try_into().unwrap(); @@ -145,7 +156,7 @@ fn populate_event_tape(self_prog_id: ProgramIdentifier) -> EventTapeType { }; EventTapeType { - self_prog_id, + self_role_id, reader: Some(canonical_ordered_temporal_hints), seen: vec![false; canonical_ordered_temporal_hints.len()], index: 0, @@ -161,17 +172,39 @@ pub fn event_emit(event: Event) { } } +/// Gets a roleID determined fully by `(Prog, instance)` tuple. It is +/// guaranteed that any call wih same `(Prog, instance)` tuple during one +/// native context will always return the same `RoleIdentifier` within that +/// context. Useful when different programs need to call the same role. +#[cfg(not(target_os = "mozakvm"))] +pub fn get_deterministic_role_id( + prog: crate::common::types::ProgramIdentifier, + instance: String, +) -> RoleIdentifier { + unsafe { + SYSTEM_TAPE + .call_tape + .get_deterministic_role_id(prog, instance) + } +} + +/// Gets a fresh & unique roleID referencible only by the `RoleIdentifier` +#[cfg(not(target_os = "mozakvm"))] +pub fn get_unique_role_id(prog: crate::common::types::ProgramIdentifier) -> RoleIdentifier { + unsafe { SYSTEM_TAPE.call_tape.get_unique_role_id(prog) } +} + /// Receive one message from mailbox targetted to us and its index /// "consume" such message. Subsequent reads will never /// return the same message. Panics on call-tape non-abidance. #[must_use] -pub fn call_receive() -> Option<(ProgramIdentifier, A, R)> +pub fn call_receive() -> Option<(crate::common::types::RoleIdentifier, A, R)> where - A: CallArgument + PartialEq, - R: CallReturn, + A: crate::common::traits::CallArgument + PartialEq, + R: crate::common::traits::CallReturn, ::Archived: Deserialize>, ::Archived: Deserialize>, { - unsafe { SYSTEM_TAPE.call_tape.receive() } + unsafe { crate::common::system::SYSTEM_TAPE.call_tape.receive() } } /// Send one message from mailbox targetted to some third-party @@ -179,19 +212,19 @@ where /// Panics on call-tape non-abidance. #[allow(clippy::similar_names)] pub fn call_send( - recipient_program: ProgramIdentifier, + recipient: crate::common::types::RoleIdentifier, argument: A, resolver: impl Fn(A) -> R, ) -> R where - A: CallArgument + PartialEq, - R: CallReturn, + A: crate::common::traits::CallArgument + PartialEq, + R: crate::common::traits::CallReturn, ::Archived: Deserialize>, ::Archived: Deserialize>, { unsafe { - SYSTEM_TAPE + crate::common::system::SYSTEM_TAPE .call_tape - .send(recipient_program, argument, resolver) + .send(recipient, argument, resolver) } } diff --git a/sdk/src/common/traits.rs b/sdk/src/common/traits.rs index 3bc0c67d2..83cbc3975 100644 --- a/sdk/src/common/traits.rs +++ b/sdk/src/common/traits.rs @@ -4,7 +4,7 @@ use rkyv::ser::{AllocSerializer, Composite}; use rkyv::util::AlignedVec; use rkyv::{Archive, Deserialize, Serialize}; -use crate::common::types::{Event, ProgramIdentifier}; +use crate::common::types::{Event, RoleIdentifier}; pub trait RkyvSerializable = rkyv::Serialize< Strategy, Panic>, Panic>, @@ -14,9 +14,9 @@ pub trait CallReturn = ?Sized + Clone + Default + RkyvSerializable + Archive; /// A data struct that is aware of it's own ID pub trait SelfIdentify { - fn get_self_identity(&self) -> ProgramIdentifier; + fn get_self_identity(&self) -> RoleIdentifier; #[allow(dead_code)] - fn set_self_identity(&mut self, id: ProgramIdentifier); + fn set_self_identity(&mut self, id: RoleIdentifier); } /// `Call` trait provides methods `send` & `receive` to use an @@ -28,7 +28,7 @@ pub trait Call: SelfIdentify { /// deserialization. This func never serializes in `mozakvm`. fn send( &mut self, - recipient_program: ProgramIdentifier, + recipient: RoleIdentifier, argument: A, resolver: impl Fn(A) -> R, ) -> R @@ -45,7 +45,7 @@ pub trait Call: SelfIdentify { /// the result that they want us to ensure is correct. /// Under the hood, wherever required, it uses `rkyv` for /// deserialization. This func never serializes in `mozakvm`. - fn receive(&mut self) -> Option<(ProgramIdentifier, A, R)> + fn receive(&mut self) -> Option<(RoleIdentifier, A, R)> where A: CallArgument + PartialEq, R: CallReturn, diff --git a/sdk/src/common/types/cross_program_call.rs b/sdk/src/common/types/cross_program_call.rs index 5c68d1340..ab85bfb24 100644 --- a/sdk/src/common/types/cross_program_call.rs +++ b/sdk/src/common/types/cross_program_call.rs @@ -7,8 +7,8 @@ )] #[allow(clippy::pub_underscore_fields)] pub struct CrossProgramCall { - pub caller: super::ProgramIdentifier, - pub callee: super::ProgramIdentifier, + pub caller: super::RoleIdentifier, + pub callee: super::RoleIdentifier, pub argument: super::RawMessage, pub return_: super::RawMessage, } diff --git a/sdk/src/common/types/mod.rs b/sdk/src/common/types/mod.rs index 9c5b0b8fe..218ef80d7 100644 --- a/sdk/src/common/types/mod.rs +++ b/sdk/src/common/types/mod.rs @@ -3,6 +3,7 @@ pub(crate) mod event; pub(crate) mod poseidon2hash; pub(crate) mod program_identifier; pub(crate) mod raw_message; +pub(crate) mod role; pub(crate) mod state_address; pub(crate) mod state_object; pub(crate) mod system_tape; @@ -12,6 +13,7 @@ pub use event::{CanonicalEvent, CanonicalOrderedTemporalHints, Event, EventType} pub use poseidon2hash::Poseidon2Hash; pub use program_identifier::ProgramIdentifier; pub use raw_message::RawMessage; +pub use role::RoleIdentifier; pub use state_address::StateAddress; pub use state_object::StateObject; pub use system_tape::{ diff --git a/sdk/src/common/types/role.rs b/sdk/src/common/types/role.rs new file mode 100644 index 000000000..6813265ba --- /dev/null +++ b/sdk/src/common/types/role.rs @@ -0,0 +1,4 @@ +/// A monotonically increasing identifier of different program executions +/// based on the order of discover +#[allow(clippy::module_name_repetitions)] +pub type RoleIdentifier = u32; diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index 6ca7cf0b3..f8d0f1894 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -15,6 +15,8 @@ pub mod common; #[cfg(feature = "std")] pub use crate::common::system::{call_receive, call_send, event_emit}; +#[cfg(all(feature = "std", not(target_os = "mozakvm")))] +pub use crate::common::system::{get_deterministic_role_id, get_unique_role_id}; #[cfg(all(feature = "std", target_os = "mozakvm"))] pub mod mozakvm; diff --git a/sdk/src/mozakvm/calltape.rs b/sdk/src/mozakvm/calltape.rs index e1d175ad0..5135565f8 100644 --- a/sdk/src/mozakvm/calltape.rs +++ b/sdk/src/mozakvm/calltape.rs @@ -2,34 +2,27 @@ use rkyv::rancor::{Panic, Strategy}; use rkyv::{Archive, Deserialize}; use crate::common::traits::{Call, CallArgument, CallReturn, SelfIdentify}; -use crate::common::types::{CrossProgramCall, ProgramIdentifier}; +use crate::common::types::{CrossProgramCall, ProgramIdentifier, RoleIdentifier}; /// Represents the `CallTape` under `mozak-vm` #[derive(Default, Clone)] pub struct CallTape { pub(crate) cast_list: Vec, - pub(crate) self_prog_id: ProgramIdentifier, + pub(crate) self_role_id: RoleIdentifier, pub(crate) reader: Option<&'static as Archive>::Archived>, pub(crate) index: usize, } -impl CallTape { - /// Checks if actor seen is casted actor - fn is_casted_actor(&self, actor: &ProgramIdentifier) -> bool { - &ProgramIdentifier::default() == actor || self.cast_list.contains(actor) - } -} - impl SelfIdentify for CallTape { - fn set_self_identity(&mut self, id: ProgramIdentifier) { self.self_prog_id = id; } + fn set_self_identity(&mut self, id: RoleIdentifier) { self.self_role_id = id; } - fn get_self_identity(&self) -> ProgramIdentifier { self.self_prog_id } + fn get_self_identity(&self) -> RoleIdentifier { self.self_role_id } } impl Call for CallTape { fn send( &mut self, - recipient_program: ProgramIdentifier, + recipient: RoleIdentifier, argument: A, _resolver: impl Fn(A) -> R, ) -> R @@ -48,8 +41,7 @@ impl Call for CallTape { // Ensure fields are correctly populated for caller and callee assert!(cpcmsg.caller == self.get_self_identity()); - assert!(cpcmsg.callee == recipient_program); - assert!(self.is_casted_actor(&recipient_program)); + assert!(cpcmsg.callee == recipient); // Deserialize the `arguments` seen on the tape, and assert let zcd_args = unsafe { rkyv::access_unchecked::(&cpcmsg.argument.0[..]) }; @@ -76,7 +68,7 @@ impl Call for CallTape { } #[allow(clippy::similar_names)] - fn receive(&mut self) -> Option<(ProgramIdentifier, A, R)> + fn receive(&mut self) -> Option<(RoleIdentifier, A, R)> where A: CallArgument + PartialEq, R: CallReturn, @@ -96,23 +88,20 @@ impl Call for CallTape { // Well, once we are sure that we were not the caller, we can // either be a callee in which case we process and send information // back or we continue searching. - let callee: ProgramIdentifier = zcd_cpcmsg + let callee: RoleIdentifier = zcd_cpcmsg .callee .deserialize(Strategy::<_, Panic>::wrap(&mut ())) .unwrap(); - if self.self_prog_id == callee { + if self.self_role_id == callee { // First, ensure that we are not the caller, no-one can call // themselves. (Even if they can w.r.t. self-calling extension, // the `caller` field would remain distinct) - let caller: ProgramIdentifier = zcd_cpcmsg + let caller: RoleIdentifier = zcd_cpcmsg .caller .deserialize(Strategy::<_, Panic>::wrap(&mut ())) .unwrap(); - assert!(caller != self.self_prog_id); - - // Before accepting, make sure that caller was a part of castlist - assert!(self.is_casted_actor(&caller)); + assert!(caller != self.self_role_id); let archived_args = unsafe { rkyv::access_unchecked::(zcd_cpcmsg.argument.0.as_slice()) }; diff --git a/sdk/src/mozakvm/eventtape.rs b/sdk/src/mozakvm/eventtape.rs index 7a2d79e5d..47dbec552 100644 --- a/sdk/src/mozakvm/eventtape.rs +++ b/sdk/src/mozakvm/eventtape.rs @@ -3,22 +3,22 @@ use rkyv::{Archive, Deserialize}; use crate::common::traits::{EventEmit, SelfIdentify}; use crate::common::types::{ - CanonicalEvent, CanonicalOrderedTemporalHints, Event, Poseidon2Hash, ProgramIdentifier, + CanonicalEvent, CanonicalOrderedTemporalHints, Event, Poseidon2Hash, RoleIdentifier, }; /// Represents the `EventTape` under native execution #[derive(Default, Clone)] pub struct EventTape { - pub(crate) self_prog_id: ProgramIdentifier, + pub(crate) self_role_id: RoleIdentifier, pub(crate) reader: Option<&'static as Archive>::Archived>, pub(crate) seen: Vec, pub(crate) index: usize, } impl SelfIdentify for EventTape { - fn set_self_identity(&mut self, id: ProgramIdentifier) { self.self_prog_id = id } + fn set_self_identity(&mut self, id: RoleIdentifier) { self.self_role_id = id } - fn get_self_identity(&self) -> ProgramIdentifier { self.self_prog_id } + fn get_self_identity(&self) -> RoleIdentifier { self.self_role_id } } impl EventEmit for EventTape { diff --git a/sdk/src/native/calltape.rs b/sdk/src/native/calltape.rs index 540f1f844..8889d2e58 100644 --- a/sdk/src/native/calltape.rs +++ b/sdk/src/native/calltape.rs @@ -1,16 +1,23 @@ use std::cell::RefCell; +use std::collections::HashMap; use std::rc::Rc; use rkyv::rancor::{Panic, Strategy}; use rkyv::Deserialize; use crate::common::traits::{Call, CallArgument, CallReturn, SelfIdentify}; -use crate::common::types::{CrossProgramCall, ProgramIdentifier, RawMessage}; +use crate::common::types::{CrossProgramCall, ProgramIdentifier, RawMessage, RoleIdentifier}; use crate::native::identity::IdentityStack; /// Represents the `CallTape` under native execution #[derive(Default, Clone, serde::Serialize, serde::Deserialize)] pub struct CallTape { + #[serde(skip)] + pub next_available_role_id: RoleIdentifier, + #[serde(skip)] + pub lookup_role_map: HashMap<(ProgramIdentifier, String), RoleIdentifier>, + #[serde(skip)] + pub unique_role_map: HashMap, #[serde(skip)] pub(crate) identity_stack: Rc>, #[serde(rename = "global_calltape")] @@ -22,17 +29,17 @@ impl std::fmt::Debug for CallTape { } impl SelfIdentify for CallTape { - fn set_self_identity(&mut self, id: ProgramIdentifier) { + fn set_self_identity(&mut self, id: RoleIdentifier) { self.identity_stack.borrow_mut().add_identity(id); } - fn get_self_identity(&self) -> ProgramIdentifier { self.identity_stack.borrow().top_identity() } + fn get_self_identity(&self) -> RoleIdentifier { self.identity_stack.borrow().top_identity() } } impl Call for CallTape { fn send( &mut self, - recipient_program: ProgramIdentifier, + recipient: RoleIdentifier, argument: A, resolver: impl Fn(A) -> R, ) -> R @@ -44,7 +51,7 @@ impl Call for CallTape { // Create a skeletal `CrossProgramCall` to be resolved via "resolver" let msg = CrossProgramCall { caller: self.get_self_identity(), - callee: recipient_program, + callee: recipient, argument: rkyv::to_bytes::<_, 256, _>(&argument).unwrap().into(), return_: RawMessage::default(), // Unfilled: we have to still resolve it }; @@ -58,7 +65,7 @@ impl Call for CallTape { self.writer.push(msg); // resolve the return value and add to where message was - self.set_self_identity(recipient_program); + self.set_self_identity(recipient); let resolved_value = resolver(argument); self.writer[inserted_idx].return_ = rkyv::to_bytes::<_, 256, _>(&resolved_value).unwrap().into(); @@ -67,7 +74,7 @@ impl Call for CallTape { resolved_value } - fn receive(&mut self) -> Option<(ProgramIdentifier, A, R)> + fn receive(&mut self) -> Option<(RoleIdentifier, A, R)> where A: CallArgument + PartialEq, R: CallReturn, @@ -77,6 +84,41 @@ impl Call for CallTape { } } +impl CallTape { + /// Gets a roleID determined fully by `(Prog, instance)` tuple. It is + /// guaranteed that any call wih same `(Prog, instance)` tuple during one + /// native context will always return the same `RoleIdentifier` within that + /// context. Useful when different programs need to call the same role. + pub fn get_deterministic_role_id( + &mut self, + prog: ProgramIdentifier, + instance: String, + ) -> RoleIdentifier { + let identifier = (prog, instance); + + if let Some(role_id) = self.lookup_role_map.get(&identifier) { + return *role_id; + }; + + // if not already found in role map + let allocated_role = self.next_available_role_id; + self.next_available_role_id += 1; + + self.lookup_role_map.insert(identifier, allocated_role); + allocated_role + } + + /// Gets a fresh & unique roleID referencible only by the `RoleIdentifier` + pub fn get_unique_role_id(&mut self, prog: ProgramIdentifier) -> RoleIdentifier { + let allocated_role = self.next_available_role_id; + self.next_available_role_id += 1; + + self.unique_role_map.insert(prog, allocated_role); + + allocated_role + } +} + #[cfg(test)] mod tests { use super::CallTape; diff --git a/sdk/src/native/eventtape.rs b/sdk/src/native/eventtape.rs index e1e4ad803..ecd10d084 100644 --- a/sdk/src/native/eventtape.rs +++ b/sdk/src/native/eventtape.rs @@ -4,7 +4,7 @@ use std::rc::Rc; use crate::common::traits::{EventEmit, SelfIdentify}; use crate::common::types::{ - CanonicalEvent, CanonicalOrderedTemporalHints, Event, Poseidon2Hash, ProgramIdentifier, + CanonicalEvent, CanonicalOrderedTemporalHints, Event, Poseidon2Hash, RoleIdentifier, }; use crate::native::identity::IdentityStack; @@ -131,7 +131,7 @@ pub struct EventTape { #[serde(skip)] pub(crate) identity_stack: Rc>, #[serde(rename = "individual_event_tapes")] - pub writer: HashMap, + pub writer: HashMap, } impl std::fmt::Debug for EventTape { @@ -139,15 +139,15 @@ impl std::fmt::Debug for EventTape { } impl SelfIdentify for EventTape { - fn set_self_identity(&mut self, _id: ProgramIdentifier) { unimplemented!() } + fn set_self_identity(&mut self, _id: RoleIdentifier) { unimplemented!() } - fn get_self_identity(&self) -> ProgramIdentifier { self.identity_stack.borrow().top_identity() } + fn get_self_identity(&self) -> RoleIdentifier { self.identity_stack.borrow().top_identity() } } impl EventEmit for EventTape { fn emit(&mut self, event: Event) { let self_id = self.get_self_identity(); - assert_ne!(self_id, ProgramIdentifier::default()); + assert_ne!(self_id, RoleIdentifier::default()); self.writer.entry(self_id).or_default().push_temporal(event); } @@ -166,7 +166,7 @@ mod tests { type_: EventType::Read, object: StateObject { address: StateAddress([1; STATE_TREE_DEPTH]), - constraint_owner: ProgramIdentifier::new_from_rand_seed(2), + constraint_owner: RoleIdentifier::new_from_rand_seed(2), data: vec![], } }; @@ -174,7 +174,7 @@ mod tests { type_: EventType::Read, object: StateObject { address: StateAddress([2; STATE_TREE_DEPTH]), - constraint_owner: ProgramIdentifier::new_from_rand_seed(3), + constraint_owner: RoleIdentifier::new_from_rand_seed(3), data: vec![], } }; @@ -182,7 +182,7 @@ mod tests { type_: EventType::Read, object: StateObject { address: StateAddress([3; STATE_TREE_DEPTH]), - constraint_owner: ProgramIdentifier::new_from_rand_seed(4), + constraint_owner: RoleIdentifier::new_from_rand_seed(4), data: vec![], } }; diff --git a/sdk/src/native/identity.rs b/sdk/src/native/identity.rs index 21cbd8a7a..74e5a20e1 100644 --- a/sdk/src/native/identity.rs +++ b/sdk/src/native/identity.rs @@ -1,26 +1,26 @@ #![allow(clippy::module_name_repetitions)] -use crate::common::types::ProgramIdentifier; +use crate::common::types::RoleIdentifier; /// Represents a stack for call contexts during native execution. #[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)] -pub struct IdentityStack(Vec); +pub struct IdentityStack(Vec); impl IdentityStack { - pub fn add_identity(&mut self, id: ProgramIdentifier) { self.0.push(id); } + pub fn add_identity(&mut self, id: RoleIdentifier) { self.0.push(id); } #[must_use] - pub fn top_identity(&self) -> ProgramIdentifier { self.0.last().copied().unwrap_or_default() } + pub fn top_identity(&self) -> RoleIdentifier { self.0.last().copied().unwrap_or_default() } pub fn rm_identity(&mut self) { self.0.truncate(self.0.len().saturating_sub(1)); } } -/// Manually add a `ProgramIdentifier` onto `IdentityStack`. Useful +/// Manually add a `RoleIdentifier` onto `IdentityStack`. Useful /// when one want to escape automatic management of `IdentityStack` /// via cross-program-calls sends (ideally temporarily). /// CAUTION: Manual function for `IdentityStack`, misuse may lead /// to system tape generation failure. #[cfg(all(feature = "std", not(target_os = "mozakvm")))] -pub fn add_identity(id: crate::common::types::ProgramIdentifier) { +pub fn add_identity(id: crate::common::types::RoleIdentifier) { unsafe { crate::common::system::SYSTEM_TAPE .call_tape @@ -30,7 +30,7 @@ pub fn add_identity(id: crate::common::types::ProgramIdentifier) { } } -/// Manually remove a `ProgramIdentifier` from `IdentityStack`. +/// Manually remove a `RoleIdentifier` from `IdentityStack`. /// Useful when one want to escape automatic management of `IdentityStack` /// via cross-program-calls sends (ideally temporarily). /// CAUTION: Manual function for `IdentityStack`, misuse may lead diff --git a/sdk/src/native/inputtape.rs b/sdk/src/native/inputtape.rs index dddcb068f..40a858f91 100644 --- a/sdk/src/native/inputtape.rs +++ b/sdk/src/native/inputtape.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use std::rc::Rc; use crate::common::traits::SelfIdentify; -use crate::common::types::{ProgramIdentifier, RawMessage}; +use crate::common::types::{RawMessage, RoleIdentifier}; use crate::native::identity::IdentityStack; /// Represents the `RawTape` under native execution @@ -12,7 +12,7 @@ pub struct RawTape { #[serde(skip)] pub(crate) identity_stack: Rc>, #[serde(rename = "individual_raw_tapes")] - pub writer: HashMap, + pub writer: HashMap, } impl std::fmt::Debug for RawTape { @@ -20,11 +20,11 @@ impl std::fmt::Debug for RawTape { } impl SelfIdentify for RawTape { - fn set_self_identity(&mut self, id: ProgramIdentifier) { + fn set_self_identity(&mut self, id: RoleIdentifier) { self.identity_stack.borrow_mut().add_identity(id); } - fn get_self_identity(&self) -> ProgramIdentifier { self.identity_stack.borrow().top_identity() } + fn get_self_identity(&self) -> RoleIdentifier { self.identity_stack.borrow().top_identity() } } /// We have to implement `std::io::Write` in native context @@ -34,7 +34,7 @@ impl SelfIdentify for RawTape { impl std::io::Write for RawTape { fn write(&mut self, buf: &[u8]) -> Result { let self_id = self.get_self_identity(); - assert_ne!(self_id, ProgramIdentifier::default()); + assert_ne!(self_id, RoleIdentifier::default()); self.writer.entry(self_id).or_default().0.extend(buf);