Repository navigation
Find out whether RSK's own opcodes were ever executed - #407
Merged
Merged
Conversation
`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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DUPN(0xa8),SWAPN(0xa9) andTXINDEX(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_censusAn 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
JUMPDESThidden inside aPUSHimmediate 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:
PUSHimmediates — a byte scan reports 10,525 occurrences of 0xa8, almost all data0xa1..0xb7, so a blob can begin with the bytes being huntedSTOP/RETURN/REVERT/INVALID/SELFDESTRUCT/JUMPuntil the nextJUMPDEST. This is what removes constant pools0xfcruns from4 + code[3]; decoding from zero would mis-align everything after itMeasured on mainnet at #3,614,799 — 9,130 contracts, 24 MB of code, 2.94M trie nodes, 17 minutes, no unresolved references:
The three
0xaasurvivors are the same string constant in three contracts — identical 24 bytes before it, andCROWDFUNDING_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_HITScounts each execution and logs the first eight with their transaction index.CONTRACTS_CREATEDcounts every creation that keeps code and logs any carrying a header — hooked atrsk_return_createrather 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_allprints 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
--opstakes any bytes and--blockany height. "Is this rule change dead code on this chain?" is a question this repository keeps asking —scan_edgesasks the same shape of it about RSKIP-144 from the headers; this asks it from the code. Indexed indocs/tools.md.Six unit tests cover the decoder: a byte inside a
PUSH2immediate, a dead region revived by aJUMPDEST, a trailer containing0xa9, the header moving where decoding starts, the unsigned0x80 = 128length, and rskj'stestScriptVersion0..3degenerate headers.🤖 Generated with Claude Code