Precompiled contracts
A precompiled smart contract is a contract with a C++ implementation in the node. When a validator processes a transaction for such a contract, it can execute this native implementation instead of TVM. This improves performance and reduces computation fees.
Config
The list of precompiled contracts is stored in the MasterChain configuration:
precompiled_smc#b0 gas_usage:uint64 = PrecompiledSmc;
precompiled_contracts_config#c0 list:(HashmapE 256 PrecompiledSmc) = PrecompiledContractsConfig;
_ PrecompiledContractsConfig = ConfigParam 45;
The list:(HashmapE 256 PrecompiledSmc)
represents a mapping of (code_hash -> precompiled_smc)
. A contract is considered precompiled if its code hash exists in this map.
Contract execution
Transactions for precompiled smart contracts (those with code hashes in ConfigParam 45
) follow this execution flow:
- Retrieve the
gas_usage
value from the masterchain config - If the contract balance cannot cover the
gas_usage
cost, the compute phase fails withcskip_no_gas
. - Execution proceeds via one of two paths:
- TVM execution: Used if precompiled execution is disabled or the C++ implementation is unavailable in the node version. TVM executes with the gas limits defined by the network configuration (see gas and fee parameters and Param20/Param21).
- Native execution: Used when precompiled implementation is both enabled and available, executing the C++ code directly
- Compute phase values are overridden:
gas_used
set togas_usage
vm_steps
,vm_init_state_hash
, andvm_final_state_hash
set to zero
- Computation fees are calculated based on
gas_usage
rather than actual TVM gas consumption
For precompiled contracts, the 17th element of c7
(index 16
) holds the precompiled gas_usage
value for the current contract as defined in ConfigParam 45
; see c7. The GETPRECOMPILEDGAS
opcode is currently reserved and returns null
; when activated, it will return the precompiled contract gas usage; see new opcodes. Non-precompiled contracts return null
for this value.
Note: Enable precompiled contract execution by running validator-engine
with the --enable-precompiled-smc
(experimental) flag. Both execution methods produce identical transactions, allowing validators with and without C++ implementations to coexist in the network. This enables gradual adoption when adding new entries to ConfigParam 45
.