Entropy Costs Calculation

When writing, testing and debugging smart contracts, you should be aware of the different OpCode call costs. This article provides details on the calculation of entropy consumption and provides links to relevant references.

OpCode Entropy

The entropy consumption of different OpCode is divided into different levels, with the cost level classification and corresponding entropy consumption as follows:

public enum Tier {
    ZeroTier(0),
    BaseTier(2),
    VeryLowTier(3),
    LowTier(5),
    MidTier(8),
    HighTier(10),
    ExtTier(20),
    SpecialTier(1),
    InvalidTier(0);
  }

The concept of memory and storage exists in virtual machines, and the OpCode consumption for these two types of operations is calculated separately.

For example, for the memory type of OpCode, the memory size of the operation affects the consumption, for example MLOAD, MSTORE OpCode, the consumption is related to the word length of the operating memory.

For the storage type SSTORE OpCode, the consumption is not only related to the operation word length, but also need to distinguish whether the operation is reset, add, or delete.

Current valueOperationEntropy ConsumptionType of Operation
val = 0x0sstore(val, n)
n != 0
(val = n)
20000SET
val != 0x0sstore(val, n)
n != 0
(val = n)
5000RESET
val != 0x0sstore(val, 0)
(val = 0)
5000CLEAR

Other higher-consumption operations

When creating an account by transferring VS or CRC10 from a contract to an inactive account, an additional 25000 entropy is consumed and there is no account creation fee of 0.1 VS.

When invoking other contracts with actions such as CALL, DELEGATECALL, etc., and additional transfers are made, an additional 9000 entropy is consumed.

When using CREATE, CREATE2 to dynamically create a contract within a contract, it consumes 32000 entropy.

Entropy consumption for deploying contracts

When deploying a contract, the contract code consumes 200 entropy per byte.

The bytecode provided when deploying a contract is generally divided into two parts, one called the creation code and the other is the contract's runtime code. The deployment code is generally used to execute the contract constructor logic and return the actual contract runtime code. To calculate the entropy consumption when deploying a contract, the length of the runtime code is used. It typically starts with the second 6060 or 6080 of the bytecode of the deployment code.


Did this page help you?