Skip to content

feat(evm): fix internal CREATE derivation and add PUSH0 behind ForkEVMCompat - #1354

Merged
33cn merged 10 commits into
33cn:masterfrom
bysomeone:feat/evm-compat
Nov 23, 2026
Merged

33cn merged 10 commits into
33cn:masterfrom
bysomeone:feat/evm-compat

Conversation

@bysomeone

@bysomeone bysomeone commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

What

Two EVM incompatibilities, both gated by one new dapp fork, ForkEVMCompat:

  1. Internal CREATE derived a colliding address (EVM 合约内部 create(Solidity new)地址冲突:同一笔交易内 factory 创建的多个子合约地址相同,后者覆盖前者,且交易显示成功 #1351). opCreate used hash(txHash || origin), so every internal create in a transaction received the same address, and EVM.Create accepted an existing account: a second child silently overwrote the first while the transaction still reported success.
  2. PUSH0 (0x5f) and BASEFEE (0x48) were missing (evm: PUSH0 (0x5f) and other Shanghai/Cancun opcodes are missing from the instruction set #1352). Solidity 0.8.20 and later emit PUSH0 by default, so a contract compiled with a current solc cannot be deployed at all.

How

ForkEVMCompat is registered at MaxHeight (off by default) and the templates keep it at -1, so each chain opts in with a concrete height. Enabling it from genesis on an existing chain would rewrite historical execution.

Fork on:

  • CREATE address = CreateAddress(executing account, its nonce). The caller nonce is incremented before any create snapshot and persisted in contract state, so it survives across transactions and failed creates. New contracts start at nonce 1 (EIP-161), an account with a nonce is no longer treated as non-existent, and CREATE / CREATE2 return ErrContractAddressCollision when the target already has a nonce or code. An account that holds code but no nonce record (deployed before the fork) is treated as nonce 1, so these contracts match Ethereum as well.
  • The top-level ETH-signed deploy derives the address from the same 20 bytes regardless of the configured address driver, and applies the same collision rule, so an address that only holds a balance can still be deployed to.
  • CREATE / CREATE2 forward 63/64 of the remaining gas (EIP-150), forward value with the same precision conversion CREATE uses, and roll back on ErrCodeStoreOutOfGas instead of keeping the account.
  • PUSH0 pushes zero and BASEFEE returns zero, both at 2 gas. The chain has no base-fee mechanism.

Fork off, execution is unchanged: the previous CREATE address derivation, the Berlin instruction set, the gas constants and the refund handling are untouched.

Address construction for the new path bypasses the chain33 address driver: a btc-format driver cannot parse a 0x hex string and produced an all-zero raw address.

Testing

  • go test ./plugin/dapp/evm/... (bn256/cloudflare fails on macOS with a dyld: missing LC_UUID load command error; the package does not depend on anything here)
  • New tests cover, for fork off and fork on:
    • the CREATE address matching crypto.CreateAddress, twice in one transaction; nonce persistence across transactions and executor instances; a failed CREATE still consuming a nonce
    • CREATE2 collision returning zero
    • PUSH0 / BASEFEE being invalid opcode when off and executing when on
    • EIP-150 63/64 forwarding for CREATE and CREATE2, a CREATE from inside a constructor, CREATE2 incrementing the caller nonce, and the pre-fork contract nonce rule
    • the top-level ETH-signed deploy landing on the raw address and being accepted on a balance-only address
    • the fork being registered off by default, the value a failed CREATE keeps when only ForkEVMCompat is on, and ForkEVMCompat being present in the three shipped configs
  • go test ./plugin/dapp/evm/executor/ -shuffle=<seed> is order-independent; a pre-existing global address-driver leak from query_test.go was fixed for that.

Deployment note

The fork has to be enabled with a real height in the deployment repository; the templates here keep it off (-1). Every existing chain configuration must list ForkEVMCompat once this release is deployed — the fork is registered in code, and a configuration that omits it fails the fork check at startup.

  • Enable it at the same height as ForkEVMTransferRevert. That fork rolls back the value attached to a failed CREATE, which this one alone does not do; the full Ethereum semantics need both.
  • Chains that accept ETH-signed transactions no longer need [exec.sub.evm] addressDriver="eth" for the deploy to land on the Ethereum address; the driver is still used by the pre-existing paths that do not create contracts.
  • Contracts compiled for evmVersion=shanghai work. Cancun and later targets emit MCOPY / TLOAD / TSTORE, which this fork does not provide.

Not covered

No gas metering changes: cold/warm access accounting, net SSTORE metering and EIP-3529 refunds are not part of this fork, so gas numbers differ from Ethereum for contracts that are sensitive to them. MCOPY, TLOAD/TSTORE, DIFFICULTY/PREVRANDAO, the precompile table and the SELFDESTRUCT pricing are unchanged.

bysomeone and others added 6 commits November 11, 2026 14:09
… fork

EVM-internal CREATE used hash(txHash||origin), so every CREATE inside one
transaction produced the same address. EVM.Create also accepted an existing
account silently, so a factory deploying two children in one call had the
second overwrite the first while the transaction still reported success.

Gate the Ethereum behaviour behind ForkEVMContractCreateV2. Registered at
MaxHeight (off by default, opt-in per chain) like ForkEVMTransferRevert:
enabling it from genesis on an existing chain would rewrite historical
execution.

- CREATE address = CreateAddress(executing account, its nonce); the caller
  nonce is incremented before any create snapshot and persisted in contract
  state, so it survives across transactions and failed creates.
- Newly created contracts start at nonce 1 (EIP-161); an account with a nonce
  is no longer treated as non-existent.
- CREATE/CREATE2 fail with ErrContractAddressCollision (consuming the
  forwarded gas) when the target already has a nonce or code.
- ErrCodeStoreOutOfGas rolls the creation back instead of keeping the account
  and pushing its address.
- CREATE/CREATE2 forward 63/64 of the remaining gas (EIP-150); CREATE2 no
  longer halves it, and converts value precision for ETH-signed transactions
  the same way CREATE does.

Address construction for the new path bypasses the chain33 address driver: a
btc-format driver cannot parse a 0x hex string and produced an all-zero raw
address.

Fork-off execution is unchanged; the nonceChange KV write is gated on the same
fork.

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
withdrawTicketBalance compared the post-withdraw coins balance with the
pre-withdraw ticket balance. The ticket balance also grows with mining
rewards, so the check only held when it happened to be exactly 100000 and
failed intermittently otherwise (observed 100005 vs 100001 on the same head).

Record the coins balance before the withdraw and assert the increase is the
withdrawn amount, allowing a small tolerance for fees and rounding.

Co-authored-by: Claude <noreply@anthropic.com>
The withdraw moves 100000 coins minus a ~0.1 coin fee, so the expected
increase is exactly 99999.9 and the previous 99999.9 lower bound sat on the
floating-point boundary: the same balances can compare either side of it
depending on their magnitude and on the awk implementation.

Use a 99999 lower bound instead, and make jq fail on a missing balance rather
than silently comparing a null as zero.

Co-authored-by: Claude <noreply@anthropic.com>
ForkEVMContractCreateV2 and ForkEVMTransferRevert are independent forks, but
the full Ethereum semantics need both: this fork keeps the nonce of a failed
CREATE, and the transfer-revert fork rolls back the value attached to it.
Add a test that enables both, and document the pairing next to the fork.

Co-authored-by: Claude <noreply@anthropic.com>
The fork now covers the whole Ethereum compatibility set (CREATE semantics,
new opcodes and the gas schedule), so a semantic name without a version
suffix is clearer: there is no "V1" fork to pair with.

Co-authored-by: Claude <noreply@anthropic.com>
Solidity 0.8.20 and later emit PUSH0 by default, so a contract built with a
current solc cannot be deployed without it. The fork selects a Berlin-based
instruction set that adds PUSH0 (0x5f) and BASEFEE (0x48); the chain has no
base-fee mechanism, so BASEFEE pushes zero. Both cost 2 gas.

Fork off keeps the existing instruction set, so the two bytes remain invalid
and nothing else changes.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@bysomeone bysomeone changed the title feat(evm): align CREATE, opcodes and gas metering behind ForkEVMCompat feat(evm): fix internal CREATE derivation and add PUSH0 behind ForkEVMCompat Nov 12, 2026
@bysomeone
bysomeone marked this pull request as ready for review November 12, 2026 07:49
@bysomeone
bysomeone requested review from 33cn and vipwzw November 12, 2026 07:49
@vipwzw

vipwzw commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

整体判断:修复方向正确。CREATE 地址改为 CreateAddress(caller, nonce)、nonce 在快照前递增并在 fork 下持久化、EIP-161 nonce=1、EIP-684 碰撞返回 0 gas、CREATE2 顺带把 /2 修正为 63/64,这些都与 geth create() 对齐;PUSH0/BASEFEE 的 gas 与栈效应正确,fork 关闭时仍是 invalid opcode。我在 19e346b78 上跑了 go test ./plugin/dapp/evm/executor/... ./plugin/dapp/evm/types/...(含 -shuffle=on)全绿,并做了两处变异验证(去掉 Raw 地址派生、nonceChange.getData 恒返回 nil),新增用例确实能抓住这两个 bug。

建议按优先级:

  1. 预分叉合约的 nonce 可以彻底对齐(现有已知限制可以去掉)
    EIP-161 下合约 nonce 恒 ≥1。fork 开启后,当地址有 code 且记录 nonce 为 0 时按 1 处理;opCreate 的地址派生与 Create 的递增必须共用一个 helper,否则地址用 1、递增从 0 开始会错位。这样存量合约的首个内部 CREATE 也能与以太坊一致。

  2. 顶层 ETH 签名部署仍受地址驱动影响
    exec.go 里 createEvmContractAddress → NewEvmContractAddress 走 FormatAddr/FromString 往返;btc 驱动的 FromString 只解 base58,0x.. 解析失败被忽略、raw 变全零。既然本 fork 已加入 NewEvmContractAddressRaw,建议 compat 下让顶层路径也用它(eth 驱动下字节完全一致),而不是要求部署方配置 addressDriver="eth"。

  3. 顶层部署的碰撞预检与内部 CREATE 不一致
    exec.go:139 用 Empty(),把“只有余额”也算非空;Create 内部只查 nonce/code。以太坊允许向仅有余额的地址部署,建议 compat 下统一。

  4. Empty() 的 nonce 分支目前不生效
    LoadContract 在 data KV 缺失时提前 return,且 GetAccount 对已缓存账户不走 Empty;正常路径下有 nonce 的账户必然有 code,前面的 CodeHash 分支已返回。建议补一个 nonce-only 账户用例说明意图,或删掉;并复用 mdb.isForkActive 去重 fork 判断。

  5. 高度来源统一
    IsEVMCompat 用 evm.BlockNumber,state 层用 mdb.blockHeight,NewInterpreter 用 StateDB.GetBlockHeight()。生产上相等,但建议统一,避免 eth_call/历史查询下判定不一致。

  6. 配置键回归测试
    initForkConfig 对缺键和未注册键都会 panic,升级时每条链都必须同时加 ForkEVMCompat。仓库里 ticket 已有解析 toml 断言段内键的先例,建议对 3 份配置补同样断言;发布说明里也建议写清“老二进制不能先加这个键”。

  7. 测试与描述
    EIP-150 的 63/64 目前没有断言;CREATE2 是否递增 caller nonce、构造函数内 CREATE、fork 默认注册为 MaxHeight 都未测。另外描述里提到“覆盖 fork 默认关闭”和“仅开 compat 时失败 CREATE 保留的 value”,代码里没有对应用例,建议补测或更新描述。

  8. 一个边界说明
    “当前 solc 合约可部署”只对 evmVersion=shanghai 成立;0.8.25+ 默认 cancun,会用到未实现的 MCOPY/TSTORE 等,建议在 issue/文档中明确。

另:docker-compose.sh 的 CI 修复方向没问题,但 local x=$(... | jq -er ...) 会吞掉 jq 退出码(读失败时 before 为空、awk 当 0 处理,可能假通过),且与本次主题无关,建议单独提交。

以上 1–3 建议合并前处理,或至少作为已知限制写进发布说明。

bysomeone and others added 3 commits November 20, 2026 15:58
A contract deployed before the fork has code but no stored nonce, so its
first post-fork internal CREATE used nonce 0 where Ethereum uses 1. Under
the fork such an account is now treated as nonce 1, through one helper
shared by the address derivation and the increment.

The top-level ETH-signed deploy now builds the address from the raw 20
bytes instead of round-tripping through the address driver, which under a
btc driver produced an all-zero address, and it applies the same nonce/code
collision rule as the internal create, so a balance-only address can be
deployed to.

The runtime fork checks read the height from the state DB, the same source
the state layer and the interpreter use. The account emptiness helper drops
its nonce branch: an account with a nonce but no code cannot be loaded from
state, so the branch never decided anything.

Tests cover the 63/64 forwarding for both creates, a create inside a
constructor, CREATE2 incrementing the caller nonce, the pre-fork nonce
rule, the top-level raw address and balance-only collision, and the fork
key in the three shipped configurations.

The ci2 withdraw check no longer hides a jq failure behind a local
declaration, so a failed read fails the script instead of asserting
against a zero.

Co-Authored-By: Claude Code <noreply@anthropic.com>
StartConsensus is started as a goroutine by SetQueueClient. When the node
is configured as a validator but its client was built without a genesis
document, MakeGenesisValidatorMgr dereferences nil and the panic takes the
process down. Log and return instead; a node without a genesis document
cannot build a validator set anyway.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@bysomeone

Copy link
Copy Markdown
Collaborator Author

已按建议逐条处理(1–7 已改,第 8 条写进 PR 描述):

  1. 预分叉合约 nonce:fork 下「有代码但没有 nonce 记录」的账户按 nonce 1 处理,地址派生与递增共用同一个 helper,不会漂移;文档里「fork 前合约首次内部 CREATE 与以太坊不一致」的限制已删除。
  2. 顶层 ETH 签名部署:fork 下改用 NewEvmContractAddressRaw,不再经过地址驱动往返,也不再要求配置 addressDriver="eth";输入为 CreateAddress(sender, txNonce)。
  3. 顶层碰撞预检:与内部 CREATE 共用同一条规则(nonce 非零或代码非空),仅有余额的地址可以部署。
  4. Empty() 的 nonce 分支:确认不可达——data KV 缺失时 LoadContract 提前返回,而正常部署的合约必然有 CodeHash;已删除该分支。
  5. 高度来源:runtime 内的 fork 判定统一读状态机高度,与 state 层、NewInterpreter 一致;checkBlockedAccount 仍用 Context.BlockNumber(黑名单版本选择,与本 fork 无关,生产上两者相等)。
  6. 配置键:新增 fork_config_test.go,对三份配置解析 [fork.sub.evm] 段断言 ForkEVMCompat 存在且为整数(当前固定 -1),并断言代码注册默认为 MaxHeight。
  7. 测试:补了 CREATE / CREATE2 的 63/64 转发、CREATE2 递增调用者 nonce、构造函数内 CREATE、单独开 compat 时失败 CREATE 保留 value、顶层 raw 地址与仅有余额地址的部署;关键几项做过变异验证。
  8. solc 版本:PR 描述已注明只覆盖 evmVersion=shanghai;cancun 及以后会用到的 MCOPY / TLOAD / TSTORE 不在本次范围。

另外,docker-compose.sh 里 local x=$(... | jq -er ...) 吞掉退出码的问题已改(赋值单独一行并传播失败)。coverage job 偶发挂在 dpos 的空 genesis 崩溃上,与本 PR 无关,另附了一个 nil 检查修复,避免继续在别的分支偶发。

@33cn
33cn merged commit 963de63 into 33cn:master Nov 23, 2026
25 checks passed
@33cn

33cn commented Nov 23, 2026

Copy link
Copy Markdown
Owner

🎉 This PR is included in version 1.77.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@33cn 33cn added the released label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants