Scratching the surface of the block - a workshop of building your own blockchain from scratch
While it might seem to some of you that this is an entry level session you will indeed be learning how to build your own basic blockchain.
Our speaker, Leonid Beder has been working in the field for a long time and has always maintained that the best practice for Blockchain professionals, both seasoned and newcomers, is to fully understand the working mechanics of the blockchain implementation which can only be achieved by building it from scratch. The lecture will be an introduction to blockchain concepts and technology and together, we will be creating a simple Blockchain. This will not just be a theoretical lecture, it will be an interactive code development session.
Come and experience it for yourself first hand.
You will not be sorry!
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
How to Build Your Own Blockchain
1.
2. • Wrote my first BASIC program at the age of 9.
Who am I?
Wasn’t able to replicate the success ever since.
… and sold my first Bitcoins in 2013
leonid@orbs.network
leonid@kik.com
leonid@cointree.capital
PGP Key:
D749 1F09 3721 0A31 72B5
DBF3 7F35 EBC3 E4B2 44B4
• Bought my first Bitcoins in 2012!
• Very enthusiastic about the “crypto revolution” and dedicating it most of my time.
3. Today:
Engineering and Security a Kik
Engineering and Security at ORBS
Advisory at CoinTree Capital
We’re hiring!
leonid@orbs.network
leonid@kik.com
leonid@cointree.capital
PGP Key:
D749 1F09 3721 0A31 72B5
DBF3 7F35 EBC3 E4B2 44B4
Where am I today?
In the past:
VP R&D at Visualead (acquired)
CTO at Jammer.fm (acquired)
Principal Engineer at Safend (acquired)
Matzov (IDF)
Technion, Computer Science
7. • You’ll understand some of the primitives and architecture
concepts of blockchain technologies.
• You’ll be able to run (and extend) your own, alas wee limited,
Blockchain.
● We’ll deal with the misconception that Blockchain
Engineering == Solidity
● To pique your curiosity; You’ll want to learn more, especially
by reading “Mastering Bitcoin” by Andreas M. Antonopoulos:
https://github.com/bitcoinbook/bitcoinbook
What we want to achive
8. ● How to get filthy rich from x10,000 investments in altcoins and other magical investment
strategies and/or speculative mumbo jumbo.
● ”Other” consensus algorithms. We’ll implement a Bitcoin-like PoW (Internet-scale distributed
Byzantine Fault Tolerant consensus), but it’s important to know that there is a plethora of
other algorithms, such as:
○ Paxos
○ Raft
○ Practical Byzantine Fault Tolerance
○ Proof of Stake (PoS)
○ Delegated Proof of Stake (DPoS)
○ Proof of Importance (PoI)
○ Proof of Authority (PoA)
○ Proof of Elapsed-time (PoEL)
○ Casper the Friendly Ghost (CTFG)
○ Casper the Friendly Finality Gadget (CFFG)
○ Algorand
○ HashGraph
○ Tenderinmt
○ Honeybadger
○ Ouroboros
○ Tezos
○ Tangle (e.g., IOTA)
○ Zilliqa
○ SPECTRE
○ ORBS
What we won’t talk about
9. ● Scaling challenges (throughput, latency, storage requirements, processing requirements,
liveness requirements, etc.).
● Incentive Models and Cryptoeconomics.
● Finality (e.g., probabilistic, theoretic, convergence, etc.).
● Smart Contracts and VM concepts.
● Privacy (ZK proofs, stealth addresses, ZK-SNARKs, etc.).
● Security (algorithms and assumption, implementation, system, etc.).
● Politics and Governance.
● Compliance and Regulation.
● Non-”chain-like” blockchains (e.g., DAG, HashGraph, Tangle, etc.).
● Off-chain protocols, atomic swaps, state/payment channels.
● Hard forks / “airdrops” (a.k.a. “quarterly dividends”).
What we won’t talk about
11. You have probably heard something
like this already…
What is Blockchain
12.
13. • Is it kind of a database?
• Is it kind of a protocol?
• Is it only good to store value?
• Is it decentralized?
• Is it public?
• Is it democratic?
• Is it immutable?
• Should there be only “one”? Should it “talk” with other blockchains?
It’s pretty much a theological debate...
What is Blockchain
17. Blockchains are good at:
• Ensuring that the application state is valid (i.e., it got to what it is now through some
process that follows the rules).
• Transparency of process (i.e., we can know how it got there).
• Irreversibility.
Vitalik Buterin, Blockchains and Privacy through Strong Cryptography, ETHWaterloo 2017
For example:
Smart Contracts
What is it good for?
19. Without loss of generality, we’d use:
• Mac OSX or UNIX-like environment (should work on Windows as well, but you know…).
• TypeScript 2.6.2: high-level and popular programming language.
• Node v9.2.1.
• express 4.6 for a minimalistic web server.
• Shell scripts and Postman to interact with the API.
The code is available at:
https://github.com/blockchain-academy/how-build-your-own-blockchain
Setup
20. We’ll define an initial class with the following interfaces:
1. We’d store all the blocks in the blockchain.
2. We’d store the pool of all the transactions waiting to be processed at
the moment.
3. We’ll differentiate the current instance from other instances (we’d use it
later).
4. Method for creation of new blocks.
5. Method for submission of new transactions to the pool.
Starting from scratch
21.
22. What should our blocks contain?
1. Block number/height: an incremental counter/id for each block.
2. Transactions (duh!).
3. Timestamp: block creation time.
4. Nonce: an arbitrary number that can only be used once (similar in spirit
to a nonce word).
5. Pointer to the next block way to chain the blocks together.
5. The hash of the previous block: by including a hash of the previous
block, we will make the blockchain immutable.
Blocks
23.
24. What should our transactions contain?
1. The public address of the sender.
2. The public address of the recipient.
3. The value of the transaction.
Transactions
25.
26. New transactions will be first submitted to the in-memory transactions
pool (also referred to as the “mempool”):
Submitting new transactions
27.
28.
29. • A method to achieve trustless consensus.
• It was invented by Cynthia Dwork and Moni Naor in Crypto’92 (as a measure to
combat spam).
• A key feature of PoW is its asymmetry: the work must be hard (but feasible) on the
requester side, but easy to check for the service provider.
Proof of Work (PoW)
30. An example for such problem (Hashcash):
Block 34
Previous block:
c216c8c6821a...
Transaction
9c9b7
Transaction
855f4
Transaction
a5c50
?
Proof of Work (PoW)
SHA256(block,?)< target
32. So, how does it help us to achieve a trustless consensus?
• Everyone can mine a block. “One CPU - one vote” (is it?).
• We can easily detect cheaters and invalidate/ignore their blocks, which means:
• Strong incentives not to cheat!
Proof of Work (PoW)
33. ?
?
Satoshi: We’ll always choose the longest chain, which is the chain with the most
invested computation.
Longest Chain
34. There are also quite a few of disadvantages to a PoW scheme:
1. Specialized hardware (ASIC, GPU) usually has an advantage (one CPU - one
vote, right?!).
2. Mining pool centralization (e.g., the top 5 Bitcoin mining pools own close to 70%
of the total computational power).
3. Energy waste: Bitcoin’s current estimated annual electricity consumption stands
at 29.05TWh, which represents 0.13% of total global electricity consumption.
Proof of Work (PoW)
35. Let’s add the difficulties and target for the PoW hash (cost) function,
the “genesis” block and some utility functions:
Mining and Validation
36.
37.
38.
39. Now we can add a method to mine a given set of transactions into a
single block:
Mining and Validation
40.
41.
42. Once we can mine/create new blocks, new block creation is pretty
straightforward:
1. Fetch transactions from the mempool. We’ll assume that blocks are limitless
(which is of course not the case).
2. Keep mining a block with these transactions until a valid PoW is found.
3. Append the new block to the blockchain.
4. Remove the mined transactions from the mempool. We will assume that
there are no race conditions needed to be taken care of.
Creating new blocks
43.
44. In order for the blockchain to be more robust, we will add support for writing and
reading the blockchain from the storage.
❑ What if someone modifies the data on the disk?
✓ We can verify that the blocks were appended correctly, starting from the genesis
block.
❑ What if someone modifies the genesis block?
✓ We’ll hardcode it directly in the protocol.
❑ What if we were offline for a very long time?
✓ We’ll bootstrap from other miners (not in this talk…).
Storing the blockchain
45. Let’s implement saving and loading the blockchain to the disk (it won’t be
very efficient, but it’ll do for now):
Storing the blockchain
46.
47.
48.
49. Let’s implement the validation of the loaded blockchain:
Storing the blockchain
54. Running a blockchain only on our own machine isn’t very useful (is it?), so let’s
add some web API to our implementation. Specifically:
1. API to list the blockchain.
2. API to list and submit transactions.
3. API to mine/create blocks (just for the demo; it could’ve been a cron-like job.
Setting up a web server
55.
56.
57. Let’s add an API to submit transactions. For the sake of the demo, we won’t
validate senders of the transaction of their balances.
Submitting transactions
58.
59. Let’s add an API to block mining.
❑ But wait… why should our node mine blocks?!
✓ Let’s bribe the node with add a mining reward!
Mining blocks
60.
61.
62.
63.
64.
65. In order to make our blockchain more decentralized, we’d want to:
1. Support multiple nodes (avoid single point of failure, faulty nodes,
improve liveliness of the system, etc.).
2. Don’t blindly trust the output of the other nodes – validate everything
yourself!
3. Reach a consensus about the state of the blockchain among the nodes.
Decentralizing the blockchain
66. • Let’s add an API for every node to be aware of other nodes.
• Since we’re developing a permissionless blockchain – everyone is
free to join.
Node discovery
67.
68.
69.
70.
71.
72. • Now, when we have a (naïve) way for the nodes to be aware of each
others – they can communicate together.
• In order to reach a consensus on the “true” state of the blockchain, every
node will:
1. Get the whole blockchain from every other node.
2. If the other blockchain is both longer and valid – use it instead.
Reaching a consensus
81. Award winning contest
• Do you have an idea for an interesting feature?
• Can you make it more secure (in of the bazillion possible ways)?
• Can you make it more performant? More efficient? Easier to govern?
You have 3 weeks to submit an interesting PR (according to the Terms of
Service in the repo). The top 3 submissions will win:
2nd place: New iPhone X 64GB or Samsung Note 8.
3rd place: New Ledger Nano S Hardware Wallet.
1st place: New MacBook Pro 13”, i5, 256 SSD with a Touch Bar.
82. Blockchain AcademyLeonid Beder December 26th, 2017
Thank You!
Next Events
JAN 25th - How To Become A Smart Contract Engineer
FEB 27th - How to Not Destroy Millions in Smart Contracts