大話區塊鏈和比特幣的技術原理

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前言:區塊鏈大熱之後,似乎淡出,但央行推出數字貨幣再次把區塊鏈推上風口浪尖。本文用大白話,簡單明瞭的講解區塊鏈和比特幣的技術原理。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"歷史"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"說到區塊鏈,不得不提一下比特幣。我們來看幾個比特幣的歷史事件:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2008年10月,署名爲Satoshi Nakamoto(中本聰)的人發表了一篇名爲“Bitcoin: A Peer-to-Peer Electronic Cash System”的論文。論述了比特幣的技術原理,也就是區塊鏈的核心原理。這個文章大家可以在網上搜到。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2009年1月,中本聰發佈了比特幣軟件。他隨即創建了區塊鏈上的第一個數據塊Block,這個Block被稱作創世塊(Genesis Block)。因爲創建了這個數據塊,他獲得了50個比特幣的獎勵,這就是傳說中的挖礦賺金幣。一直沒有人知道他具體是誰。後來他就慢慢消失了,他擁有至少100萬個比特幣。退休啦!"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"發佈當天,一個名爲Hal Finney的哥們下載了比特幣軟件,中本聰轉給了它10個比特幣,這是比特幣軟件上第一筆交易。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2010年5月22日,名爲Hanyecz的人做了第一個現實中的比特幣交易,他用10000個比特幣買了兩個價值25美元的披薩,平均每個比特幣價值0.008美元。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之後比特幣價格扶搖直上,最高達到近20000多美元。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比特幣現象引領了區塊鏈技術的熱潮,區塊鏈技術以中本聰的論文爲核心基礎,當然後面大家也有很多改進和創新。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"定義"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"區塊鏈是一種去中心化的數據庫,同一份數據在參與者的電腦上都複製了一份;數據一旦被寫入區塊鏈,就很難被修改;不同電腦上的數據不斷相互同步並驗證數據的有效性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"區塊鏈沒有官方的定義,我這個定義很清晰的說明了區塊鏈技術的3個核心特徵。我們一個個來看。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"一種數據庫"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"區塊鏈是一種去中心化的數據庫,同一份數據在參與者的電腦上都有保存。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏有兩個要點:它是數據庫,通俗點可以理解成賬本;它是去中心化的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"先說去中心化,簡單說就是:這個賬本所有相關人都人手一份。沒有一箇中心人物掌握和控制這個賬本,人人平等。大家想想我們的銀行系統呢?這個賬本只有銀行有,你沒有,是中心化的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它是一種數據庫,什麼樣的數據庫呢?它是由數據塊Block組成的,這些數據塊前後相連,連成一個鏈,所以被稱爲區塊鏈。對於懂技術的同學,這就是一個鏈表。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1b/1bda088f5acdb80397441f1ee5bd5511.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這些塊裏面可以放任何數據。對於比特幣來說,放的就是交易信息:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"張三轉給麥叔3個比特幣"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"李四裝給麥叔4個比特幣"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"王五轉給麥叔5個比特幣"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比特幣的第一個塊是由中本聰創建的,根據比特幣軟件的規則,因爲他成功創建了這個數據塊,系統獎勵給他50個比特幣。系統的比特幣哪裏來的?是憑空創造出來的,就像中央銀行印刷鈔票一樣。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比特幣的參與者就是在努力的去創造數據塊,因爲創造數據塊可以獎勵比特幣。這就是挖礦。但是創造數據塊沒那麼容易,需要不停的挖呀挖呀,這個等下我會說。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在比特幣一共有多少個Block呢?截止到我正在碼字的這一刻,比特幣一共有553568個Block。最後這塊是一箇中國哥們挖出來的,這個數據塊裏面包含了2795筆交易數據,爲此他獲得了12.5個比特幣的獎勵。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"獎勵每4年就會減半,直到最後不再有獎勵。所以越來越難挖礦到比特幣,發財要趁早啊!比特幣最後一共有2100萬個,然後就不會再有新的比特幣了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比特幣的信仰者認爲比特幣最後總量是固定的,所以不會有通貨膨脹,因此具有價值。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/64/64516bc53da459eae588f7b6b249b467.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有興趣的同學可以在這裏查看比特幣的Block信息:https://www.blockchain.com/explorer"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"很難被修改"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"區塊鏈的數據塊,一旦被挖出來了,就很難被修改,因爲它的數據塊不是一般的數據塊,是數據塊中的戰鬥塊!來看一下結構:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/96/96a4dfa60ef32c2589188d8b6733a927.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"圖中有兩個數據塊,我用3個要點來說它是怎麼不一般:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每個數據塊都有一個指紋(懂技術的同學,指紋就是Hash)。這個指紋是把這個塊裏面的所有數據經過一種算法轉換成的一個64位的字符串。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦數據塊裏的任何數據變化了,這個64位的指紋都會發生完全的變化,這就是我說的雪崩效應。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個算法就是哈希算法,常用的是sha256(傻256)。我們來試驗一下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"數據是:中本聰轉給麥叔88個比特幣”"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"Hash是:90621272001aed0b5f986943adc34ce2feae5d1e9ed9684f73d1ec1673de3247"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果聰明的我,把數據修改一下,悄悄加了1個0,Hash值將會變得完全不一樣。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"數據是:中本聰轉給麥叔808個比特幣”"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"Hash是:0bdaababb3efa3149f30c0f45319fcf0249f4d289d071b93e7694b5c3471a179"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有興趣的同學可以自己網上找個在線計算器玩一下,比如這個地址:https://emn178.github.io/online-tools/sha256.html"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"後一個數據塊保存了前一個數據塊的Hash,這也就是說,如果前一個數據塊變化了,後一個數據塊保存的Hash和前一個數據塊就完全不同了。區塊鏈軟件會進行這個驗證,如果不同就說明數據是無效的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果我把後一個Block修改一下,加上新的前一個塊的Hash呢?那麼根據要點1,後一個塊的Hash就又變了,造成了再後一個塊又無效了。造成多米諾骨牌式的崩塌,雪崩效應加劇了!"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f5/f5bbb30f248f18198759a8ed1cb17967.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每個塊的Hash必須以多個數字0開頭,加大計算Hash的難度。要點2提到修改了某Block後,就需要修改所有後續Block中的Hash。但如果計算機運算速度快,就一個個修改過去吧,計算Hash是非常快的。比特幣到現在也只有55萬個Block,找一臺怪獸機來恐怕一會就都計算好了,就成功篡改了數據。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以比特幣引入了要點3:不是你隨便計算出來一個Hash就可以的,你計算出來的Hash必須以若干個0開頭(假設6個0開頭)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Hash不是根據數據自動算出來的嗎,怎麼能隨便修改成0開頭呢?所以有了挖礦專用數字。在每個Block中添加了一個額外的數字,這個數字你可以隨便改,直到你能讓整個Block的Hash是以6個0開頭。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所謂的挖礦就是找到這個數字,讓Hash以6個0開頭。就是我圖中下面標的綠油油的綠寶石一樣的數字。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d4/d410d784b2034f19dffe19e8567cee6a.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"具體幾個0呢?具體幾個是會變化的。比特幣設計的原則是整個網絡中大約每10分鐘能有1個人挖出來1個Block。如果挖的人少了,就會減少要求的0的個數,這樣就容易挖點,相當於降低要求;如果挖的人多了,就會增加難度,要求更多的0。軟件會定期自動調整要求的0個數,比特幣是每週調整一次。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣綜合起來看:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你要改一個數據,需要從新計算當前Block的Hash,爲了保證它的Hash符合0開頭的條件,你要不停計算,整個參與比特幣網絡的人要大約10分鐘才能計算出來,你自己機器肯定需要更長時間,假設10天吧;10天后你找到了,然後你又要10天的時間計算下一個數據塊的。如果你修改的Block後面有1000個Block,你需要10000天才能算完。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"10000天后,大功告成了嗎?問題是,在這10000天時間裏,整個網絡上已經又新加了大約10000x24x6個新的數據塊了。你還能跟得上嗎?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"同步機制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就算你可以修改自己的那份數據,也都計算出了Hash,但你還需要和網絡上的其他參與者同步。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同步是一個複雜的過程,但至少驗證以下幾件事情:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"是否每個Block都有符合條件的Hash,就是以多個0開頭並且確實是根據Block的數據計算出來的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏注意:計算Hash,也就是挖礦是很費時,但是驗證是否正確是非常快的!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"是否每個Block都保存了正確的前一個Block的Hash。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比特幣等數字貨幣還會驗證裏面的轉賬交易是否合理,比如張三要轉給我3個比特幣,但他實際上1個幣都沒有,那交易肯定不合理了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最後還會比較誰的有效的鏈條更長。如果出現不一致,誰的長聽誰的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以我們前面討論的情況,修改數據的人幾乎永遠跟不上大部隊的長度,除非他的計算能力超過了所有其他人的總和!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7d/7dca4d1062f70f76fc3fad4ef4e8c8fd.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"應用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在比特幣的價格飛流直下,反對者認爲它的價值可能會歸零。不管比特幣如何,區塊鏈技術都是大有用武之地的,否則我也不會寫這麼長了吧。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有人這麼說:人工智能(AI)技術是生產力的革命,而區塊鏈是生產關係的革命,因爲它去除了中心機構,從軟件底層機制上解決了人與人之間的信任關係。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦理解了它的本質,就可以在很多場景上去應用它,而不僅僅是比特幣這樣的數字貨幣,比如:"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"產品(藥品,食物)溯源"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"我們前面說的警察檔案庫"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"投票系統"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"供應鏈系統等"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就具體的應用案例,我們後面可以再細聊。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"歡迎在InfoQ平臺關注我,也可以關注我的公衆號:麥叔編程。"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章