Solidity Course
#1 - Blockchain
Miguel Palhas / Hugo Gonçalves
Who are we?
Hugo Goncalves
@hgg
Miguel Palhas
@naps62
The plan
The plan
- Blockchain 101
- Ethereum 101
- Solidity, pt. 1
- Solidity, pt. 2
- Distributed apps (Dapps)
Resources
Metamask
https://metamask.io/
Etherscan
https://etherscan.io/
Uniswap
https://uniswap.org/
Technical Resources
Hardhat
https://hardhat.org/
Ganache
https://www.trufflesuite.com/ganache
Remix
https://remix.ethereum.org/
What is a blockchain?
What is a blockchain?
A storage system, similar to a database, with additional guarantees on security, censorship-resistance, decentralization & trust.
Security: Data is immutable and cryptographically trustworthy;
Censorship Resistance: Impossible to impose restrictions beyond the protocol rules
Decentralization: No need to rely and trust on a third party;
Trust: The system itself enforces the rules, not its creator/host or any individual entity.
What's so innovative about it?
Bitcoin's scientific breakthrough was not in inventing a new technology, but in combining existing fields of research to build something new:
Distributed Systems: where no central authority exists who can unilaterally manipulate the system
Cryptography: Public/Private key pairs allow establishing identity and mathematical proofs
Economics: Using economical principles to incentivize good behavior while deterring bad behavior (cryptoeconomics)
Ok, but what is it technically?
-
An append-only database
-
Each entry is a block, containing transactions;
-
Each transaction is cryptographically signed;
-
Nodes process transactions in blocks, and publish them to other nodes;
-
Every node validates incoming blocks;
-
If majority of nodes are good actors, the system ensures all the rules, without any central authority
-
Infeasible to hack the system (e.g.: a 51% attack)
{
from: "origin_account",
to: "destination_account",
value: "1 BTC",
fee: "0.0001 BTC",
timestamp: "2021-01-01 12:00:00"
...
}
Why?
Byzantine General's Problem
It's hard for a decentralized system to reach agreement on a single truth.
A video that explains it much better than I ever could:
Byzantine Generals Problem
The ability to message and agree on something, without needing to trust the other participants, or the means of communication.
Mining a transaction
When you send a transaction...
- Alice sends 1 BTC to Bob, e.g. via node 1
- Node 1 broadcasts the new transaction to other nodes
- All nodes are working to create block 4 (More on this later)
- Node 2 successfully creates block 4, containing Alice's transaction
- Node 2 broadcasts new block to the network
- Each node receives the new block, validates it, and updates its own copy
- All nodes are now up-to-date on block 4, and begin work on block 5
Block 1
tx 1
tx 2
tx 3
Block 2
tx 4
tx 5
tx 6
Node 1
Node 2
Candidate block 3a
tx 7 (alice's)
tx 8
tx 9
Block 1
tx 1
tx 2
tx 3
Block 2
tx 4
tx 5
tx 6
Candidate block 3b
tx 7 (alice's)
tx 10
one proof-of-work later...
Block 1
tx 1
tx 2
tx 3
Block 2
tx 4
tx 5
tx 6
Node 1 wins the race, creates block 3
Node 2 is still unaware of it
Candidate block 4
...
Block 1
tx 1
tx 2
tx 3
Block 2
tx 4
tx 5
tx 6
Candidate block 3b
tx 7 (alice's)
tx 10
Block 3
tx 7
tx 8
tx 9
Block 1
tx 1
tx 2
tx 3
Block 2
tx 4
tx 5
tx 6
Candidate block 4a
...
Candidate block 4b
...
Block 3
tx 7
tx 8
tx 9
After a small broadcast delay, both nodes get in sync again
Block 1
tx 1
tx 2
tx 3
Block 2
tx 4
tx 5
tx 6
Block 3
tx 7
tx 8
tx 9
Double Spending
(or why Proof-of-Work is important)
Alice has 1 BTC in her account
What if she submits a transaction sending "1 BTC to Bob", but also another sending "1 BTC to Charlie"?
A centralized authority (bank) would catch that, and refuse to process the 2nd transaction.
But without centralization, who is the police?
Double Spending
Miners want transaction fees.
They're economically incentivized to accept as many transactions as possible
Proof-of-work ensures they need work, which will be peer-reviewed by everyone else.
It ensures everyone polices everyone else.
Double Spending
Explaining Proof-of-Work
Proof of Work
It's a consensus mechanism (one of many)
Blockchain capacity is finite and time sensitive
- Users want to send fast and cheap transactions
- Miners want the highest fees, and want other miners to agree on their proposed blocks
A supply-demand problem is created
Proof of Work
Proof-of-work creates both security and fairness in the system:
- All miners are forced to play the same cryptographic game
- Whoever "wins" (mines a block), gets the rewards for it, and others need to agree (it's in their best interest to do so)
{demo}
Proof-of-Work
Energy waste?
A real problem in Bitcoin and others, but not an inherent problem with blockchain, or proof-of-work.
Consensus protocol require a form of value to be staked by miners. That value is turned into protocol security.
In PoW, the value is given by forcing miners to spend computational power:
- incentivizes good behaviour, since rewards provide profit
- deters bad behaviour, since energy spent is costly to the miner
Energy waste?
sha256 is compute-bound
more computational power ➔more mining power ➔ more profits
This causes overall network power (Hashrate) to escalate.
But PoW can also be memory-bound, latency-bound, etc, which does not scale as well.
Other consensus methods
- Proof-of-stake
- Instead of computing power, you lock some of your funds as insurance
- Instead of computing power, you lock some of your funds as insurance
- Delegated proof-of-stake
- Proof-of-useful-work (mostly theoretical right now)
- Replaces sha256 with useful computation
Finality
What if two nodes broadcast a block at the same time?
For every new block, a lot of work is put into the chain.
The chain with the most work wins
Tutorial:
Attacking Ethereum
To revert Block #3, we need to:
- Create a new Block #3b
- Send it to everyone, before someone else creates Block #4
Not easy, but statistically doable
(happens all the time, by accident)
To revert Block #2, we need to:
- Create a new Block #2b
- Create a new Block #3b as well
- Send them to everyone, before someone else creates Block #4 is created
Twice as hard, but might still be doable
To revert Block #1, we need to:
- Create a new Block #1b
- Create a new Block #2b
- Create a new Block #3b
- Send them to everyone, before someone else creates Block #4
Probably 99% unlikely, or something
Not real math
You need more power than the rest of network.
As in: You need at least 51%
Each new block helps confirm all previous blocks
By adding work on top of it
Finality:
~6 confirmations in BTC
~20-30 confirmations in ETH
Solidity Course
#1 - Blockchain
Miguel Palhas / Hugo Gonçalves
Solidity Course 01
By Miguel Palhas
Solidity Course 01
- 262