Skip to content
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
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
34 changes: 24 additions & 10 deletions examples/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -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 {}
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion hdp_cairo/src/debug.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod printer;
pub use printer::{print, print_array};
pub use printer::{print, print_array};
8 changes: 2 additions & 6 deletions hdp_cairo/src/debug/printer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ pub fn print<T, +Display<T>, +Drop<T>>(value: T) {
}

pub fn print_array(array: Array<felt252>) {
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();
}
}
3 changes: 3 additions & 0 deletions hdp_cairo/src/evm/block_receipt.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
]
Expand Down
3 changes: 3 additions & 0 deletions hdp_cairo/src/evm/block_tx.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
]
Expand Down
26 changes: 15 additions & 11 deletions src/memorizers/evm/memorizer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,36 @@ 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
) {
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_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);
}
}

Expand Down
13 changes: 12 additions & 1 deletion tests/src/evm/receipt_modules.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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 },
);
}
}
2 changes: 1 addition & 1 deletion tests/src/evm/receipt_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod evm;
pub mod starknet;
pub mod utils;
pub mod utils;
2 changes: 1 addition & 1 deletion tests/src/utils.cairo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod debug;
pub mod debug;
10 changes: 6 additions & 4 deletions tests/src/utils/debug.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ mod test_debug_print {

impl PointDisplay of Display<Point> {
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(())
}
Expand All @@ -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]);


}
}