Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Add skeleton for the get_block_hash syscall.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jun 6, 2023
1 parent c11e3f0 commit 8cc92e0
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ mod TestContract {
starknet::syscalls::emit_event_syscall(keys.span(), data.span()).unwrap_syscall();
}

#[external]
fn test_get_block_hash(block_number_as_felt: felt252) -> felt252 {
let block_number = Into::<_, u64>::into(block_number_as_felt);
starknet::syscalls::get_block_hash_syscall(block_number).unwrap_syscall()
}

#[external]
fn test_get_execution_info(
// Expected block info.
Expand Down
3 changes: 3 additions & 0 deletions crates/blockifier/src/abi/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ pub const N_STEPS_RESOURCE: &str = "n_steps";
// Casm hash calculation-related constants.
pub const CAIRO0_ENTRY_POINT_STRUCT_SIZE: usize = 2;
pub const N_STEPS_PER_PEDERSEN: usize = 8;

// Operation-related constants.
pub const BLOCK_NUMBER_TO_BLOCK_HASH_ADDRESS: u64 = 0x1;
2 changes: 2 additions & 0 deletions crates/blockifier/src/execution/deprecated_syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub enum DeprecatedSyscallSelector {
DelegateL1Handler,
Deploy,
EmitEvent,
GetBlockHash,
GetBlockNumber,
GetBlockTimestamp,
GetCallerAddress,
Expand Down Expand Up @@ -70,6 +71,7 @@ impl TryFrom<StarkFelt> for DeprecatedSyscallSelector {
b"DelegateL1Handler" => Ok(Self::DelegateL1Handler),
b"Deploy" => Ok(Self::Deploy),
b"EmitEvent" => Ok(Self::EmitEvent),
b"GetBlockHash" => Ok(Self::GetBlockHash),
b"GetBlockNumber" => Ok(Self::GetBlockNumber),
b"GetBlockTimestamp" => Ok(Self::GetBlockTimestamp),
b"GetCallerAddress" => Ok(Self::GetCallerAddress),
Expand Down
9 changes: 5 additions & 4 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ use crate::execution::execution_utils::{
ReadOnlySegment, ReadOnlySegments,
};
use crate::execution::syscalls::{
call_contract, deploy, emit_event, get_execution_info, library_call, library_call_l1_handler,
replace_class, send_message_to_l1, storage_read, storage_write, StorageReadResponse,
StorageWriteResponse, SyscallRequest, SyscallRequestWrapper, SyscallResponse,
SyscallResponseWrapper, SyscallResult, SyscallSelector,
call_contract, deploy, emit_event, get_block_hash, get_execution_info, library_call,
library_call_l1_handler, replace_class, send_message_to_l1, storage_read, storage_write,
StorageReadResponse, StorageWriteResponse, SyscallRequest, SyscallRequestWrapper,
SyscallResponse, SyscallResponseWrapper, SyscallResult, SyscallSelector,
};
use crate::state::errors::StateError;
use crate::state::state_api::State;
Expand Down Expand Up @@ -174,6 +174,7 @@ impl<'a> SyscallHintProcessor<'a> {
SyscallSelector::CallContract => self.execute_syscall(vm, call_contract),
SyscallSelector::Deploy => self.execute_syscall(vm, deploy),
SyscallSelector::EmitEvent => self.execute_syscall(vm, emit_event),
SyscallSelector::GetBlockHash => self.execute_syscall(vm, get_block_hash),
SyscallSelector::GetExecutionInfo => self.execute_syscall(vm, get_execution_info),
SyscallSelector::LibraryCall => self.execute_syscall(vm, library_call),
SyscallSelector::LibraryCallL1Handler => {
Expand Down
39 changes: 39 additions & 0 deletions crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use self::hint_processor::{
read_call_params, read_calldata, read_felt_array, write_segment, SyscallExecutionError,
SyscallHintProcessor,
};
use crate::abi::constants;
use crate::execution::deprecated_syscalls::DeprecatedSyscallSelector;
use crate::execution::entry_point::{
CallEntryPoint, CallType, MessageToL1, OrderedEvent, OrderedL2ToL1Message,
Expand Down Expand Up @@ -272,6 +273,44 @@ pub fn emit_event(
Ok(EmitEventResponse {})
}

// GetBlockHash syscall.

#[derive(Debug, Eq, PartialEq)]
pub struct GetBlockHashRequest {
pub block_number: StarkFelt,
}

impl SyscallRequest for GetBlockHashRequest {
fn read(vm: &VirtualMachine, ptr: &mut Relocatable) -> SyscallResult<GetBlockHashRequest> {
let block_number = stark_felt_from_ptr(vm, ptr)?;
Ok(GetBlockHashRequest { block_number })
}
}

#[derive(Debug, Eq, PartialEq)]
pub struct GetBlockHashResponse {
pub block_hash: StarkFelt,
}

impl SyscallResponse for GetBlockHashResponse {
fn write(self, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
write_stark_felt(vm, ptr, self.block_hash)?;
Ok(())
}
}
pub fn get_block_hash(
request: GetBlockHashRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut SyscallHintProcessor<'_>,
) -> SyscallResult<GetBlockHashResponse> {
let key = StorageKey::try_from(request.block_number)?;
let contract_address =
ContractAddress::try_from(StarkFelt::from(constants::BLOCK_NUMBER_TO_BLOCK_HASH_ADDRESS))?;
let block_hash = syscall_handler.state.get_storage_at(contract_address, key)?;

Ok(GetBlockHashResponse { block_hash })
}

// GetExecutionInfo syscall.

type GetExecutionInfoRequest = EmptyRequest;
Expand Down
16 changes: 16 additions & 0 deletions crates/blockifier/src/execution/syscalls/syscalls_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ fn test_emit_event() {
);
}

#[test]
fn test_get_block_hash() {
let mut state = create_test_state();

let calldata = calldata![
stark_felt!(1800_u16) // Block number.
];
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_get_block_hash"),
calldata,
..trivial_external_entry_point()
};

let _call_info = entry_point_call.execute_directly(&mut state).unwrap();
}

#[test]
fn test_get_execution_info() {
let mut state = create_test_state();
Expand Down
5 changes: 5 additions & 0 deletions crates/blockifier/src/fee/os_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ fn os_resources() -> serde_json::Value {
"n_memory_holes": 0,
"n_steps": 19
},
"GetBlockHash": {
"builtin_instance_counter": {},
"n_memory_holes": 0,
"n_steps": 0
},
"GetBlockNumber": {
"builtin_instance_counter": {},
"n_memory_holes": 0,
Expand Down

0 comments on commit 8cc92e0

Please sign in to comment.