曾筱善
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 【學習筆記】區塊鏈介紹 ###### tags: `ncku` `blockchain` --- ## 1. 什麼是區塊鏈,對我們有什麼好處 區塊鏈是分散式帳本 (Distributed Ledger Technology) 的一種應用,透過分散式的節點替人們保存有用的資訊,而不依靠中央存儲節點。因此區塊鏈也可視為一種**資料庫**,並以密碼學、共識演算法等技術保障其安全、同步。 ![](https://i.imgur.com/anM6f5D.png) ### 區塊鏈的起源 (Bitcoin) 中本聰在 2008 年發表《Bitcoin: A Peer-to-Peer Electronic Cash System》,提到了如 P2P 網路、transaction、timestamp server、密碼學等區塊鏈的核心技術。並於 2009 年一月三號產生第一個區塊,稱為創世區塊 (Genesis block);一月九號產生第二個區塊,兩者相連誕生區塊鏈。 ### 應用領域 舉凡會產生資料、保存資料、交換資料等需求的應用,都可以加入區塊鏈。 e.g., 數位貨幣、車載網路、工業控制網路等等 --- ## 2. 區塊鏈的資料結構 ### Transactions 在比特幣中,一個區塊裡包含多筆交易 (Transaction) 資訊,在非數位貨幣的區塊鏈應用可能不是指真實的交易,但仍會將儲存在區塊鏈中的訊息叫做 transaction。 以比特幣系統為例,為了能夠追朔每筆交易的來源以及去向,transactions 間也會串聯起來,如下圖所示。 ![](https://i.imgur.com/XTmpDP1.png) 假設今天有一個人 `Owner 1`,他要把錢付款給 `Owner 2`,他就會把 `Owner 2` 的 public key,以及這筆錢的來源 (`Owner 0`) 連起來做雜湊 (Hash) 運算,並使用他自己的 private key 簽章 (Sign),存在該筆交易的末端。 但這邊會有一個問題是儘管 `Owner 2` 可以透過 public key 去驗證 (Verify) 知道該筆交易來自 `Owner 1`,卻無法知道他有沒有重覆花費這筆錢給其他人,因此要嘛經過一個中央管理者來確認該交易的唯一,要嘛有一套規則讓大家都能知道該交易曾經發生過,且並非重複的交易 ~~(共識演算法)~~。 ### Chain-structured blockchain 以比特幣為例,區塊鏈是一條從創始區塊 (Genesis) 到最新區塊具有最長距離的主鏈組成,其他位於非主鏈上的區塊稱為 invalid block,如下圖所示。 ![](https://i.imgur.com/bX3ruxn.png) 此外,(比特幣的)區塊鏈結構是一種 timestamp server 應用,會在區塊內加入時間戳以防雙重支付的攻擊 ![](https://i.imgur.com/qfJwctl.png) ### DAG-structured blockchain 還有另一種基於有向無環圖 (directed acyclic graph) 的區塊鏈結構,該種結構最大的不同是區塊有多個插入點,不再只有一個選項。且不再是以區塊當作區塊鏈中的基本單位,而是以交易 (transaction),如下圖方塊所示。往後在講這個結構的區塊鏈時不會再以區塊稱之。 ![](https://i.imgur.com/HJcVZbT.png) --- ## 3. 共識演算法-區塊上鏈的機制 因為區塊鏈中的各節點都能產生新的區塊,但是以鏈狀的區塊鏈來說區塊只能一個個依序插入,此時需要有一個機制來決定插入的順序,也就是共識 (Consensus) 演算法。 ### 工作量證明 (Proof-of-Work, PoW) 透過窮舉一個數字 (Nonce) 來找到一個值,使得在經過雜湊 (Hash) 函數之後的輸出能滿足需求。 > e.g., 找到一個 nonce 能使得 hash("hello World" || nonce) 輸出有 4 個 prefix 0 bit? > Ans: When nonce = 107105, then hash(Hello World107105) = **0000**BFE6AF4232F78B0C8EBA37A6BA6C17B9B8671473B0B82305880BE077EDD9 透過不斷消耗運算資源得出正確的值之後,該區塊內容便無法任意被更改,除非重新計算能滿足需求的 Nonce。當後繼有區塊欲加入區塊鏈時,需要將當前鏈中最新區塊的雜湊值包含在該區塊當中,如下圖。 ![](https://i.imgur.com/Wm8mwLI.png) 如此一來當有區塊內容變動時,其後繼的區塊皆能透過雜湊值去偵測出改變,也就是說若有人想要竄改其中一個區塊資訊的話,需要重新計算該區塊及其之後所有區塊的 Nonce。 使用 PoW 的系統便是看誰最快求出 Nonce 的節點方能加入到區塊鏈中,也就是說看誰運算資源最多就最有可能成功發佈區塊。 在[比特幣白皮書](https://bitcoin.org/bitcoin.pdf)提到,不同於一個IP位址有一個發佈區塊的投票權,PoW 是以一個 CPU 具一個投票權的方式,來解決掌握多個IP位址的霸權問題 ~~(但 PoW 不就只是把戰場拉到算力上嗎ˊ_>ˋ)~~ > If the majority were based on one-IP-address-one-vote, it could be subverted by anyone able to allocate many IPs. Proof-of-work is essentially one-CPU-one-vote. The majority decision is represented by the longest chain, which has the greatest proof-of-work effort invested in it. If a majority of CPU power is controlled by honest nodes, the honest chain will grow the fastest and outpace any competing chains. ### 權益證明 (Proof-of-Stake, PoS) PoS 用加密貨幣抵押量取代礦工算力的比拚,節點不需要花大筆錢買礦機或繳交鉅額電費,相反的,他們必須花錢買加密貨幣,並將這些加密貨幣抵押在智能合約中。 簡單來說「抵押越多加密貨幣、抵押時間越長且距離上次獲得記帳權越久的節點,獲得下一次記帳權的機率越高」 > \- 引用自 [桑幣知識+](https://know.zombit.info/pos/) 節點會將幣抵押在智能合約,區塊鏈系統會透過**偽亂數**、**持有時間**來決定獲得該次記帳權的節點,該節點便能將區塊加入鏈中。 #### 優點 * 不再需要浪費運算資源 * 提高驗證區塊效率 #### 缺點 * 富者恆富 ### 實用拜占庭容錯 (Practical Byzantine Fault Tolerance, PBFT) 在一個私有的區塊鏈中,只有幾個特別的節點能發佈區塊,它們叫做 Leader。這些 Leaders 會輪流發佈區塊,而為了確保節點間的區塊鏈能同步,他們會採多輪投票的方式來決定該區塊是否能加到鏈中,PBFT的主要流程分為 pre-prepare、prepare、commit 三個階段,如下圖所示。 下圖是一個精簡的PBFT流程,假設一個區塊鏈網路中具有 4 個 Leaders (下圖編號0~3) 節點及其他 (下圖編號C),假設節點 C 欲發佈一個新的區塊時,會發送請求給當時主導 PBFT 的 Leader (編號0),稱為 request 階段。 1. 編號 0 的 Leader 會將該區塊廣播給其餘 Leaders,稱作 pre-prepare 階段 2. 編號 1~3 Leaders 會去驗證該區塊是否合法,若贊成該區塊的發佈會廣播給其他 Leaders,若反對則不會發送任何訊息 (如編號 3),稱為 prepare 階段 3. 當 Leader 收到足夠多的贊成訊息,表示本次投票同意將區塊加入到鏈中,並將自己的決定回傳給發起請求的節點 (編號 C) ![](https://i.imgur.com/1ltJOnm.png) #### 優點 * 在私有的區塊鏈中,較不消耗運算資源來競爭加入區塊 #### 缺點 * 多輪投票大幅增加傳輸成本 ## 4. 安全威脅 區塊鏈並不是資訊安全仙丹,在 P2P 分散式系統中會遇到的威脅區塊鏈也同樣存在,如女巫攻擊 (Sybil attack),這邊以針對區塊鏈會產生的攻擊說明。 ### Transaction Privacy Leakage 發起交易需要對該交易使用密鑰 (Private Key) 進行簽章,若該密鑰被外人知道後,便可能被偽造身分進行交易。 ### 51% Attack 51% 攻擊並不能夠將別人的錢,轉成自己的錢 (因為這得取得數位簽章,也就是密鑰),而是可以把付出去的錢拿回來,重複使用那筆錢 (Double-spending)。 當「攻擊者的計算能力達全網一半以上 (51%)」,換句話說是攻擊者拳頭比其他全部人加起來還要大時,有大於一半的機率能夠取得新增區塊的權利 (記帳權),也就能「控制區塊鏈」。 以比特幣來說,攻擊者在獲得記帳權後會在區塊鏈中進行一次交易 **`A`** (將新增的區塊廣播出去),然而攻擊者同時保有一個不對外廣播的秘密塊鏈,在這個祕密的區塊鏈是花同樣的錢做其他的交易 **`B`**。因為攻擊者具有一半以上的算力,只要攻擊者把祕密區塊鏈的長度大於原本的主鏈時,便將該區塊鏈廣播出去。 因為在鏈狀結構 (Chain-structured) 的區塊鏈中,最長的 (或稱工作量最多) 的鏈稱為主鏈,若攻擊者將較長的秘密區塊鏈發佈出來,其他節點會將交易 **`B`** 及其之後的交易視為合法,原本含交易 **`A`** 的鏈會被視為無效,但攻擊者仍擁有經過交易 **`A`** 得到的商品。 > ref [Crypto51](https://www.crypto51.app/about.html)、[桑幣知識+](https://know.zombit.info/51%E6%94%BB%E6%93%8A/)、[知乎](https://zhuanlan.zhihu.com/p/73951414) 最近發生過較大型的 51% Attack 事件發生在 2020 年 8/6 的以太坊經典 (Ethereum Classic) 。造成 4268 個區塊重組以及造成 168 萬美金的 double-spending > ref [Ethereum Classic Attacker Successfully Double-Spends $1.68M in Second Attack: Report](https://www.coindesk.com/ethereum-classic-attacker-successfully-double-spends-1-68m-in-second-attack-report) --- ## 其他區塊鏈的介紹 ### 以太坊 以太坊是一個具有**智慧合同 (Smart Contract)** 功能的區塊鏈,2013-2014 年間由比特幣的程式設計師開發,有時被稱作下一代加密貨幣與去中心化應用平台。 智慧合同 (Smart Contract) 用意與現實中的合同(契約)無異,不過是合同呈現的方式不同。透過合同可以建立交易的規範、對象、金額。若把智慧合同比擬成募資平台([嘖嘖](https://www.zeczec.com/)),consumer 與 provider 之間透過嘖嘖交易,consumer 把錢交給嘖嘖保管,而 provider 也相信嘖嘖會付錢給他們。 而將智慧合同結合區塊鏈的最大用意就是消除第三方的存在(募資平台),讓 consumer 與 provider 能夠直接進行交易,同時又能確保交易不會被任一方反悔、竄改。 ### IOTA IOTA 是目前使用 DAG 結構區塊鏈最著名的組織之一,於 2015 年左右發明,其區塊鏈稱為 Tangle,原因是 DAG 結構的區塊鏈看起來就像是纏結一樣 --- ## 學習資源 > 區塊鏈的白皮書,及入門所需理解的各種知識 **Bitcoin** [【白皮書】Bitcoin: A Peer-to-Peer Electronic Cash System](https://bitcoin.org/bitcoin.pdf) [Bitcoin 軟體開發](http://davidederosa.com/basic-blockchain-programming/) **IOTA - IOTA 1.0/1.5** [【白皮書】The Tangle](https://assets.ctfassets.net/r1dr6vzfxhev/2t4uxvsIqk0EUau6g2sw0g/45eae33637ca92f85dd9f4a3a218e1ec/iota1_4_3.pdf) **Ethereum** [【白皮書】Ethereum Whitepaper](https://ethereum.org/en/whitepaper/) **密碼學** [ECC](https://andrea.corbellini.name/2015/05/17/elliptic-curve-cryptography-a-gentle-introduction/) **共識演算法** ###

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully