Skip to content

Commit

Permalink
Fix wrong caller address bug (#870)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #868

## Introduced changes

<!-- A brief description of the changes -->

- Correct caller address in constructor and called contract

## Breaking changes

<!-- List of all breaking changes, if applicable -->

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [x] Updated relevant documentation
- [x] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`
  • Loading branch information
MaksymilianDemitraszek authored Oct 12, 2023
1 parent e6a47f1 commit 38ec4a6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Forge

#### Fixed
- incorrect caller address bug

## [0.8.1] - 2023-10-12
### Forge

Expand Down
11 changes: 5 additions & 6 deletions crates/cheatnet/src/cheatcodes/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::constants as crate_constants;
use crate::constants::TEST_ADDRESS;
use crate::constants::{build_block_context, build_transaction_context};
use crate::state::BlockifierState;
use crate::CheatnetState;
Expand All @@ -13,8 +13,10 @@ use std::sync::Arc;
use blockifier::state::state_api::State;
use cairo_felt::Felt252;
use cairo_vm::vm::errors::hint_errors::HintError::CustomHint;
use conversions::StarknetConversions;
use num_traits::ToPrimitive;
use starknet_api::core::PatriciaKey;
use starknet_api::hash::StarkHash;
use starknet_api::patricia_key;

use crate::cheatcodes::EnhancedHintError;
use crate::execution::syscalls::execute_deployment;
Expand Down Expand Up @@ -59,17 +61,14 @@ pub fn deploy_at(
class_hash: *class_hash,
code_address: Some(contract_address),
storage_address: contract_address,
caller_address: crate_constants::TEST_ADDRESS
.to_string()
.to_contract_address(),
caller_address: ContractAddress(patricia_key!(TEST_ADDRESS)),
};

let calldata = Calldata(Arc::new(
calldata.to_vec().iter().map(felt_to_stark_felt).collect(),
));

let resources = &mut ExecutionResources::default();

let result = execute_deployment(
blockifier_state_raw,
resources,
Expand Down
6 changes: 5 additions & 1 deletion crates/cheatnet/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Result;
use std::collections::HashMap;
use std::sync::Arc;

use crate::constants::TEST_ADDRESS;
use crate::panic_data::try_extract_panic_data;
use crate::state::BlockifierState;
use crate::{
Expand All @@ -18,6 +19,8 @@ use blockifier::execution::{
use blockifier::state::errors::StateError;
use cairo_felt::Felt252;
use cairo_lang_runner::short_string::as_cairo_short_string;
use starknet_api::core::PatriciaKey;
use starknet_api::patricia_key;
use starknet_api::{
core::{ContractAddress, EntryPointSelector},
deprecated_contract_class::EntryPointType,
Expand Down Expand Up @@ -219,7 +222,8 @@ pub fn call_entry_point(
entry_point_selector,
calldata,
storage_address: *contract_address,
caller_address: ContractAddress::default(),
// test_contract address
caller_address: ContractAddress(patricia_key!(TEST_ADDRESS)),
call_type: CallType::Call,
initial_gas: u64::MAX,
};
Expand Down
88 changes: 88 additions & 0 deletions crates/forge/tests/integration/test_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,91 @@ fn test_inconsistent_syscall_pointers() {

assert_passed!(result);
}

#[test]
fn test_caller_address_in_called_contract() {
let test = test_case!(
indoc!(
r#"
use result::ResultTrait;
use array::ArrayTrait;
use option::OptionTrait;
use traits::TryInto;
use starknet::ContractAddress;
use starknet::Felt252TryIntoContractAddress;
use snforge_std::{ declare, ContractClassTrait, test_address };
#[starknet::interface]
trait IPrankChecker<TContractState> {
fn get_caller_address(ref self: TContractState) -> felt252;
}
#[starknet::interface]
trait IConstructorPrankChecker<TContractState> {
fn get_stored_caller_address(ref self: TContractState) -> ContractAddress;
}
#[test]
fn test_contract() {
let prank_checker = declare('PrankChecker');
let contract_address_prank_checker = prank_checker.deploy(@ArrayTrait::new()).unwrap();
let dispatcher_prank_checker = IPrankCheckerDispatcher { contract_address: contract_address_prank_checker };
assert(dispatcher_prank_checker.get_caller_address() == test_address().into(), 'Incorrect caller address');
let constructor_prank_checker = declare('ConstructorPrankChecker');
let contract_address_constructor_prank_checker = constructor_prank_checker.deploy(@ArrayTrait::new()).unwrap();
let dispatcher_constructor_prank_checker = IConstructorPrankCheckerDispatcher { contract_address: contract_address_constructor_prank_checker };
assert(dispatcher_constructor_prank_checker.get_stored_caller_address() == test_address(), 'Incorrect caller address');
}
"#
),
Contract::from_code_path(
"PrankChecker".to_string(),
Path::new("tests/data/contracts/prank_checker.cairo"),
)
.unwrap(),
Contract::new(
"ConstructorPrankChecker",
indoc!(
r#"
use starknet::ContractAddress;
#[starknet::interface]
trait IConstructorPrankChecker<TContractState> {
fn get_stored_caller_address(ref self: TContractState) -> ContractAddress;
}
#[starknet::contract]
mod ConstructorPrankChecker {
use starknet::ContractAddress;
#[storage]
struct Storage {
caller_address: ContractAddress,
}
#[constructor]
fn constructor(ref self: ContractState) {
let address = starknet::get_caller_address();
self.caller_address.write(address);
}
#[external(v0)]
impl IConstructorPrankChecker of super::IConstructorPrankChecker<ContractState> {
fn get_stored_caller_address(ref self: ContractState) -> ContractAddress {
self.caller_address.read()
}
}
}
"#
)
)
);
let result = run_test_case(&test);

assert_passed!(result);
}

0 comments on commit 38ec4a6

Please sign in to comment.