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 5, 2023
1 parent c11e3f0 commit 9f442ae
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ mod TestContract {
starknet::syscalls::emit_event_syscall(keys.span(), data.span()).unwrap_syscall();
}

#[external]
fn test_get_block_hash(block_number: felt252) -> felt252 {
starknet::syscalls::get_block_hash_syscall(block_number).unwrap_syscall()
}

#[external]
fn test_get_execution_info(
// Expected block info.
Expand Down
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
16 changes: 12 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 @@ -66,6 +66,8 @@ pub enum SyscallExecutionError {
SyscallError { error_data: Vec<StarkFelt> },
#[error("Invalid address domain: {address_domain}.")]
InvalidAddressDomain { address_domain: StarkFelt },
#[error("Syscall not implemented.")]
Unimplemented,
}

// Needed for custom hint implementations (in our case, syscall hints) which must comply with the
Expand Down Expand Up @@ -174,6 +176,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 Expand Up @@ -309,6 +312,11 @@ impl<'a> SyscallHintProcessor<'a> {
Ok(tx_info_start_ptr)
}

// TODO(Arni, 6/6/2023): Implement this logic.
pub fn get_block_hash(&mut self, _block_number: StarkFelt) -> SyscallResult<StarkFelt> {
Err(SyscallExecutionError::UnImplemented)
}

pub fn get_contract_storage_at(
&mut self,
key: StorageKey,
Expand Down
35 changes: 35 additions & 0 deletions crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,41 @@ 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 block_hash = syscall_handler.get_block_hash(request.block_number)?;

Ok(GetBlockHashResponse { block_hash })
}

// GetExecutionInfo syscall.

type GetExecutionInfoRequest = EmptyRequest;
Expand Down
18 changes: 18 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,24 @@ 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()
};

// TODO(spapini): Fix the "UNEXPECTED ERROR".
// TODO(Arni): Remove the "Unimplemented" error.
entry_point_call.execute_directly(&mut state).unwrap_err();
}

#[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 9f442ae

Please sign in to comment.