3

I have a question regarding costs of executing a cross-contract call, in which one contract calls the method of another, already instantiated contract.

Specifically how much more overhead (understood as gas costs) does the caller incur versus executing an inline instruction?

Is it charged for reading the entire callee code from the storage (certainly looks like it)?

Is there any caching - say I make two consecutive calls from A to B, will it double the fee?

1

1 Answer 1

5

The overhead is significant. This is because for cross-contract calls the pallet-contracts needs to:

  1. Load the callee contract code from storage.
  2. Compile the code (using wasmi).
  3. Set up a new sandboxed Wasm instance for the code and execute it.

You can exemplarily see the difference here:

Inline

#[ink(message)]
pub fn flip(&mut self) {
    self.value = !self.value;
}

Invoked via:

cargo contract call.  \
  --suri //Alice      \
  --contract $FLIPPER \
  --message flip      \
  --manifest-path=ink-examples/flipper/Cargo.toml \
  -x                  \

Gas Usage:

Weight(ref_time: 7983333376, proof_size: 262144)

Cross-contract Call

#[ink(message)]
pub fn invoke(&mut self, address: AccountId) {
    use ink::env::call::build_call;
    const fn selector() -> Selector {
        Selector::new(ink::selector_bytes!("flip"))
    }

    build_call::<DefaultEnvironment>()
        .call(address)
        .exec_input(
            ExecutionInput::new(selector())
        )
        .returns::<()>()
        .invoke()
}

You could reduce gas costs further by hard-coding AccountId in the contract, but this is not the significant part here.

Invoked via:

cargo contract call               \
  --suri //Alice                  \
  --contract $DELEGATING_CONTRACT \
  --message invoke                \
  --args $FLIPPER_CONTRACT        \
  -x

Gas Usage:

Weight(ref_time: 8847490545, proof_size: 268208)

Summary

The refTime overhead is ~11%, for the proofSize ~2%.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.