Skip to content

Commit c3e6519

Browse files
cptarturksew1
authored andcommitted
Support get_block_hash syscall
1 parent cdce605 commit c3e6519

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

crates/cheatnet/src/runtime_extensions/forge_runtime_extension/cheatcodes/cheat_block_hash.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ impl CheatnetState {
7878
self.cheat_block_hash(block_number, Operation::StopGlobal);
7979
}
8080

81-
#[expect(clippy::result_large_err)]
82-
pub fn get_block_hash_for_contract(
81+
pub fn get_cheated_block_hash_for_contract(
8382
&mut self,
8483
contract_address: ContractAddress,
8584
block_number: u64,
86-
syscall_handler: &mut SyscallHintProcessor,
87-
) -> SyscallResult<BlockHash> {
85+
) -> Option<BlockHash> {
8886
if let Some((cheat_span, block_hash)) = self
8987
.block_hash_contracts
9088
.get(&(contract_address, block_number))
@@ -110,17 +108,33 @@ impl CheatnetState {
110108
}
111109
CheatSpan::Indefinite => {}
112110
}
113-
return Ok(BlockHash(StarkHash::from(block_hash)));
111+
return Some(BlockHash(StarkHash::from(block_hash)));
114112
}
115113

116114
if let Some((block_hash, except)) = self.global_block_hash.get(&block_number)
117115
&& !except.contains(&contract_address)
118116
{
119-
return Ok(BlockHash(StarkHash::from(*block_hash)));
117+
return Some(BlockHash(StarkHash::from(*block_hash)));
120118
}
121119

122-
Ok(BlockHash(
123-
syscall_handler.base.get_block_hash(block_number)?,
124-
))
120+
None
121+
}
122+
123+
#[expect(clippy::result_large_err)]
124+
pub fn get_block_hash_for_contract(
125+
&mut self,
126+
contract_address: ContractAddress,
127+
block_number: u64,
128+
syscall_handler: &mut SyscallHintProcessor,
129+
) -> SyscallResult<BlockHash> {
130+
let cheated_block_hash =
131+
self.get_cheated_block_hash_for_contract(contract_address, block_number);
132+
if let Some(cheated_block_hash) = cheated_block_hash {
133+
Ok(cheated_block_hash)
134+
} else {
135+
Ok(BlockHash(
136+
syscall_handler.base.get_block_hash(block_number)?,
137+
))
138+
}
125139
}
126140
}

crates/cheatnet/src/runtime_extensions/native/native_syscall_handler.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,33 @@ impl StarknetSyscallHandler for &mut CheatableNativeSyscallHandler<'_> {
383383
block_number: u64,
384384
remaining_gas: &mut u64,
385385
) -> SyscallResult<Felt> {
386-
self.native_syscall_handler
387-
.get_block_hash(block_number, remaining_gas)
386+
self.pre_execute_syscall(
387+
remaining_gas,
388+
self.native_syscall_handler
389+
.gas_costs()
390+
.syscalls
391+
.get_block_hash
392+
.base_syscall_cost(),
393+
SyscallSelector::GetBlockHash,
394+
)?;
395+
396+
let block_hash = self.cheatnet_state.get_cheated_block_hash_for_contract(
397+
self.native_syscall_handler.base.call.storage_address,
398+
block_number,
399+
);
400+
401+
if let Some(block_hash) = block_hash {
402+
Ok(block_hash.0)
403+
} else {
404+
match self
405+
.native_syscall_handler
406+
.base
407+
.get_block_hash(block_number)
408+
{
409+
Ok(value) => Ok(value),
410+
Err(e) => Err(self.handle_error(remaining_gas, e)),
411+
}
412+
}
388413
}
389414

390415
fn get_execution_info(&mut self, remaining_gas: &mut u64) -> SyscallResult<ExecutionInfo> {

crates/forge/tests/e2e/running.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ fn simple_package_with_cheats() {
6363
[..]Finished[..]
6464
6565
66-
Collected 4 test(s) from simple_package_with_cheats package
66+
Collected 5 test(s) from simple_package_with_cheats package
6767
Running 0 test(s) from src/
68-
Running 4 test(s) from tests/
68+
Running 5 test(s) from tests/
6969
[PASS] simple_package_with_cheats_integrationtest::contract::call_and_invoke [..]
7070
[PASS] simple_package_with_cheats_integrationtest::contract::call_and_invoke_proxy [..]
7171
[PASS] simple_package_with_cheats_integrationtest::contract::call_and_invoke_library_call [..]
7272
[PASS] simple_package_with_cheats_integrationtest::contract::deploy_syscall [..]
73-
Tests: 4 passed, 0 failed, 0 ignored, 0 filtered out
73+
[PASS] simple_package_with_cheats_integrationtest::contract::block_hash [..]
74+
Tests: 5 passed, 0 failed, 0 ignored, 0 filtered out
7475
"},
7576
);
7677
}

0 commit comments

Comments
 (0)