Research/Bitcoin Dev Archive / lightning-dev list
Minisketch and lightning gossip
2 messages, Dec 12, 2018. Started by Rusty Russell.
Rusty Russell, Pieter Wuille
Hi all,
In case you're bored with the limited range of improvements going into the 1.1 spec, you might like to ruminate on:
https://github.com/sipa/minisketch/blob/master/README.md
It's a library for efficient summaries of data, such as bitcoin transaction gossip. It has an implementation sweet-spot at 64-bits, which almost works well for our gossip messages. I've a straw-man protocol below.
===
1. type: 260 (`gossip_sync`) (`option_gossip_sync`)
2. data:
* [`32`:`chain_hash`]
* [`32`:`latest_block_hash`]
* [`minisketch_len`:`minisketch`]
The `latest_block_hash` is because the whole sync is less reliable if this differs between nodes, so a node may choose to wait, or adapt accordingly if the other node is behind.
Because there is some overhead in maintaining the minisketch, it'd be nice if we can have global agreement on the format so each peer need only maintain one (i.e. no seed, no changing encodings depending on block height, etc).
Fitting everything we need into 64 bits is possible, with various degrees of ugliness; I've proposed one here:
We currently use a short_channel_id which has 3-byte block height, 3-byte txindex, 2-byte output index. The biggest win is to combine block height & txindex into a "txnumber since block 500,000", which only needs about 27 bits per year; 40 bits for this number is sufficient for the forseeable future. 2 bits for type, 1 for channel direction, leaving 21 bits for output number and timestamp.
We can encode output number as N ones followed by a zero followed by N*2+1 bits (commonly, this means 2 bits, but future mixers may make this much larger). The remaining bits are used as the lower bits of the timestamp[1].
A node announcement is encoded by using the scid of the oldest channel associated with the node, and the direction bit.
Using this direct encoding (rather than a hash of values) allows us to immediately use an INV-style query, or maybe send automatically the stuff the peer doesn't have.
The required size of the minisketch we send depends on the number of differences with our peer. We can use some minimum value (maybe based on past gossip rates?), and add the number of changes we've received since last time, increasing if we failed to reconstruct a previous one.
There doesn't need to be consensus between peers on the minisketch size though, since truncated minisketches regrow into whole ones when tossed back into the ocean[3].
Cheers,
Rusty.
[1] You're supposed to refresh gossip every week, 17 bits should
be sufficient. And since the originator controls timestamps
they can mitigate collisions themselves.[2]
[2] I wish I'd insisted we use block numbers for timestamps though
[3] I may have misunderstood this part, but it's basically magic.On Tue, 11 Dec 2018 at 18:07, Rusty Russell <rusty at rustcorp.com.au> wrote:>
Quoted text (11 lines)
> Hi all, > > In case you're bored with the limited range of improvements > going into the 1.1 spec, you might like to ruminate on: > > https://github.com/sipa/minisketch/blob/master/README.md > > It's a library for efficient summaries of data, such as bitcoin > transaction gossip. It has an implementation sweet-spot at 64-bits, > which almost works well for our gossip messages. I've a straw-man > protocol below.
Cool to see there is interesting in exploring using minisketch for this application.
Generally when trying to minimize the amount of data to transfer, I think you should pick a field size that is appropriate for the data to send, or if you're hashing the data, a size that gives an acceptable collision rate. In case it isn't quite clear what the trade-off is, you can prefer a size that permits faster implementation (like 15, 22, 28, 30, 46, 60, or 64 bits).
Also, if there is a use for, it wouldn't be too much work to support fields over 64 bits in size. I expect it'd come at a ~2x performance reduction (compared to the expected continuation of the performance in function of field size), but we'd need to benchmark to be sure.
Quoted text (4 lines)
> The required size of the minisketch we send depends on the number of > differences with our peer. We can use some minimum value (maybe based > on past gossip rates?), and add the number of changes we've received > since last time, increasing if we failed to reconstruct a previous one.
There is some research on estimating the sizes of differences that are linked to in https://github.com/sipa/minisketch/blob/master/doc/protocoltips.md which may be useful.
There are also techniques based on subdivision. In such a model, both sides maintain a separate sketch for (say) the first and the second half of the domain (one for all elements starting with leading bit 0, and one for all elements with leading bit 1). Let's call those sketches A1, A2, B1, B2. * A initially sends A12=merge(A1,A2) (a sketch of the entire set). * B tries to reconcile A12 against B12=merge(B1,B2). If that works, all good; otherwise B responds "I needz moar". * A now just sends its first sketch A1 (and not A2) * B reconciles A1 against B1, and also reconciles merge(A1,A12) against B2.
The trick here is that merge(A1,A12) is equal to A2, but A2 never needed transferring; it was computed by combining the two sketches B already received. This can be done several levels deep of course if needed, and reduces the amount of "overestimation" needed for likely reconciliation in exchange for more roundtrips (one per level) in case of larger differences.
Quoted text (3 lines)
> There doesn't need to be consensus between peers on the minisketch size > though, since truncated minisketches regrow into whole ones when tossed > back into the ocean[3].
Eh... if you have a sketch of size p and a sketch of size q, you can reconcile up to min(p,q) elements with them. They don't need to agree in size, but the excess of one size is not useful.
Cheers,