區塊鏈太複雜,那咱們就講點簡單的。用JS來構建你本身的區塊鏈系統,寥寥幾行代碼就能夠說明區塊鏈的底層數據結構、POW挖礦思想和交易過程等。固然了,真實的場景遠遠遠比這複雜。本文的目的僅限於讓你們初步瞭解、初步認識區塊鏈。node
文章內容主要參考視頻:Building a blockchain with Javascript (https://www.youtube.com/playlist?list=PLzvRQMJ9HDiTqZmbtFisdXFxul5k0F-Q4)數組
感謝原做者,本文在原視頻基礎上作了修改補充,並加入了我的理解。數據結構
區塊鏈顧名思義是由區塊鏈接而成的鏈,所以最基本的數據結構是Block。每一個Block都含有timestamp、data、hash、previousHash等信息。其中data用來存儲數據,previousHash是前一個區塊的hash值。示意以下:區塊鏈
hash是對區塊信息的摘要存儲,hash的好處是任意長度的信息通過hash均可以映射成固定長度的字符串,如可用sha256:ui
calculateHash() { return SHA256(this.previousHash + this.timestamp + JSON.stringify(this.data)).toString(); }
Block的最基本數據結構以下:this
class Block { constructor(timestamp, data, previousHash = '') { this.timestamp = timestamp; this.data = data; this.previousHash = previousHash; //對hash的計算必須放在最後,保證全部數據賦值正確後再計算 this.hash = this.calculateHash(); } calculateHash() { return SHA256(this.previousHash + this.timestamp + JSON.stringify(this.data)).toString(); } }
多個Block連接而成BlockChain,顯然可用用數組或鏈表來表示,如:spa
class BlockChain { constructor() { this.chain = []; } }
正所謂萬物始於一,區塊鏈的第一個區塊老是須要人爲來手動建立,這個區塊的previousHash爲空,如:code
createGenesisBlock() { return new Block("2018-11-11 00:00:00", "Genesis block of simple chain", ""); }
區塊鏈的構造方法也應改成:視頻
class BlockChain { constructor() { this.chain = [this.createGenesisBlock()]; } }
每新加一個區塊,必須保證與原有區塊鏈鏈接起來,即:blog
class BlockChain { getLatestBlock() { return this.chain[this.chain.length - 1]; } addBlock(newBlock) { //新區塊的前一個hash值是現有區塊鏈的最後一個區塊的hash值; newBlock.previousHash = this.getLatestBlock().hash; //從新計算新區塊的hash值(由於指定了previousHash); newBlock.hash = newBlock.calculateHash(); //把新區塊加入到鏈中; this.chain.push(newBlock); } ... }
區塊鏈數據結構的核心是保證先後連接、沒法篡改,可是若是有人真的篡改了某個區塊,咱們該如何校驗發現呢?最笨也是最天然是想法就是遍歷全部狀況,逐一校驗,如:
isChainValid() { //遍歷全部區塊 for (let i = 1; i < this.chain.length; i++) { const currentBlock = this.chain[i]; const previousBlock = this.chain[i - 1]; //從新計算當前區塊的hash值,若發現hash值對不上,說明該區塊有數據被篡改,hash值未從新計算 if (currentBlock.hash !== currentBlock.calculateHash()) { console.error("hash not equal: " + JSON.stringify(currentBlock)); return false; } //判斷當前區塊的previousHash是否真的等於前一個區塊的hash,若不等,說明前一個區塊被篡改,雖然hash值被從新計算正確,可是後續區塊的hash值並未從新計算,致使整個鏈斷裂 if (currentBlock.previousHash !== previousBlock.calculateHash) { console.error("previous hash not right: " + JSON.stringify(currentBlock)); return false; } } return true; }
跑起來看看,即:
let simpleChain = new BlockChain(); simpleChain.addBlock(new Block("2018-11-11 00:00:01", {amount: 10})); simpleChain.addBlock(new Block("2018-11-11 00:00:02", {amount: 20})); console.log(JSON.stringify(simpleChain, null, 4)); console.log("is the chain valid? " + simpleChain.isChainValid());
結果以下:
ali-186590cc4a7f:simple-chain shanyao$ node main_1.js { "chain": [ { "timestamp": "2018-11-11 00:00:00", "data": "Genesis block of simple chain", "previousHash": "", "hash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89" }, { "timestamp": "2018-11-11 00:00:01", "data": { "amount": 10 }, "previousHash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89", "hash": "150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529" }, { "timestamp": "2018-11-11 00:00:02", "data": { "amount": 20 }, "previousHash": "150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529", "hash": "274a7a13ed20118e8cb745654934a7e24a4d59333ba17dfbf5d4cfe0fa8a6e34" } ] } is the chain valid? true
注意看其中的previousHash與hash,確實是當前區塊的previousHash指向前一個區塊的hash。
都說區塊鏈不可篡改,是真的嗎?讓咱們篡改第2個區塊試試,如:
let simpleChain = new BlockChain(); simpleChain.addBlock(new Block("2018-11-11 00:00:01", {amount: 10})); simpleChain.addBlock(new Block("2018-11-11 00:00:02", {amount: 20})); console.log("is the chain valid? " + simpleChain.isChainValid()); //將第2個區塊的數據,由10改成15 simpleChain.chain[1].data = {amount: 15}; console.log("is the chain still valid? " + simpleChain.isChainValid()); console.log(JSON.stringify(simpleChain, null, 4));
結果以下:
ali-186590cc4a7f:simple-chain shanyao$ node main_1.js is the chain valid? true hash not equal: {"timestamp":"2018-11-11 00:00:01","data":{"amount":15},"previousHash":"fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89","hash":"150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529"} is the chain still valid? false { "chain": [ { "timestamp": "2018-11-11 00:00:00", "data": "Genesis block of simple chain", "previousHash": "", "hash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89" }, { "timestamp": "2018-11-11 00:00:01", "data": { "amount": 15 }, "previousHash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89", "hash": "150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529" }, { "timestamp": "2018-11-11 00:00:02", "data": { "amount": 20 }, "previousHash": "150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529", "hash": "274a7a13ed20118e8cb745654934a7e24a4d59333ba17dfbf5d4cfe0fa8a6e34" } ] }
顯然,篡改了數據以後,hash值並未從新計算,致使該區塊的hash值對不上。
那麼,若是咱們聰明點,篡改後把hash值也從新計算會如何?
let simpleChain = new BlockChain(); simpleChain.addBlock(new Block("2018-11-11 00:00:01", {amount: 10})); simpleChain.addBlock(new Block("2018-11-11 00:00:02", {amount: 20})); console.log("is the chain valid? " + simpleChain.isChainValid()); //篡改後從新計算hash值 simpleChain.chain[1].data = {amount: 15}; simpleChain.chain[1].hash = simpleChain.chain[1].calculateHash(); console.log("is the chain still valid? " + simpleChain.isChainValid()); console.log(JSON.stringify(simpleChain, null, 4));
結果以下:
ali-186590cc4a7f:simple-chain shanyao$ node main_1.js is the chain valid? true previous hash not right: {"timestamp":"2018-11-11 00:00:02","data":{"amount":20},"previousHash":"150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529","hash":"274a7a13ed20118e8cb745654934a7e24a4d59333ba17dfbf5d4cfe0fa8a6e34"} is the chain still valid? false { "chain": [ { "timestamp": "2018-11-11 00:00:00", "data": "Genesis block of simple chain", "previousHash": "", "hash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89" }, { "timestamp": "2018-11-11 00:00:01", "data": { "amount": 15 }, "previousHash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89", "hash": "74d139274fb692495b7c805dd5822faa0c5b5e6058b6beef96e87e18ab83a6b1" }, { "timestamp": "2018-11-11 00:00:02", "data": { "amount": 20 }, "previousHash": "150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529", "hash": "274a7a13ed20118e8cb745654934a7e24a4d59333ba17dfbf5d4cfe0fa8a6e34" } ] }
顯然,第3個區塊的previousHash並未指向第2個區塊的hash。
其實並非,若是咱們再聰明一點,把後續區塊的hash值也從新計算一下,不就OK了嗎? 確實如此,如:
let simpleChain = new BlockChain(); simpleChain.addBlock(new Block("2018-11-11 00:00:01", {amount: 10})); simpleChain.addBlock(new Block("2018-11-11 00:00:02", {amount: 20})); console.log("is the chain valid? " + simpleChain.isChainValid()); //篡改第2個區塊 simpleChain.chain[1].data = {amount: 15}; simpleChain.chain[1].hash = simpleChain.chain[1].calculateHash(); //並把第3個區塊也從新計算 simpleChain.chain[2].previousHash = simpleChain.chain[1].hash; simpleChain.chain[2].hash = simpleChain.chain[2].calculateHash(); console.log("is the chain still valid? " + simpleChain.isChainValid()); console.log(JSON.stringify(simpleChain, null, 4