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!
ICT role in 21st century education and its challenges
Â
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
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