a tour of ethereum ecosystem
TRANSCRIPT
⽐特幣起源• 2008 年 10 ⽉
– Satoshi Nakamoto (中本聰) 提出⽐特幣設計⽩⽪書• 2009 年
– 公開程式碼– 開源⽐特幣客⼾端程式– 以 10,000 ⽐特幣兌換⼀個披薩
• 2010 年– 開始流⾏– https://bitcoin.org/
2
去中⼼化• 公私鑰
– 錢幣無法偽造– 交易的不可否認性 (⾝分性)
• 可信任第三⽅ 每個⼈都是銀⾏– 管理資產,追蹤錢幣流向,避免雙花
14
銀⾏帳本 公開共享的帳本區塊鏈 (Blochchain)
ProofofWork• 網路⽤⼾驗證交易計算成本⾼• 交易驗證無法被網路單元數⺫控制 (Sybil attack)• 提供獎勵(誘因)讓他們願意幫忙驗證交易
• 解題困難 (解題時間⻑)• 避免帳本遭受竄改 (解題時間短)
22
區塊產⽣時間 T
SHA-2562(“abc”+“1”)=158c09e82d88955d8a051934d12f74a53ea205743778165d1140a8903686e1acSHA-2562(“abc”+“2”)=c72b0720d3302d76cd7b6b3f3dcb554d05f14fee8567cdda3ee8b7ff51e02015...SHA-2562(“abc”+“19”)=005eQ2ff3e871185b95bf7c8628e212d15a7d08c42235a381e06caa55856a0c
PoW 設計原則• 解題困難,驗證容易,調整難度
24
Hash(TXs+nonce)<number
00000000000000005b95bf7c8628e212d15a7d08c42235a381e06caa55856a0c
SHA-2562(“abc”+“1”)=158c09e82d88955d8a051934d12f74a53ea205743778165d1140a8903686e1acSHA-2562(“abc”+“2”)=c72b0720d3302d76cd7b6b3f3dcb554d05f14fee8567cdda3ee8b7ff51e02015...SHA-2562(“abc”+“19”)=005eQ2ff3e871185b95bf7c8628e212d15a7d08c42235a381e06caa55856a0c
PoW 設計原則• 解題困難,驗證容易,調整難度
25
算力越強的人越有機會擁有寫帳權 但不代表掌握 70% 算力別人就沒機會,只是概率低
挖礦
27
Block1...
Block2...
Block3...
Block4...
找到區塊
礦⼯礦⼯
礦⼯
礦⼯礦⼯
礦⼯
Hash(Block3|TXs|nonce)挖礦獎勵 + 區塊所有交易⼿續費
區塊
區塊鏈 (Blockchain)
28
Block4...
Block5...
Block6...
Block7...
Block8...
Block9...
mean: ~10分鐘 (Poisson distribution)
BlockN...⋯⋯⋯
困難度 (Difficulty)
區塊高度 (Block height) 區塊深度 (Block depth)
區塊越深表⽰重新打造⼀條區塊鏈難度越⾼
Recap• 如何避免雙花攻擊?
– PoW 降低雙花攻擊概率– 交易驗證分散到各個節點– 控制算⼒或網絡仍存在,但成本⾼
• 帳本更新的時間間隔?• 多少⼈驗證交易才更新帳本?
29
Alice
Alice 控制網路
WhatisEthereum?• VitalikButerinwrote
“BitcoinwasdesignedtobeaSMTP.It’saprotocolthatis very good at one par`cular task. It is good fortransferring money, but it was not designed as afounda`onallayerforanykindofprotocolstobebuiltontop.”
30
WhatisEthereum?• VitalikButerinwroteThe need for a technology that was more expansive,and that replicated the func`onality of Turing-completeprogramminglanguagesinawaythatwouldbe so powerful as to describe any blockchainapplica`on.
31
Bitcoin Ethereum Creator Satoshi Nakamoto Ethereum
Player type cryptocurrency Smart contract platform with own cryptocurrency
Liquidity decent decent currency code Bitcoin/฿/BTC Ether/Ξ/ETH
Public/Private blockchain public public User identity pseudonymous pseudonymous
Consensus algorithm PoW PoW/PoS/Casper
Scripting language Bitcoin Script
Language Solidity
Smart contracts yes (limited) yes Turing completeness no yes
Governance (development decisions, etc) Core team Ethereum Foundation
Transaction speed >10.0m 14s
Rapid payments Lightning Network Raiden Network
Processing cost (CPU,GPU,ASIC) high high Other features UTXO GHOST
32
Components• Acryptographictoken• Anaddresssystem• Anetworkofvalidators(miners)• Aconsensusalgorithm• Ablockchainledger• TheEthereumVirtualMachine• Asetofprograminglanguages• Complexeconomicstructures
33
EthereumClients• 底層 (fullnode)
– go-ethereum(Golang)– cpp-ethereum(C++)– EthereumJ(Java)– pyethereum(Python)– Parity(Rust)– ruby-ethereum(Ruby)
35
HighLevelLanguage(HLL)• 撰寫合約的語⾔
– Solidity(JavaScript-like)– Serpent(Python-like)– LLL(Lisp-like)– Viper– Bamboo
36
與⽐特幣相似處• Ethereumhasablockchain• Ethereumispublicandpermissionless• EthereumhasProof-of-Work(PoW)mining
– Ethash• Ethereumhasaninbuiltcryptocurrency
37
與⽐特幣相異處• Ethereum’sblock`meisshorter• Ethereumhassmallerblocks
– 2,000,000Gasperblock– Limitedbythegasà(2,000,000/x)transac`ons
• TheEthereumVirtualMachinecanrunsmartcontracts• ETHtokenissuance
38
ETHtokenissuanceTotal ETH = Pre-mine + Block rewards + Uncle rewards + Uncle referencing rewards
• Pre-mine: 以太坊正式運營之間,預先產出7200萬個以太幣,之後限制每年的⽣成以不超過四分之⼀的預先發⾏量,即1800萬個以太幣
• Block reward: ⺫前每⼀個區塊獎勵是5個以太幣,⼀個區塊的⽣成速度約14秒,換⾔之,⼀年相當於有225萬個區塊 (365x24x60x60/14),1125萬個以太幣⽣成
• Uncle reward: 在以太坊中,有些區塊在同⼀時間被其他礦⼯所發現,但卻不是接在主鏈上,稱之為Uncle區塊,相當於⽐特幣中的孤兒區塊(Orphan block)。這類區塊可能被之後的區塊所參照,此外這類區塊的獎勵為正常獎勵的⼋分之七,即5x(7/8)=4.375個以太幣。依據Uncle區塊的產⽣速率,⼀年約有接近700萬個以太幣⽣成
• Uncle referencing reward: 接在Uncle區塊之後的區塊,可得0.15以太幣(三⼗⼆分之⼀)。
• 這種獎勵⽅式,為以太坊的Ghost protocol (Greedy Heaviest-Observed Sub-Tree)39
ETH 地址1. 產⽣私鑰 (64 字元 / 32 bytes)2. 私鑰推導公鑰, 使⽤ ECDSA 算法 (128 字元 / 64 bytes)3. 公鑰推導地址 (40 字元/20 bytes)
– 公鑰 64 bytes– 對公鑰進⾏ hash, hash 的演算法為 Keccak-256 hash, hash 後變為 32 bytes– 將 32 bytes 前⾯的 12 bytes 移除, 剩下的 20 bytes 就是地址 (40 字元)
• 帳⼾地址• 合約地址
41
什麼是智能合約• 由程式碼直接控制數字資產或業務流程
– 程式碼對合約具有相同理解 – 不因合約多次更迭而有所混淆 – 對外部資料看法一致 (油價或股價等)
• 智能不是指人工智能
• 智能合約(目前)不是法律合約
• 程式碼僅執行復雜的規則和條件,不具有學習能力 – 電梯 – 販賣機
43
圖片來自 https://bitsonblocks.net
與⾃動⽀付銀⾏有何不同• 去中心化
– 銀行沒有絕對控制權 • 程式碼
– 都有程式碼,但只有傳統銀行有控制和對帳的能力,用戶無法從外部檢驗 • 透明度
– 優點:利益關系者(參與方)彼此透明可見 – 缺點:合約需要有隱私保護
• 彈性 – 可客制各式合約
44
Gas• ⽤來衡量交易 (transaction) 或合約 (contract) 在 Ethereum 上被執
⾏的⼯作量,即多少個指令或多少個動作所需要耗費的單位;由於每⼀個操作都會消耗某些數量的 gas,越複雜的操作需要消耗較多的計算資源,從⽽所需的 gas 也越⾼
• 之所以設計 gas 這個機制,是因為要確保合約能夠被停⽌,不會永無⽌盡的消耗資源
46
Gas• 在 Bitcoin 中,每⼀筆交易需要⼿續費,衡量標准是看交易的⼤⼩
(transaction size);但站在 Ethereum 的⾓度則認為,⼿續費的計算應該取決於在區塊鏈平台上所需要的⼯作量
• 舉例來說,在 Ethereum 當中,兩個合約,程式碼⼤⼩不同,有可能短的程式碼,耗費的 gas ⾼,因為裡⾯執⾏的動作很多牽涉到 sha3;但在 Bitcoin 世界,交易包含的資料越多,占的⼤⼩越⼤,⼿續費較⾼
47
Gasprice• gas price 決定 1 個 gas 價值多少 ether,⽽ gas price 會隨市場需求波動• gas price 的單位是 wei ,1 wei = 10^(-18) ether• gas price 的歷史資料
49
合約開發⼯具介紹• ⼀般開發
– 安裝 Ethereum-client– ⽤ solidity 撰寫合約– 透過 solidity compiler 進⾏編譯– 以 web3.js 將合約送出佈署
• 需要有測試鏈或以太鏈(測試昂貴)• 步驟繁雜
51
Truffle+Testrpc• Truffle
– hGps://github.com/ConsenSys/truffle• Testrpc
– hGps://github.com/ethereumjs/testrpc• hGps://cl.ly/i7Sd/truffle.html
• 不需要佈建完整節點• 可直接在本機測試與執⾏合約• 無法理解完整運作
52
INFURA• INFURA Ethereum
token JSON-RPChGp MainNet
TestNet – hGps://infura.io– hGps://infura.io/register.html– hGps://cl.ly/i7PG/INFURA.html
53
DatacarrierforDApps
55
• Oraclize:hGp://www.oraclize.it/– hGps://github.com/oraclize/ethereum-examples/tree/master/solidity
• 與外部資料互動
EthereumAlarmClock• Scheduletransac`onstobeexecutedatalater`me.• En`reserviceoperatesassmartcontractsontheEthereum
blockchain,withnopriviledgedaccessgiventoanyparty.– hGp://www.ethereum-alarm-clock.com
56
EthereumComputaKonMarket• Thecomputa`onmarketisaserviceforEthereumthatallows
forverifiableoff-chainexecu`onofcomputa`onsthatwouldbeotherwiseprohibi`velyexpensivetoexecutewithintheEVM.– hGp://www.ethereum-computa`on-market.com
57
zeppelin-solidity• AFrameworktoBuildSecureSmartContracts
– CommoncontractsecuritypaGerns– IntheSoliditylanguage– hGps://openzeppelin.org
58
EthereumDev1. 搭建 Ethereum 私有鏈2. 運⾏兩個 node 讓彼此互連3. 熟悉 geth JavaScript Console4. 建⽴其他帳號, 建⽴交易, 觀察 block5. 運⾏私有鏈的 miner6. RPC protocol 使⽤與串接7. Ethereum Block explorer8. Smart Contract on Ethereum9. Mist10. Solidity11. DApp programming
59
參考資料• ⿈⽪書
– hGp://gavwood.com/Paper.pdf
• 學習資源– hGps://github.com/EtherTW/Taipei-Ethereum-Wiki/wiki/Learning-Resources
• 演講影⽚– hGps://www.youtube.com/channel/UC8CB0ZkvogP7tnCTDR-zV7g/videos
60