Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cheatcode for getting hash of the block #2648

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4298e64
cheatcode block_hash
PedroRosalba Nov 6, 2024
7243cfd
fix: names
PedroRosalba Nov 6, 2024
8abeeca
Merge remote-tracking branch 'upstream/master'
PedroRosalba Nov 7, 2024
9c95d80
fix: types
PedroRosalba Nov 8, 2024
26f10c4
still in progress
PedroRosalba Nov 8, 2024
df1a1ee
Merge branch 'master' into master
kkawula Nov 12, 2024
59c5d76
minor fix
Nov 12, 2024
65b4839
minor fix: undo create book
Nov 12, 2024
9fee226
Merge remote-tracking branch 'origin/master'
Nov 12, 2024
845a2d9
minor fix
Nov 12, 2024
2b1690f
ran scarbfmt.sh
Nov 12, 2024
2fe2baa
Merge branch 'master' into master
kkawula Nov 12, 2024
a450fa1
added changelog and fix imports Felt252
Nov 12, 2024
7bc764f
added module to lib and fmt code
Nov 13, 2024
b35e0dd
Merge branch 'master' into master
kkawula Nov 14, 2024
9b77098
fix: adding import
PedroRosalba Nov 17, 2024
cf6e429
Ignore .cargo/ and .rustup/ directories
PedroRosalba Dec 1, 2024
ab96aca
updates
PedroRosalba Dec 1, 2024
8b75ef5
updating. should I get the contractAddress from the cheated block num…
PedroRosalba Dec 4, 2024
40caccc
Merge branch 'foundry-rs:master' into master
PedroRosalba Dec 13, 2024
634bb44
updating
PedroRosalba Dec 13, 2024
cad9900
Merge branch 'master' into master
kkawula Dec 20, 2024
cb477c1
Apply suggestions from code review
kkawula Dec 20, 2024
2101152
Merge branch 'master' into master
kkawula Dec 20, 2024
e7b95f4
Merge branch 'master' into master
PedroRosalba Jan 8, 2025
9d48bb0
updating
PedroRosalba Jan 8, 2025
1ca9f84
Merge branch 'master' into master
kkawula Jan 9, 2025
50759d0
Merge branch 'master' into master
kkawula Jan 10, 2025
49096fc
Merge branch 'master' into master
kkawula Jan 17, 2025
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Scarb.lock
snfoundry_trace/
coverage/
**/.forge_e2e_cache/

2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.7.0
scarb 2.7.0
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Forge

#### Added

- Cheatcode for getting block hash.

## [0.36.0] - 2025-01-15

### Forge
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
use crate::abi::constants;
use self::hint_processor::{
create_retdata_segment,
execute_inner_call,
execute_library_call,
felt_to_bool,
read_call_params,
read_calldata,
read_felt_array,
write_segment,
EmitEventError,
SyscallExecutionError,
SyscallHintProcessor,
BLOCK_NUMBER_OUT_OF_RANGE_ERROR,
};

use crate::runtime_extensions::call_to_blockifier_runtime_extension::execution::entry_point::execute_constructor_entry_point;
use crate::runtime_extensions::call_to_blockifier_runtime_extension::CheatnetState;
use blockifier::execution::syscalls::hint_processor::SyscallHintProcessor;
use blockifier::execution::syscalls::{
DeployRequest, DeployResponse, LibraryCallRequest, SyscallResponse, SyscallResult,
DeployRequest, DeployResponse, GetBlockHashRequest, GetBlockHashResponse, LibraryCallRequest, SyscallResponse, SyscallResult
};
use blockifier::execution::{call_info::CallInfo, entry_point::ConstructorContext};
use blockifier::execution::{
Expand Down Expand Up @@ -57,6 +73,45 @@ pub fn get_execution_info_syscall(
})
}

pub fn get_block_hash_syscall(
request: GetBlockHashRequest,
vm: &mut VirtualMachine,
syscall_handler: &mut SyscallHintProcessor<'_>,
cheatnet_state: &mut CheatnetState,
_remaining_gas: &mut u64,
) -> SyscallResult<GetBlockHashResponse> {

// let execution_info_ptr = syscall_handler.get_or_allocate_execution_info_segment(vm)?;

// let cheated_data = cheatnet_state.get_cheated_data(syscall_handler.storage_address());

// let ptr_cheated_block_info = get_cheated_block_info_ptr(vm, execution_info_ptr, &cheated_data);

let cheated_block_number = cheatnet_state.block_info.block_number;

let requested_block_number = request.block_number;

if cheated_block_number < constants::STORED_BLOCK_HASH_BUFFER
|| requested_block_number > cheated_block_number - constants::STORED_BLOCK_HASH_BUFFER
{
let out_of_range_error =
Felt::from_hex(BLOCK_NUMBER_OUT_OF_RANGE_ERROR).map_err(SyscallExecutionError::from)?;
return Err(SyscallExecutionError::SyscallError { error_data: vec![out_of_range_error] });
}

let key = StorageKey::try_from(Felt::from(requested_block_number))?;

let block_hash_contract_address =
ContractAddress::try_from(Felt::from(constants::BLOCK_HASH_CONTRACT_ADDRESS))?;

//should I get the contractAddress from the cheated block number? how do I do that?

let cheated_block_hash = BlockHash(syscall_handler.state.get_storage_at(block_hash_contract_address, key)?);

Ok(GetBlockHashResponse { block_hash: cheated_block_hash })

}

// blockifier/src/execution/syscalls/mod.rs:222 (deploy_syscall)
pub fn deploy_syscall(
request: DeployRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ impl<'a> ExtensionLogic for CheatableStarknetRuntimeExtension<'a> {
)
.map(|()| SyscallHandlingResult::Handled),
_ => Ok(SyscallHandlingResult::Forwarded),
SyscallSelector::GetBlockHash => self
.execute_syscall(
syscall_handler,
vm,
cheated_syscalls::get_block_hash_syscall,
SyscallSelector::Deploy,
)
.map(|()| SyscallHandlingResult::Handled),
_ => Ok(SyscallHandlingResult::Forwarded),
Comment on lines 80 to +89
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ => Ok(SyscallHandlingResult::Forwarded),
SyscallSelector::GetBlockHash => self
.execute_syscall(
syscall_handler,
vm,
cheated_syscalls::get_block_hash_syscall,
SyscallSelector::Deploy,
)
.map(|()| SyscallHandlingResult::Handled),
_ => Ok(SyscallHandlingResult::Forwarded),
SyscallSelector::GetBlockHash => self
.execute_syscall(
syscall_handler,
vm,
cheated_syscalls::get_block_hash_syscall,
SyscallSelector::Deploy,
)
.map(|()| SyscallHandlingResult::Handled),
_ => Ok(SyscallHandlingResult::Forwarded),

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use super::cheat_execution_info::{
BlockInfoMockOperations, CheatArguments, ExecutionInfoMockOperations, Operation,
};
use crate::state::CheatSpan;
use crate::CheatnetState;
use cairo_vm::Felt252;
use starknet_api::core::ContractAddress;

impl CheatnetState {
pub fn cheat_block_hash(
&mut self,
contract_address: ContractAddress,
block_hash: Felt252,
span: CheatSpan,
) {
self.cheat_execution_info(ExecutionInfoMockOperations {
block_info: BlockInfoMockOperations {
block_hash: Operation::Start(CheatArguments {
value: block_hash,
span,
target: contract_address,
}),
..Default::default()
},
..Default::default()
});
}

pub fn start_cheat_block_hash_global(&mut self, block_hash: Felt252) {
self.cheat_execution_info(ExecutionInfoMockOperations {
block_info: BlockInfoMockOperations {
block_hash: Operation::StartGlobal(block_hash),
..Default::default()
},
..Default::default()
});
}

pub fn start_cheat_block_hash(
&mut self,
contract_address: ContractAddress,
block_hash: Felt252,
) {
self.cheat_block_hash(contract_address, block_hash, CheatSpan::Indefinite);
}

pub fn stop_cheat_block_hash(&mut self, contract_address: ContractAddress) {
self.cheat_execution_info(ExecutionInfoMockOperations {
block_info: BlockInfoMockOperations {
block_hash: Operation::Stop(contract_address),
..Default::default()
},
..Default::default()
});
}

pub fn stop_cheat_block_hash_global(&mut self) {
self.cheat_execution_info(ExecutionInfoMockOperations {
block_info: BlockInfoMockOperations {
block_hash: Operation::StopGlobal,
..Default::default()
},
..Default::default()
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,4 @@ impl CheatnetState {

for_all_fields!(decrement!);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cairo_vm::vm::errors::hint_errors::HintError;
use runtime::EnhancedHintError;
use starknet_types_core::felt::Felt;

pub mod cheat_block_hash;
pub mod cheat_block_number;
pub mod cheat_block_timestamp;
pub mod cheat_caller_address;
Expand Down
8 changes: 8 additions & 0 deletions crates/cheatnet/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ impl CheatnetState {
ContractAddressSalt(Felt::from(self.deploy_salt_base))
}

// #[must_use]
// pub fn get_cheated_block_hash(&mut self, address: ContractAddress) -> Option<Felt> {
// self.get_cheated_execution_info_for_contract(address)
// .block_info
// .block_hash
// .as_value()
// } not sure about impl this one

#[must_use]
pub fn get_cheated_block_number(&mut self, address: ContractAddress) -> Option<u64> {
self.get_cheated_execution_info_for_contract(address)
Expand Down
Loading
Loading