Skip to content

starknet_os: add messages load_into to the aggregator load output hint #8266

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,61 @@ use std::collections::HashMap;

use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable};
use cairo_vm::vm::vm_core::VirtualMachine;
use starknet_api::transaction::MessageToL1;
use starknet_types_core::felt::Felt;

use crate::io::os_output::FullOsOutput;
use crate::io::os_output::{
FullOsOutput,
MessageToL2,
MESSAGE_TO_L1_CONST_FIELD_SIZE,
MESSAGE_TO_L2_CONST_FIELD_SIZE,
};
use crate::vm_utils::{IdentifierGetter, LoadCairoObject, VmUtilsResult};

pub(crate) trait ToMaybeRelocatables {
fn to_maybe_relocatables(&self) -> Vec<MaybeRelocatable>;
}

impl ToMaybeRelocatables for MessageToL1 {
fn to_maybe_relocatables(&self) -> Vec<MaybeRelocatable> {
let mut res = Vec::<MaybeRelocatable>::with_capacity(
MESSAGE_TO_L1_CONST_FIELD_SIZE + self.payload.0.len(),
);
res.push(Felt::from(self.from_address).into());
res.push(Felt::from(self.to_address).into());
res.push(Felt::from(self.payload.0.len()).into());
res.extend(self.payload.0.iter().map(|felt| felt.into()));
res
}
}

impl ToMaybeRelocatables for MessageToL2 {
fn to_maybe_relocatables(&self) -> Vec<MaybeRelocatable> {
let mut res = Vec::<MaybeRelocatable>::with_capacity(
MESSAGE_TO_L2_CONST_FIELD_SIZE + self.payload.0.len(),
);
res.push(Felt::from(self.from_address).into());
res.push(Felt::from(self.to_address).into());
res.push((self.nonce.0).into());
res.push((self.selector.0).into());
res.push(Felt::from(self.payload.0.len()).into());
res.extend(self.payload.0.iter().map(|felt| felt.into()));
res
}
}

impl<IG: IdentifierGetter, T: ToMaybeRelocatables> LoadCairoObject<IG> for T {
fn load_into(
&self,
vm: &mut VirtualMachine,
_identifier_getter: &IG,
address: Relocatable,
_constants: &std::collections::HashMap<String, Felt>,
) -> VmUtilsResult<Relocatable> {
Ok(vm.load_data(address, &self.to_maybe_relocatables())?)
}
}

struct StateEntryManager {
_state_entry_ptr: Relocatable,
_storage_dict_ptr: Relocatable,
Expand Down Expand Up @@ -40,13 +90,29 @@ impl FullStateDiffWriter {

/// Writes the given `FullOsOutput` to the VM at the specified address.
fn write_full_os_output<IG: IdentifierGetter>(
_output: &FullOsOutput,
_vm: &mut VirtualMachine,
_identifier_getter: &IG,
output: &FullOsOutput,
vm: &mut VirtualMachine,
identifier_getter: &IG,
_address: Relocatable,
_constants: &std::collections::HashMap<String, Felt>,
constants: &std::collections::HashMap<String, Felt>,
_state_diff_writer: &mut FullStateDiffWriter,
) -> VmUtilsResult<Relocatable> {
let FullOsOutput { common_os_output, .. } = output;
let messages_to_l1_start = vm.add_temporary_segment();
let _messages_to_l1_end = common_os_output.messages_to_l1.load_into(
vm,
identifier_getter,
messages_to_l1_start,
constants,
)?;

let messages_to_l2_start = vm.add_temporary_segment();
let _messages_to_l2_end = common_os_output.messages_to_l2.load_into(
vm,
identifier_getter,
messages_to_l2_start,
constants,
)?;
todo!()
}

Expand Down
18 changes: 9 additions & 9 deletions crates/starknet_os/src/io/os_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use crate::io::os_output_types::{
};
use crate::metrics::OsMetrics;

// Cairo DictAccess types for concrete objects.

const MESSAGE_TO_L1_CONST_FIELD_SIZE: usize = 3; // from_address, to_address, payload_size.
// from_address, to_address, payload_size.
pub(crate) const MESSAGE_TO_L1_CONST_FIELD_SIZE: usize = 3;
// from_address, to_address, nonce, selector, payload_size.
const MESSAGE_TO_L2_CONST_FIELD_SIZE: usize = 5;
pub(crate) const MESSAGE_TO_L2_CONST_FIELD_SIZE: usize = 5;

#[derive(Debug, thiserror::Error)]
pub enum OsOutputError {
#[error("Missing expected field: {0}.")]
Expand Down Expand Up @@ -101,12 +101,12 @@ pub fn message_l1_from_output_iter<It: Iterator<Item = Felt>>(
// An L1 to L2 message header, the message payload is concatenated to the end of the header.
pub struct MessageToL2 {
// The L1 address of the contract sending the message.
from_address: EthAddress,
pub(crate) from_address: EthAddress,
// The L2 address of the contract receiving the message.
to_address: ContractAddress,
nonce: Nonce,
selector: EntryPointSelector,
payload: L1ToL2Payload,
pub(crate) to_address: ContractAddress,
pub(crate) nonce: Nonce,
pub(crate) selector: EntryPointSelector,
pub(crate) payload: L1ToL2Payload,
}

impl TryFromOutputIter for MessageToL2 {
Expand Down
Loading