Skip to content

Commit

Permalink
fix: add domaining logic to tx and receipt memorizers
Browse files Browse the repository at this point in the history
  • Loading branch information
petscheit committed Feb 5, 2025
1 parent c5b02db commit 02530ac
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 32 deletions.
2 changes: 2 additions & 0 deletions crates/sound_hint_processor/src/syscall_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,14 @@ impl traits::SyscallHandler for CallContractHandlerRelay {
type Response = CallContractResponse;

fn read_request(&mut self, vm: &VirtualMachine, ptr: &mut Relocatable) -> SyscallResult<Self::Request> {
println!("reading request");
let ret = Self::Request::from_memory(vm, *ptr)?;
*ptr = (*ptr + Self::Request::cairo_size())?;
Ok(ret)
}

async fn execute(&mut self, request: Self::Request, vm: &mut VirtualMachine) -> SyscallResult<Self::Response> {
println!("executing request");
match request.contract_address {
v if v == call_contract::debug::CONTRACT_ADDRESS => self.debug_call_contract_handler.execute(request, vm).await,
_ => {
Expand Down
15 changes: 9 additions & 6 deletions crates/types/src/keys/evm/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,37 @@ use crate::cairo::traits::CairoType;
#[derive(Debug, Clone)]
pub struct CairoKey {
chain_id: Felt252,
domain: Felt252,
block_number: Felt252,
transaction_index: Felt252,
}

impl CairoKey {
pub fn hash(&self) -> Felt252 {
poseidon_hash_many(&[self.chain_id, self.block_number, self.transaction_index])
poseidon_hash_many(&[self.chain_id, self.domain, self.block_number, self.transaction_index])
}
}

impl CairoType for CairoKey {
fn from_memory(vm: &VirtualMachine, ptr: Relocatable) -> Result<Self, MemoryError> {
Ok(Self {
chain_id: *vm.get_integer((ptr + 0)?)?,
block_number: *vm.get_integer((ptr + 1)?)?,
transaction_index: *vm.get_integer((ptr + 2)?)?,
domain: *vm.get_integer((ptr + 1)?)?,
block_number: *vm.get_integer((ptr + 2)?)?,
transaction_index: *vm.get_integer((ptr + 3)?)?,
})
}

fn to_memory(&self, vm: &mut VirtualMachine, address: Relocatable) -> Result<(), MemoryError> {
vm.insert_value((address + 0)?, self.chain_id)?;
vm.insert_value((address + 1)?, self.block_number)?;
vm.insert_value((address + 2)?, self.transaction_index)?;
vm.insert_value((address + 1)?, self.domain)?;
vm.insert_value((address + 2)?, self.block_number)?;
vm.insert_value((address + 3)?, self.transaction_index)?;
Ok(())
}

fn n_fields() -> usize {
3
4
}
}

Expand Down
16 changes: 10 additions & 6 deletions crates/types/src/keys/evm/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,38 @@ use crate::cairo::traits::CairoType;
#[derive(Debug, Clone)]
pub struct CairoKey {
chain_id: Felt252,
domain: Felt252,
block_number: Felt252,
transaction_index: Felt252,
}

impl CairoKey {
pub fn hash(&self) -> Felt252 {
poseidon_hash_many(&[self.chain_id, self.block_number, self.transaction_index])
poseidon_hash_many(&[self.chain_id, self.domain, self.block_number, self.transaction_index])
}
}

impl CairoType for CairoKey {
fn from_memory(vm: &VirtualMachine, ptr: Relocatable) -> Result<Self, MemoryError> {
println!("tx key from memory: {:?}", ptr);
Ok(Self {
chain_id: *vm.get_integer((ptr + 0)?)?,
block_number: *vm.get_integer((ptr + 1)?)?,
transaction_index: *vm.get_integer((ptr + 2)?)?,
domain: *vm.get_integer((ptr + 1)?)?,
block_number: *vm.get_integer((ptr + 2)?)?,
transaction_index: *vm.get_integer((ptr + 3)?)?,
})
}

fn to_memory(&self, vm: &mut VirtualMachine, address: Relocatable) -> Result<(), MemoryError> {
vm.insert_value((address + 0)?, self.chain_id)?;
vm.insert_value((address + 1)?, self.block_number)?;
vm.insert_value((address + 2)?, self.transaction_index)?;
vm.insert_value((address + 1)?, self.domain)?;
vm.insert_value((address + 2)?, self.block_number)?;
vm.insert_value((address + 3)?, self.transaction_index)?;
Ok(())
}

fn n_fields() -> usize {
3
4
}
}

Expand Down
38 changes: 27 additions & 11 deletions examples/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#[starknet::contract]
mod example {
use hdp_cairo::evm::storage::StorageTrait;
use hdp_cairo::{HDP, evm::storage::{StorageKey, StorageImpl}};
use hdp_cairo::{
HDP, evm::block_receipt::{BlockReceiptTrait, BlockReceiptKey, BlockReceiptImpl},
evm::block_tx::{BlockTxTrait, BlockTxKey, BlockTxImpl}
};

use hdp_cairo::debug::{print, print_array};
use core::fmt::{Display, Formatter, Error};

#[storage]
struct Storage {}
Expand All @@ -11,14 +16,25 @@ mod example {
assert!(
hdp
.evm
.storage_get_slot(
StorageKey {
chain_id: 11155111,
block_number: 6000000,
address: 0x75cec1db9dceb703200eaa6595f66885c962b920,
storage_slot: 0x1,
.block_receipt_get_cumulative_gas_used(
BlockReceiptKey {
chain_id: 11155111, block_number: 5382809, transaction_index: 217,
},
) == u256 { low: 0x1a4bd13, high: 0x0 },
);

print(1);

assert!(
hdp
.evm
.block_tx_get_nonce(
BlockTxKey {
chain_id: 11155111, block_number: 5382809, transaction_index: 217,
},
) == u256 { low: 0x12309ce54000, high: 0x0 },
)
) == u256 { low: 0x3, high: 0x0 },
);

print(2);
}
}
}
1 change: 1 addition & 0 deletions hdp_cairo/src/evm/block_receipt.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub impl BlockReceiptImpl of BlockReceiptTrait {
*self.dict.segment_index,
*self.dict.offset,
key.chain_id,
'block_receipt',
key.block_number,
key.transaction_index,
]
Expand Down
1 change: 1 addition & 0 deletions hdp_cairo/src/evm/block_tx.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub impl BlockTxImpl of BlockTxTrait {
*self.dict.segment_index,
*self.dict.offset,
key.chain_id,
'block_tx',
key.block_number,
key.transaction_index,
]
Expand Down
20 changes: 11 additions & 9 deletions src/memorizers/evm/memorizer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,34 @@ namespace EvmPackParams {
return (params=params, params_len=5);
}

const BLOCK_TX_PARAMS_LEN = 3;
const BLOCK_TX_PARAMS_LEN = 4;
func block_tx(chain_id: felt, block_number: felt, index: felt) -> (
params: felt*, params_len: felt
) {
alloc_locals;
local params: felt* = nondet %{ segments.add() %};
assert params[0] = chain_id;
assert params[1] = block_number;
assert params[2] = index;
assert params[1] = 'block_tx';
assert params[2] = block_number;
assert params[3] = index;

return (params=params, params_len=3);
return (params=params, params_len=4);
}

const BLOCK_RECEIPT_PARAMS_LEN = 3;
func block_receipt{poseidon_ptr: PoseidonBuiltin*}(
const BLOCK_RECEIPT_PARAMS_LEN = 4;
func block_receipt(
chain_id: felt, block_number: felt, index: felt
) -> (params: felt*, params_len: felt) {
alloc_locals;
local params: felt* = nondet %{ segments.add() %};
assert params[0] = chain_id;
assert params[1] = block_number;
assert params[2] = index;
assert params[1] = 'block_receipt';
assert params[2] = block_number;
assert params[3] = index;

return (params=params, params_len=3);
return (params=params, params_len=4);
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/src/evm/receipt_modules.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ mod receipts_get_status {
mod receipts_get_cumulative_gas_used {
use hdp_cairo::{
HDP, evm::block_receipt::{BlockReceiptTrait, BlockReceiptKey, BlockReceiptImpl},
evm::block_tx::{BlockTxTrait, BlockTxKey, BlockTxImpl}
};

#[storage]
Expand All @@ -82,5 +83,15 @@ mod receipts_get_cumulative_gas_used {
},
) == u256 { low: 0x1a4bd13, high: 0x0 },
);

assert!(
hdp
.evm
.block_tx_get_nonce(
BlockTxKey {
chain_id: 11155111, block_number: 5382809, transaction_index: 217,
},
) == u256 { low: 0x3, high: 0x0 },
);
}
}

0 comments on commit 02530ac

Please sign in to comment.