Skip to content

Instantly share code, notes, and snippets.

@SmaugPool
Last active December 17, 2023 22:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SmaugPool/e44dfc8ac202cf6f1eebc75ad778e153 to your computer and use it in GitHub Desktop.
Save SmaugPool/e44dfc8ac202cf6f1eebc75ad778e153 to your computer and use it in GitHub Desktop.
Scripts Memory Units & Execution Steps used by Cardano transactions & blocks

Smart Contracts on Cardano are Plutus scripts. We often talk about the size of transactions and blocks compared to the maximum currently authorized by the protocol parameters, but scripts are also limited in CPU and memory units.

Here is the description of those 3 properties from Cardano docs:

  • The total on-chain transaction size in bytes: a simple transaction, for example, is around 300 bytes, one with metadata is around 650 bytes, and Plutus scripts are typically 4,000-8,000 bytes (future optimizations and improvements will reduce this).
  • The number of computational (CPU) steps that the script uses: each step represents 1 picosecond of execution time on a benchmark machine. Typical scripts should consume less than 1,000,000,000 (1 millisecond).
  • The number of memory units that the script uses: this represents the number of bytes that the script allocates. Typical scripts should consume less than 1,000,000 memory units (1MB of memory allocation).

All together, those 3 properties are used to determine the transaction fees needed by scripts.

Both block and transactions are limited in size, execution and memory units, and the visualizations show how blocks and transactions over the last 3 months compare with their current limits:

  • X axis represents the % of available memory units used
  • Y axis represents the % of available CPU units used
  • Color represents the % of available bytes used

We can see for example that blocks often use all their CPU & memory units available, but transactions are limited mainly by their memory, and very rarely by their CPU.

Current limits are:

Blocks:

  • max size: 90,112 bytes
  • max execution steps: 20,000,000,000
  • max memory units: 62,000,000

Transactions:

  • max size: 16,384 bytes
  • max execution steps: 10,000,000,000
  • max memory units: 14,000,000

blocks txs

Cardano-db-sync SQL queries

TXs:

COPY
  (SELECT tx.size/16384.0 AS size,
          sum(unit_mem)/14000000 AS mem,
          sum(unit_steps)/10000000000 AS steps
   FROM tx
   JOIN redeemer ON tx.id=redeemer.tx_id
   JOIN BLOCK ON block.id=tx.block_id
   WHERE block.time > (NOW() AT TIME ZONE 'UTC' - INTERVAL '90 DAY')
   GROUP BY tx.id
   ORDER BY tx.id) TO 'txs.csv' csv header;

Blocks:

COPY
  (SELECT block.size/90112.0 AS size,
          sum(unit_mem)/62000000 AS mem,
          sum(unit_steps)/20000000000 AS steps
   FROM tx
   JOIN redeemer ON tx.id=redeemer.tx_id
   JOIN BLOCK ON block.id=tx.block_id
   WHERE block.time > (NOW() AT TIME ZONE 'UTC' - INTERVAL '90 DAY')
   GROUP BY block.id
   ORDER BY block.id) TO 'blocks.csv' csv header;

R ggplot2 script

library(ggplot2)
library(dplyr)
library(hrbrthemes)
library(viridis)
library(scales)
library(ggblend)

data <- read.csv("txs.csv")
txplot <- data %>% 
    arrange(size) %>% 
    ggplot(aes(x=mem, y=steps, color=size)) +
        geom_point(alpha=0.5, size=1) +
        scale_x_continuous(labels = scales::percent) +
        scale_y_continuous(labels = scales::percent) +
        scale_color_gradient(low="green", high="red", labels = scales::percent) +
        theme_modern_rc(caption_size=6, caption_margin=20) +
        xlab("Memory Units") +
        ylab("Execution Steps") +
        labs(title="Cardano TXs", subtitle="May/June/July 2023", caption="@SmaugPool") +
png("txs.png", width=2048, height=2048, res=300)
print(txplot)
dev.off()

data <- read.csv("blocks.csv")
txplot <- data %>% 
    arrange(size) %>% 
    ggplot(aes(x=mem, y=steps, color=size)) +
        geom_point(alpha=0.5, size=1) |> blend("lighten") +
        scale_x_continuous(labels = scales::percent) +
        scale_y_continuous(labels = scales::percent) +
        scale_color_gradient(low="green", high="red", labels = scales::percent) +
        theme_modern_rc(caption_size=6, caption_margin=20) +
        xlab("Memory Units") +
        ylab("Execution Steps") +
        labs(title="Cardano Blocks", subtitle="May/June/July 2023", caption="@SmaugPool") +
png("blocks.png", width=2048, height=2048, res=300)
print(txplot)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment