ethereum / go-ethereum

Go implementation of the Ethereum protocol
https://geth.ethereum.org
GNU Lesser General Public License v3.0
47.25k stars 20k forks source link

Synchronization seems to be going on, but I can't get the blocks. #19660

Closed niutaifan closed 5 years ago

niutaifan commented 5 years ago

System information

Geth version: v1.8.27 OS & Version: Linux Commit hash : (if develop)

Expected behaviour

Actual behaviour

Welcome to the Geth JavaScript console!

instance: Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.11.9 coinbase: 0x3eea7738230131f7f2aebe74fc57d28db5e110e2 at block: 0 (Thu, 01 Jan 1970 08:00:00 CST) datadir: /home/eth/gethdata modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

eth.syncing { currentBlock: 7892470, highestBlock: 7892561, knownStates: 184462880, pulledStates: 184462027, startingBlock: 7279999 } eth.blocNumber undefined eth.blockNumber 0 eth.blockNumber

Steps to reproduce the behaviour

Backtrace

[backtrace]
niutaifan commented 5 years ago

INFO [06-04|19:42:11.135] Imported new state entries count=460 elapsed=2.022ms processed=184483737 pending=474 retry=0 duplicate=255 unexpected=2485 INFO [06-04|19:42:32.641] Imported new block headers count=1 elapsed=10.295ms number=7892620 hash=fc10c0猞c8fb23 age=1m52s INFO [06-04|19:42:45.045] Imported new state entries count=542 elapsed=2.373ms processed=184484279 pending=401 retry=0 duplicate=255 unexpected=2485 INFO [06-04|19:42:56.376] Imported new block headers count=1 elapsed=7.059ms number=7892621 hash=601454猞b6e88c age=2m8s INFO [06-04|19:43:00.037] Imported new block headers count=1 elapsed=6.715ms number=7892622 hash=591822猞051ea0 age=1m8s INFO [06-04|19:43:07.058] Imported new block headers count=1 elapsed=6.750ms number=7892623 hash=b4a815猞a9e413 INFO [06-04|19:43:17.418] Imported new block headers count=1 elapsed=6.794ms number=7892624 hash=647895猞1be8f6 INFO [06-04|19:43:17.989] Imported new state entries count=540 elapsed=2.386ms processed=184484819 pending=328 retry=0 duplicate=255 unexpected=2485 INFO [06-04|19:43:21.097] Imported new block headers count=1 elapsed=6.885ms number=7892625 hash=c08f02猞b58afa INFO [06-04|19:43:41.477] Imported new block headers count=1 elapsed=7.417ms number=7892626 hash=9cfcf3猞e1c500 INFO [06-04|19:43:48.772] Imported new state entries count=500 elapsed=2.212ms processed=184485319 pending=353 retry=0 duplicate=255 unexpected=2485 INFO [06-04|19:44:05.434] Imported new block headers count=1 elapsed=6.928ms number=7892627 hash=aca18e猞4f4c1d INFO [06-04|19:44:12.217] Imported new block headers count=1 elapsed=6.712ms number=7892628 hash=56a84e猞926640 INFO [06-04|19:44:20.864] Imported new state entries count=544 elapsed=2.488ms processed=184485863 pending=193 retry=0 duplicate=255 unexpected=2485 INFO [06-04|19:44:39.277] Imported new block headers count=1 elapsed=6.641ms number=7892629 hash=ff1a2b猞0df405 INFO [06-04|19:44:43.028] Imported new block headers count=1 elapsed=7.554ms number=7892630 hash=0cf849猞daae24 INFO [06-04|19:44:52.302] Imported new state entries count=364 elapsed=1.532ms processed=184486227 pending=329 retry=0 duplicate=255 unexpected=2485 INFO [06-04|19:45:06.676] Imported new block headers count=1 elapsed=6.746ms number=7892631 hash=9a5ffc猞7c6693 INFO [06-04|19:45:20.396] Imported new block headers count=1 elapsed=6.588ms number=7892632 hash=f28e31猞267ef5 INFO [06-04|19:45:23.920] Imported new state entries count=469 elapsed=2.578ms processed=184486696 pending=141 retry=0 duplicate=255 unexpected=2485 INFO [06-04|19:45:30.757] Imported new block headers count=1 elapsed=6.772ms number=7892633 hash=52a73c猞bdba81

karalabe commented 5 years ago

Syncing Ethereum is a pain point for many people, so I'll try to detail what's happening behind the scenes so there might be a bit less confusion.

The current default mode of sync for Geth is called fast sync. Instead of starting from the genesis block and reprocessing all the transactions that ever occurred (which could take weeks), fast sync downloads the blocks, and only verifies the associated proof-of-works. Downloading all the blocks is a straightforward and fast procedure and will relatively quickly reassemble the entire chain.

Many people falsely assume that because they have the blocks, they are in sync. Unfortunately this is not the case, since no transaction was executed, so we do not have any account state available (ie. balances, nonces, smart contract code and data). These need to be downloaded separately and cross checked with the latest blocks. This phase is called the state trie download and it actually runs concurrently with the block downloads; alas it take a lot longer nowadays than downloading the blocks.

So, what's the state trie? In the Ethereum mainnet, there are a ton of accounts already, which track the balance, nonce, etc of each user/contract. The accounts themselves are however insufficient to run a node, they need to be cryptographically linked to each block so that nodes can actually verify that the account's are not tampered with. This cryptographic linking is done by creating a tree data structure above the accounts, each level aggregating the layer below it into an ever smaller layer, until you reach the single root. This gigantic data structure containing all the accounts and the intermediate cryptographic proofs is called the state trie.

Ok, so why does this pose a problem? This trie data structure is an intricate interlink of hundreds of millions of tiny cryptographic proofs (trie nodes). To truly have a synchronized node, you need to download all the account data, as well as all the tiny cryptographic proofs to verify that noone in the network is trying to cheat you. This itself is already a crazy number of data items. The part where it gets even messier is that this data is constantly morphing: at every block (15s), about 1000 nodes are deleted from this trie and about 2000 new ones are added. This means your node needs to synchronize a dataset that is changing 200 times per second. The worst part is that while you are synchronizing, the network is moving forward, and state that you begun to download might disappear while you're downloading, so your node needs to constantly follow the network while trying to gather all the recent data. But until you actually do gather all the data, your local node is not usable since it cannot cryptographically prove anything about any accounts.

If you see that you are 64 blocks behind mainnet, you aren't yet synchronized, not even close. You are just done with the block download phase and still running the state downloads. You can see this yourself via the seemingly endless Imported state entries [...] stream of logs. You'll need to wait that out too before your node comes truly online.


Q: The node just hangs on importing state enties?!

A: The node doesn't hang, it just doesn't know how large the state trie is in advance so it keeps on going and going and going until it discovers and downloads the entire thing.

The reason is that a block in Ethereum only contains the state root, a single hash of the root node. When the node begins synchronizing, it knows about exactly 1 node and tries to download it. That node, can refer up to 16 new nodes, so in the next step, we'll know about 16 new nodes and try to download those. As we go along the download, most of the nodes will reference new ones that we didn't know about until then. This is why you might be tempted to think it's stuck on the same numbers. It is not, rather it's discovering and downloading the trie as it goes along.

Q: I'm stuck at 64 blocks behind mainnet?!

A: As explained above, you are not stuck, just finished with the block download phase, waiting for the state download phase to complete too. This latter phase nowadays take a lot longer than just getting the blocks.

Q: Why does downloading the state take so long, I have good bandwidth?

A: State sync is mostly limited by disk IO, not bandwidth.

The state trie in Ethereum contains hundreds of millions of nodes, most of which take the form of a single hash referencing up to 16 other hashes. This is a horrible way to store data on a disk, because there's almost no structure in it, just random numbers referencing even more random numbers. This makes any underlying database weep, as it cannot optimize storing and looking up the data in any meaningful way.

Not only is storing the data very suboptimal, but due to the 200 modification / second and pruning of past data, we cannot even download it is a properly pre-processed way to make it import faster without the underlying database shuffling it around too much. The end result is that even a fast sync nowadays incurs a huge disk IO cost, which is too much for a mechanical hard drive.

Q: Wait, so I can't run a full node on an HDD?

A: Unfortunately not. Doing a fast sync on an HDD will take more time than you're willing to wait with the current data schema. Even if you do wait it out, an HDD will not be able to keep up with the read/write requirements of transaction processing on mainnet.

You however should be able to run a light client on an HDD with minimal impact on system resources. If you wish to run a full node however, an SSD is your only option.