Skip to content

Find out whether RSK's own opcodes were ever executed - #407

Merged
SergioDemianLerner merged 2 commits into
mainfrom
opcodes/rsk-opcode-census
Nov 22, 2026
Merged

SergioDemianLerner merged 2 commits into
mainfrom
opcodes/rsk-opcode-census

Conversation

@SDL-A1

@SDL-A1 SDL-A1 commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

DUPN (0xa8), SWAPN (0xa9) and TXINDEX (0xaa) were live from genesis until RSKIP191 removed them at iris300, #3,614,800. Nothing in the code says whether any transaction ever reached one. The question has a cheap half that can rule it out and an exact half that costs hours; this adds both, and answers the cheap one.

The cheap half: opcode_census

An opcode can only execute if its byte sits at a position the EVM calls an instruction, and that set is exactly what a linear decode from offset zero yields — jumpdest analysis is computed over the same decode, so a JUMPDEST hidden inside a PUSH immediate is not a valid target and there is no second decoding of the same code. So one walk of the state at a block, decoding every contract, answers "could any of this have executed it", and a negative is a proof rather than a sample.

The filters matter more than the search. Each only ever removes a position the EVM could not reach:

filter removes
linear decode PUSH immediates — a byte scan reports 10,525 occurrences of 0xa8, almost all data
metadata trailer solc's CBOR tail. Not generic noise: CBOR map headers are 0xa1..0xb7, so a blob can begin with the bytes being hunted
dead regions bytes after STOP/RETURN/REVERT/INVALID/SELFDESTRUCT/JUMP until the next JUMPDEST. This is what removes constant pools
RSK code header code of ≥4 bytes starting with 0xfc runs from 4 + code[3]; decoding from zero would mis-align everything after it

Measured on mainnet at #3,614,799 — 9,130 contracts, 24 MB of code, 2.94M trie nodes, 17 minutes, no unresolved references:

occurrences ANYWHERE in code (what a byte scan would report):
  0xa8  10525    0xa9  13355    0xaa  12566

occurrences AT AN INSTRUCTION POSITION (what can execute):
  0xa8  0        0xa9  0        0xaa  3

The three 0xaa survivors are the same string constant in three contracts — identical 24 bytes before it, and CROWDFUNDING_AUTH_FA… after. So nothing alive at the fork could reach any of the three, and the RSK code header is never used either.

A missing node would void all of that, so every unresolvable reference is counted and makes the exit status non-zero: an absent subtree is exactly where the answer could hide.

The exact half: counters in the three handlers

RSK_OPCODE_HITS counts each execution and logs the first eight with their transaction index. CONTRACTS_CREATED counts every creation that keeps code and logs any carrying a header — hooked at rsk_return_create rather than at frame open, because "deployed" is a different question from "executed" and a headered contract nobody called leaves no trace in an execution counter. reexec_all prints both beside its verdict, including on an incomplete run, since a run that stopped early still observed what it executed.

One relaxed atomic add per execution of these three opcodes only, and nothing for any other opcode — cheap enough to leave in rather than hide behind a flag someone has to remember.

What the census cannot cover

Contracts created and destroyed before the block scanned are in no state at all. That is what the replay answers, and it is staged in #405.

Not specific to this question

--ops takes any bytes and --block any height. "Is this rule change dead code on this chain?" is a question this repository keeps asking — scan_edges asks the same shape of it about RSKIP-144 from the headers; this asks it from the code. Indexed in docs/tools.md.

Six unit tests cover the decoder: a byte inside a PUSH2 immediate, a dead region revived by a JUMPDEST, a trailer containing 0xa9, the header moving where decoding starts, the unsigned 0x80 = 128 length, and rskj's testScriptVersion0..3 degenerate headers.

🤖 Generated with Claude Code

SergioDemianLerner and others added 2 commits November 21, 2026 22:05
`DUPN` (0xa8), `SWAPN` (0xa9) and `TXINDEX` (0xaa) were live from genesis until
RSKIP191 removed them at iris300, #3,614,800. Nothing in the code says whether
any transaction ever reached one, and the question has two halves: a cheap one
that can rule it out, and an exact one that costs hours.

`opcode_census` is the cheap half. An opcode can only execute if its byte sits
at a position the EVM calls an instruction, and that set is *exactly* what a
linear decode from offset zero yields -- jumpdest analysis is computed over the
same decode, so a `JUMPDEST` hidden inside a `PUSH` immediate is not a valid
target and there is no second decoding of the same code. So one walk of the
state at a block, decoding every contract, answers "could any of this have
executed it", and a negative is a proof rather than a sample.

The filters matter more than the search, and each only ever removes a position
the EVM could not reach:

  - `PUSH` immediates. A plain byte scan reports 10,525 occurrences of 0xa8 in
    the state at #3,614,799, almost all of them data.
  - solc's metadata trailer, which is appended after the runtime code. Not
    generic noise: CBOR map headers are 0xa1..0xb7, so a metadata blob can
    begin with the very bytes being looked for.
  - dead regions. Control flow cannot fall through `STOP`, `RETURN`, `REVERT`,
    `INVALID`, `SELFDESTRUCT` or an unconditional `JUMP`, so bytes after one are
    unreachable until the next `JUMPDEST` -- which is where a jump could land.
    This is what removes constant pools, and it is conservative: every
    `JUMPDEST` counts as a possible entry, because targets are computed at
    runtime.
  - RSK's code header. Code of four or more bytes starting with 0xfc runs from
    `4 + code[3]`, so decoding from zero would mis-align every instruction
    after it. The degenerate forms -- a 0xfc too short to be a header, or one
    reaching the code's end -- execute nothing at all, and are skipped.

Measured on mainnet at #3,614,799: 9,130 contracts, 24 MB of code, 2.94M trie
nodes, 17 minutes, no unresolved references. 0xa8 and 0xa9 are reachable
nowhere. 0xaa survives in three contracts, and all three turn out to be the
same string constant -- `CROWDFUNDING_AUTH_FA...` follows it, and the 24 bytes
before it are identical across all three. So nothing alive at the fork could
reach any of them. The RSK code header is never used either.

What that cannot cover is a contract created and destroyed before the fork,
whose code is in no state, or code a contract computed at runtime. Only
re-execution settles those, which is the second half: `RSK_OPCODE_HITS` counts
each execution of the three opcodes and logs the first eight with their
transaction index, and `reexec_all` prints the totals beside its verdict --
including on an incomplete run, because a run that stopped early still observed
what it executed. One relaxed atomic add per execution of these three opcodes
and nothing for any other, which is cheap enough to leave in rather than hide
behind a flag someone has to remember.

The tool is not specific to this question. `--ops` takes any bytes and
`--block` any height, and "is this rule change dead code on this chain?" is a
question this repository keeps asking -- `scan_edges` asks the same shape of it
about RSKIP-144 from the headers.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Whether the RSK code header -- a 0xfc first byte, four bytes plus code[3] more,
intended for selecting a VM version -- was ever *deployed* is a different
question from whether one was ever *executed*, and an execution counter cannot
answer it: a headered contract nobody called would leave no trace.

So the hook is `rsk_return_create`, where the deposit has been paid and the code
committed. That is the point at which a contract exists with that code, so it
sees contracts later destroyed -- which is exactly the gap a census of one
block's state cannot see, and the reason issue #405 exists. A creation that ran
out of gas or exceeded the size limit deploys empty in rskj and is not counted
as a contract.

Every headered deployment is logged, uncapped, unlike the opcode counter: the
expectation is zero, so each one earns a line, and if the expectation is wrong
the count will be small enough to stay readable. `reexec_all` prints the totals
beside its verdict.

The scoped pre-iris300 replay therefore answers two questions for the price of
one: whether DUPN, SWAPN or TXINDEX ever executed below the fork, and every
contract deployed below it, headered or not.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@SergioDemianLerner
SergioDemianLerner merged commit bdccd86 into main Nov 22, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants