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

bugfix: fix rkyv alignment #1780

Draft
wants to merge 4 commits into
base: bing/rkyv-access
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
fix wallet program + handle proper access for data in stateobject
  • Loading branch information
codeblooded1729 committed Jun 17, 2024
commit 333ffd02a044eb7939b85720d4e470563983fa1c
5 changes: 4 additions & 1 deletion examples/wallet/core-logic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ extern crate alloc;

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

/// A generic private key used by the wallet.
@@ -60,7 +61,9 @@ pub struct TokenObject {

impl From<StateObject> for TokenObject {
fn from(value: StateObject) -> Self {
let archived = rkyv::access::<TokenObject, Failure>(&value.data[..]).unwrap();
let mut aligned_data = AlignedVec::with_capacity(value.data.len());
aligned_data.extend_from_slice(&value.data);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems suspicious. I suspect we want our StateObject's data to be an AlignedVec in the first place, then we don't need to copy here. (Assuming this code also gets executed on guest.)

let archived = rkyv::access::<TokenObject, Failure>(&aligned_data).unwrap();
let token_object: TokenObject = archived
.deserialize(Strategy::<_, Panic>::wrap(&mut ()))
.unwrap();
18 changes: 10 additions & 8 deletions examples/wallet/native/src/main.rs
Original file line number Diff line number Diff line change
@@ -21,19 +21,21 @@ use mozak_sdk::common::types::ProgramIdentifier;
use wallet_core_logic::{dispatch, BlackBox, MethodArgs, PrivateKey, PublicKey, TokenObject};

fn main() {
let wallet_program = ProgramIdentifier::new_from_rand_seed(1);
let remitter_program = ProgramIdentifier::new_from_rand_seed(2);
let remittee_program = ProgramIdentifier::new_from_rand_seed(3);
let private_key = PrivateKey::new_from_rand_seed(4);
let public_key = PublicKey(mozak_sdk::native::poseidon::poseidon2_hash_no_pad(
&private_key.0,
let remitter_private_key = PrivateKey::new_from_rand_seed(4);
let remitter_public_key = PublicKey(mozak_sdk::native::poseidon::poseidon2_hash_no_pad(
&remitter_private_key.0,
));
mozak_sdk::add_identity(remitter_program); // Manual override for `IdentityStack`
let _ = mozak_sdk::write(&mozak_sdk::InputTapeType::PrivateTape, &private_key.0[..]);
let _ = mozak_sdk::write(
&mozak_sdk::InputTapeType::PrivateTape,
&remitter_private_key.0[..],
);
mozak_sdk::rm_identity(); // Manual override for `IdentityStack`

let token_object = TokenObject {
pub_key: public_key.clone(),
pub_key: remitter_public_key.clone(),
amount: 10.into(),
};

@@ -44,8 +46,8 @@ fn main() {
};

mozak_sdk::call_send(
wallet_program,
MethodArgs::ApproveSignature(public_key, black_box.clone()),
remitter_program,
MethodArgs::ApproveSignature(remitter_public_key, black_box.clone()),
dispatch,
);

Loading