# Polkadot
## High level idea
- A sharded multichain network.
- Level 0 chain is polkadot. It's also named as relaychain.
- Each sharded chains connected to Polkadot are called "parachains", they are level 1 chains.
- During regular interval parachains can access resources on relaychain, i.e sending messages to other parachains.
- Parachains can update core logic without hard forks , thanks to WASM based executables.
## Consenus
#### Block authoring :
- BABE is block production engine inspired by Ouroboros Praos. Nominated proof-of-stake protocol.
- BASE is slot-based algorithm for each slot a leader is chosen based on verifiable random function (VRF), if VRF fails to chose leader a secondary leader is chosen based on round-robin.
#### Finalization:
- GRANDPA finalization is based on BFT, but validators vote on chains, not on blocks. Thus protocol applies votes transitively and can finalize several blocks in one round.
- After complete round if proposed candidates does not get 2/3 vote they are not chosen to be finalized.
- if a validator votes on conlficting chains other validators will expose it and their stack will be heavily slashed.
##### Technical details
- Each parachain defines its state transition function which is also named as parachain validation function (PVF).
- PVF a WebAssembly module takes the current state of parachain, a set of transactions, and produces a new state of the parachain.
- Relay chain nodes which performs validation of PVF are called validators.
- Validators do not store state of each parachains, it only stores current block header.
- Validator needs transactions and merkle proofs, this combination is called proof-of-validity (PoV).
- Nodes out of the parachain consensus creates candidates, those are called collators. Collators collect transactions and submit to validators. (similar to sequencers)
- Validators begins with BABE round after attesting blocks received from collators.
- The next step is ensuring availability. Sending and storing whole PoVs is prohibitively expensive. So PoVs are split into chunks and distributed among validators. So Polkadot leverages erasure coding to split the PoV into chunks, and a large subset of chunks can recover the original PoV. Each validator stores only one chunk for a particular PoV.
### Polkadot SDK
- A monorepo combining all tools and frameworks to build parachain for Polkadot.
- Main components:
- Substrate: Define FRAME based framework to develope modules which are used to build runtime of parachain. This modules are known as pallets.
- Cumulus: This contains code which can convert a standalone parachain to PVF which can be deployed on relaychains like Polkadot and Kusama.
The libraries that enable a Substrate node to handle its network responsibilities, including consensus and block execution are Rust crates that use the `sc_` prefix in the crate name.
The libraries that provide the communication layer between the outer node and the runtime are Rust crates that use the `sp_` prefix in the crate name.
The libraries that enable you to build the runtime logic and to encode and decode the information passed into and out of the runtime are Rust crates that use the `frame_` prefix in the crate name. The frame_* libraries provide the infrastructure for the runtime.
In addition to the infrastructure provided by the frame_* libraries, the runtime can include one or more pallet_* libraries. Each Rust crate that uses the pallet_ prefix represents a single FRAME module.
You can build a Substrate runtime without using the frame_* or pallet_* libraries using the primitives exposed by the sp_* core libraries. However, the frame_* or pallet_* libraries provide the most efficient path to composing a Substrate runtime.
Most of the core work of substrate is located in frame_system and frame_support pallets.