Skip to content

Commit f429564

Browse files
committed
Merge branch 'rust-hints' of github.com:HerodotusDev/hdp-cairo into optimize/fetcher
2 parents 86c1d8e + 5de3f1d commit f429564

File tree

8 files changed

+77
-35
lines changed

8 files changed

+77
-35
lines changed

crates/types/src/keys/evm/receipt.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,37 @@ use crate::cairo::traits::CairoType;
1313
#[derive(Debug, Clone)]
1414
pub struct CairoKey {
1515
chain_id: Felt252,
16+
domain: Felt252,
1617
block_number: Felt252,
1718
transaction_index: Felt252,
1819
}
1920

2021
impl CairoKey {
2122
pub fn hash(&self) -> Felt252 {
22-
poseidon_hash_many(&[self.chain_id, self.block_number, self.transaction_index])
23+
poseidon_hash_many(&[self.chain_id, self.domain, self.block_number, self.transaction_index])
2324
}
2425
}
2526

2627
impl CairoType for CairoKey {
2728
fn from_memory(vm: &VirtualMachine, ptr: Relocatable) -> Result<Self, MemoryError> {
2829
Ok(Self {
2930
chain_id: *vm.get_integer((ptr + 0)?)?,
30-
block_number: *vm.get_integer((ptr + 1)?)?,
31-
transaction_index: *vm.get_integer((ptr + 2)?)?,
31+
domain: *vm.get_integer((ptr + 1)?)?,
32+
block_number: *vm.get_integer((ptr + 2)?)?,
33+
transaction_index: *vm.get_integer((ptr + 3)?)?,
3234
})
3335
}
3436

3537
fn to_memory(&self, vm: &mut VirtualMachine, address: Relocatable) -> Result<(), MemoryError> {
3638
vm.insert_value((address + 0)?, self.chain_id)?;
37-
vm.insert_value((address + 1)?, self.block_number)?;
38-
vm.insert_value((address + 2)?, self.transaction_index)?;
39+
vm.insert_value((address + 1)?, self.domain)?;
40+
vm.insert_value((address + 2)?, self.block_number)?;
41+
vm.insert_value((address + 3)?, self.transaction_index)?;
3942
Ok(())
4043
}
4144

4245
fn n_fields() -> usize {
43-
3
46+
4
4447
}
4548
}
4649

crates/types/src/keys/evm/transaction.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,38 @@ use crate::cairo::traits::CairoType;
1313
#[derive(Debug, Clone)]
1414
pub struct CairoKey {
1515
chain_id: Felt252,
16+
domain: Felt252,
1617
block_number: Felt252,
1718
transaction_index: Felt252,
1819
}
1920

2021
impl CairoKey {
2122
pub fn hash(&self) -> Felt252 {
22-
poseidon_hash_many(&[self.chain_id, self.block_number, self.transaction_index])
23+
poseidon_hash_many(&[self.chain_id, self.domain, self.block_number, self.transaction_index])
2324
}
2425
}
2526

2627
impl CairoType for CairoKey {
2728
fn from_memory(vm: &VirtualMachine, ptr: Relocatable) -> Result<Self, MemoryError> {
29+
println!("tx key from memory: {:?}", ptr);
2830
Ok(Self {
2931
chain_id: *vm.get_integer((ptr + 0)?)?,
30-
block_number: *vm.get_integer((ptr + 1)?)?,
31-
transaction_index: *vm.get_integer((ptr + 2)?)?,
32+
domain: *vm.get_integer((ptr + 1)?)?,
33+
block_number: *vm.get_integer((ptr + 2)?)?,
34+
transaction_index: *vm.get_integer((ptr + 3)?)?,
3235
})
3336
}
3437

3538
fn to_memory(&self, vm: &mut VirtualMachine, address: Relocatable) -> Result<(), MemoryError> {
3639
vm.insert_value((address + 0)?, self.chain_id)?;
37-
vm.insert_value((address + 1)?, self.block_number)?;
38-
vm.insert_value((address + 2)?, self.transaction_index)?;
40+
vm.insert_value((address + 1)?, self.domain)?;
41+
vm.insert_value((address + 2)?, self.block_number)?;
42+
vm.insert_value((address + 3)?, self.transaction_index)?;
3943
Ok(())
4044
}
4145

4246
fn n_fields() -> usize {
43-
3
47+
4
4448
}
4549
}
4650

examples/src/lib.cairo

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#[starknet::contract]
22
mod example {
3-
use hdp_cairo::evm::storage::StorageTrait;
4-
use hdp_cairo::{HDP, evm::storage::{StorageKey, StorageImpl}};
3+
use hdp_cairo::{
4+
HDP, evm::block_receipt::{BlockReceiptTrait, BlockReceiptKey, BlockReceiptImpl},
5+
evm::block_tx::{BlockTxTrait, BlockTxKey, BlockTxImpl},
6+
};
7+
use hdp_cairo::debug::print;
58

69
#[storage]
710
struct Storage {}
@@ -11,14 +14,25 @@ mod example {
1114
assert!(
1215
hdp
1316
.evm
14-
.storage_get_slot(
15-
StorageKey {
16-
chain_id: 11155111,
17-
block_number: 6000000,
18-
address: 0x75cec1db9dceb703200eaa6595f66885c962b920,
19-
storage_slot: 0x1,
17+
.block_receipt_get_cumulative_gas_used(
18+
BlockReceiptKey {
19+
chain_id: 11155111, block_number: 5382809, transaction_index: 217,
2020
},
21-
) == u256 { low: 0x12309ce54000, high: 0x0 },
22-
)
21+
) == u256 { low: 0x1a4bd13, high: 0x0 },
22+
);
23+
24+
print(1);
25+
26+
assert!(
27+
hdp
28+
.evm
29+
.block_tx_get_nonce(
30+
BlockTxKey {
31+
chain_id: 11155111, block_number: 5382809, transaction_index: 217,
32+
},
33+
) == u256 { low: 0x3, high: 0x0 },
34+
);
35+
36+
print(2);
2337
}
2438
}

hdp_cairo/src/evm/block_receipt.cairo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const BLOCK_RECEIPT_GET_CUMULATIVE_GAS_USED: felt252 = 1;
99
const BLOCK_RECEIPT_GET_BLOOM: felt252 = 2;
1010
const BLOCK_RECEIPT_GET_LOGS: felt252 = 3;
1111

12+
const BLOCK_RECEIPT_LABEL: felt252 = 'block_receipt';
13+
1214
#[derive(Serde, Drop)]
1315
pub struct BlockReceiptKey {
1416
pub chain_id: felt252,
@@ -39,6 +41,7 @@ pub impl BlockReceiptImpl of BlockReceiptTrait {
3941
*self.dict.segment_index,
4042
*self.dict.offset,
4143
key.chain_id,
44+
BLOCK_RECEIPT_LABEL,
4245
key.block_number,
4346
key.transaction_index,
4447
]

hdp_cairo/src/evm/block_tx.cairo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const BLOCK_TX_GET_TX_TYPE: felt252 = 15;
2323
const BLOCK_TX_GET_SENDER: felt252 = 16;
2424
const BLOCK_TX_GET_HASH: felt252 = 17;
2525

26+
const BLOCK_TX_LABEL: felt252 = 'block_tx';
27+
2628
#[derive(Serde, Drop)]
2729
pub struct BlockTxKey {
2830
pub chain_id: felt252,
@@ -109,6 +111,7 @@ pub impl BlockTxImpl of BlockTxTrait {
109111
*self.dict.segment_index,
110112
*self.dict.offset,
111113
key.chain_id,
114+
BLOCK_TX_LABEL,
112115
key.block_number,
113116
key.transaction_index,
114117
]

src/memorizers/evm/memorizer.cairo

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,36 @@ namespace EvmPackParams {
4848
return (params=params, params_len=5);
4949
}
5050
51-
const BLOCK_TX_PARAMS_LEN = 3;
51+
const BLOCK_TX_LABEL = 'block_tx';
52+
const BLOCK_TX_PARAMS_LEN = 4;
5253
func block_tx(chain_id: felt, block_number: felt, index: felt) -> (
5354
params: felt*, params_len: felt
5455
) {
5556
alloc_locals;
5657
5758
local params: felt* = nondet %{ segments.add() %};
5859
assert params[0] = chain_id;
59-
assert params[1] = block_number;
60-
assert params[2] = index;
60+
assert params[1] = BLOCK_TX_LABEL;
61+
assert params[2] = block_number;
62+
assert params[3] = index;
6163
62-
return (params=params, params_len=3);
64+
return (params=params, params_len=4);
6365
}
6466
65-
const BLOCK_RECEIPT_PARAMS_LEN = 3;
66-
func block_receipt{poseidon_ptr: PoseidonBuiltin*}(
67-
chain_id: felt, block_number: felt, index: felt
68-
) -> (params: felt*, params_len: felt) {
67+
const BLOCK_RECEIPT_LABEL = 'block_receipt';
68+
const BLOCK_RECEIPT_PARAMS_LEN = 4;
69+
func block_receipt(chain_id: felt, block_number: felt, index: felt) -> (
70+
params: felt*, params_len: felt
71+
) {
6972
alloc_locals;
7073
7174
local params: felt* = nondet %{ segments.add() %};
7275
assert params[0] = chain_id;
73-
assert params[1] = block_number;
74-
assert params[2] = index;
76+
assert params[1] = BLOCK_RECEIPT_LABEL;
77+
assert params[2] = block_number;
78+
assert params[3] = index;
7579
76-
return (params=params, params_len=3);
80+
return (params=params, params_len=4);
7781
}
7882
}
7983

tests/src/evm/receipt_modules.cairo

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ mod receipts_get_status {
6363
}
6464

6565
#[starknet::contract]
66-
mod receipts_get_cumulative_gas_used {
66+
mod receipts_get_and_tx_get {
6767
use hdp_cairo::{
6868
HDP, evm::block_receipt::{BlockReceiptTrait, BlockReceiptKey, BlockReceiptImpl},
69+
evm::block_tx::{BlockTxTrait, BlockTxKey, BlockTxImpl},
6970
};
7071

7172
#[storage]
@@ -82,5 +83,15 @@ mod receipts_get_cumulative_gas_used {
8283
},
8384
) == u256 { low: 0x1a4bd13, high: 0x0 },
8485
);
86+
87+
assert!(
88+
hdp
89+
.evm
90+
.block_tx_get_nonce(
91+
BlockTxKey {
92+
chain_id: 11155111, block_number: 5382809, transaction_index: 217,
93+
},
94+
) == u256 { low: 0x3, high: 0x0 },
95+
);
8596
}
8697
}

tests/src/evm/receipt_modules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async fn test_modules_receipt_get_status() {
1212
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
1313
async fn test_modules_receipt_get_cumulative_gas_used() {
1414
run(serde_json::from_slice(include_bytes!(
15-
"../../../target/dev/modules_receipts_get_cumulative_gas_used.compiled_contract_class.json"
15+
"../../../target/dev/modules_receipts_get_and_tx_get.compiled_contract_class.json"
1616
))
1717
.unwrap())
1818
.await

0 commit comments

Comments
 (0)