The adaptive block size algorithm of Nexa

jQrgen
2 min readJan 20, 2023

ChatGPT: Write a medium post explaining the adoptive block size of Nexa that is described in the following documentation and add a code sample in kotlin: https://spec.nexa.org/doc/adaptive-blocksize

Nexa.org

Nexa, a blockchain network, has adopted an adaptive block size algorithm to automatically adjust the maximum block size without the need for hard forks. The algorithm calculates the next maximum block size using median block sizes from the last 90 and 365 days, respectively. By using the median instead of the mean value, the algorithm prevents any one miner from artificially causing a block size increase by mining occasional and huge blocks.

The algorithm is simple and works as follows:

  1. Calculate the median block size value from the last 90 and 365 days. If the sets of values are incomplete, use the default maximum block size of 100K.
  2. Take the largest of the two median values and multiply it by the block size multiplier of “10”, which gives us the next maximum block size value.
  3. If the calculated value is still less than the default maximum size of 100K, then use the default value as the maximum block size of the next block.
val DEFAULT_MAX_BLOCK_SIZE = 100_000
val BLOCK_SIZE_MULTIPLIER = 10
fun calculateNextMaxBlockSize(MEDIAN_BLOCK_SIZE_90_DAYS: Int?, MEDIAN_BLOCK_SIZE_365_DAYS: Int?): Int {
val median = when {
MEDIAN_BLOCK_SIZE_90_DAYS != null && MEDIAN_BLOCK_SIZE_365_DAYS != null -> max(MEDIAN_BLOCK_SIZE_90_DAYS, MEDIAN_BLOCK_SIZE_365_DAYS)
MEDIAN_BLOCK_SIZE_90_DAYS != null -> MEDIAN_BLOCK_SIZE_90_DAYS
MEDIAN_BLOCK_SIZE_365_DAYS != null -> MEDIAN_BLOCK_SIZE_365_DAYS
else -> return DEFAULT_MAX_BLOCK_SIZE
}
val nextMaxBlockSize = median * BLOCK_SIZE_MULTIPLIER
return if (nextMaxBlockSize > DEFAULT_MAX_BLOCK_SIZE) nextMaxBlockSize else DEFAULT_MAX_BLOCK_SIZE

Selecting the median value from both a long and short window allows block sizes to increase more rapidly than they will decrease. The median ranges were chosen with long enough windows so no miner could temporarily game them to disrupt the network.

The block size multiplier serves a vital role in times of a sudden and short-lived surge in transaction volumes. It allows the network to mine transactions quickly and prevents undue delays in confirmation.

Overall, the adaptive block size algorithm allows for a more flexible and dynamic approach to adjusting the maximum block size without the need for hard forks and potential disruption to the network.

--

--