spacemeshos / go-spacemesh

Go Implementation of the Spacemesh protocol full node. 💾⏰💪
https://spacemesh.io
MIT License
752 stars 211 forks source link

syncer: minimize number and size of requests in ForkFinder #4517

Open fasmat opened 1 year ago

fasmat commented 1 year ago

Description

The ForkFinder in the syncer package at the moment requests the aggregated hashes of multiple layers at once, up to MaxHashesInReq (100) to find where the local state started differing from a peers state. This is can be made more efficient by instead using a binary search:

The local node should request the aggregated hash in the middle between the From and To and then narrow the range until the first divergent layer is found. This limits the number of requests and hashes to log2(To - From) and removes the need for checks on the client and server side limiting the amount of layers that are requested at once.

Affected code

For more info see discussions in PR https://github.com/spacemeshos/go-spacemesh/pull/4480/

fasmat commented 1 year ago

As an alternative the ForkFinder can also just request a stream of layer hashes covering the range of From and To, compare hashes and stop the stream as soon as the first divergent layer is found.

fasmat commented 1 year ago

Before changing the implementation we should performance test the current solution and answer the following questions:

The larger the usual search range is the better a binary search should perform compared to a streaming solution, unless it is more likely to find the forking layer close to the beginning (or end) of the search range.

Additionally it needs to be tested if fewer large request or more smaller requests are more efficient (requesting up to 100 at once vs. requesting only 1 layer).