open-dust / cairo-foundry

Foundry like framework for starknet contracts
MIT License
77 stars 32 forks source link

Alter the current way `mock_call` injects a felt (mock_ret_value) inside a HashMap so that the `mocked_value` can be injected inside `ap` when the mock is called #102

Open Eikix opened 1 year ago

Eikix commented 1 year ago

Currently this is how mock works: get the felt from mock_ret_value, and inject it inside a HashMap<function_to_mock_offset, mock_ret_value>. Later in the execution, when this function is called, the hook will intercept the call and inject mock_ret_value at the current ap location!

Now, we need to store inside the HashMap the expanded array from #101


pub fn mock_call(
    vm: &mut VirtualMachine,
    exec_scopes: &mut ExecutionScopes,
    ids_data: &HashMap<String, HintReference>,
    ap_tracking: &ApTracking,
    _constants: &HashMap<String, BigInt>,
) -> Result<(), VirtualMachineError> {
    let func_to_mock = get_ptr_from_var_name("func_to_mock", vm, ids_data, ap_tracking)?;
    let mock_ret_value = get_integer_from_var_name("mock_ret_value", vm, ids_data, ap_tracking)?;

    let mocks = exec_scopes
        .get_any_boxed_mut(MOCK_CALL_KEY)?
        .downcast_mut::<HashMap<usize, BigInt>>()
        .ok_or_else(|| VirtualMachineError::VariableNotInScopeError(MOCK_CALL_KEY.to_string()))?;
    mocks.insert(func_to_mock.offset, (*mock_ret_value).clone());

    Ok(())
}

Tim: ce que je veux dire c'est qu'il faut register dans le scope le fait que la fonction x est mainteannt mock avec comme return value le content de l'array nouvellement créee Comme on le fait déjà en fait, sauf que pas juste avec un fetl ou un pointer, mais avec l'array

chirag-bgh commented 1 year ago

Hey @Eikix @tdelabro , is anyone working on this? If not I would like to take this on

tdelabro commented 1 year ago

@chirag-bgh It has some dependecy on some ongoing work. Let me a few days to merge those PR and I will ping you when it's doable again