## Introduction to Substrate

----
### Who am I?
- I crouch in front of a monitor for the last 35 years
- Working on decentralization
and privacy since 2016
- Worked for Parity for about a year
- Working for Vidákovics Attila since June
- I prefer in-real-life relations to social media
- Still, I do have [github](https://github.com/wigy-opensource-developer/), because of society :see_no_evil:
---
### Why do we need more than Ethereum
- Single threaded, public "world computer"
- Fees tie applications together into a single economical model
- Proxy contracts
----
### Proxy contracts
- Implementation can be replaced
- Most calls are **delegated** to an implementation
- State is stored in the proxy
- But who can replace the implementation?
- Usually the simplest solution is used and a single person owns the private key
- Multisig contract, say 3/5
- A DAO contract with voting
- Both expensive in fees
---
### What is a dApp?
- Just a glorified Web2 service?
- Some aspects stored on-chain
- Where does value come from
- Is liquidity trapped on a single chain
----
### Future is multichain
- Bridges
- Trusted
- Multisig
- DAO - almost trustless
- Economic security of PoS
- "One chain to rule them all"? :smile_cat:
- [Avalanche](https://www.avax.network/)
- [Cardano](https://cardano.org/)
- [Cosmos](https://cosmos.network/)
- [Polkadot](https://www.polkadot.network/)
---
### Polkadot

----
### Polkadot ecosystem
- Relay chain (Polkadot, Kusama)
- Parachains
- Parathreads
- Auctions for paraslots
- Crowdloan
----
### Polkadot governance
- nPoS elections every 4 hours
- Treasury
- Proposals
- [Decision](https://polkadot.subsquare.io/) [preparation](https://polkadot.polkassembly.io/opengov)
- Open voting
- Tracks
- Technical Fellowship
----
### Advantages over a solochain
- Cross-chain messaging (XCM)
- Shared untrusted bridges to other ecosystems (BTC, ETH, BNB, AVAX, ADA, ATOM)
- Economic security of parachains
---
### Substrate (Polkadot SDK)
- Building blocks for [creating a chain](https://invidious.flokinet.to/watch?v=0IoUZdDi5Is&t=3270) for your own application/community/DAO
- Blockchain node is written in Rust
- Most of the code is on-chain
- Governance can replace it democratically
- Compare with BCC, ETC forking events
- Frontend templates in React/TS
- Open-source indexers, tooling all the way
---
### Rust language
- Fast, Reliable, Productive. Pick Three.
- Pretty good support for cryptography
- Pure functional subset for pallet writing
- "Fighting with the borrow checker"
- Microsoft, Cloudflare, KeyRock
- Mozilla, Linux
- WASM
---
### What is a pallet?
- A reusable piece of storage+rpc+calls+hooks
```rust=
#[frame_support::pallet]
pub mod pallet {
..
#[pallet::storage]
pub type TotalIssuance<..> = StorageValue<..T::Balance..>;
#[pallet::storage]
pub type Account<..> = StorageMap<..T::AccountId, AccountData<T::Balance>..>;
..
#[pallet::call(weight(..))]
impl<..> Pallet<..> {
#[pallet::call_index(0)]
pub fn transfer_allow_death(
origin: OriginFor<T>,
dest: AccountIdLookupOf<T>,
value: T::Balance,
) -> DispatchResult {..}
}
..
}
```
[balances](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/balances/src/lib.rs) pallet
----
### What is a pallet?
```rust=
#[derive(Encode, Decode,
Clone, PartialEq, Eq, Default,
RuntimeDebug, MaxEncodedLen, TypeInfo)]
pub struct AccountData<Balance> {
pub free: Balance,
pub reserved: Balance,
pub frozen: Balance,
pub flags: ExtraFlags,
}
```
----
### What is a pallet?
- Has external dependencies to other pallets and the runtime configuration
```rust=
#[frame_support::pallet]
pub mod pallet {
..
#[pallet::config(with_default)]
pub trait Config<I: 'static = ()>: frame_system::Config {
..
type Balance: Parameter
+ Member
+ AtLeast32BitUnsigned
+ Codec
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ Debug
+ MaxEncodedLen
+ TypeInfo
+ FixedPointOperand;
..
}
}
```
----
### What is a pallet?
- BTW, Polkadot deletes accounts without a meaningful balance held (Shanghai attack)
```rust=
#[frame_support::pallet]
pub mod pallet {
..
#[pallet::config(with_default)]
pub trait Config<I: 'static = ()>: frame_system::Config {
..
#[pallet::constant]
type ExistentialDeposit: Get<Self::Balance>;
type DustRemoval: OnUnbalanced<CreditOf<Self, I>>;
..
}
}```
---
### What is a runtime?
- A set of pallets woven together...
```rust=
construct_runtime!(
pub struct Runtime {
System: frame_system,
Timestamp: pallet_timestamp,
Aura: pallet_aura,
Grandpa: pallet_grandpa,
Balances: pallet_balances,
TransactionPayment: pallet_transaction_payment,
Sudo: pallet_sudo,
}
);
```
----
### What is a runtime?
- ...by configuring pallets
```rust=
pub type Balance = u128;
pub const EXISTENTIAL_DEPOSIT: u128 = 500;
impl pallet_balances::Config for Runtime {
..
type Balance = Balance;
type ExistentialDeposit = ConstU128<EXISTENTIAL_DEPOSIT>;
..
}
```
---
### Nodes
- weirdly called "clients" :man-shrugging:
- p2p networking
- consensus (!)
- storage
- WASM executor
----
### Types of nodes
- solo chain nodes
- relay chain nodes
- collators
- parachain node +
- relay chain node +
- forwarding logic
----
### Big picture
```plantuml
@startuml
cloud "p2p network" as p2p
node client {
component libp2p
libp2p -> p2p
component consensus
component cryptography
component executor
database storage
consensus -down-> storage
consensus -down-> cryptography
}
node "on-chain" as chain {
component runtime
component "consensus pallet" as c_pallet
component governance
runtime *-down-> c_pallet
runtime *-down-> governance
}
executor o-up-> "*" runtime
@enduml
```
---
### Wrap up
- Multiple chains vs ecosystem
- Runtime vs smart contracts
- "code is law" vs communities
- Web2 walled gardens vs open-source tooling
---
### Thank you! :dog:
You can find me through
Attila and DLabs :stuck_out_tongue:
{"title":"Introduction to Substrate","breaks":true,"description":"View the slide with \"Slide Mode\".","slideOptions":"{\"theme\":\"beige\",\"autoslide\":0,\"progress\":true}","contributors":"[{\"id\":\"713818f1-63fa-4243-8c70-d2179c68324e\",\"add\":6745,\"del\":2812}]"}