ethereum / go-ethereum

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

Sync stopped before 100 blocks behind #22174

Closed Jorgeadverd closed 3 years ago

Jorgeadverd commented 3 years ago

Hi All,

We have been setup new node on our server and after sync almost complete it stuck almost 100 blocks behind. And we wait for 6-7 hours but it's always showing the BlockNumber : 0

Also if we have removed old mainnet node and setup new node again, what are the major steps we need to follow ?

we have been following these article to setup node and it's working fine on testner rinkeby but on mainnet it's not able to sync completely.

https://www.quiknode.io/guides/infrastructure/how-to-install-and-run-a-geth-node instance: Geth/v1.8.2-stable/linux-amd64/go1.9.4 blockNumber: 0,

Could you please suggest us what we are missing here, Thanks in Advance.

holiman commented 3 years ago

Bottled response:

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.

holiman commented 3 years ago

Also, Geth/v1.8.2-stable/linux-amd64/go1.9.4 is way old, it'll be three years this march. Use a recent build

Jorgeadverd commented 3 years ago

Hi @holiman

We have setup another node with below version get version - Geth/v1.9.13-stable-cbc4ac26/linux-amd64/go1.14.2

Also from last 6-7 hours we are just getting 70-100 Blocks behind and still sync not completed yet, can below details as well

eth.syncing { currentBlock: 11655003, highestBlock: 11655073, knownStates: 517084680, pulledStates: 516977987, startingBlock: 11654085 } eth.syncing { currentBlock: 11655003, highestBlock: 11655073, knownStates: 517110778, pulledStates: 517006812, startingBlock: 11654085 } eth.syncing { currentBlock: 11655003, highestBlock: 11655073, knownStates: 517125025, pulledStates: 517013346, startingBlock: 11654085 } eth.syncing { currentBlock: 11655003, highestBlock: 11655073, knownStates: 517142152, pulledStates: 517025256, startingBlock: 11654085 } ^C ^C eth.syncing { currentBlock: 11655003, highestBlock: 11655082, knownStates: 517908121, pulledStates: 517824695, startingBlock: 11654085 } eth.syncing { currentBlock: 11655003, highestBlock: 11655082, knownStates: 517911865, pulledStates: 517827650, startingBlock: 11654085 } eth.syncing { currentBlock: 11655003, highestBlock: 11655082, knownStates: 517916042, pulledStates: 517831869, startingBlock: 11654085 }

holiman commented 3 years ago

Please read the text above. 

Jorgeadverd commented 3 years ago

Hi Holiman,

We had wait till now and it's almost 12 hours, and we found below result in morning right now, Just wondering we are on right direction or did something wrong while doing setup.

syncing: { currentBlock: 11657742, highestBlock: 11657810, knownStates: 616653463, pulledStates: 616635562, startingBlock: 11654085 },

Appreciate your support

holiman commented 3 years ago

Yes, it looks like you're on track

Jorgeadverd commented 3 years ago

Hi @holiman

Thanks for support, And we are happy to hear that we are on right track, Just wondering how much time it will take more on average basis ?

eth.syncing { currentBlock: 11659091, highestBlock: 11659176, knownStates: 625344322, pulledStates: 625334817, startingBlock: 11654085 }

holiman commented 3 years ago

Average is hard to say, on a good infra it can be ~6 hours (on our benchmarkers which have huge RAM, fast nvme ssds, public ip addresses and good connectivity). A more common case is a day or two. More notorious cases, where e.g. the network latency is worse, or the disk speeds aren't great, it takes even longer. The longer the sync takes, the more inefficient it becomes, since the chain moves as the sync progresses. A really fast sync only needs to pull somewhere around ~680M states, whereas a week-long sync needs to pull in maybe 8-900 M states, or more.

Jorgeadverd commented 3 years ago

eth.syncing { currentBlock: 11659351, highestBlock: 11659432, knownStates: 626823455, pulledStates: 626815961, startingBlock: 11654085 }

And we are still in syncing mode from almost last 18 hours, also below are the specifications of Hardware>

 *-memory
      description: System memory
      physical id: 0
      size: 15GiB
 *-cpu
      product: Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
      vendor: Intel Corp.
      physical id: 1
      bus info: cpu@0
      width: 64 bits

And SSD with 1.5 T

Jorgeadverd commented 3 years ago

Hi @holiman

Thanks for you support and right direction provided, Finally we had wait till around 48 Hours and Sync has been completed. Also we are able to fetch all operations now from the private node.

Many thanks for your support @holiman