智能合約結合區塊鏈簡介
TRANSCRIPT
智慧合約SMART CONTRACT
林修平
CONTRACT
• 什麼是合約?• 雙方或多方基於意思表示而成立的法律行為• 買賣、租賃、保險、投資等• 履約方式:主動、被動
“SMART” CONTRACT
• 履約方式:自動執行• 前提:條件達成
• “SMART”? How?
• 生活中的智慧合約:• 自動販賣機• U-bike, 線上投注• 和自動化服務有什麼不一樣?
“SMART” CONTRACT
smart contract on blockchain
• 有什麼差別?• 區塊鏈的價值?
• 相信誰?• 一個人 ?
• 一家企業 ? https://en.wikipedia.org/wiki/Financial_crisis_of_2007–08
• 政府 ?https://en.wikipedia.org/wiki/Watergate_scandal
• 或程式碼和數學原理?• 選擇權在你手上
• 和沒有信任基礎的對象達成共識
smart contract on blockchain
• https://www.thenewslens.com/article/11796
• 會自動購買洗衣粉的洗衣機• 在沒有區塊鏈的情況下
• 金流: • 銀行、第三方支付 -> 洗衣粉公司 -> 貨運公司
• 成本:• 時間和人力檢查、核對金流和物流狀況
• 如果今天其中某一環節出錯了? • 要花多久的時間發現問題? • 誰會發現問題?• 消費者要等多久?
smart contract on blockchain
• https://www.thenewslens.com/article/11796
• 會自動購買洗衣粉的洗衣機• 在有區塊鏈的情況下
• 金流: • 銀行、第三方支付 -> 洗衣粉公司 -> 貨運公司
• 成本:• 物流狀況檢查
• 如果今天其中某一環節出錯了? • 要花多久的時間發現問題? • 誰會發現問題?• 消費者要等多久?
smart contract on blockchain
BLOCKCHAIN + IOT
• IOT
• 連網裝置?• 裝置互連
• 要怎麼確保連上的是不是惡意的裝置?• 要怎麼確保裝置所說的話是可信的?
• 有同樣的共識、同樣的資訊• 分散式?
• 還要考慮裝置的硬體資源• 多中心?
BLOCKCHAIN + IOT?
• IOT 裝置的問題• 安全• 硬體資源
BLOCKCHAIN + IOT?
CHALLENGE
CHALLENGE
• 外部資訊
• 資產流動性• 鏈外的掌控
• 假設你和朋友對賭明天的比賽分數• 智慧合約要怎麼獲取分數這個資訊?
• 去抓取外部資訊?• 要相信哪個來源?• 每個節點都能從同樣一個來源獲得資訊嗎?• 這個資訊會過期嗎?不見嗎?隨時間改變嗎?• 目前區塊鏈上的智慧合約不支援主動抓取外部資訊
• 由第三方提供
CHALLENGE
• 外部資訊• 資產流動性
• 鏈外的掌控
• 如果你向保險公司買了一份保險,你付的保費會一直待在“某個地方”嗎?• 像銀行儲蓄一樣,這筆錢會被保險公司或銀行拿去進行投資等行為,讓效益最大化,直到你需要這筆錢• 那這樣要怎麼確保智慧合約執行的效力?
CHALLENGE
• 外部資訊• 資產流動性• 鏈外的掌控
• 並非所有事情皆可在鏈上完成,那鏈外的行為要怎麼執行?• 由法律管道?
Ethereum smart contract
Ethereum smart contract
• 圖靈完備• 條件分支( conditional branching )
• 迴圈• 改變任意數量的記憶體
Ethereum smart contract
• 比特幣• 圖靈不完備
• OP_IF, OP_NOTIF, OP_ELSE, OP_ENDIF
• 不保存狀態• stack-based language
• 不支援迴圈• 有意的設計
• 避免複雜的計算及無窮迴圈的可能性
Ethereum smart contract
• 圖靈完備• 條件分支( conditional branching )
• 迴圈• 改變任意數量的記憶體• 能做到更複雜、更強大的功能• 怎麼對付無窮迴圈等的惡意攻擊?
• gas
• 每種運算都有相對應的成本( gas )• 花了多少 gas就要付出相對的錢,但使用者可以自己決定每單位 gas 的價錢• 用 ether 支付
Ethereum smart contract
• 要怎麼建立合約?• 1. 寫合約
Ethereum smart contract
• 要怎麼建立合約?• 1. 寫合約• 2. 編譯合約,轉成 bytecode
Ethereum smart contract
• 要怎麼建立合約?• 1. 寫合約• 2. 編譯合約,轉成
bytecode
• 3. 廣播並等待被放入區塊中
Ethereum smart contract
• 要怎麼建立合約?• 1. 寫合約• 2. 編譯合約,轉成
bytecode
• 3. 廣播並等待被放入區塊中• 4. 執行合約
Ethereum smart contract
• 合約語言• Serpent
• python-like
• Solidity
• javascript-like
Ethereum smart contract
• 要怎麼建立合約?• 1. 寫合約• 2. 編譯合約,轉成 bytecode
• 3. 廣播並等待被放入區塊中• 4. 執行合約
https://ethereum.github.io/browser-solidity/
自己跑一個節點 :https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum
或使用錢包 :https://github.com/ethereum/mist/releases
living example : DAO
living example : DAO
• Decentralized Autonomous Organization
• by slock.it
• crowdfunding
• vote by your share
• profit by your share
• child DAO
• to prevent abuse of majority voting
living example : DAO
• Recursive calling vulnerability
• propose a split -> execute the split -> withdraw
• propose a split -> execute the split -> propose a split -> execute the split -> withdraw
living example : DAO
• function splitDAO
problem1problem2
living example : DAO
• function withdrawRewardFor
problem1
living example : DAO
• function payOut
problem1
• address.call.value(amount)• supply with enough gas• if address refers to a contract, it triggers fallback function
• do what whatever you want as long as enough gas remaining• call to splitDAO again in fallback function
living example : DAO
• function splitDAO
problem2
• cleans up his/her balance after sending the money
living example : DAO
• counter measure :
• use address.send(amount)
• 2300 gas
• don’t execute address.call.value(amount) to untrusted code
• limit gas in address.call.value(amount)
• address.call.gas(gas_amount).value(amount)
• cleans up the balance before sending money
• then check if sending succed, if not ,rollback
living example : DAO
• what happens after?
• votes for hard fork
• choose to stay, becomes ETC
• choose to fork, becomes ETH
• Ethereum foundation, token holders, immutability believers
• too much money to fail? lawsuits?
• what do you believe?