nimbus-eth1/nimbus
Jamie Lokier c435409292
Sync: Move `blockchain_sync` code and use it with `eth/65`
Move `blockchain_sync.nim` from `nim-eth` to `nimbus-eth1`.

This lets `blockchain_sync` use the `eth/65` protocol to synchronise with more
modern peers than before.

Practically, the effect is the sync process runs more quickly and reliably than
before.  It finds usable peers, and they are up to date.

Note, this is mostly old code, and it mostly performs "classic sync", the
original Ethereum method.  Here's a summary of this code:

- It decides on a blockchain canonical head by sampling a few peers.
- Starting from block 0 (genesis), it downloads each block header and
  block, mostly in order.
- After it downloads each block, it executes the EVM transactions in that block
  and updates state trie from that, before going to the next block.
- This way the database state is updated by EVM executions in block order,
  and new state is persisted to the trie database after each block.

Even though it mentions Geth "fast sync" (comments near end of file), and has
some elements, it isn't really.  The most obvious missing part is this code
_doesn't download a state trie_, it calculates all state from block 0.
Geth "fast sync" has several parts:

1. Find an agreed common chain among several peers to treat as probably secure,
   and a sufficiently long suffix to provide "statistical economic consensus"
   when it is validated.
2. Perform a subset of PoW calculations, skipping forward over a segment to
   verify some of the PoWs according to a pattern in the relevant paper.
3. Download the state trie from the block at the start of that last segment.
4. Execute only the blocks/transactions in that last segment, using the
   downloaded state trie, to fill out the later states and properly validate the
   blocks in the last segment.

Some other issues with `blockchain_sync` code:

- If it ever reaches the head of the chain, it doesn't follow new blocks with
  increasing block numbers, at least not rapidly.
- If the chain undergoes a reorg, this code won't fetch a block number it has
  already fetched, so it can't accept the reorg.  It will end up conflicted
  with peers. This hasn't mattered because the development focus has been on
  the bulk of the catching up process, not the real-time head and reorgs.
- So it probably doesn't work correctly when it gets close to the head due to
  many small reorgs, though it might for subtle reasons.
- Some of the network message handling isn't sufficiently robust, and it
  discards some replies that have valid data according to specification.
- On rare occasions the initial query mapping block hash to number can
  fail (because the peer's state changes).
- It makes some assumptions about the state of peers based on their responses
  which may not be valid (I'm not convinced they are).  The method for working
  out "trusted" peers that agree a common chain prefix is clever.  It compares
  peers by asking each peer if it has the header matching another peer's
  canonical head block by hash.  But it's not clear that merely knowing about a
  block constitutes agreement about the canonical chain.  (If it did, query by
  block number would give the same answer more authoritatively.)

Nonetheless, being able to run this sync process on `eth/65` is useful.

<# interactive rebase in progress; onto 66532e8a

Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-07-27 14:12:53 +01:00
..
db Add a first, simple, content network test (#760) 2021-07-15 15:12:33 +02:00
graphql preparation for EIP-1559 implementation 2021-06-29 07:33:48 +07:00
p2p Sync: Move `blockchain_sync` code and use it with `eth/65` 2021-07-27 14:12:53 +01:00
rpc RPC: Calculate `eth_protocolVersion` from live protocol 2021-07-27 14:12:51 +01:00
sync Eth65: Rename and stop exporting `protocolVersion` 2021-07-27 14:12:52 +01:00
transaction EIP-3529: Replace SSTORE_CLEARS_SCHEDULE in evmc host_service 2021-06-30 20:35:10 +07:00
utils Feature/implement poa processing (#748) 2021-07-14 16:13:27 +01:00
vm EIP-3541: Fixes typo, 0xFE -> 0xEF 2021-06-30 20:44:34 +07:00
vm2 EIP-3541: Fixes typo, 0xFE -> 0xEF 2021-06-30 20:44:34 +07:00
block_types.nim reduce compiler warnings 2019-11-13 21:49:39 +07:00
chain_config.nim EIP-1559: Fee market change for ETH 1.0 chain 2021-06-30 20:30:39 +07:00
conf_utils.nim Jordan/fix some failing nohive tests (#727) 2021-06-24 16:29:21 +01:00
config.nim preparation for London hard fork 2021-06-29 07:34:45 +07:00
constants.nim Clear up meaning of `ZERO_ADDRESS`, delete `CREATE_CONTRACT_ADDRESS` 2021-05-27 12:14:51 +01: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 EIP-1559: Fee market change for ETH 1.0 chain 2021-06-30 20:30:39 +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 Sync: Move `blockchain_sync` code and use it with `eth/65` 2021-07-27 14:12:53 +01:00
random_keys.nim move rng to configuration 2020-07-21 00:16:59 +07:00
tracer.nim Feature/goerli replay clique poa (#743) 2021-07-06 14:14:45 +01:00
transaction.nim EIP-3529: Reduce the max gas refunded after a transaction 2021-06-29 07:37:17 +07:00
utils.nim implement more eth rpc and keystore management 2020-07-23 14:54:32 +07:00
validation.nim reduce compiler warnings 2019-11-13 21:49:39 +07:00
vm_compile_info.nim print compiler warning about the VM used 2021-04-28 15:24:14 +03: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