forked from w3f/polkadot-spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc06-consensus.tm
2215 lines (1783 loc) · 83 KB
/
c06-consensus.tm
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<TeXmacs|1.99.16>
<project|host-spec.tm>
<style|<tuple|tmbook|algorithmacs-style>>
<\body>
<chapter|Consensus><label|chap-consensu>
Consensus in the Polkadot Host is achieved during the execution of two
different procedures. The first procedure is the block-production and the
second is finality. The Polkadot Host must run these procedures if (and
only if) it is running on a validator node.
<section|Common Consensus Structures>
<subsection|Consensus Authority Set><label|sect-authority-set>
Because Polkadot is a proof-of-stake protocol, each of its consensus
engines has its own set of nodes represented by known public keys, which
have the authority to influence the protocol in pre-defined ways explained
in this Section. To verify the validity of each block, the Polkadot node
must track the current list of authorities for that block as formalized in
Definition <reference|defn-authority-list>
<\definition>
<label|defn-authority-list>The <strong|authority list> of block
<math|<math-it|B>> for consensus engine <math|C> noted as
<strong|<math|Auth<rsub|C><around*|(|B|)>>> is an array that contains the
following pair of types for each of its authorities
<math|A\<in\>Auth<rsub|C><around*|(|B|)> >:
<\equation*>
<around*|(|pk<rsub|A>,w<rsub|A>|)>
</equation*>
<math|pk<rsub|A>> is the session public key of authority A as defined in
Definition <reference|defn-session-key>. And <math|w<rsub|A>> is a
<verbatim|u64> value, indicating the authority weight. The value of
<math|Auth<rsub|C><around*|(|B|)>> is part of the Polkadot state. The
value for <math|Auth<rsub|C><around*|(|B<rsub|0>|)>> is set in the
genesis state (see Section <reference|sect-genesis-block>) and can be
retrieved using a runtime entry corresponding to consensus engine
<math|C>.
</definition>
<\remark>
In Polkadot, all authorities have the weight <math|w<rsub|A><rsub|>=1>.
The weight <math|w<rsub|A>> in Definition <reference|defn-authority-list>
exists for potential improvements in the protocol and could have a
use-case in the future.
</remark>
<subsection|Runtime-to-Consensus Engine
Message><label|sect-consensus-message-digest>
The authority list (see Definition <reference|defn-authority-list>) is part
of the Polkadot state and the Runtime has the authority to update this list
in the course of any state transitions. The Runtime informs the
corresponding consensus engine about the changes in the authority set by
adding the appropriate consensus message as defined in Definition
<reference|defn-consensus-message-digest>, in the form of a digest item to
the block header of block <math|B> which caused the transition in the
authority set.\
<\definition>
<label|defn-consensus-message-digest> Consensus Message is a digest item
of type 4 as defined in Definition <reference|defn-digest> and consists
of the pair:
<\equation*>
<around*|(|E<rsub|id>,CM|)>
</equation*>
Where <math|E<rsub|id>\<in\>\<bbb-B\><rsub|4>> is the consensus engine
unique identifier which can hold the following possible values
\;
<\equation*>
E<rsub|id>\<assign\><around*|{|<tabular*|<tformat|<table|<row|<cell|<rprime|''>BABE<rprime|''>>|<cell|>|<cell|For
messages related to BABE protocol referred to as
E<rsub|id><around*|(|BABE|)>>>|<row|<cell|<rprime|''>FRNK<rprime|''>>|<cell|>|<cell|For
messages related to GRANDPA protocol referred to as
E<rsub|id><around*|(|FRNK|)>>>>>>|\<nobracket\>>
</equation*>
and CM is of varying data type which can hold one of the types described
in Table <reference|tabl-consensus-messages-babe> or
<reference|tabl-consensus-messages-grandpa>:
<\center>
<\small-table|<tabular|<tformat|<cwith|1|1|1|-1|cell-tborder|0ln>|<cwith|1|1|1|-1|cell-bborder|1ln>|<cwith|2|2|1|-1|cell-tborder|1ln>|<cwith|1|1|1|1|cell-tborder|0ln>|<cwith|1|-1|1|1|cell-lborder|0ln>|<cwith|1|1|2|2|cell-tborder|0ln>|<cwith|1|-1|2|2|cell-lborder|1ln>|<cwith|1|-1|1|1|cell-rborder|1ln>|<cwith|1|-1|2|2|cell-rborder|1ln>|<table|<row|<cell|<strong|Type
Id>>|<cell|<strong|Type>>|<cell|<strong|Sub-components>>>|<row|<cell|1>|<cell|Next
Epoch Data>|<cell|<math|<around*|(|Auth<rsub|BABE>,\<cal-R\>|)>>>>|<row|<cell|2>|<cell|On
Disabled>|<cell|<math|Auth<rsub|ID>>>>|<row|<cell|3>|<cell|Next Config
Data>|<cell|<math|<around*|(|c,s<rsub|2nd><rsup|>|)>>>>>>>>
<label|tabl-consensus-messages-babe>The consensus digest item for
BABE authorities
</small-table>
</center>
Where:
<\itemize-minus>
<item>Auth<math|<rsub|BABE>> is the authority list for the next epoch,
as defined in definition <reference|defn-authority-list>.
<item><math|\<cal-R\>> is the 32-byte randomness seed for the next
epoch, as defined in definition <reference|defn-epoch-randomness>
<item><math|Auth<rsub|ID>> is an unsigned 64-bit integer pointing to an
individual authority in the current authority list.
<item><math|c> is the probability that a slot will not be empty, as
defined in definition <reference|defn-babe-constant>. It is encoded as
a tuple of two unsigned 64-bit integers
<math|<around*|(|c<rsub|nominator>,c<rsub|denominator>|)>> which are
used to compute the rational <math|c=<frac|c<rsub|nominator>|c<rsub|denominator>>>.
<item><math|s<rsub|2nd>> is the second slot configuration encoded as an
8-bit enum.\
</itemize-minus>
<\center>
<\small-table|<tabular|<tformat|<cwith|1|1|1|-1|cell-tborder|0ln>|<cwith|1|1|1|-1|cell-bborder|1ln>|<cwith|2|2|1|-1|cell-tborder|1ln>|<cwith|1|1|1|1|cell-tborder|0ln>|<cwith|1|-1|1|1|cell-lborder|0ln>|<cwith|1|1|2|2|cell-tborder|0ln>|<cwith|1|-1|2|2|cell-lborder|1ln>|<cwith|1|-1|1|1|cell-rborder|1ln>|<cwith|1|-1|2|2|cell-rborder|1ln>|<table|<row|<cell|<strong|Type
Id>>|<cell|<strong|Type>>|<cell|<strong|Sub-components>>>|<row|<cell|1>|<cell|Scheduled
Change>|<cell|<math|<around*|(|Auth<rsub|C>,N<rsub|delay>|)>>>>|<row|<cell|2>|<cell|Forced
Change>|<cell|<math|<around*|(|Auth<rsub|C>,N<rsub|delay>|)>>>>|<row|<cell|3>|<cell|On
Disabled>|<cell|<math|Auth<rsub|ID>>>>|<row|<cell|4>|<cell|Pause>|<cell|<math|N<rsub|delay>>>>|<row|<cell|5>|<cell|Resume>|<cell|N<math|<rsub|delay>>>>>>>>
<label|tabl-consensus-messages-grandpa>The consensus digest item for
GRANDPA authorities
</small-table>
</center>
Where:
<\itemize-minus>
<item>Auth<math|<rsub|C>> is the authority list as defined in
definition <reference|defn-authority-list>.
<item><math|N<rsub|delay>\<assign\><around*|\|||\<nobracket\>>><name|SubChain><math|<around*|(|B,B<rprime|'>|)><around*|\|||\<nobracket\>>>
is an unsigned 32-bit integer indicating the length of the subchain
starting at <math|B>, the block containing the consensus message in its
header digest and ending when it reaches <math|N<rsub|delay>> length as
a path graph. The last block in that subchain, <math|B<rprime|'>>,
depending on the message type, is either finalized or imported (and
therefore validated by the block production consensus engine according
to Algorithm <reference|algo-import-and-validate-block>. See below for
details).
<item><math|Auth<rsub|ID>> is an unsigned 64-bit integer pointing to an
individual authority in the current authority list.
</itemize-minus>
</definition>
The Polkadot Host should inspect the digest header of each block and
delegate consensus messages to their consensus engines.
The BABE consensus engine should react based on the type of consensus
messages it receives as follows:
<\itemize-minus>
<item><with|font-series|bold|Next Epoch Data:> The Runtime issues this
message on every first block of an epoch <math|\<cal-E\><rsub|n>>. The
supplied authority set and randomness are intended to be used in the next
epoch <math|\<cal-E\><rsub|n+1>>. \
<item><strong|On Disabled>: An index to the individual authority in the
current authority list that should be immediately disabled until the next
authority set changes. When an authority gets disabled, the node should
stop performing any authority functionality for that authority, including
authoring blocks. Similarly, other nodes should ignore all messages from
the indicated authority which pertain to their authority role.\
<item><with|font-series|bold|Next Config Data:> These messages are only
issued on configuration change and in the first block of an epoch. The
supplied configuration data are intended to be used from the next epoch
onwards.\
</itemize-minus>
The GRANDPA consensus engine should react based on the type of consensus
messages it receives as follows:
<\itemize-minus>
\ <item><strong|Scheduled Change>: Schedule an authority set change after
the given delay of <math|N<rsub|delay>\<assign\><around*|\|||\<nobracket\>>><name|SubChain><math|<around*|(|B,B<rprime|'>|)><around*|\|||\<nobracket\>>>
where <math|B<rprime|'>> in the definition of <math|N<rsub|delay>>, is a
block <em|finalized> by the finality consensus engine. The earliest
digest of this type in a single block will be respected. No change should
be scheduled if one is already finalized and the delay has not passed
completely. If such an inconsistency occurs, the scheduled change should
be ignored.
<item><strong|Forced Change>: Force an authority set change after the
given delay of <math|N<rsub|delay>\<assign\><around*|\|||\<nobracket\>>><name|SubChain><math|<around*|(|B,B<rprime|'>|)><around*|\|||\<nobracket\>>>
where <math|B<rprime|'>> in the definition of <math|N<rsub|delay>>, is an
<em|imported> block that has been validated by the block production
consensus engine. Hence, the authority changeset is valid for every
subchain containing <em|B> and where the delay has been exceeded. If one
or more blocks gets finalized before the change takes effect, the
authority set change should be disregarded. The earliest digest of this
type in a single block will be respected. No change should be scheduled
if one is already finalized and the delay has not passed completely. If
such an inconsistency occurs, the scheduled change should be ignored.
<item><strong|On Disabled>: <with|font-series|bold|<underline|This
message is currently ignored by the Polkadot Host and will be for the
forseeable future.> >An index to the individual authority in the current
authority list that should be immediately disabled until the next
authority set changes. \ This message initial intension was to cause an
imediatly suspension of all authority functionality with the specified
authority.
<item><strong|Pause>: A signal to pause the current authority set after
the given delay of <math|N<rsub|delay>\<assign\><around*|\|||\<nobracket\>>><name|SubChain><math|<around*|(|B,B<rprime|'>|)><around*|\|||\<nobracket\>>>
where <math|B<rprime|'>> in the definition of <math|N<rsub|delay>>, is a
block <em|finalized> by the finality consensus engine. After finalizing
block <math|B<rprime|'>>, the authorities should stop voting.
<item><strong|Resume>: A signal to resume the current authority set after
the given delay of <math|N<rsub|delay>\<assign\><around*|\|||\<nobracket\>>><name|SubChain><math|<around*|(|B,B<rprime|'>|)><around*|\|||\<nobracket\>>>
where <math|B<rprime|'>> in the definition of <math|N<rsub|delay>>, is an
<em|imported> block and validated by the block production consensus
engine. After authoring block <math|B<rprime|'>>, the authorities should
resume voting.
</itemize-minus>
The active GRANDPA authorities can only vote for blocks that occurred after
the finalized block in which they were selected. Any votes for blocks
before the <verbatim|Scheduled Change> came into effect would get rejected.
<section|Block Production><label|sect-babe><label|sect-block-production>
The Polkadot Host uses BABE protocol <cite|w3f_research_group_blind_2019>
for block production. It is designed based on Ouroboros praos
<cite|david_ouroboros_2018>. BABE execution happens in sequential
non-overlapping phases known as an <strong|<em|epoch>>. Each epoch on its
turn is divided into a predefined number of slots. All slots in each epoch
are sequentially indexed starting from 0. At the beginning of each epoch,
the BABE node needs to run Algorithm <reference|algo-block-production-lottery>
to find out in which slots it should produce a block and gossip to the
other block producers. In turn, the block producer node should keep a copy
of the block tree and grow it as it receives valid blocks from other block
producers. A block producer prunes the tree in parallel by eliminating
branches that do not include the most recent finalized blocks according to
Definition <reference|defn-pruned-tree>.
<subsection|Preliminaries>
<\definition>
A <strong|block producer>, noted by <math|\<cal-P\><rsub|j>>, is a node
running the Polkadot Host which is authorized to keep a transaction queue
and which it gets a turn in producing blocks.
</definition>
<\definition>
<strong|Block authoring session key pair
<math|<around*|(|sk<rsup|s><rsub|j>,pk<rsup|s><rsub|j>|)>>> is an SR25519
key pair which the block producer <math|\<cal-P\><rsub|j>> signs by their
account key (see Definition <reference|defn-account-key>) and is used to
sign the produced block as well as to compute its lottery values in
Algorithm <reference|algo-block-production-lottery>.\
</definition>
<\definition>
<label|defn-epoch-slot>A block production <strong|epoch>, formally
referred to as <math|\<cal-E\>>, is a period with a pre-known starting
time and fixed-length during which the set of block producers stays
constant. Epochs are indexed sequentially, and we refer to the
<math|n<rsup|th>> epoch since genesis by <math|\<cal-E\><rsub|n>>. Each
epoch is divided into <math|>equal-length periods known as block
production <strong|slots>, sequentially indexed in each epoch. The index
of each slot is called a <strong|slot number>. The equal length duration
of each slot is called the <strong|slot duration> and indicated by
<math|\<cal-T\>>. Each slot is awarded to a subset of block producers
during which they are allowed to generate a block.
</definition>
<\remark>
Substrate refers to an epoch as \Psession\Q in some places, however,
epoch should be the preferred and official name for these periods.
</remark>
<\notation>
<label|note-slot>We refer to the number of slots in epoch
<math|\<cal-E\><rsub|n>> by <math|sc<rsub|n>>. <math|sc<rsub|n>> is set
to the <verbatim|duration> field in the returned data from the call of
the Runtime entry <verbatim|BabeApi_configuration> (see
<reference|sect-rte-babeapi-epoch>) at genesis. For a given block
<math|B>, we use the notation <strong|<math|s<rsub|B>>> to refer to the
slot during which <math|B> has been produced. Conversely, for slot
<math|s>, <math|\<cal-B\><rsub|s>> is the set of Blocks generated at slot
<math|s>.
</notation>
Definition <reference|defn-epoch-subchain> provides an iterator over the
blocks produced during a specific epoch.
<\definition>
<label|defn-epoch-subchain> By <name|SubChain(<math|\<cal-E\><rsub|n>>)>
for epoch <math|\<cal-E\><rsub|n>>, we refer to the path graph of
<math|BT> containing all the blocks generated during the slots of epoch
<math|\<cal-E\><rsub|n>>. When there is more than one block generated at
a slot, we choose the one which is also on
<name|Longest-Chain(<math|BT>)>.
</definition>
<\definition>
A block producer <strong|equivocates> if they produce more than one block
at the same slot. The proof of equivocation are the given distinct
headers that were signed by the validator and which include the slot
number.
\;
The Polkadot Host must detect equivocations committed by other validators
and submit those to the Runtime as described in Section
<reference|sect-babeapi_submit_report_equivocation_unsigned_extrinsic>.
</definition>
<subsection|Block Production Lottery>
<\definition>
<label|defn-babe-constant>The <with|font-series|bold|BABE constant>
<math|<with|font-series|medium|c>\<in\><around*|(|0,1<rsub|>|)>> is the
probability that a slot will not be empty and used in the winning
threshold calculation (see Definition
<reference|defn-winning-threshold>).
</definition>
The babe constant (Definition <reference|defn-babe-constant>) is
initialized at genesis to the value returned by calling
<verbatim|BabeApi_configuration> (see <reference|sect-rte-babeapi-epoch>).
For efficiency reasons, it is generally updated by the Runtime through the
\PNext Config Data\Q consensus message (see Definition
<reference|defn-consensus-message-digest>) in the digest of the first block
of an epoch for the next epoch.\
<\definition>
<label|defn-winning-threshold>The <strong|Winning threshold> denoted by
<strong|<math|\<tau\><rsub|\<varepsilon\><rsub|n>>>> is the threshold
that is used alongside the result of Algorithm
<reference|algo-block-production-lottery> to decide if a block producer
is the winner of a specific slot. <math|\<tau\><rsub|\<varepsilon\><rsub|n>>>
is calculated \ as follows:
<\equation*>
\<tau\><rsub|\<varepsilon\><rsub|n>>\<assign\>1-<around*|(|1-c|)><rsup|<frac|1|<around*|\||AuthorityDirectory<rsup|\<cal-E\><rsub|n>>|\|>>>
</equation*>
where the <math|AuthorityDirectory<rsup|\<cal-E\><rsub|n>>> is the set of
BABE authorities for epoch <math|\<varepsilon\><rsub|n>> and
<math|c\<in\><around*|(|0,1|)>> is the BABE constant as defined in
definition <reference|defn-babe-constant>.
</definition>
\ A block producer aiming to produce a block during
<math|\<cal-E\><rsub|n>> should run Algorithm
<reference|algo-block-production-lottery> to identify the slots it is
awarded. These are the slots during which the block producer is allowed to
build a block. The <math|sk> is the block producer lottery secret key and
<math|n> is the index of the epoch for whose slots the block producer is
running the lottery.
<\algorithm>
<label|algo-block-production-lottery><name|Block-production-lottery>(<math|sk:>
the session secret key of the producer,
<math|n:> the epoch index)
<|algorithm>
<\algorithmic>
<\state>
<math|r\<leftarrow\>><name|Epoch-Randomness<math|<around*|(|n|)>>>
</state>
<\state>
<FOR-TO|<math|i\<assign\>1>|<math|sc<rsub|n>>>
</state>
<\state>
<math|<around*|(|\<pi\>,d|)>\<leftarrow\>><em|<name|VRF>>(<math|r,i,sk>)
</state>
<\state>
<math|A<around*|[|i|]>\<leftarrow\><around*|(|d,\<pi\>|)>><END>
</state>
<\state>
<\RETURN>
A
</RETURN>
</state>
</algorithmic>
</algorithm>
For any slot <math|i> in epoch <math|n> where <math|d\<less\>\<tau\>>, the
block producer is required to produce a block. For the definitions of
<name|Epoch-Randomness<math|>> and <em|<name|VRF>> functions, see Section
<reference|sect-epoch-randomness> and Section <reference|sect-vrf>
respectively.
<subsection|Slot Number Calculation><label|sect-slot-number-calculation>
<\definition>
<label|defn-slot-offset>Let <math|s<rsub|i>> and <math|s<rsub|j>> be two
slots belonging to epochs <math|\<cal-E\><rsub|k>> and
<math|\<cal-E\><rsub|l>>. By <with|font-series|bold|<name|Slot-Offset><math|<around*|(|s<rsub|i>,s<rsub|j>|)>>>
we refer to the function whose value is equal to the number of slots
between <math|s<rsub|i>> and <math|s<rsub|j>> (counting <math|s<rsub|j>>)
on the time continuum. As such, we have
<name|Slot-Offset><math|<around*|(|s<rsub|i>,s<rsub|i>|)>=0>.
</definition>
It is imperative for the security of the network that each block producer
correctly determines the current slot numbers at a given time by regularly
estimating the local clock offset in relation to the network (Definition
<reference|defn-relative-syncronization>).\
<\definition>
<label|defn-relative-syncronization>The <with|font-series|bold|relative
time synchronization> is a tuple of a slot number and a local clock
timestamp <math|<around*|(|s<rsub|sync>, t<rsub|sync>|)>> describing the
last point at which the slot numbers have been synchronized with the
local clock.
</definition>
<\algorithm>
<with|font-shape|small-caps|Slot-Time(<with|font-shape|right|<math|s:>
slot number>)>
<|algorithm>
<\algorithmic>
<\state>
<\RETURN>
<with|font-shape|small-caps|<math|t<rsub|sync>+>Slot-Offset>(<math|><math|s<rsub|sync>>,
<math|s>)<math|\<times\>\<cal-T\>>
</RETURN>
</state>
</algorithmic>
</algorithm>
<\note>
<with|font-series|bold|The calculation described in this section is still
to be implemented and deployed.> For now, each block producer is required
to synchronize its local clock using NTP instead. The current slot
<math|s> is then calculated by <math|s<rsub|>=t<rsub|unix>/\<cal-T\>>
where <math|t<rsub|unix>> is the current UNIX time in seconds since
1970-01-01 00:00:00 UTC. That also entails that slot numbers are
currently not reset at the beginning of each epoch.\
</note>
Polkadot does this synchronization without relying on any external clock
source (e.g. through the <with|font-shape|italic|Network Time Protocol> or
the <with|font-shape|italic|Global Positioning System>). To stay in
synchronization, each producer is therefore required to periodically
estimate its local clock offset in relation to the rest of the network.\
This estimation depends on the two fixed parameters
<with|font-series|bold|<math|k>> (Definition <reference|defn-prunned-best>)
and <math|<with|font-series|bold|s<rsub|cq>>> (Definition
<reference|defn-chain-quality>). These are chosen based on the results of a
formal security analysis, currently assuming a <math|1 s \ >clock drift per
day and targeting a probability lower than <math|0.5%> for an adversary to
break BABE in 3 years with resistance against a network delay up to
<math|<frac*|1|3>> of the slot time and a Babe constant
(Definit<line-break>ion <reference|defn-babe-constant>) of <math|c=0.38>.
<\definition>
<label|defn-prunned-best>The <with|font-series|bold|pruned best chain>
<math|<with|font-series|bold|C<rsup|\<#250C\>k>>> is the longest chain
selected according to Definition <reference|defn-longest-chain> with the
last k Blocks pruned. We chose <math|k=140>. The
<with|font-series|bold|last (probabilistically) finalized block>
describes the last block in this pruned best chain.
</definition>
<\definition>
<label|defn-chain-quality>The <with|font-series|bold|chain quality>
<math|<with|font-series|bold|s<rsub|cq>>> represents the number of slots
that are used to estimate the local clock offset. Currently, it is set to
<math|s<rsub|cq>=3000>.
</definition>
The prerequisite for such a calculation is that each producer stores the
arrival time of each block (Definition <reference|defn-block-time>)
measured by a clock that is otherwise not adjusted by any external
protocol.\
<\definition>
<label|defn-block-time>The <strong|block arrival time> of block <math|B>
for node <math|j> formally represented by
<strong|<math|T<rsup|j><rsub|B>>> is the local time of<verbatim|> node
<math|j> when node <math|j> has received block <math|B> for the first
time. If the node <math|j> itself is the producer of <math|B>,
<math|T<rsub|B><rsup|j>> is set equal to the time that the block is
produced. The index <math|j> in <math|T<rsup|j><rsub|B>> notation may be
dropped and B's arrival time is referred to by <math|T<rsub|B>> when
there is no ambiguity about the underlying node.
</definition>
<todo|Currently still lacks a clear definition of when block arrival times
are considered valid and how to differentiated imported block on initial
sync from \Pfresh\Q blocks that were just produced.>
<\definition>
<label|defn-sync-period>A <with|font-series|bold|sync period> is an
interval at which each validator (re-)evaluates its local clock offsets.
The first sync period <math|\<frak-E\><rsub|1>> starts just after the
genesis block is released. Consequently, each sync period
<math|\<frak-E\><rsub|i>> starts after <math|\<frak-E\><rsub|i-1>>. The
length of the sync period is equal to <math|s<rsub|qc>> as defined in
Definition <reference|defn-chain-quality> and expressed in the number of
slots.
</definition>
All validators are then required to run Algorithm
<reference|algo-slot-time> at the beginning of each sync period (Definition
<reference|defn-sync-period>) to update their synchronization using all
block arrival times of the previous period. The algorithm should only be
run once all the blocks in this period have been finalized, even if only
probabilistically (Definition <reference|defn-prunned-best>). The target
slot to which to synchronize should be the first slot in the new sync
period.
<\algorithm>
<label|algo-slot-time><name|Median-Algorithm>(<math|\<frak-E\><rsub|j>>:
sync period used for the estimate, <math|s<rsub|sync>:> slot time to
estimate)
<|algorithm>
<\algorithmic>
<\state>
<math|T<rsub|s>\<leftarrow\><around*|{||}>>
</state>
<\state>
<FOR-IN|<math|B<rsub|i>>|<math|\<frak-E\><rsub|j>><name|>>
</state>
<\state>
<name|<math|t<rsub|estimate><rsup|B<rsub|i>>\<leftarrow\>T<rsup|><rsub|B<rsub|i>>>+Slot-Offset(<math|s<rsub|B<rsub|i>>,s<rsub|sync>>)<math|\<times\>\<cal-T\><rsub|>>>
</state>
<\state>
<math|T<rsub|s>\<leftarrow\>T<rsub|s>\<cup\>><math|t<rsub|estimate><rsup|B<rsub|i>>><END>
</state>
<\state>
<\RETURN>
Median(<math|T<rsub|s>>)
</RETURN>
</state>
</algorithmic>
</algorithm>
\ <math|\<cal-T\>> is the slot duration defined in Definition
<reference|defn-epoch-slot>.
<\big-figure|<image|figures/c06-babe_time_sync.eps|1par|||>>
An exemplary result of Median Algorithm in first sync epoch with
<math|s<rsub|cq>=9> and <math|k=1>.
</big-figure>
<subsection|Block Production>
Throughout each epoch, each block producer should run Algorithm
<reference|algo-block-production> to produce blocks during the slots it has
been awarded during that epoch. The produced block needs to carry the
<em|BABE header> as well as the <em|block signature> as Pre-Runtime and
Seal digest items defined in Definition <reference|defn-babe-header> and
<reference|defn-block-signature> respectively.
<\definition>
<label|defn-babe-header>The <strong|BABE Header> of block B, referred to
formally by <strong|<math|H<rsub|BABE><around*|(|B|)>>> is a tuple and
consists of the following components:
<\equation*>
<around*|(|d,\<pi\>,j,s|)>
</equation*>
in which:
<\with|par-mode|center>
<tabular|<tformat|<cwith|1|-1|1|1|cell-halign|r>|<cwith|3|3|1|1|cell-halign|r>|<cwith|1|-1|1|-1|cell-hyphen|n>|<table|<row|<cell|<math|\<pi\>,d>:>|<cell|the
results of the block lottery for slot s.
>>|<row|<cell|<math|j>:>|<cell|the index of the block producer in the
authority directory of the current epoch>>|<row|<cell|s:>|<cell|the
slot at which the block is produced.>>>>>
\;
</with>
\;
<math|H<rsub|BABE><around*|(|B|)>> must be included as a digest item of
Pre-Runtime type in the header digest <math|H<rsub|d><around*|(|B|)>> as
defined in Definition <reference|defn-digest>.\
</definition>
<\definition>
<label|defn-block-signature><label|defn-babe-seal>The <strong|Block
Signature> <math|S<rsub|B>> is a signature of the block header hash (see
Definition<reference|defn-block-header-hash>) and defined as
<\equation*>
Sig<rsub|SR25519,sk<rsup|s><rsub|j>><around*|(|H<rsub|h><around*|(|B|)>|)>
</equation*>
\ <math|S<rsub|B>> should be included in <math|H<rsub|d><around*|(|B|)>>
as the Seal digest item according to Definition <reference|defn-digest>
of value:
<\equation*>
<around*|(|E<rsub|id><around*|(|BABE|)>,S<rsub|B>|)>
</equation*>
in which, <math|E<rsub|id><around*|(|BABE|)>> is the BABE consensus
engine unique identifier defined in Definition
<reference|defn-consensus-message-digest>. The Seal digest item is
referred to as the <strong|BABE Seal>.
</definition>
\;
<\algorithm|<label|algo-block-production><verbatim|><name|Invoke-Block-Authoring>(<math|sk>,
pk, <math|n>, <math|BT:Current Block Tree>)>
<\algorithmic>
<\state>
<math|A\<leftarrow\>><name|Block-production-lottery(<math|sk>,
<math|n>)>
</state>
<\state>
<FOR-TO|<\math>
s\<leftarrow\>1
</math>|<math|sc<rsub|n>>>
</state>
<\state>
<name|Wait>(<strong|until> <name|Slot-Time>(s))
</state>
<\state>
<math|<around*|(|d,\<pi\>|)>\<leftarrow\>A<around*|[|s|]>>
</state>
<\state>
<\IF>
<math|d\<less\>\<tau\>>
</IF>
</state>
<\state>
<math|C<rsub|Best>\<leftarrow\>><name|Longest-Chain>(BT)
</state>
<\state>
<name|<math|B<rsub|s>\<leftarrow\>>Build-Block(<math|C<rsub|Best>>)>
</state>
<\state>
<name|Add-Digest-Item>(<math|B<rsub|s>>,Pre-Runtime,<math|E<rsub|id><around*|(|BABE|)>,H<rsub|BABE><around*|(|B<rsub|s>|)>>)
</state>
<\state>
<name|Add-Digest-Item>(<math|B<rsub|s>>,Seal,<math|S<rsub|B>>)
</state>
<\state>
<name|Broadcast-Block>(<math|B<rsub|s>>)
</state>
</algorithmic>
</algorithm>
<name|Add-Digest-Item> appends a digest item to the end of the header
digest <math|H<rsub|d><around*|(|B|)>> according to Definition
<reference|defn-digest>.
<subsection|Epoch Randomness><label|sect-epoch-randomness>
<\definition>
<label|defn-epoch-randomness>For epoch <math|\<cal-E\>>, there is a
32-byte <with|font-series|bold|randomness seed>
<math|\<cal-R\><rsub|\<cal-E\>>> computed based on the previous epochs
VRF outputs. For <math|\<cal-E\><rsub|0>> and <math|\<cal-E\><rsub|1>>,
the randomness seed is provided in the genesis state.
</definition>
At the beginning of each epoch, <math|\<cal-E\><rsub|n>> the host will
receive the randomness seed <math|\<cal-R\><rsub|\<cal-E\><rsub|n+1>>>(Definition
<reference|defn-epoch-randomness>) necessary to participate in the block
production lottery in the next epoch <math|\<cal-E\><rsub|n+1>> from the
Runtime, through the <with|font-shape|italic|Next Epoch Data> consensus
message (Definition <reference|defn-consensus-message-digest>) in the
digest of the first block.
<subsection|Verifying Authorship Right><label|sect-verifying-authorship>
When a Polkadot node receives a produced block, it needs to verify if the
block producer was entitled to produce the block in the given slot by
running Algorithm <reference|algo-verify-authorship-right> where:
<\itemize-minus>
<item>T<math|<rsub|B>> is <math|B>'s arrival time defined in Definition
<reference|defn-block-time>.
<item><math|H<rsub|d><around*|(|B|)>> is the digest sub-component of
<math|Head<around*|(|B|)>> defined in Definitions
<reference|defn-block-header> and <reference|defn-digest>.
<item>The Seal <math|D<rsub|s>> is the last element in the digest array
<math|H<rsub|d><around*|(|B|)>> as defined in Definition
<reference|defn-digest>.
<item><name|Seal-Id> is the type index showing that a digest item of
variable type is of <em|Seal> type (See Definitions
<reference|defn-scale-variable-type> and <reference|defn-digest>)
<item><math|AuthorityDirectory<rsup|\<cal-E\><rsub|c>>> is the set of
Authority ID for block producers of epoch <math|\<cal-E\><rsub|c>>.
<item><name|verify-Slot-Winner> is defined in Algorithm
<reference|algo-verify-slot-winner>.
</itemize-minus>
<\algorithm>
<label|algo-verify-authorship-right><name|Verify-Authorship-Right>(<math|Head<rsub|s><around*|(|B|)>>:
The header of the block being verified)\
<|algorithm>
<\algorithmic>
<\state>
<math|s\<leftarrow\>><name|Slot-Number-At-Given-Time>(<math|T<rsub|B<rsub|>>>)
</state>
<\state>
<math|\<cal-E\><rsub|c>\<leftarrow\>><name|Current-Epoch>()
</state>
<\state>
<math|<around*|(|D<rsub|1>,\<ldots\>,D<rsub|length<around*|(|H<rsub|d><around*|(|B|)>|)>>|)>\<leftarrow\>H<rsub|d><around*|(|B|)>>
</state>
<\state>
<math|D<rsub|s>\<leftarrow\>><math|D<rsub|length<around*|(|H<rsub|d><around*|(|B|)>|)>>>
</state>
<\state>
<math|H<rsub|d><around*|(|B|)>\<leftarrow\><around*|(|D<rsub|1>,\<ldots\>,D<rsub|length<around*|(|H<rsub|d><around*|(|B|)>|)>-1>|)>>
//remove the seal from the digest
</state>
<\state>
(<math|id,Sig<rsub|B>>)<math|\<leftarrow\>Dec<rsub|SC><around*|(|D<rsub|s>|)>>
</state>
<\state>
<\IF>
<math|id\<neq\>><name|Seal-Id>
</IF>
</state>
<\state>
<\ERROR>
\PSeal missing\Q<END>
</ERROR>
</state>
<\state>
AuthorID <math|\<leftarrow\>AuthorityDirectory<rsup|\<cal-E\><rsub|c>><around*|[|H<rsub|BABE><around*|(|B|)>.SingerIndex|]>>
</state>
<\state>
<name|Verify-Signature>(<math|AuthorID,H<rsub|h><around*|(|B|)>,Sig<rsub|B>>)
</state>
<\state>
<\IF>
<math|\<exists\>B<rprime|'>\<in\>BT:H<rsub|h><around*|(|B|)>\<neq\>H<rsub|h><around*|(|B|)>>
<strong|and> <math|s<rsub|B>=s<rprime|'><rsub|B>> <strong|and>
<math|><math|SignerIndex<rsub|B>=SignerIndex<rsub|B<rprime|'>>>
</IF>
</state>
<\state>
<\ERROR>
\PBlock producer is equivocating\Q<END>
</ERROR>
</state>
<\state>
<name|Verify-Slot-Winner>(<math|<around*|(|d<rsub|B>,\<pi\><rsub|B>|)>,s>,AuthorID)
</state>
</algorithmic>
</algorithm>
Algorithm <reference|algo-verify-slot-winner> runs as a part of the
verification process, when a node is importing a block, in which:
<\itemize-minus>
<item><name|Epoch-Randomness> is defined in Definition
<reference|defn-epoch-randomness>.
<item><math|H<rsub|BABE><around*|(|B|)>> is the BABE header defined in
Definition <reference|defn-babe-header>.
<item><name|Verify-VRF> is described in Section <reference|sect-vrf>.
<item><math|\<tau\>> is the winning threshold defined in
<reference|defn-winning-threshold>.
</itemize-minus>
<\algorithm>
<label|algo-verify-slot-winner><name|Verify-Slot-Winner>(<math|B>: the
block whose winning status to be verified)
<|algorithm>
<\algorithmic>
<\state>
<math|\<cal-E\><rsub|c>\<leftarrow\>><name|Current-Epoch>
</state>
<\state>
<math|\<rho\>\<leftarrow\>><name|Epoch-Randomness><math|<around*|(|c|)>>
</state>
<\state>
<name|Verify-VRF>(<math|\<rho\>,H<rsub|BABE><around*|(|B|)>.<around*|(|d<rsub|B>,\<pi\><rsub|B>|)>,H<rsub|BABE><around*|(|B|)>.s,c>)
</state>
<\state>
<\IF>
<math|d<rsub|B>\<geqslant\>\<tau\>>
</IF>
</state>
<\state>
<\ERROR>
\PBlock producer is not a winner of the slot\Q<END>
</ERROR>
</state>
</algorithmic>
</algorithm>
<math|<around*|(|d<rsub|B>,\<pi\><rsub|B>|)>><math|>: Block Lottery Result
for Block <math|B>,\
<math|s<rsub|n>>: the slot number,
<math|n>: Epoch index
AuthorID: The public session key of the block producer
<subsection|Block Building Process><label|sect-block-building>
The blocks building process is triggered by Algorithm
<reference|algo-block-production> of the consensus engine which runs
Alogrithm <reference|algo-build-block>.
<\algorithm>
<label|algo-build-block><name|Build-Block>(<math|C<rsub|Best>>: The chain
is where at its head, the block to be constructed, is
s: Slot number)
<|algorithm>
<\algorithmic>
<\state>
<math|P<rsub|B>\<leftarrow\><rsub|>><name|Head(<math|C<rsub|Best>>)>
</state>
<\state>
<math|Head<around*|(|B|)>\<leftarrow\>>(<math|H<rsub|p>\<leftarrow\>H<rsub|h><rsub|><around*|(|P<rsub|B>|)>,H<rsub|i>\<leftarrow\>H<rsub|i><around*|(|P<rsub|B>|)>+1,H<rsub|r>\<leftarrow\>\<phi\>,H<rsub|e>\<leftarrow\>\<phi\>,H<rsub|d>\<leftarrow\>\<phi\>>)
</state>
<\state>
<name|Call-Runtime-Entry><math|<around*|(|<text|<verbatim|Core_initialize_block>>,Head<around*|(|<rsub|>B|)>|)>>
</state>
<\state>
<name|<em|I-D>> \<leftarrow\> <name|Call-Runtime-Entry><math|<around*|(|<text|<verbatim|BlockBuilder_inherent_extrinsics>>,<text|<name|Inherent-Data>>|)>><END>
</state>
<\state>
<strong|for> <em|E> <strong|in> <em|I-D>
</state>
<\state>
<space|2em><name|Call-Runtime-Entry>(<verbatim|BlockBuilder_apply_extrinsics,E>)
</state>
<\state>
<\WHILE>
<strong|not> <name|End-Of-Slot(s)>
</WHILE>
</state>
<\state>
<math|E\<leftarrow\>><name|Next-Ready-Extrinsic()>
</state>
<\state>
<math|R\<leftarrow\>><name|Call-Runtime-Entry(><verbatim|BlockBuilder_apply_extrinsics>,<em|E>)
</state>
<\state>
<\IF>
<name|Block-Is-FULL(<math|R>>)
</IF>
</state>
<\state>
<BREAK><END><END>
</state>
<\state>
<space|2em><strong|if> <name|Should-Drop(R)>
</state>
<\state>
<space|4em><name|Drop(E)>
</state>
<\state>
<math|Head<around*|(|B|)>\<leftarrow\>><name|Call-Runtime-Entry(><verbatim|BlockBuilder_finalize_block>,<em|B>)
</state>
<\state>
B \<leftarrow\> <name|Add-Seal>(B)
</state>
</algorithmic>
</algorithm>
<\itemize-minus>
<item><math|Head<around*|(|B|)>> is defined in Definition
<reference|defn-block-header>.
<item><name|Call-Runtime-Entry> is defined in Notation
<reference|nota-call-into-runtime>.
<item><name|Inherent-Data> is defined in Definition
<reference|defn-inherent-data>.
<item><name|End-Of-Slot> indicates the end of the BABE slot as defined in
Algorithm <reference|algo-slot-time> respectively Definition
<reference|defn-epoch-slot>.
<item><name|Next-Ready-Extrinsic> indicates picking an extrinsic from the
extrinsics queue (Definition <reference|defn-transaction-queue>).
<item><name|Block-Is-Full> indicates that the maximum block size is being
used.
<item><name|Should-Drop> determines based on the result <em|R> whether
the extrinsic should be dropped or remain in the extrinsics queue and
scheduled for the next block. The <verbatim|ApplyExtrinsicResult> as
defined in Definition <reference|defn-rte-apply-extrinsic-result>
describes this behavior in more detail.
<item><name|Drop> indicates removing the extrinsic from the extrinsic
queue (Definition <reference|defn-transaction-queue>).
<item><name|Add-Seal> adds the seal to the block as defined in Definition
<reference|defn-digest> before sending it to peers. The seal is removed
again before submitting it to the Runtime.
</itemize-minus>
<section|Finality><label|sect-finality>
The Polkadot Host uses GRANDPA Finality protocol
<cite|stewart_grandpa:_2019> to finalize blocks. Finality is obtained by
consecutive rounds of voting by the validator nodes. Validators execute
GRANDPA finality process in parallel to Block Production as an independent
service. In this section, we describe the different functions that GRANDPA
service performs to successfully participate in the block-finalization
process.
<subsection|Preliminaries>
<\definition>
<label|defn-grandpa-voter>A <strong|GRANDPA Voter>,
<math|v><glossary-explain|<math|v>|GRANDPA voter node which casts vote in
the finality protocol>, represented by a key pair
<math|<around|(|k<rsup|pr><rsub|v>,v<rsub|id>|)>> where
<math|k<rsub|v><rsup|pr>><glossary-explain|<math|k<rsub|v><rsup|pr>>|The
private key of voter <math|v>> represents an <math|ED25519> private key,
and <math|v<rsub|id>><glossary-explain|<math|v<rsub|id>>|The public key
of voter <math|v>> represents the public key of <math|v>. It is a node
running a GRANDPA protocol and broadcasting votes to finalize blocks in a
Polkadot Host-based chain. The <strong|set of all GRANDPA voters> for a
given block B is indicated by <math|\<bbb-V\><rsub|B>><glossary-explain|<math|\<bbb-V\><rsub|B>,\<bbb-V\>>|The
set of all GRANDPA voters for at block <math|B>>. In that regard, we have
<todo|change function name, only call at genesis, adjust V_B over the
sections>
<\equation*>
\<bbb-V\><rsub|B>=<text|<verbatim|grandpa_authorities>><around*|(|B|)>
</equation*>
where <math|<math-tt|grandpa_authorities>> is the entry into the Runtime
described in Section <reference|sect-rte-grandpa-auth>. We refer to
<math|\<bbb-V\><rsub|B> > as <math|\<bbb-V\>><glossary-dup|<math|\<bbb-V\><rsub|B>,\<bbb-V\>>|>
when there is no chance of ambiguity.