diff --git a/crates/types/src/keys/evm/receipt.rs b/crates/types/src/keys/evm/receipt.rs index ad8d2368..e913a268 100644 --- a/crates/types/src/keys/evm/receipt.rs +++ b/crates/types/src/keys/evm/receipt.rs @@ -13,13 +13,14 @@ 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]) } } @@ -27,20 +28,22 @@ impl CairoType for CairoKey { fn from_memory(vm: &VirtualMachine, ptr: Relocatable) -> Result { 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 } } diff --git a/crates/types/src/keys/evm/transaction.rs b/crates/types/src/keys/evm/transaction.rs index ad8d2368..df0e55de 100644 --- a/crates/types/src/keys/evm/transaction.rs +++ b/crates/types/src/keys/evm/transaction.rs @@ -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 { + 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 } } diff --git a/examples/src/lib.cairo b/examples/src/lib.cairo index d69fd0a9..3146e697 100644 --- a/examples/src/lib.cairo +++ b/examples/src/lib.cairo @@ -1,7 +1,10 @@ #[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; #[storage] struct Storage {} @@ -11,14 +14,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: 0x12309ce54000, high: 0x0 }, - ) + ) == 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: 0x3, high: 0x0 }, + ); + + print(2); } } diff --git a/hdp_cairo/src/debug.cairo b/hdp_cairo/src/debug.cairo index d414462a..32d48df1 100644 --- a/hdp_cairo/src/debug.cairo +++ b/hdp_cairo/src/debug.cairo @@ -1,2 +1,2 @@ mod printer; -pub use printer::{print, print_array}; \ No newline at end of file +pub use printer::{print, print_array}; diff --git a/hdp_cairo/src/debug/printer.cairo b/hdp_cairo/src/debug/printer.cairo index 3af6cbad..c022ba0f 100644 --- a/hdp_cairo/src/debug/printer.cairo +++ b/hdp_cairo/src/debug/printer.cairo @@ -16,10 +16,6 @@ pub fn print, +Drop>(value: T) { } pub fn print_array(array: Array) { - call_contract_syscall( - DEBUG_CONTRACT_ADDRESS.try_into().unwrap(), - PRINT_ARRAY, - array.span() - ) + call_contract_syscall(DEBUG_CONTRACT_ADDRESS.try_into().unwrap(), PRINT_ARRAY, array.span()) .unwrap_syscall(); -} \ No newline at end of file +} diff --git a/hdp_cairo/src/evm/block_receipt.cairo b/hdp_cairo/src/evm/block_receipt.cairo index 60d50b1c..7877adb6 100644 --- a/hdp_cairo/src/evm/block_receipt.cairo +++ b/hdp_cairo/src/evm/block_receipt.cairo @@ -9,6 +9,8 @@ const BLOCK_RECEIPT_GET_CUMULATIVE_GAS_USED: felt252 = 1; const BLOCK_RECEIPT_GET_BLOOM: felt252 = 2; const BLOCK_RECEIPT_GET_LOGS: felt252 = 3; +const BLOCK_RECEIPT_LABEL: felt252 = 'block_receipt'; + #[derive(Serde, Drop)] pub struct BlockReceiptKey { pub chain_id: felt252, @@ -39,6 +41,7 @@ pub impl BlockReceiptImpl of BlockReceiptTrait { *self.dict.segment_index, *self.dict.offset, key.chain_id, + BLOCK_RECEIPT_LABEL, key.block_number, key.transaction_index, ] diff --git a/hdp_cairo/src/evm/block_tx.cairo b/hdp_cairo/src/evm/block_tx.cairo index 5fae232a..10a31cfc 100644 --- a/hdp_cairo/src/evm/block_tx.cairo +++ b/hdp_cairo/src/evm/block_tx.cairo @@ -23,6 +23,8 @@ const BLOCK_TX_GET_TX_TYPE: felt252 = 15; const BLOCK_TX_GET_SENDER: felt252 = 16; const BLOCK_TX_GET_HASH: felt252 = 17; +const BLOCK_TX_LABEL: felt252 = 'block_tx'; + #[derive(Serde, Drop)] pub struct BlockTxKey { pub chain_id: felt252, @@ -109,6 +111,7 @@ pub impl BlockTxImpl of BlockTxTrait { *self.dict.segment_index, *self.dict.offset, key.chain_id, + BLOCK_TX_LABEL, key.block_number, key.transaction_index, ] diff --git a/src/memorizers/evm/memorizer.cairo b/src/memorizers/evm/memorizer.cairo index 2df7a741..2a4e976a 100644 --- a/src/memorizers/evm/memorizer.cairo +++ b/src/memorizers/evm/memorizer.cairo @@ -48,7 +48,8 @@ namespace EvmPackParams { return (params=params, params_len=5); } - const BLOCK_TX_PARAMS_LEN = 3; + const BLOCK_TX_LABEL = 'block_tx'; + const BLOCK_TX_PARAMS_LEN = 4; func block_tx(chain_id: felt, block_number: felt, index: felt) -> ( params: felt*, params_len: felt ) { @@ -56,24 +57,27 @@ namespace EvmPackParams { 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_LABEL; + 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*}( - chain_id: felt, block_number: felt, index: felt - ) -> (params: felt*, params_len: felt) { + const BLOCK_RECEIPT_LABEL = 'block_receipt'; + 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_LABEL; + assert params[2] = block_number; + assert params[3] = index; - return (params=params, params_len=3); + return (params=params, params_len=4); } } diff --git a/tests/src/evm/receipt_modules.cairo b/tests/src/evm/receipt_modules.cairo index 15add4dc..4eae99fc 100644 --- a/tests/src/evm/receipt_modules.cairo +++ b/tests/src/evm/receipt_modules.cairo @@ -63,9 +63,10 @@ mod receipts_get_status { } #[starknet::contract] -mod receipts_get_cumulative_gas_used { +mod receipts_get_and_tx_get { use hdp_cairo::{ HDP, evm::block_receipt::{BlockReceiptTrait, BlockReceiptKey, BlockReceiptImpl}, + evm::block_tx::{BlockTxTrait, BlockTxKey, BlockTxImpl}, }; #[storage] @@ -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 }, + ); } } diff --git a/tests/src/evm/receipt_modules.rs b/tests/src/evm/receipt_modules.rs index f9b5a3cb..ce504e42 100644 --- a/tests/src/evm/receipt_modules.rs +++ b/tests/src/evm/receipt_modules.rs @@ -12,7 +12,7 @@ async fn test_modules_receipt_get_status() { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_modules_receipt_get_cumulative_gas_used() { run(serde_json::from_slice(include_bytes!( - "../../../target/dev/modules_receipts_get_cumulative_gas_used.compiled_contract_class.json" + "../../../target/dev/modules_receipts_get_and_tx_get.compiled_contract_class.json" )) .unwrap()) .await diff --git a/tests/src/lib.cairo b/tests/src/lib.cairo index c1762f74..99fa67e9 100644 --- a/tests/src/lib.cairo +++ b/tests/src/lib.cairo @@ -1,3 +1,3 @@ pub mod evm; pub mod starknet; -pub mod utils; \ No newline at end of file +pub mod utils; diff --git a/tests/src/utils.cairo b/tests/src/utils.cairo index 477cc78a..2f365233 100644 --- a/tests/src/utils.cairo +++ b/tests/src/utils.cairo @@ -1 +1 @@ -pub mod debug; \ No newline at end of file +pub mod debug; diff --git a/tests/src/utils/debug.cairo b/tests/src/utils/debug.cairo index c482f1dd..b53c3dcd 100644 --- a/tests/src/utils/debug.cairo +++ b/tests/src/utils/debug.cairo @@ -15,7 +15,11 @@ mod test_debug_print { impl PointDisplay of Display { fn fmt(self: @Point, ref f: Formatter) -> Result<(), Error> { - let str: ByteArray = format!("PointThatIAmMakingQuiteABitLongerToEnsureWeHaveMoreFelts ({}, {})", *self.x, *self.y); + let str: ByteArray = format!( + "PointThatIAmMakingQuiteABitLongerToEnsureWeHaveMoreFelts ({}, {})", + *self.x, + *self.y, + ); f.buffer.append(@str); Result::Ok(()) } @@ -24,11 +28,9 @@ mod test_debug_print { #[external(v0)] pub fn main(ref self: ContractState, hdp: HDP) { let p = Point { x: 1, y: 3 }; - + print(p); print(1); print_array(array![1, 2, 3]); - - } }