Share
Embed
A Tour of Ethereum Ecosystem
Taipei Ethereum Meetup
https://www.meetup.com/Taipei-Ethereum-Meetup/events/235265976/
changwu
November 15, 2016
Transcript
61 slides
-
⽐特幣起源 • 2008 年 10 ⽉ – Satoshi Nakamoto (中本聰)
提出⽐特幣設計⽩⽪書 • 2009 年 – 公開程式碼 – 開源⽐特幣客⼾端程式 – 以 10,000 ⽐特幣兌換⼀個披薩 • 2010 年 – 開始流⾏ – https://bitcoin.org/ 2 -
我, Alice, 要給 Bob ⼀個硬幣 8 Alice Bob Alice 以私鑰對數位貨幣進⾏簽名
Bob 以公鑰解開密⽂ 數字貨幣可能會被複製 如何證明發錢的是 Alice ૡ໓ -
我, Alice, 要給 Bob ⼀個硬幣 9 Alice Bob Alice 無法否認要給
Bob ⼀個硬幣的事實 數字貨幣還是會被複製 ૡ໓ -
需要硬幣流⽔號 10 Alice Bob 1. Bob 無法辨識拿到的硬幣有何不同 2. 如何管理 Alice
帳⼾餘額? 3. 如何確認這個硬幣沒有被重複消費? -
驗證交易是否合法 12 Alice Bob #12345 1. 這筆錢是不是 Alice 的? 2.
Alice 有沒花過這筆錢? #12345 Alice: 100 -> 99 Bob: 100 -> 101 -
Recap • 公私鑰 – 錢幣無法偽造 – 交易的不可否認性 (⾝分性) • 可信任第三⽅
– 管理資產,追蹤錢幣流向,避免雙花 13 -
去中⼼化 • 公私鑰 – 錢幣無法偽造 – 交易的不可否認性 (⾝分性) • 可信任第三⽅
每個⼈都是銀⾏ – 管理資產,追蹤錢幣流向,避免雙花 14 銀⾏帳本 公開共享的帳本 區塊鏈 (Blochchain) -
我, Alice, 要給 Bob ⼀個硬幣 15 Alice Bob #12345 Alice:
100 -> 99 Bob: 100 -> 101 廣播 -
18 Alice Bob #12345 廣播 #12345 Charlie 所有⼈都收到交易並作驗證,如果 Alice 送出同樣的貨幣
給 Bob 和 Charlie,問題就會產⽣ -
Recap • 如何避免雙花攻擊? • 帳本更新的時間間隔? • 多少⼈驗證交易才更新帳本? 20 Alice Alice
控制網路 Sybil aGack -
Proof of Work • 網路⽤⼾驗證交易計算成本⾼ • 交易驗證無法被網路單元數⺫控制 (Sybil attack) •
提供獎勵(誘因)讓他們願意幫忙驗證交易 • 解題困難 (解題時間⻑) • 避免帳本遭受竄改 (解題時間短) 22 區塊產⽣時間 T -
PoW 設計原則 • 每個⼈都能參與解題 (Puzzle) – ⼀般主機都能參與 • 不會有⼈掌控寫帳權 (寫區塊的權⼒)
– 概率相同 23 擲到數字 n <= 3 的⼈可以寫帳本 -
SHA-2562 (“abc” + “1”) = 158c09e82d88955d8a051934d12f74a53ea205743778165d1140a8903686e1ac SHA-2562 (“abc” + “2”)
= c72b0720d3302d76cd7b6b3f3dcb554d05f14fee8567cdda3ee8b7ff51e02015 . . . SHA-2562 (“abc” + “19”) = 005eQ2ff3e871185b95bf7c8628e212d15a7d08c42235a381e06caa55856a0c PoW 設計原則 • 解題困難,驗證容易,調整難度 24 Hash(TXs+nonce) < number 00000000000000005b95bf7c8628e212d15a7d08c42235a381e06caa55856a0c -
SHA-2562 (“abc” + “1”) = 158c09e82d88955d8a051934d12f74a53ea205743778165d1140a8903686e1ac SHA-2562 (“abc” + “2”)
= c72b0720d3302d76cd7b6b3f3dcb554d05f14fee8567cdda3ee8b7ff51e02015 . . . SHA-2562 (“abc” + “19”) = 005eQ2ff3e871185b95bf7c8628e212d15a7d08c42235a381e06caa55856a0c PoW 設計原則 • 解題困難,驗證容易,調整難度 25 ෘ৯ᄀル֥ದᄀႵὨ∭Ⴕ⇔≷ ҂սіᅧෘ৯⦁ದࣼ㢻Ὠđᆺ൞ۀੱ֮ -
挖礦 27 Block 1 ... Block 2 ... Block 3
... Block 4 ... 找到區塊 礦⼯ 礦⼯ 礦⼯ 礦⼯ 礦⼯ 礦⼯ Hash( Block 3 | TXs | nonce) 挖礦獎勵 + 區塊所有交易⼿續費 ⃯ΰ -
區塊鏈 (Blockchain) 28 Block 4 ... Block 5 ... Block
6 ... Block 7 ... Block 8 ... Block 9 ... mean: ~10分鐘 (Poisson distribution) Block N ... lll ₒ؇ %JGGJDVMUZ ⃯ΰۚ؇ #MPDLIFJHIU ⃯ΰധ؇ #MPDLEFQUI 區塊越深表⽰重新打造⼀條區塊鏈難度越⾼ -
Recap • 如何避免雙花攻擊? – PoW 降低雙花攻擊概率 – 交易驗證分散到各個節點 – 控制算⼒或網絡仍存在,但成本⾼
• 帳本更新的時間間隔? • 多少⼈驗證交易才更新帳本? 29 Alice Alice 控制網路 -
What is Ethereum? • Vitalik Buterin wrote “Bitcoin was designed
to be a SMTP. It’s a protocol that is very good at one par`cular task. It is good for transferring money, but it was not designed as a founda`onal layer for any kind of protocols to be built on top.” 30 -
What is Ethereum? • Vitalik Buterin wrote The need for
a technology that was more expansive, and that replicated the func`onality of Turing- complete programming languages in a way that would be so powerful as to describe any blockchain applica`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 • A cryptographic token • An address system •
A network of validators (miners) • A consensus algorithm • A blockchain ledger • The Ethereum Virtual Machine • A set of programing languages • Complex economic structures 33 -
Ethereum Clients • 底層 (fullnode) – go-ethereum (Golang) – cpp-ethereum
(C++) – EthereumJ (Java) – pyethereum (Python) – Parity (Rust) – ruby-ethereum (Ruby) 35 -
High Level Language (HLL) • 撰寫合約的語⾔ – Solidity (JavaScript-like) –
Serpent (Python-like) – LLL (Lisp-like) – Viper – Bamboo 36 -
與⽐特幣相似處 • Ethereum has a blockchain • Ethereum is public
and permissionless • Ethereum has Proof-of-Work (PoW) mining – Ethash • Ethereum has an inbuilt cryptocurrency 37 -
與⽐特幣相異處 • Ethereum’s block `me is shorter • Ethereum has
smaller blocks – 2,000,000 Gas per block – Limited by the gas à (2,000,000/x) transac`ons • The Ethereum Virtual Machine can run smart contracts • ETH token issuance 38 -
ETH token issuance Total 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 ⅾோῲሱIUUQTCJUTPOCMPDLTOFU -
與⾃動⽀付銀⾏有何不同 • ಀᇏྏ߄ – ∝ྛ㢻Ⴕ䌉Ớ॥ᇅ • ӱൔ – ႵӱൔđᆺႵẖⅼ∝ྛႵ॥ᇅބỚ≷֥ି৯đႨ㊒↥مạຓ҆ᾋ⇼ •
ૼ؇ – ∱ềğ০ၭἬ༢ᆀ Ṛ∻ٚ дՎૼॖᾖ – ಌềğކ≌ླေႵ∟ЌὊ • Ằྟ – ॖग़ᇅ۲ൔކ≌ 44 -
Gas • ⽤來衡量交易 (transaction) 或合約 (contract) 在 Ethereum 上被執 ⾏的⼯作量,即多少個指令或多少個動作所需要耗費的單位;由於每
⼀個操作都會消耗某些數量的 gas,越複雜的操作需要消耗較多的計 算資源,從⽽所需的 gas 也越⾼ • 之所以設計 gas 這個機制,是因為要確保合約能夠被停⽌,不會永無 ⽌盡的消耗資源 46 -
Gas • 在 Bitcoin 中,每⼀筆交易需要⼿續費,衡量標准是看交易的⼤⼩ (transaction size);但站在 Ethereum 的⾓度則認為,⼿續費的計算 應該取決於在區塊鏈平台上所需要的⼯作量
• 舉例來說,在 Ethereum 當中,兩個合約,程式碼⼤⼩不同,有可能 短的程式碼,耗費的 gas ⾼,因為裡⾯執⾏的動作很多牽涉到 sha3; 但在 Bitcoin 世界,交易包含的資料越多,占的⼤⼩越⼤,⼿續費較 ⾼ 47 -
Gas price • 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-RPC
๐ 率牧Իฃ砺֢ܨݢ蝚螂 hGp 蝱ᤈ牐অ蒂ฎ牧ইຎ襑ᥝࣁ MainNet ฎ TestNet Ӥ蝱ᤈ介手牧犋襑ᥝᛔ૩砇戔ଘݣ牐 – ਥො翕ᒊ物hGps://infura.io – 戢㲘癱蒈物hGps://infura.io/register.html – hGps://cl.ly/i7PG/INFURA.html 53 -
Ethereum Alarm Clock • Schedule transac`ons to be executed at
a later `me. • En`re service operates as smart contracts on the Ethereum blockchain, with no priviledged access given to any party. – hGp://www.ethereum-alarm-clock.com 56 -
Ethereum ComputaKon Market • The computa`on market is a service
for Ethereum that allows for verifiable off-chain execu`on of computa`ons that would be otherwise prohibi`vely expensive to execute within the EVM. – hGp://www.ethereum-computa`on-market.com 57 -
zeppelin-solidity • A Framework to Build Secure Smart Contracts –
Common contract security paGerns – In the Solidity language – hGps://openzeppelin.org 58 -
Ethereum Dev 1. 搭建 Ethereum 私有鏈 2. 運⾏兩個 node 讓彼此互連
3. 熟悉 geth JavaScript Console 4. 建⽴其他帳號, 建⽴交易, 觀察 block 5. 運⾏私有鏈的 miner 6. RPC protocol 使⽤與串接 7. Ethereum Block explorer 8. Smart Contract on Ethereum 9. Mist 10. Solidity 11. 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 -
Taipei Ethereum Meetup • 2016 區塊鏈愛好者⼤會 – http://lovechain.tw – 智能合約⼯作坊
• ⾹港商翱鶚股份有限公司 • http://cepave.com/blockchain-job-description/ 61