mimblewimble / grin

Minimal implementation of the Mimblewimble protocol.
https://grin.mw/
Apache License 2.0
5.04k stars 992 forks source link

[PIBD_IMPL] BitmapAccumulator Serialization Fix #3705

Closed yeastplume closed 2 years ago

yeastplume commented 2 years ago

Fixes a crucial issue in which peers serializing BitmapAccumulator blocks could potentially either select the incorrect serialization mode for the block or write an incorrect length for the number of negative entries written. (This, in turn could potentially cause a panic on deserialization causing PIBD to fail).

When sending an block that isn't full (i.e. contains less than 2^16 bits), the previous version of the code was failing to remove the unused bits from calculations concerning the number of entries, causing the read length to be incorrectly written (or the wrong serialization mode to be chosen). This fix adjusts the calculation with the difference between the max block size and the actual.

Also adds another capability, PIBD_HIST_1, which peers will now need to respond with before another peer can request a PIBD segment from them, to ensure they have this fix.

Also adds unit tests to exercise the negative failure case for non-full blocks, (as well as the positive instance).