Function Calls Between Native & Wasm
There are two ways to call another function from either Native and Wasm functions.
- Call: Sync call. The caller will wait for the subroutine to complete and return before continuing.
- Post: Async call. The caller will not wait for the subroutine to complete, i.e. the subroutine will run separately from the caller.
Every native function will have a gas-estimate helper function that stays inside the Tea-codec. This function will run logic to estimate how much gas the function run will cost. Although there's no way to precisely measure the cost, this function will be used to get the best estimate.
When a caller calls/posts another function, the calling activity is measured as estimated cost deducted from gas limit At time of call. The system will run the Gas Estimate function first. If the caller cannot afford this cost (exceeds the gas limit), the function call will fail with error "Gas limit ran out". If the gas limit can cover the cost, the estimated gas cost will immediately be deducted from the calling function's gas limit. This will guarantee that the bill can get paid at the time of settlement.
A Native call or post to Native is free because Native can't run the estimate function. Even if it could estimate the gas cost, it wouldn't being able to alter the gas limit of the Native function calling it.