Skip to content

Commit a0f41b2

Browse files
authored
added/updated pallet level docs to grandpa and messages pallets (#1771)
1 parent 6d69d1f commit a0f41b2

File tree

3 files changed

+137
-236
lines changed

3 files changed

+137
-236
lines changed

docs/high-level-overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ the source chain GRANDPA justifications stream and submits every new justificati
115115
target chain GRANDPA light client. In addition, relay is searching for mandatory headers and
116116
submits their justifications - without that the pallet will be unable to move forward.
117117

118-
More: [GRANDPA Finality Relay Sequence Diagram](./grandpa-finality-relay.html), [code](../relays/finality/).
118+
More: [GRANDPA Finality Relay Sequence Diagram](./grandpa-finality-relay.html), [pallet level documentation and code](../relays/finality/).
119119

120120
### Parachains Finality Relay
121121

@@ -158,7 +158,7 @@ relay submits transactions to both source and target chains, it requires both _s
158158
_target-to-source_ finality relays. They can be GRANDPA finality relays or GRANDPA+parachains finality relays,
159159
depending on the type of connected chain.
160160

161-
More: [Messages Relay Sequence Diagram](./messages-relay.html), [code](../relays/messages/).
161+
More: [Messages Relay Sequence Diagram](./messages-relay.html), [pallet level documentation and code](../relays/messages/).
162162

163163
### Complex Relay
164164

modules/grandpa/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Bridge GRANDPA Pallet
2+
3+
The bridge GRANDPA pallet is a light client for the GRANDPA finality gadget, running at the bridged chain.
4+
It may import headers and their GRANDPA finality proofs (justifications) of the bridged chain. Imported
5+
headers then may be used to verify storage proofs by other pallets. This makes the bridge GRANDPA pallet
6+
a basic pallet of all bridges with Substrate-based chains. It is used by all bridge types (bridge between
7+
standalone chains, between parachains and any combination of those) and is used by other bridge pallets.
8+
It is used by the parachains light client (bridge parachains pallet) and by messages pallet.
9+
10+
## A Brief Introduction into GRANDPA Finality
11+
12+
You can find detailed information on GRANDPA, by exploring its [repository](https://github.com/paritytech/finality-grandpa).
13+
Here is the minimal reqiuired GRANDPA information to understand how pallet works.
14+
15+
Any Substrate chain may use different block authorship algorithms (like BABE or Aura) to determine block producers and
16+
generate blocks. This has nothing common with finality, though - the task of block authorship is to coordinate
17+
blocks generation. Any block may be reverted (if there's a fork) if it is not finalized. The finality solution
18+
for (standalone) Substrate-based chains is the GRANDPA finality gadget. If some block is finalized by the gadget, it
19+
can't be reverted.
20+
21+
In GRANDPA, there are validators, identified by their public keys. They select some generated block and produce
22+
signatures on this block hash. If there are enough (more than `2 / 3 * N`, where `N` is number of validators)
23+
signatures, then the block is considered finalized. The set of signatures for the block is called justification.
24+
Anyone who knows the public keys of validators is able to verify GRANDPA justification and that it is generated
25+
for provided header.
26+
27+
There are two main things in GRANDPA that help building light clients:
28+
29+
- there's no need to import all headers of the bridged chain. Light client may import finalized headers or just
30+
some of finalized headders that it consider useful. While the validators set stays the same, the client may
31+
import any header that is finalized by this set;
32+
- when validators set changes, the GRANDPA gadget adds next set to the header. So light client doesn't need to
33+
verify storage proofs when this happens - it only needs to look at the header and see if it changes the set.
34+
Once set is changed, all following justifications are generated by the new set. Header that is changing the
35+
set is called "mandatory" in the pallet. As the name says, the light client need to import all such headers
36+
to be able to operate properly.
37+
38+
## Pallet Operations
39+
40+
The main entrypoint of the pallet is the `submit_finality_proof` call. It has two arguments - the finalized
41+
headers and associated GRANDPA justification. The call simply verifies the justification using current
42+
validators set and checks if header is better than the previous best header. If both checks are passed, the
43+
header (only its useful fields) is inserted into the runtime storage and may be used by other pallets to verify
44+
storage proofs.
45+
46+
The submitter pays regular fee for submitting all headers, except for the mandatory header. Since it is
47+
required for the pallet operations, submitting such header is free. So if you're ok with session-length
48+
lags (meaning that there's exactly 1 mandatory header per session), the cost of pallet calls is zero.
49+
50+
When the pallet sees mandatory header, it updates the validators set with the set from the header. All
51+
following justifications (until next mandatory header) must be generated by this new set.
52+
53+
## Pallet Initialization
54+
55+
As the previous section states, there are two things that are mandatory for pallet operations: best finalized
56+
header and the current validators set. Without it the pallet can't import any headers. But how to provide
57+
initial values for these fields? There are two options.
58+
59+
First option, while it is easier, doesn't work in all cases. It is to start chain with initial header and
60+
validators set specified in the chain specification. This won't work, however, if we want to add bridge
61+
to already started chain.
62+
63+
For the latter case we have the `initialize` call. It accepts the initial header and initial validators set.
64+
The call may be called by the governance, root or by the pallet owner (if it is set).
65+
66+
## Non-Essential Functionality
67+
68+
There may be a special account in every runtime where the bridge GRANDPA module is deployed. This
69+
account, named 'module owner', is like a module-level sudo account - he's able to halt and
70+
resume all module operations without requiring runtime upgrade. Calls that are related to this
71+
account are:
72+
- `fn set_owner()`: current module owner may call it to transfer "ownership" to another account;
73+
- `fn set_operating_mode()`: the module owner (or sudo account) may call this function to stop all
74+
module operations. After this call, all finality proofs will be rejected until further `set_operating_mode` call'.
75+
This call may be used when something extraordinary happens with the bridge;
76+
- `fn initialize()`: module owner may call this function to initialize the bridge.
77+
78+
If pallet owner is not defined, the governance may be used to make those calls.
79+
80+
## GRANDPA Finality Relay
81+
82+
We have an offchain actor, who is watching for GRANDPA justifications and submits them to the bridged chain.
83+
It is the finality relay - you may look at the [crate level documentation and the code](../../relays/finality/).

0 commit comments

Comments
 (0)