Skip to content
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

Use checked rkyv::access #1737

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/inputtape/core-logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ version = "0.1.0"

[dependencies]
mozak-sdk = { path = "../../../sdk" }
rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] }
rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc", "bytecheck"] }
rkyv_derive = "=0.8.0-alpha.1"
2 changes: 2 additions & 0 deletions examples/inputtape/core-logic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use rkyv::{Archive, Deserialize, Serialize};

#[derive(Archive, Deserialize, Serialize, PartialEq, Clone)]
#[cfg_attr(not(target_os = "mozakvm"), derive(Debug))]
#[archive(check_bytes)]
pub enum MethodArgs {
RawTapesTest,
}

#[derive(Archive, Default, Deserialize, Serialize, PartialEq, Clone)]
#[cfg_attr(not(target_os = "mozakvm"), derive(Debug))]
#[archive(check_bytes)]
pub enum MethodReturns {
#[default]
Noop,
Expand Down
2 changes: 2 additions & 0 deletions examples/token/core-logic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rkyv::{Archive, Deserialize, Serialize};

#[derive(Archive, Deserialize, Serialize, PartialEq, Clone)]
#[cfg_attr(not(target_os = "mozakvm"), derive(Debug))]
#[archive(check_bytes)]
pub enum MethodArgs {
// Mint,
// Burn,
Expand All @@ -22,6 +23,7 @@ pub enum MethodArgs {

#[derive(Archive, Default, Deserialize, Serialize, PartialEq, Clone)]
#[cfg_attr(not(target_os = "mozakvm"), derive(Debug))]
#[archive(check_bytes)]
pub enum MethodReturns {
// TODO: Remove later
#[default]
Expand Down
4 changes: 2 additions & 2 deletions examples/token/mozakvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version = "0.1.0"

[dependencies]
mozak-sdk = { path = "../../../sdk" }
rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] }
rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc", "bytecheck"] }
rkyv_derive = "=0.8.0-alpha.1"
token-core-logic = { path = "../core-logic" }
wallet-core-logic = { path = "../../wallet/core-logic" }
Expand All @@ -17,4 +17,4 @@ std = []
# The following is read by `run_examples.py`
[package.metadata.mozak]
example_dependents = ["wallet"]
example_program_id = "MZK-b10da48cea4c09676b8e0efcd806941465060736032bb898420d0863dca72538"
example_program_id = "MZK-63236d3b0bc73b9cb18ab2aacbbcf741b84d0560e00172374ddfcffea7b409cc"
2 changes: 1 addition & 1 deletion examples/wallet/core-logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "0.1.0"

[dependencies]
mozak-sdk = { path = "../../../sdk" }
rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] }
rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc", "bytecheck"] }
rkyv_derive = "=0.8.0-alpha.1"

[target.'cfg(not(target_os="mozakvm"))'.dependencies]
Expand Down
10 changes: 8 additions & 2 deletions examples/wallet/core-logic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate alloc;

use mozak_sdk::common::types::{Poseidon2Hash, ProgramIdentifier, StateObject};
use rkyv::rancor::{Panic, Strategy};
use rkyv::rancor::{Failure, Panic, Strategy};
use rkyv::{Archive, Deserialize, Serialize};

/// A generic private key used by the wallet.
Expand All @@ -15,6 +15,7 @@ pub struct PrivateKey(pub [u8; 32]);
/// a poseidon2 hash.
#[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)]
#[cfg_attr(not(target_os = "mozakvm"), derive(Debug))]
#[archive(check_bytes)]
pub struct PublicKey(pub Poseidon2Hash);

impl From<[u8; 32]> for PrivateKey {
Expand All @@ -38,6 +39,7 @@ impl PrivateKey {
/// `TokenObject`.
#[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)]
#[cfg_attr(not(target_os = "mozakvm"), derive(Debug))]
#[archive(check_bytes)]
pub struct Amount(u64);

impl From<u64> for Amount {
Expand All @@ -47,6 +49,7 @@ impl From<u64> for Amount {
/// A token object is represented in the `data` section of a `StateObject`, and
/// contains information about the token that is being used in a program.
#[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)]
#[archive(check_bytes)]
#[cfg_attr(not(target_os = "mozakvm"), derive(Debug))]
pub struct TokenObject {
/// The public key that is the economic owner of this `TokenObject`.
Expand All @@ -57,7 +60,7 @@ pub struct TokenObject {

impl From<StateObject> for TokenObject {
fn from(value: StateObject) -> Self {
let archived = unsafe { rkyv::access_unchecked::<TokenObject>(&value.data[..]) };
let archived = rkyv::access::<TokenObject, Failure>(&value.data[..]).unwrap();
let token_object: TokenObject = archived
.deserialize(Strategy::<_, Panic>::wrap(&mut ()))
.unwrap();
Expand All @@ -73,6 +76,7 @@ impl From<StateObject> for TokenObject {
/// differentiate between transactions.
#[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)]
#[cfg_attr(not(target_os = "mozakvm"), derive(Debug))]
#[archive(check_bytes)]
pub struct BlackBox {
pub remitter_program: ProgramIdentifier,
pub remittee_program: ProgramIdentifier,
Expand All @@ -95,11 +99,13 @@ impl BlackBox {

#[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)]
#[cfg_attr(not(target_os = "mozakvm"), derive(Debug))]
#[archive(check_bytes)]
pub enum MethodArgs {
ApproveSignature(PublicKey, BlackBox),
}

#[derive(Archive, Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
#[archive(check_bytes)]
pub enum MethodReturns {
ApproveSignature(()),
}
Expand Down
5 changes: 3 additions & 2 deletions examples/wallet/mozakvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions sdk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions sdk/src/common/system.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use once_cell::unsync::Lazy;
use rkyv::rancor::{Panic, Strategy};
use rkyv::Deserialize;
#[cfg(target_os = "mozakvm")]
use {
crate::common::merkle::merkleize,
Expand All @@ -9,12 +7,15 @@ use {
crate::core::ecall::{
call_tape_read, event_tape_read, ioread_private, ioread_public, self_prog_id_tape_read,
},
rkyv::rancor::{Panic, Strategy},
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::{
ArchivedCallArgument, ArchivedCallReturn, Call, CallArgument, CallReturn, EventEmit,
};
use crate::common::types::{
CallTapeType, Event, EventTapeType, PrivateInputTapeType, ProgramIdentifier,
PublicInputTapeType, SystemTape,
Expand Down Expand Up @@ -104,7 +105,7 @@ fn populate_call_tape(self_prog_id: ProgramIdentifier) -> CallTapeType {
let buf: &'static mut Vec<u8> = Box::leak(Box::new(vec![0; len]));
call_tape_read(buf);

let archived_cpc_messages = unsafe { rkyv::access_unchecked::<Vec<CrossProgramCall>>(buf) };
let archived_cpc_messages = rkyv::access::<Vec<CrossProgramCall>, Panic>(buf).unwrap();

let cast_list: Vec<ProgramIdentifier> = archived_cpc_messages
.iter()
Expand Down Expand Up @@ -136,12 +137,13 @@ fn populate_call_tape(self_prog_id: ProgramIdentifier) -> CallTapeType {
fn populate_event_tape(self_prog_id: ProgramIdentifier) -> EventTapeType {
let mut len_bytes = [0; 4];
event_tape_read(&mut len_bytes);

let len: usize = u32::from_le_bytes(len_bytes).try_into().unwrap();
let buf: &'static mut Vec<u8> = Box::leak(Box::new(vec![0; len]));
event_tape_read(buf);

let canonical_ordered_temporal_hints =
unsafe { rkyv::access_unchecked::<Vec<CanonicalOrderedTemporalHints>>(buf) };
rkyv::access::<Vec<CanonicalOrderedTemporalHints>, Panic>(buf).unwrap();
EventTapeType {
self_prog_id,
reader: Some(canonical_ordered_temporal_hints),
Expand All @@ -167,8 +169,8 @@ pub fn call_receive<A, R>() -> Option<(ProgramIdentifier, A, R)>
where
A: CallArgument + PartialEq,
R: CallReturn,
<A as rkyv::Archive>::Archived: Deserialize<A, Strategy<(), Panic>>,
<R as rkyv::Archive>::Archived: Deserialize<R, Strategy<(), Panic>>, {
<A as rkyv::Archive>::Archived: ArchivedCallArgument<A>,
<R as rkyv::Archive>::Archived: ArchivedCallReturn<R>, {
unsafe { SYSTEM_TAPE.call_tape.receive() }
}

Expand All @@ -184,8 +186,8 @@ pub fn call_send<A, R>(
where
A: CallArgument + PartialEq,
R: CallReturn,
<A as rkyv::Archive>::Archived: Deserialize<A, Strategy<(), Panic>>,
<R as rkyv::Archive>::Archived: Deserialize<R, Strategy<(), Panic>>, {
<A as rkyv::Archive>::Archived: ArchivedCallArgument<A>,
<R as rkyv::Archive>::Archived: ArchivedCallReturn<R>, {
unsafe {
SYSTEM_TAPE
.call_tape
Expand Down
Loading
Loading