Transaction Calculation

The math behind gas fee calculation on the VeChainThor blockchain.

What is gas?

Blockchain networks often refer to transaction fees as gas. Gas refers to the unit that measures the amount of computation effort required to execute operations on the blockchain network. This is a fee that is paid by the transaction sender and received by the blockchain network validator.

Intrinsic Gas Calculation

The VeChainThor blockchain transaction model is capable of containing clauses which allows a single transaction to carry out multiple tasks. Therefore, the total gas cost of the transaction needs to include all the clauses gas costs in the transaction.

The total gas, gtotalg_{total}, required for a transaction can be computed as:

gtotal=g0+i(gtypei+gdatai+gvmi)g_{total} = g0 + \sum_i(g_{type}^i+g_{data}^i+g_{vm}^i)

  • where g0=5,000g_0 = 5,000

  • There are two types of gtypeg_{type}

    • Regular transaction : 16,000

    • Contract creation : 48,000

  • gdatai=4nzi+68nnzig_{data}^i = 4 \cdot n_z^i + 68 \cdot n_{nz}^i

    • nzin_z^i is the number of bytes equal to zero within the data in the ithi^{th} clause and nnzin_{nz}^i the number of bytes not equal to zero

  • gvmig_{vm}^i is the gas cost returned by the virtual machine for executing the ithi^{th} clause.

Proof of Work

The VeChainThor blockchain allows for transaction-level proof of work (PoW) and converts the proved work into extra gas price that will be used by the system to generate more reward to the block generator, the Authority Masternode, that validates the transaction. In other words, users can utilize their local computational power to make their transactions more likely to be included in a new block.

In particular, the computational work can be proved through fields Nonce and BlockRef in the transaction model. Let nn and gg represent the values of the transaction fields Nonce and Gas, respectively. We use bb to denote the number of the block indexed by transaction field BlockRef and hh the number of the block that includes the transaction. Let Ω\Omega denote the transaction without fields Nonce and Signature, SS the transaction sender's account address, PP the base gas price, HH the hash function and EE the recursive length prefix (RLP) encoding function.

The PoW, ww, is defined as:

w=min(2641,22561H(H(E(ΩS))n))w = min(2^{64}-1, \frac {{2^{256}-1}}{H(H(E(\Omega \parallel S))\parallel n)})

The extra gas price, Δ\Delta, is computed as:

ΔP=P0(1gmin[g,w103(11.04)h13600243])\Delta P = P_0(\frac1{g}min[g,\frac w{10^3}(\frac 1{1.04})^\frac {h-1}{3600\cdot24\cdot3}])

with the following constraint

hh030\mid h - h_0 \mid \leq 30

The VTHO reward for packing the transaction into a new block is computed as:

r=310g(P0(1+ϕ)+ΔP)r = \frac3{10}g^*(P_0(1+\phi) + \Delta P)

where ϕ[0,1]\phi \in [0,1] is the gas price coefficient and gg^* the actual amount of gas used for executing the transaction.

From the above equations, we know that

  1. Since h0h_0 is a valid block number, BlockRef must refer to an existing block, that is, it's value must equal the first four bytes of an existing block ID;

  2. The transaction must be packed into a block within the period of 30 blocks after block bb, or otherwise, the PoW would not be recognized by the system;

  3. The extra gas price ΔP\Delta P can not be greater than base gas price P.

Total Gas Price

The total gas price for the transaction sender is computed as:

gtotal=gbase+gbaseϕ255g^{total} = g^{base} + g^{base} \frac\phi{255}

and the total price for block generators as

gtotal=gbase+gbaseϕ255+ΔPg^{total} = g^{base} + g^{base} \frac\phi{255} + \Delta P

Where gbaseg^{base} is the gas used by the transaction and ϕ\phi is the value of field GasPriceCoef(a value between 0-255) and ΔP\Delta P the extra gas price converted from the proven local computational work.

It can be seen that the gas price used to calculate the transaction cost depends solely on the input gas-price coefficient while the reward for packing the transaction into a block varies due to the transaction-level proof-of-work mechanism.

Last updated