CKB Pulse

All lessons · Developer tooling · Start here · Glossary

Charging load_program bytes as cycles when building a custom ckb-vm machineAdvancedFrom one fix

From one fix · Check: right · Checked 2026-11-19

When you build a ckb-vm machine yourself (DefaultMachineBuilder or AsmMachine) and call load_program, charge the returned byte count with machine.add_cycles_no_checking(ckb_script::cost_model::transferred_byte_cycles(bytes)). That count covers the loaded program and the stack and argument setup, and TransactionScriptsVerifier charges it on chain, so without it your cycle count comes out too low (in ckb-debugger the fib test went from 1361 to 3364 cycles). For AsmMachine call it on the inner machine, and pass the error up instead of unwrapping.

Why it matters: A hand-built ckb-vm machine that skips charging the loaded program bytes reports fewer cycles than on-chain verification does (the fib example showed 1361 instead of 3364), so cycle estimates and limits based on it are wrong.

Pattern

Prefer
machine.add_cycles_no_checking(transferred_byte_cycles(bytes))?;

Taken from the commit, shortened.

A test changed in the same commit

It sits next to the fix. We did not run it.

Concepts
Testing and devnet, Other
Evidence
From one fix: from one project, checked against its commit
First seen
2026-11-03
Last changed
2026-11-03
Last checked
2026-11-19
Check against the commit
right: the lesson follows from its commit and still holds in the code today

Sources (1)

More about Developer tooling

All 35 in this topic

Do you maintain a project this lesson came from? Confirm or correct it

Cite this lesson
LusoCryptoLabs, "Charging load_program bytes as cycles when building a custom ckb-vm machine", CKB lessons, https://pulse.lusocryptolabs.com/learned/tooling/charging-load-program-bytes-as-cycles-when-building-a-custom, CC BY 4.0, last changed 2026-11-03.

Drawn from public commits and checked against the commit each one came from and the project's code today. Read the source before you rely on one. Text under CC BY 4.0: credit LusoCryptoLabs, CKB lessons, with a link to the lesson. Want a lesson changed or removed? Open an issue.