Skip to content

Commit 38ec4a6

Browse files
Fix wrong caller address bug (#870)
<!-- 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`
1 parent e6a47f1 commit 38ec4a6

File tree

4 files changed

+104
-7
lines changed

4 files changed

+104
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
10+
### Forge
11+
12+
#### Fixed
13+
- incorrect caller address bug
14+
915
## [0.8.1] - 2023-10-12
1016
### Forge
1117

crates/cheatnet/src/cheatcodes/deploy.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::constants as crate_constants;
1+
use crate::constants::TEST_ADDRESS;
22
use crate::constants::{build_block_context, build_transaction_context};
33
use crate::state::BlockifierState;
44
use crate::CheatnetState;
@@ -13,8 +13,10 @@ use std::sync::Arc;
1313
use blockifier::state::state_api::State;
1414
use cairo_felt::Felt252;
1515
use cairo_vm::vm::errors::hint_errors::HintError::CustomHint;
16-
use conversions::StarknetConversions;
1716
use num_traits::ToPrimitive;
17+
use starknet_api::core::PatriciaKey;
18+
use starknet_api::hash::StarkHash;
19+
use starknet_api::patricia_key;
1820

1921
use crate::cheatcodes::EnhancedHintError;
2022
use crate::execution::syscalls::execute_deployment;
@@ -59,17 +61,14 @@ pub fn deploy_at(
5961
class_hash: *class_hash,
6062
code_address: Some(contract_address),
6163
storage_address: contract_address,
62-
caller_address: crate_constants::TEST_ADDRESS
63-
.to_string()
64-
.to_contract_address(),
64+
caller_address: ContractAddress(patricia_key!(TEST_ADDRESS)),
6565
};
6666

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

7171
let resources = &mut ExecutionResources::default();
72-
7372
let result = execute_deployment(
7473
blockifier_state_raw,
7574
resources,

crates/cheatnet/src/rpc.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::Result;
22
use std::collections::HashMap;
33
use std::sync::Arc;
44

5+
use crate::constants::TEST_ADDRESS;
56
use crate::panic_data::try_extract_panic_data;
67
use crate::state::BlockifierState;
78
use crate::{
@@ -18,6 +19,8 @@ use blockifier::execution::{
1819
use blockifier::state::errors::StateError;
1920
use cairo_felt::Felt252;
2021
use cairo_lang_runner::short_string::as_cairo_short_string;
22+
use starknet_api::core::PatriciaKey;
23+
use starknet_api::patricia_key;
2124
use starknet_api::{
2225
core::{ContractAddress, EntryPointSelector},
2326
deprecated_contract_class::EntryPointType,
@@ -219,7 +222,8 @@ pub fn call_entry_point(
219222
entry_point_selector,
220223
calldata,
221224
storage_address: *contract_address,
222-
caller_address: ContractAddress::default(),
225+
// test_contract address
226+
caller_address: ContractAddress(patricia_key!(TEST_ADDRESS)),
223227
call_type: CallType::Call,
224228
initial_gas: u64::MAX,
225229
};

crates/forge/tests/integration/test_state.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,91 @@ fn test_inconsistent_syscall_pointers() {
654654

655655
assert_passed!(result);
656656
}
657+
658+
#[test]
659+
fn test_caller_address_in_called_contract() {
660+
let test = test_case!(
661+
indoc!(
662+
r#"
663+
use result::ResultTrait;
664+
use array::ArrayTrait;
665+
use option::OptionTrait;
666+
use traits::TryInto;
667+
use starknet::ContractAddress;
668+
use starknet::Felt252TryIntoContractAddress;
669+
use snforge_std::{ declare, ContractClassTrait, test_address };
670+
671+
#[starknet::interface]
672+
trait IPrankChecker<TContractState> {
673+
fn get_caller_address(ref self: TContractState) -> felt252;
674+
}
675+
676+
#[starknet::interface]
677+
trait IConstructorPrankChecker<TContractState> {
678+
fn get_stored_caller_address(ref self: TContractState) -> ContractAddress;
679+
}
680+
681+
#[test]
682+
fn test_contract() {
683+
let prank_checker = declare('PrankChecker');
684+
let contract_address_prank_checker = prank_checker.deploy(@ArrayTrait::new()).unwrap();
685+
let dispatcher_prank_checker = IPrankCheckerDispatcher { contract_address: contract_address_prank_checker };
686+
687+
assert(dispatcher_prank_checker.get_caller_address() == test_address().into(), 'Incorrect caller address');
688+
689+
690+
let constructor_prank_checker = declare('ConstructorPrankChecker');
691+
let contract_address_constructor_prank_checker = constructor_prank_checker.deploy(@ArrayTrait::new()).unwrap();
692+
let dispatcher_constructor_prank_checker = IConstructorPrankCheckerDispatcher { contract_address: contract_address_constructor_prank_checker };
693+
694+
assert(dispatcher_constructor_prank_checker.get_stored_caller_address() == test_address(), 'Incorrect caller address');
695+
696+
}
697+
"#
698+
),
699+
Contract::from_code_path(
700+
"PrankChecker".to_string(),
701+
Path::new("tests/data/contracts/prank_checker.cairo"),
702+
)
703+
.unwrap(),
704+
Contract::new(
705+
"ConstructorPrankChecker",
706+
indoc!(
707+
r#"
708+
use starknet::ContractAddress;
709+
710+
#[starknet::interface]
711+
trait IConstructorPrankChecker<TContractState> {
712+
fn get_stored_caller_address(ref self: TContractState) -> ContractAddress;
713+
}
714+
715+
#[starknet::contract]
716+
mod ConstructorPrankChecker {
717+
use starknet::ContractAddress;
718+
719+
#[storage]
720+
struct Storage {
721+
caller_address: ContractAddress,
722+
}
723+
724+
#[constructor]
725+
fn constructor(ref self: ContractState) {
726+
let address = starknet::get_caller_address();
727+
self.caller_address.write(address);
728+
}
729+
730+
#[external(v0)]
731+
impl IConstructorPrankChecker of super::IConstructorPrankChecker<ContractState> {
732+
fn get_stored_caller_address(ref self: ContractState) -> ContractAddress {
733+
self.caller_address.read()
734+
}
735+
}
736+
}
737+
"#
738+
)
739+
)
740+
);
741+
let result = run_test_case(&test);
742+
743+
assert_passed!(result);
744+
}

0 commit comments

Comments
 (0)