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 16 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

#### Added

- Cheatcode for getting block hash.


### Forge

#### Added
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 @@ -2,6 +2,7 @@ use crate::{
state::{CheatSpan, CheatStatus},
CheatnetState,
};
use cairo_vm::Felt252;
use conversions::serde::{deserialize::CairoDeserialize, serialize::CairoSerialize};
use starknet_api::core::ContractAddress;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -51,6 +52,7 @@ pub struct TxInfoMock {
pub struct BlockInfoMock {
pub block_number: CheatStatus<u64>,
pub block_timestamp: CheatStatus<u64>,
pub block_hash: CheatStatus<Felt252>,
pub sequencer_address: CheatStatus<ContractAddress>,
}

Expand Down Expand Up @@ -82,6 +84,7 @@ pub struct TxInfoMockOperations {
pub struct BlockInfoMockOperations {
pub block_number: Operation<u64>,
pub block_timestamp: Operation<u64>,
pub block_hash: Operation<Felt252>,
pub sequencer_address: Operation<ContractAddress>,
}

Expand All @@ -98,6 +101,7 @@ macro_rules! for_all_fields {

$macro!(block_info.block_number);
$macro!(block_info.block_timestamp);
$macro!(block_info.block_hash);
$macro!(block_info.sequencer_address);

$macro!(tx_info.version);
Expand Down
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
Loading
Loading