Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kaitai Struct definitions for block header digest items #583

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions 01_host/03_transition/03_state-replication.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[#sect-state-replication]
=== State Replication
:kaitai-imports: scale

Polkadot nodes replicate each other’s state by syncing the history of the
extrinsics. This, however, is only practical if a large set of transactions are
Expand Down
170 changes: 169 additions & 1 deletion 01_host/05_consensus/01_common.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ stem:[C].

The authorities and their corresponding weights can be retrieved from the
Runtime (<<sect-rte-grandpa-auth>>).

[kaitai#authority_set]
++++
seq:
- id: num_items
type: scale::compact_int
- id: items
type: authority_item
repeat: expr
repeat-expr: num_items.value
types:
authority_item:
seq:
- id: key
type: scale::bytes
- id: weight
type: u8
++++
====

[#sect-consensus-message-digest]
Expand Down Expand Up @@ -122,10 +140,69 @@ s_"2nd" = {
(2,->,"secondary slot with VRF output")
:}
++++

[kaitai#babe_message, kaitai-dependencies=authority_set]
++++
seq:
- id: type_id
type: u1
enum: message_type
- id: message
type:
switch-on: type_id
cases:
'message_type::next_epoch_data': next_epoch_data
'message_type::on_disabled': on_disabled
'message_type::next_epoch_descriptor': next_epoch_descriptor
enums:
message_type:
1: next_epoch_data
2: on_disabled
3: next_epoch_descriptor
types:
next_epoch_data:
seq:
- id: authority_set
type: authority_set
- id: randomness
size: 32
on_disabled:
seq:
- id: authority_id
type: u4
next_epoch_descriptor:
seq:
- id: type_id
type: u1
enum: descriptor_type
- id: content
type:
switch-on: type_id
cases:
'descriptor_type::default': default_descriptor
enums:
descriptor_type:
1: default
types:
default_descriptor:
seq:
- id: nominator
type: u8
- id: denominator
type: u8
- id: secondary_slot
type: u1
enum: secondary_type
enums:
secondary_type:
0: no_slot
1: plain_slot
2: vrf_slot
++++
====

[#defn-consensus-message-grandpa]
.<<defn-consensus-message-babe, GRANDPA Consensus Message>>
.<<defn-consensus-message-grandpa, GRANDPA Consensus Message>>
====
stem:["CM"_g], the consensus message for GRANDPA, is of the following format:

Expand All @@ -134,6 +211,11 @@ stem:["CM"_g], the consensus message for GRANDPA, is of the following format:
"CM"_g = {(1,("Auth"_C,N_("delay"))),(2,(m,"Auth"_C,N_("delay"))),(3,"A"_i),(4,N_("delay")),(5,N_("delay")):}
++++

TODO:
* Define stem:["Auth_C"].
* Define stem:[m].
* All variants need explicit type definition.

where::
[horizontal]
asciimath:[N_"delay"]:: is an unsigned 32-bit integer indicating how deep in the
Expand Down Expand Up @@ -164,6 +246,57 @@ voting.
given delay of stem:[N_("delay") := |"SubChain"(B,B')|] where stem:[B'] is the
block where the change is applied. Once applied, the authorities should resume
voting.

[kaitai#grandpa_message]
++++
seq:
- id: type_id
type: u1
enum: message_type
- id: message
type:
switch-on: type_id
cases:
'message_type::scheduled_change': scheduled_change
'message_type::forced_change': forced_change
'message_type::on_disabled': on_disabled
'message_type::pause': pause
'message_type::resume': resume
enums:
message_type:
1: scheduled_change
2: forced_change
3: on_disabled
4: pause
5: resume
types:
scheduled_change:
seq:
- id: authority_list
type: scale::bytes
- id: delay_of
type: u4
forced_change:
seq:
- id: starting_at
type: u4
- id: authority_list
type: scale::bytes
- id: delay
type: u4
on_disabled:
seq:
- id: to_disable
type: u4
pause:
seq:
- id: delay_of
type: u4
resume:
seq:
- id: delay_of
type: u4
++++
====

[#defn-consensus-message-beefy]
Expand All @@ -188,4 +321,39 @@ identifier of the remote validator set.
2:: implies **on disabled**: an index to the individual authorty in stem:[V_B]
that should be immediately disabled until the next authority change.
3:: implies **MMR root**: a 32-byte array containing the MMR root.

[kaitai#beefy_message]
++++
seq:
- id: type
type: u1
enum: message_type
- id: message
type:
switch-on: type
cases:
'message_type::authority_change': authority_change
'message_type::on_disabled': on_disabled
'message_type::mmr_root': mmr_root
enums:
message_type:
1: authority_change
2: on_disabled
3: mmr_root
types:
authority_change:
seq:
- id: authorities
type: scale::bytes
- id: set_id
type: u4
on_disabled:
seq:
- id: authority_id
type: u4
mmr_root:
seq:
- id: root
size: 32
++++
====
41 changes: 39 additions & 2 deletions 01_host/05_consensus/02_block-production.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,49 @@ authority in the authority set (<<sect-authority-set>>) who authored the
block.
* stem:[s] is the slot number (<<defn-epoch-slot>>).
* stem:[o] is VRF output (<<algo-block-production-lottery>> respectively
<<defn-babe-secondary-slots>>).
<<defn-babe-vrf-transcript>>).
* stem:[p] is VRF proof (<<algo-block-production-lottery>> respectively
<<defn-babe-secondary-slots>>).
<<defn-babe-vrf-transcript>>).

The Pre-Digest must be included as a digest item of Pre-Runtime type in the
header digest (<<defn-digest>>) stem:[H_d(B)].

[kaitai#pre_runtime]
++++
seq:
- id: type_id
type: u1
enum: message_type
- id: digest
type:
switch-on: type_id
cases:
'message_type::primary_vrf': vrf_digest
'message_type::primary_plain': plain_digest
'message_type::secondary_vrf': vrf_digest
enums:
message_type:
1: primary_vrf
2: primary_plain
3: secondary_vrf
types:
vrf_digest:
seq:
- id: authority_id
type: u4
- id: slot_number
type: u8
- id: vrf_out
size: 32
- id: vrf_proof
size: 64
plain_digest:
seq:
- id: authority_id
type: u4
- id: slot_number
type: u8
++++
====

****
Expand Down