Closed yangby-cryptape closed 11 months ago
Patch coverage: 100.00%
and project coverage change: +0.06%
:tada:
Comparison is base (
ae04578
) 69.80% compared to head (fc5f1b8
) 69.86%.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
Issue
If there are too many epochs, calculate the upper limit of total difficulty may cause an overflow.
Let $n$ denote the length of epochs which will be synchronized in one message. Let $D{fisrt}$ denote the epoch difficulty of the first block which will be synchronized. Let $D{last}$ denote the epoch difficulty of the last block which will be synchronized. Let $l = \Big \lceil {\log{\tau}{\frac{D{first}}{D{last}}}} \Big \rceil$ denote the length that how many epochs, $D{fisrt}$ can be increased or increased to $D_{last}$.
The upper limit of total difficulty of blocks which will be synchronized is:
$$ D{upper} = D{fisrt} \cdot (\large\sum{i=1}^{\lceil \frac{n+l}{2} \rceil}{\tau^i} + \large\sum{i=1}^{\lceil \frac{n-l}{2} \rceil}{\tau^{\lceil \frac{n+l}{2} \rceil-i}}) $$
If $D_{upper}$ is greater than $2^{256}-1$, the client will be panicked.
Solution
So, we could use a short-circuit algorithm.
Pseudo code to explain the changes:
Previous version is:
New version is: