1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
|
Return-Path: <rich@gotenna.com>
Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138])
by lists.linuxfoundation.org (Postfix) with ESMTP id D59FBC016F
for <bitcoin-dev@lists.linuxfoundation.org>;
Mon, 11 May 2020 12:07:45 +0000 (UTC)
Received: from localhost (localhost [127.0.0.1])
by whitealder.osuosl.org (Postfix) with ESMTP id C02E5878BC
for <bitcoin-dev@lists.linuxfoundation.org>;
Mon, 11 May 2020 12:07:45 +0000 (UTC)
X-Virus-Scanned: amavisd-new at osuosl.org
Received: from whitealder.osuosl.org ([127.0.0.1])
by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id NvyypvHeCzOk
for <bitcoin-dev@lists.linuxfoundation.org>;
Mon, 11 May 2020 12:07:43 +0000 (UTC)
X-Greylist: delayed 00:15:06 by SQLgrey-1.7.6
Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com
[209.85.221.46])
by whitealder.osuosl.org (Postfix) with ESMTPS id 409CB878B0
for <bitcoin-dev@lists.linuxfoundation.org>;
Mon, 11 May 2020 12:07:43 +0000 (UTC)
Received: by mail-wr1-f46.google.com with SMTP id 50so10094293wrc.11
for <bitcoin-dev@lists.linuxfoundation.org>;
Mon, 11 May 2020 05:07:43 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gotenna-com.20150623.gappssmtp.com; s=20150623;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=JHEmbO4Lr6HhCYkabioLlkOgCIo6oujGyrHCqlGBNuI=;
b=V1mbvq/yrdlfLF9P6CKhpLc5jEU/sOVq8+5mM230LX3oRk3onYH0STYNFzVz66sYcE
lEj/95miCpVirkZj9NmtOLlNWvdYA2NvjfRW+NTi+0fs8vc8ev1tGu4eTIEfwxWnP2Km
5dUnuFJ+xzGy7nPCzxu7V3Sbg6VjZPj2PAzsQSK7BmBpBYSm3J+dHVyS7zmUyLOxzoI+
pBdZSI80yZk0DsCPoHe8bZBGZTNz/Ac+cGQBXC3JYFfwuvsicWtldYk+TYy3emVmJibw
OB8D/0+ttGovUd0vt5NjH5MrysySnfiOhCNGTssIQNka5fKLiIbpqYH34Ta4/6dvKs14
B/wA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=JHEmbO4Lr6HhCYkabioLlkOgCIo6oujGyrHCqlGBNuI=;
b=sDjx2YFNX8ONxQokH3po0vSWs1v7waCvoSdu7NpsiWSdXeUGkK8g27dwMOyxhAqjRP
HUQ9hcZSlkQ8VbYh7L/q+FOqCZbG3Sy3shb5beCdXojwtKzaaiYaIdBeP/kNaQQRhdEt
mojXj0J9W7BxTsD3y7TjgF0+tbz6SRuZoz7VKWoNeR4Nm48MpTo3GarwUQJNTZ77pEZr
zHa/jURLtgDtZQ2ppEEor6o/SswGuCDUjpyfKc/V0HdS33luVi5bWWFya4RmLRKukEAv
KD26WyrjCubYOPIGL66zP6PrPEe40LAhv/214LrkqzOVBGtBoa81KmMrIZMS4KWECPfd
zZbQ==
X-Gm-Message-State: AGi0PuapJDd9UOSh9h0xfAQAKRy5SH1hVfF7qamvpGiZ4Jpoy22DjOlV
bX0H3fgBijJ+X/ID88Y+kSzyAGNanM9TXJhMKYa2ZCozxxK8LQ==
X-Google-Smtp-Source: APiQypKevpKjnxO+fNZCJJMDoqAbGEwjAKz8aYOVQAnd33+rsVgFNg1oJr0LpyhaospqLU54pJhYg6Q9AHvs4OUyLF8=
X-Received: by 2002:a17:906:48db:: with SMTP id
d27mr9649868ejt.281.1589197575946;
Mon, 11 May 2020 04:46:15 -0700 (PDT)
MIME-Version: 1.0
References: <40DB3DBE-A1C9-4D20-A3C7-F5660307D9D7@gmail.com>
In-Reply-To: <40DB3DBE-A1C9-4D20-A3C7-F5660307D9D7@gmail.com>
From: Richard Myers <rich@gotenna.com>
Date: Mon, 11 May 2020 13:46:04 +0200
Message-ID: <CACJVCg+dfucWVb8rwJgMdZ-VHiSZ+yoJ568jpTK3-beE+DE9TQ@mail.gmail.com>
To: Will Clark <will8clark@gmail.com>,
Bitcoin Protocol Discussion <bitcoin-dev@lists.linuxfoundation.org>
Content-Type: multipart/alternative; boundary="000000000000fe1fca05a55de4f9"
X-Mailman-Approved-At: Mon, 11 May 2020 12:21:35 +0000
Subject: Re: [bitcoin-dev] Compressed block headers
X-BeenThere: bitcoin-dev@lists.linuxfoundation.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: Bitcoin Protocol Discussion <bitcoin-dev.lists.linuxfoundation.org>
List-Unsubscribe: <https://lists.linuxfoundation.org/mailman/options/bitcoin-dev>,
<mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=unsubscribe>
List-Archive: <http://lists.linuxfoundation.org/pipermail/bitcoin-dev/>
List-Post: <mailto:bitcoin-dev@lists.linuxfoundation.org>
List-Help: <mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=help>
List-Subscribe: <https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev>,
<mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=subscribe>
X-List-Received-Date: Mon, 11 May 2020 12:07:46 -0000
--000000000000fe1fca05a55de4f9
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Thanks for resurrecting this idea for discussion Will.
I see three reasons for reducing block header bandwidth:
1. support for long range block header broadcast via alternative
communication modalities like radio where every byte counts
2. where repurposed mobile devices with SPV wallets are used because
metered bandwidth and hardware costs are high relative to income
3. full nodes could potentially support twice as many header only peers
(is that a thing?) for better eclipse protection
Nodes could also run an additional daemon (eg. electrs) that serves
compressed block headers to light clients, but then it would be less likely
to see widespread use to reduce bandwidth between full nodes.
What are the negatives?
- higher computation? probably minimal compared to serving the same
uncompressed headers.
- memory for caching the last few versions? bounded to last seven, so not
too large.
- complexity/bugs? minor and opt in for node operators, though you could
argue the gain isn't worth any kind of change for nodes with high bandwidth
connections.
- use of low-bandwidth light clients should not be encouraged? that is a
separate discussion, but I do not currently see any proposals to remove
light client support.
I'm curious what other people think. Are the motivations enough to justify
a change to the protocol that produces a high percentage (but low absolute)
bandwidth reduction for transmitting block headers?
-- Richard
On Fri, May 8, 2020 at 3:34 PM Will Clark via bitcoin-dev <
bitcoin-dev@lists.linuxfoundation.org> wrote:
> Hello list,
>
> I would like to propose a compressed block header scheme for IBD and bloc=
k
> announcements. This proposal is derivative of previous proposals found on
> this list (see links in spec below) with some modifications and
> clarifications.
>
> The below specification (also found at
> https://github.com/willcl-ark/compressed-block-headers/blob/v1.0/compress=
ed-block-headers.adoc
> ) details the compression recommended along with the generated bandwidth
> savings in the best-case scenario.
>
> I look forward to any feedback anyone has to offer on the specification
> itself, as well as any additions or objections to the motivation.
>
> Cheers,
> Will
>
>
> =3D Compressed block headers
> Will Clark <will8clark@gmail.com>
> v1.0, May 2020:
> :toc: preamble
> :toclevels: 4
>
>
> This work is a derivation of these mailing list posts:
>
> 1.
> https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2017-August/01487=
6.html[bitcoin-dev:
> "Compressed" headers stream - 2017] (with resurrection
> https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2017-December/015=
385.html[here]
> )
>
> 2.
> https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2018-March/015851=
.html[bitcoin-dev:
> Optimized Header Sync]
>
> '''
>
> =3D=3D Motivation
>
> Block headers as exchanged by nodes over the p2p network are currently 81
> bytes each.
>
> For low bandwidth nodes who are doing a headers-only sync, reducing the
> size of the headers can provide a significant bandwidth saving. Also, nod=
es
> can support more header-only peers for IBD and protection against eclipse
> attacks if header bandwidth is reduced.
>
> =3D=3D=3D Background
>
> Currently headers are sent over the p2p network as a vector of
> `block_headers`, which are composed of the following sized fields:
>
> [cols=3D"<,>"]
> |=3D=3D=3D
> |Field |Size
>
> |Version |4 bytes
> |Previous block hash |32 bytes
> |Merkle root hash |32 bytes
> |Time |4 bytes
> |nBits |4 bytes
> |nonce |4 bytes
> |txn_count |1 byte
> |*Total* |81 bytes
> |=3D=3D=3D
>
> Some fields can be removed completely, others can be compressed under
> certain conditions.
>
> =3D=3D Proposed specification
>
> =3D=3D=3D block_header2 data type
>
> The following table illustrates the proposed `block_header2` data type
> specification.
>
> [cols=3D"<,>,>"]
> |=3D=3D=3D
> |Field |Size |Compressed
>
> |Bitfield |1 byte | 1 byte
> |Version |4 bytes |0 \| 4 bytes
> |Previous block hash |32 bytes |0 \| 32 bytes
> |Merkle root hash |32 bytes |32 bytes
> |Time |4 bytes |2 \| 4 bytes
> |nBits |4 bytes |0 \| 4 bytes
> |nonce |4 bytes |4 bytes
> |*Total* |81 bytes |range: 39 - 81 bytes
> |=3D=3D=3D
>
> This compression results in a maximum reduction from an 81 byte header to
> best-case 39 byte header. With 629,474 blocks in the current blockchain, =
a
> continuous header sync from genesis (requiring a single full 81 byte head=
er
> followed by only compressed `block_header2`) has been tested to have its
> required bandwidth reduced from 50.98MB down to 25.86MB, a saving of 49%.
>
> =3D=3D=3D=3D Bitfield
>
> To make parsing of header messages easier and further increase header
> compression, a single byte bitfield was suggested by gmaxwell footnote:[
> https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2017-December/015=
397.html].
> We propose the following amended bitfield meanings (bits re-ordered to
> match `headers2` field order):
>
> [cols=3D"<,<"]
> |=3D=3D=3D
> |Bit |Meaning + field size to read
>
> |0 +
> 1 +
> 2 |version: same as the last *distinct* value 1st ... 7th (0 byte
> field) or a new 32bit distinct value (4 byte field).
> |3 |prev_block_hash: is omitted (0 byte field) or included (32 byte
> field)
> |4 |timestamp: as small offset (2 byte field) or full (4 byte field).
> |5 |nbits: same as last header (0 byte field) or new (4 byte field).
> |6 |possibly to signal "more headers follow" to make the encoding
> self-delimiting.
> |7 |currently undefined
> |=3D=3D=3D
>
> This bitfield adds 1 byte for every block in the chain, for a current
> total increase of 629,474B.
>
> =3D=3D=3D=3D Version
>
> In most cases the Version field will be identical to one referenced in on=
e
> of the previous 7 unique versions, as indicated by bits 0,1,2 of the
> Bitfield.
>
> To block 629,474 there were 616,137 blocks whose version was in the
> previous 7 distinct versions, and only 13,338 blocks whose version was no=
t,
> this includes any version bit manipulation done via overt ASIC boost.
>
> [cols=3D">,>,>,>"]
> |=3D=3D=3D
> |Genesis to block |Current (B) |Compressed (B) |Saving (%)
>
> |629,474 |2,517,896 |53,352 |98
> |=3D=3D=3D
>
> =3D=3D=3D=3D Previous block hash
>
> The previous block hash will always be the
> `SHA256(SHA256(<previous_header>))` so is redundant, presuming you have
> the previous header in the chain.
>
> [cols=3D">,>,>,>"]
> |=3D=3D=3D
> |Genesis to block |Current (B) |Compressed (B) |Saving (%)
>
> |629,474 |20,143,168 |0 |100
> |=3D=3D=3D
>
> =3D=3D=3D=3D Time
>
> The timestamp (in seconds) is consensus bound, based both on the time in
> the previous
> header: `MAX_FUTURE_BLOCK_TIME =3D 2 * 60 * 60 =3D 7200`, and being great=
er
> than the `MedianTimePast` of the previous 11 blocks. Therefore this can b=
e
> safely represented as an offset from the previous headers' timestamp usin=
g
> a 2 byte `signed short int`.
>
> [cols=3D">,>,>,>"]
> |=3D=3D=3D
> |Genesis to block |Current (B) |Compressed (B) |Saving (%)
>
> |629,474 |2,517,896 |1,258,952 |50
> |=3D=3D=3D
>
> =3D=3D=3D=3D nBits
>
> nBits currently changes once every 2016 blocks. It could be entirely
> calculated by the client from the timestamps of the previous 2015 blocks
> footnote:[2015 blocks are used in the adjustment calculation due to an
> off-by-one error:
> https://bitcointalk.org/index.php?topic=3D43692.msg521772#msg521772"].
>
> To simplify 'light' client implementations which would otherwise require
> consensus-valid calculation of the adjustments, we propose to transmit th=
is
> according to the <<Bitfield>> specification above.
>
> To block 629,474 there have been 298 nBits adjustments (vs an expected 31=
1
> -- there was none before block 32,256).
>
> [cols=3D">,>,>,>"]
> |=3D=3D=3D
> |Genesis to block |Current (B) |Compressed (B) |Saving (%)
>
> |629,474 |2,517,896 |1,196 |99.6
> |=3D=3D=3D
>
> =3D=3D=3D=3D txn_count
>
> txn_count is included to make parsing of these messages compatible with
> parsing of `block` messages footnote:[
> https://bitcoin.stackexchange.com/questions/2104/why-is-the-block-header-=
txn-count-field-always-zero].
> Therefore this field and its associated byte can be removed for
> transmission of compact headers.
>
> [cols=3D">,>,>,>"]
> |=3D=3D=3D
> |Genesis to block |Current (B) |Compressed (B) |Saving (%)
>
> |629,474 |629,474 |0 |100
> |=3D=3D=3D
>
> =3D=3D=3D Service Bit
>
> A new service bit would be required so that the nodes can advertise their
> ability to supply compact headers.
>
> =3D=3D=3D P2P Messages
>
> Three new messages would be used by nodes that enable compact block heade=
r
> support, two query messages: `getheaders2` and `sendheaders2` and one
> response: `headers2`.
>
> =3D=3D=3D=3D `getheaders2` -- Requesting compact headers
>
> The new p2p message required to request compact block headers would
> require the same fields as the current `getheaders` message:
>
> [cols=3D">,<,<,<"]
> |=3D=3D=3D
> |Field Size |Description |Data type |Comments
>
> |4 |version |uint32_t |the protocol version
> |1+ |hash count |var_int |number of block locator has=
h
> entries
> |32+ |block locator hashes |char[32] |block locator object; newes=
t
> back to genesis block (dense to start, but then sparse)
> |32 |hash_stop |char[32] |hash of the last desired
> block header; set to zero to get as many blocks as possible (2000)
> |=3D=3D=3D
>
> =3D=3D=3D=3D `sendheaders2` -- Request compact header announcements
>
> Since
> https://github.com/bitcoin/bips/blob/master/bip-0130.mediawiki[BIP-130],
> nodes have been able to request to receive new headers directly in
> `headers` messages, rather than via an `inv` of the new block hash and
> subsequent `getheader` request and `headers` response (followed by a fina=
l
> `getdata` to get the tip block itself, if desired). This is requested by
> transmitting an empty `sendheaders` message after the version handshake i=
s
> complete.]
>
> Upon receipt of this message, the node is permitted, but not required, to
> preemptively announce new headers with the `headers2` message (instead of
> `inv`). Preemptive header announcement is supported by the protocol versi=
on
> =E2=89=A5 70012 | Bitcoin Core version =E2=89=A5 0.12.0.
>
> For the motivational use-case it makes sense to also update this mechanis=
m
> to support sending header updates using compact headers using a new messa=
ge.
>
> =3D=3D=3D=3D `headers2` -- Receiving compact headers
>
> A `headers2` message is returned in response to `getheaders2` or at new
> header announcement following a `sendheaders2` request. It contains both
> `length` and `headers` fields. The `headers` field contains a variable
> length vector of `block_header2`:
>
> |=3D=3D=3D
> |Field Size |Description |Data type |Comments
>
> |1+ |length |var_int |Length of `headers`
> |39-81x? |headers |block_header2[] |Compressed block headers in
> <<block_header2 data type>> format
> |=3D=3D=3D
>
> =3D=3D=3D Implementation
>
> * The first header in the first `block_header2[]` vector to a
> newly-connected client MUST contain the full nBits`, `timestamp`, `versio=
n`
> and `prev_block_hash` fields, along with a correctly populated `bitfield`
> byte.
> * Subsequent headers in a contiguous vector SHOULD follow the compressed
> <<block_header2 data type>> format.
> * Subsequent compressed headers supplied to an already-connected client
> (requesting compressed headers), SHOULD follow the compressed
> <<block_header2 data type>> format.
>
>
>
>
>
> _______________________________________________
> bitcoin-dev mailing list
> bitcoin-dev@lists.linuxfoundation.org
> https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev
>
--=20
Richard Myers
Decentralized Applications Engineer, goTenna
gotenna.com
@gotenna
--000000000000fe1fca05a55de4f9
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Thanks for resurrecting this idea for discussion Will.<div=
><br></div><div>I see three reasons for reducing block header bandwidth:=C2=
=A0</div><div><br>=C2=A01. support for long range block header broadcast vi=
a alternative communication modalities like radio where every byte counts</=
div><div>=C2=A02. where repurposed mobile devices with SPV wallets are used=
because metered bandwidth and hardware costs are high relative to income</=
div><div>=C2=A03. full nodes could potentially support twice as many header=
only peers (is that a thing?) for better eclipse protection</div><div><br>=
</div><div>Nodes could also run an additional daemon (eg. electrs) that ser=
ves compressed block headers to light clients, but then it would be less li=
kely to see widespread use to reduce bandwidth between full nodes.</div><di=
v><br></div><div>What are the negatives?=C2=A0</div><div>=C2=A0- higher com=
putation? probably minimal compared to serving the same uncompressed header=
s.</div><div>=C2=A0- memory for caching the last few versions? bounded to l=
ast seven, so not too large.</div><div>=C2=A0- complexity/bugs? minor and o=
pt in for node operators, though you could argue the gain isn't worth a=
ny kind of change for nodes with high bandwidth connections.</div><div>=C2=
=A0- use of low-bandwidth light clients should not be encouraged? that is a=
separate discussion, but I do not currently see=C2=A0any proposals to remo=
ve light client support.</div><div><br></div><div>I'm curious what othe=
r people think. Are the motivations enough to justify a change to the proto=
col that produces a high percentage (but low absolute) bandwidth reduction =
for transmitting block headers?</div><div><br></div><div>=C2=A0 -- Richard<=
/div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_a=
ttr">On Fri, May 8, 2020 at 3:34 PM Will Clark via bitcoin-dev <<a href=
=3D"mailto:bitcoin-dev@lists.linuxfoundation.org">bitcoin-dev@lists.linuxfo=
undation.org</a>> wrote:<br></div><blockquote class=3D"gmail_quote" styl=
e=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);paddin=
g-left:1ex">Hello list,<br>
<br>
I would like to propose a compressed block header scheme for IBD and block =
announcements. This proposal is derivative of previous proposals found on t=
his list (see links in spec below) with some modifications and clarificatio=
ns.<br>
<br>
The below specification (also found at <a href=3D"https://github.com/willcl=
-ark/compressed-block-headers/blob/v1.0/compressed-block-headers.adoc" rel=
=3D"noreferrer" target=3D"_blank">https://github.com/willcl-ark/compressed-=
block-headers/blob/v1.0/compressed-block-headers.adoc</a> ) details the com=
pression recommended along with the generated bandwidth savings in the best=
-case scenario.<br>
<br>
I look forward to any feedback anyone has to offer on the specification its=
elf, as well as any additions or objections to the motivation.<br>
<br>
Cheers,<br>
Will<br>
<br>
<br>
=3D Compressed block headers<br>
Will Clark <<a href=3D"mailto:will8clark@gmail.com" target=3D"_blank">wi=
ll8clark@gmail.com</a>><br>
v1.0, May 2020:<br>
:toc: preamble<br>
:toclevels: 4<br>
<br>
<br>
This work is a derivation of these mailing list posts:<br>
<br>
1. <a href=3D"https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2017-=
August/014876.html%5Bbitcoin-dev" rel=3D"noreferrer" target=3D"_blank">http=
s://lists.linuxfoundation.org/pipermail/bitcoin-dev/2017-August/014876.html=
[bitcoin-dev</a>: "Compressed" headers stream - 2017] (with resur=
rection <a href=3D"https://lists.linuxfoundation.org/pipermail/bitcoin-dev/=
2017-December/015385.html%5Bhere%5D" rel=3D"noreferrer" target=3D"_blank">h=
ttps://lists.linuxfoundation.org/pipermail/bitcoin-dev/2017-December/015385=
.html[here]</a>)<br>
<br>
2. <a href=3D"https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2018-=
March/015851.html%5Bbitcoin-dev" rel=3D"noreferrer" target=3D"_blank">https=
://lists.linuxfoundation.org/pipermail/bitcoin-dev/2018-March/015851.html[b=
itcoin-dev</a>: Optimized Header Sync]<br>
<br>
'''<br>
<br>
=3D=3D Motivation<br>
<br>
Block headers as exchanged by nodes over the p2p network are currently 81 b=
ytes each.<br>
<br>
For low bandwidth nodes who are doing a headers-only sync, reducing the siz=
e of the headers can provide a significant bandwidth saving. Also, nodes ca=
n support more header-only peers for IBD and protection against eclipse att=
acks if header bandwidth is reduced.<br>
<br>
=3D=3D=3D Background<br>
<br>
Currently headers are sent over the p2p network as a vector of `block_heade=
rs`, which are composed of the following sized fields:<br>
<br>
[cols=3D"<,>"]<br>
|=3D=3D=3D<br>
|Field=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|Size<br>
<br>
|Version=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|4 bytes<br>
|Previous block hash |32 bytes<br>
|Merkle root hash=C2=A0 =C2=A0 |32 bytes<br>
|Time=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |4 bytes<br>
|nBits=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|4 bytes<br>
|nonce=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|4 bytes<br>
|txn_count=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|1 byte<br>
|*Total*=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|81 bytes<br>
|=3D=3D=3D<br>
<br>
Some fields can be removed completely, others can be compressed under certa=
in conditions.<br>
<br>
=3D=3D Proposed specification<br>
<br>
=3D=3D=3D block_header2 data type<br>
<br>
The following table illustrates the proposed `block_header2` data type spec=
ification.<br>
<br>
[cols=3D"<,>,>"]<br>
|=3D=3D=3D<br>
|Field=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|Size=C2=A0 =
=C2=A0 =C2=A0|Compressed<br>
<br>
|Bitfield=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |1 byte=C2=A0 =C2=A0| 1 =
byte<br>
|Version=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|4 bytes=C2=A0 |0 \=
| 4 bytes<br>
|Previous block hash |32 bytes |0 \| 32 bytes<br>
|Merkle root hash=C2=A0 =C2=A0 |32 bytes |32 bytes<br>
|Time=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |4 bytes=C2=A0=
|2 \| 4 bytes<br>
|nBits=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|4 bytes=C2=A0=
|0 \| 4 bytes<br>
|nonce=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|4 bytes=C2=A0=
|4 bytes<br>
|*Total*=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|81 bytes |range: 3=
9 - 81 bytes<br>
|=3D=3D=3D<br>
<br>
This compression results in a maximum reduction from an 81 byte header to b=
est-case 39 byte header. With 629,474 blocks in the current blockchain, a c=
ontinuous header sync from genesis (requiring a single full 81 byte header =
followed by only compressed `block_header2`) has been tested to have its re=
quired bandwidth reduced from 50.98MB down to 25.86MB, a saving of 49%.<br>
<br>
=3D=3D=3D=3D Bitfield<br>
<br>
To make parsing of header messages easier and further increase header compr=
ession, a single byte bitfield was suggested by gmaxwell footnote:[<a href=
=3D"https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2017-December/0=
15397.html" rel=3D"noreferrer" target=3D"_blank">https://lists.linuxfoundat=
ion.org/pipermail/bitcoin-dev/2017-December/015397.html</a>]. We propose th=
e following amended bitfield meanings (bits re-ordered to match `headers2` =
field order):<br>
<br>
[cols=3D"<,<"]<br>
|=3D=3D=3D<br>
|Bit |Meaning + field size to read<br>
<br>
|0 +<br>
1 +<br>
2=C2=A0 =C2=A0 |version: same as the last *distinct* value 1st ... 7th (0 b=
yte field) or a new 32bit distinct value (4 byte field).<br>
|3=C2=A0 =C2=A0|prev_block_hash: is omitted (0 byte field) or included (32 =
byte field)<br>
|4=C2=A0 =C2=A0|timestamp: as small offset (2 byte field) or full (4 byte f=
ield).<br>
|5=C2=A0 =C2=A0|nbits: same as last header (0 byte field) or new (4 byte fi=
eld).<br>
|6=C2=A0 =C2=A0|possibly to signal "more headers follow" to make =
the encoding self-delimiting.<br>
|7=C2=A0 =C2=A0|currently undefined<br>
|=3D=3D=3D<br>
<br>
This bitfield adds 1 byte for every block in the chain, for a current total=
increase of 629,474B.<br>
<br>
=3D=3D=3D=3D Version<br>
<br>
In most cases the Version field will be identical to one referenced in one =
of the previous 7 unique versions, as indicated by bits 0,1,2 of the Bitfie=
ld.<br>
<br>
To block 629,474 there were 616,137 blocks whose version was in the previou=
s 7 distinct versions, and only 13,338 blocks whose version was not, this i=
ncludes any version bit manipulation done via overt ASIC boost.<br>
<br>
[cols=3D">,>,>,>"]<br>
|=3D=3D=3D<br>
|Genesis to block |Current (B) |Compressed (B) |Saving (%)<br>
<br>
|629,474=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |2,517,896=C2=A0 =C2=A0|53,352=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|98<br>
|=3D=3D=3D<br>
<br>
=3D=3D=3D=3D Previous block hash<br>
<br>
The previous block hash will always be the<br>
`SHA256(SHA256(<previous_header>))` so is redundant, presuming you ha=
ve the previous header in the chain.<br>
<br>
[cols=3D">,>,>,>"]<br>
|=3D=3D=3D<br>
|Genesis to block |Current (B) |Compressed (B) |Saving (%)<br>
<br>
|629,474=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |20,143,168=C2=A0 |0=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |100<br>
|=3D=3D=3D<br>
<br>
=3D=3D=3D=3D Time<br>
<br>
The timestamp (in seconds) is consensus bound, based both on the time in th=
e previous<br>
header: `MAX_FUTURE_BLOCK_TIME =3D 2 * 60 * 60 =3D 7200`, and being greater=
than the `MedianTimePast` of the previous 11 blocks. Therefore this can be=
safely represented as an offset from the previous headers' timestamp u=
sing a 2 byte `signed short int`.<br>
<br>
[cols=3D">,>,>,>"]<br>
|=3D=3D=3D<br>
|Genesis to block |Current (B) |Compressed (B) |Saving (%)<br>
<br>
|629,474=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |2,517,896=C2=A0 =C2=A0|1,258,95=
2=C2=A0 =C2=A0 =C2=A0 |50<br>
|=3D=3D=3D<br>
<br>
=3D=3D=3D=3D nBits<br>
<br>
nBits currently changes once every 2016 blocks. It could be entirely calcul=
ated by the client from the timestamps of the previous 2015 blocks footnote=
:[2015 blocks are used in the adjustment calculation due to an off-by-one e=
rror: <a href=3D"https://bitcointalk.org/index.php?topic=3D43692.msg521772#=
msg521772" rel=3D"noreferrer" target=3D"_blank">https://bitcointalk.org/ind=
ex.php?topic=3D43692.msg521772#msg521772</a>"].<br>
<br>
To simplify 'light' client implementations which would otherwise re=
quire consensus-valid calculation of the adjustments, we propose to transmi=
t this according to the <<Bitfield>> specification above.<br>
<br>
To block 629,474 there have been 298 nBits adjustments (vs an expected 311 =
-- there was none before block 32,256).<br>
<br>
[cols=3D">,>,>,>"]<br>
|=3D=3D=3D<br>
|Genesis to block |Current (B) |Compressed (B) |Saving (%)<br>
<br>
|629,474=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |2,517,896=C2=A0 =C2=A0|1,196=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |99.6<br>
|=3D=3D=3D<br>
<br>
=3D=3D=3D=3D txn_count<br>
<br>
txn_count is included to make parsing of these messages compatible with par=
sing of `block` messages footnote:[<a href=3D"https://bitcoin.stackexchange=
.com/questions/2104/why-is-the-block-header-txn-count-field-always-zero" re=
l=3D"noreferrer" target=3D"_blank">https://bitcoin.stackexchange.com/questi=
ons/2104/why-is-the-block-header-txn-count-field-always-zero</a>]. Therefor=
e this field and its associated byte can be removed for transmission of com=
pact headers.<br>
<br>
[cols=3D">,>,>,>"]<br>
|=3D=3D=3D<br>
|Genesis to block |Current (B) |Compressed (B) |Saving (%)<br>
<br>
|629,474=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |629,474=C2=A0 =C2=A0 =C2=A0|0=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |100<br>
|=3D=3D=3D<br>
<br>
=3D=3D=3D Service Bit<br>
<br>
A new service bit would be required so that the nodes can advertise their a=
bility to supply compact headers.<br>
<br>
=3D=3D=3D P2P Messages<br>
<br>
Three new messages would be used by nodes that enable compact block header =
support, two query messages: `getheaders2` and `sendheaders2` and one respo=
nse: `headers2`.<br>
<br>
=3D=3D=3D=3D `getheaders2` -- Requesting compact headers<br>
<br>
The new p2p message required to request compact block headers would require=
the same fields as the current `getheaders` message:<br>
<br>
[cols=3D">,<,<,<"]<br>
|=3D=3D=3D<br>
|Field Size |Description=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |Data type |Comm=
ents<br>
<br>
|4=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |version=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 |uint32_t=C2=A0 |the protocol version<br>
|1+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|hash count=C2=A0 =C2=A0 =C2=A0 =C2=A0=
=C2=A0 =C2=A0|var_int=C2=A0 =C2=A0|number of block locator hash entries<br=
>
|32+=C2=A0 =C2=A0 =C2=A0 =C2=A0 |block locator hashes |char[32]=C2=A0 |bloc=
k locator object; newest back to genesis block (dense to start, but then sp=
arse)<br>
|32=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|hash_stop=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 |char[32]=C2=A0 |hash of the last desired block header; set t=
o zero to get as many blocks as possible (2000)<br>
|=3D=3D=3D<br>
<br>
=3D=3D=3D=3D `sendheaders2` -- Request compact header announcements<br>
<br>
Since <a href=3D"https://github.com/bitcoin/bips/blob/master/bip-0130.media=
wiki%5BBIP-130%5D" rel=3D"noreferrer" target=3D"_blank">https://github.com/=
bitcoin/bips/blob/master/bip-0130.mediawiki[BIP-130]</a>, nodes have been a=
ble to request to receive new headers directly in `headers` messages, rathe=
r than via an `inv` of the new block hash and subsequent `getheader` reques=
t and `headers` response (followed by a final `getdata` to get the tip bloc=
k itself, if desired). This is requested by transmitting an empty `sendhead=
ers` message after the version handshake is complete.]<br>
<br>
Upon receipt of this message, the node is permitted, but not required, to p=
reemptively announce new headers with the `headers2` message (instead of `i=
nv`). Preemptive header announcement is supported by the protocol version =
=E2=89=A5 70012 | Bitcoin Core version =E2=89=A5 0.12.0.<br>
<br>
For the motivational use-case it makes sense to also update this mechanism =
to support sending header updates using compact headers using a new message=
.<br>
<br>
=3D=3D=3D=3D `headers2` -- Receiving compact headers<br>
<br>
A `headers2` message is returned in response to `getheaders2` or at new hea=
der announcement following a `sendheaders2` request. It contains both `leng=
th` and `headers` fields. The `headers` field contains a variable length ve=
ctor of `block_header2`:<br>
<br>
|=3D=3D=3D<br>
|Field Size |Description |Data type=C2=A0 =C2=A0 =C2=A0 =C2=A0|Comments<br>
<br>
|1+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|length=C2=A0 =C2=A0 =C2=A0 |var_int=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|Length of `headers`<br>
|39-81x?=C2=A0 =C2=A0 |headers=C2=A0 =C2=A0 =C2=A0|block_header2[] |Compres=
sed block headers in <<block_header2 data type>> format<br>
|=3D=3D=3D<br>
<br>
=3D=3D=3D Implementation<br>
<br>
* The first header in the first `block_header2[]` vector to a newly-connect=
ed client MUST contain the full nBits`, `timestamp`, `version` and `prev_bl=
ock_hash` fields, along with a correctly populated `bitfield` byte.<br>
* Subsequent headers in a contiguous vector SHOULD follow the compressed &l=
t;<block_header2 data type>> format.<br>
* Subsequent compressed headers supplied to an already-connected client (re=
questing compressed headers), SHOULD follow the compressed <<block_he=
ader2 data type>> format.<br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
bitcoin-dev mailing list<br>
<a href=3D"mailto:bitcoin-dev@lists.linuxfoundation.org" target=3D"_blank">=
bitcoin-dev@lists.linuxfoundation.org</a><br>
<a href=3D"https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev" =
rel=3D"noreferrer" target=3D"_blank">https://lists.linuxfoundation.org/mail=
man/listinfo/bitcoin-dev</a><br>
</blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"=
class=3D"gmail_signature">Richard Myers<br>Decentralized Applications Engi=
neer, goTenna<br><a href=3D"http://gotenna.com" target=3D"_blank">gotenna.c=
om</a><br>@gotenna</div>
--000000000000fe1fca05a55de4f9--
|