nimbus-eth1/nimbus
Jamie Lokier 5a5edb392a Bugfix: Incorrect processing of self-destructed, new contract
Fixes #868 "Gas usage consensus error at Mainnet block 6001128", and equivalent
on other networks.  Mainnet sync is able to continue past 6001128 after this.

Here's a trace:

```
TRC 2021-09-29 15:13:21.532+01:00 Persisting blocks                  file=persist_blocks.nim:43 fromBlock=6000961 toBlock=6001152
...
DBG 2021-09-29 15:14:35.925+01:00 gasUsed neq cumulativeGasUsed      file=process_block.nim:68 gasUsed=7999726 cumulativeGasUsed=7989726
TRC 2021-09-29 15:14:35.925+01:00 peer disconnected                  file=blockchain_sync.nim:407 peer=<PEER:IP>
```

Similar output is seen at many blocks in the range 6001128..6001204.

The bug is when handling a combination of `CREATE` or `CREATE2`, along with
`SELFDESTRUCT` applied to the new contract address.

Init code for a contract can't return non-empty code and do `SELFDESTRUCT` at
the same time, because `SELFDESTRUCT` returns empty data.

But it is possible to return non-empty code in a newly created, self-destructed
account if the init code calls `DELEGATECALL` or `CALLCODE` to other code which
uses `SELFDESTRUCT`.

In this case we must still charge gas and write the code.  This shows on
Mainnet blocks 6001128..6001204, where the gas difference matters.  The code
must be written because the new code can be called later in the transaction
too, before self-destruction wipes the account at the end.

There are actually three semantic changes here for a self-destructed, new
contract:

- Gas is charged.
- The code is written to the account.
- It can fail due to insufficient gas.

This patch almost exactly reverts a15805e4 "fix applyCreateMessage" from
2019-02-28.  I wonder what that fixed.

Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-10-19 14:24:46 +01:00
..
accounts config: remove accounts management from NimbusConfiguration 2021-09-07 22:02:29 +07:00
db fixes nimbus evm tracer, add missing networkParams when constructing chainDB 2021-09-26 10:45:52 +07:00
graphql grpahql: add EIP-2718 and EIP-1559 features to graphql API 2021-09-21 13:35:49 +07:00
p2p Sync fix: `GetBlockBodies` logic preventing sync, dropping peers 2021-10-19 10:20:26 +01:00
rpc config: fix new config based on input from jamie and zahary 2021-09-18 17:34:51 +07:00
sync code cleanup removed unused imports 2021-08-18 10:35:36 +07:00
transaction make macro_assembler to use the same testEvmCall 2021-10-14 15:10:12 +07:00
utils remove unused calcGasLimit code 2021-08-24 18:30:52 +07:00
vm Bugfix: Incorrect processing of self-destructed, new contract 2021-10-19 14:24:46 +01:00
vm2 Bugfix: Incorrect processing of self-destructed, new contract 2021-10-19 14:24:46 +01:00
block_types.nim reduce compiler warnings 2019-11-13 21:49:39 +07:00
chain_config.nim config: fix new config based on input from jamie and zahary 2021-09-18 17:34:51 +07:00
conf_utils.nim config: replace stdlib parseOpt with nim-confutils 2021-09-18 17:34:46 +07:00
config.nim config: fix new config based on input from jamie and zahary 2021-09-18 17:34:51 +07:00
constants.nim Bugfix: Off by 1 in EIP-170 code size checks in `stateless` 2021-10-19 10:30:53 +01:00
context.nim config: remove global rng from NimbusConfiguration 2021-09-07 22:02:29 +07:00
errors.nim remove unused eraseReturnData 2020-01-20 18:36:58 +02:00
forks.nim preparation for London hard fork 2021-06-29 07:34:45 +07:00
genesis.nim config: fix new config based on input from jamie and zahary 2021-09-18 17:34:51 +07:00
genesis_alloc.nim add goerli testnet genesis data[skip ci] 2020-04-09 19:13:17 +03:00
launcher.nim Premix-related changes 2019-01-28 12:38:23 +02:00
lightchain_shell.nim Some renames 2018-05-28 13:22:28 +03:00
makefile Basic tests for Clique PoA/Consensus engine 2021-06-17 08:03:57 +01:00
nim.cfg NAT port mapping 2019-04-17 03:56:28 +02:00
nimbus.nim config: fix new config based on input from jamie and zahary 2021-09-18 17:34:51 +07:00
sealer.nim config: replace stdlib parseOpt with nim-confutils 2021-09-18 17:34:46 +07:00
tracer.nim fixes nimbus evm tracer, add missing networkParams when constructing chainDB 2021-09-26 10:45:52 +07:00
transaction.nim EIP-3529: Reduce the max gas refunded after a transaction 2021-06-29 07:37:17 +07:00
utils.nim config: remove accounts management from NimbusConfiguration 2021-09-07 22:02:29 +07:00
validation.nim reduce compiler warnings 2019-11-13 21:49:39 +07:00
vm_compile_info.nim config: replace stdlib parseOpt with nim-confutils 2021-09-18 17:34:46 +07:00
vm_computation.nim EVM: Apply EIP-6 in the code (affects both vm and vm2) 2021-06-08 15:36:30 +01:00
vm_gas_costs.nim EIP-3529: Replace SSTORE_CLEARS_SCHEDULE 2021-06-29 07:37:17 +07:00
vm_internals.nim vm2: Remove vm2 `forks_list` everywhere, use common forks list 2021-06-08 15:36:31 +01:00
vm_precompiles.nim re-named some v2*.nim sources to its original name *.nim (without the v2) 2021-04-28 15:24:14 +03:00
vm_state.nim EIP-1559: Fee market change for ETH 1.0 chain 2021-06-30 20:30:39 +07:00
vm_state_transactions.nim Transaction: Prepare txRefundGas to support txCallEvm 2021-05-03 19:51:20 +01:00
vm_types.nim re-named some v2*.nim sources to its original name *.nim (without the v2) 2021-04-28 15:24:14 +03:00