您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關js如何實現blockchain區塊鏈的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
了解blockchain的概念很簡單(區塊鏈,交易鏈塊):它是分布式的(即不是放置在同一臺機器上,不同的網絡設備上的)數據庫支持主辦記錄日益增長的名單。但是,這也是容易混淆blockchain與我們試圖幫他解決了目標 - 在人們心中的那一刻,這個詞是相當強烈的交易,合同或智能cryptocurrency的概念有關。
只有在這里blockchain - 是不是一回事比特幣,并理解鏈塊的基本知識比它似乎更容易,尤其是在,它是基于源代碼的情況下。在本文中,我們提出了建立與在JavaScript中200行代碼的簡單模型。這個項目,我們稱之為NaiveChain的源代碼,可以在GitHub上找到。第1部分和第2部分:如果您需要刷上它的功能,使用我們的備忘單,我們將使用標準的ECMAScript 6。
塊結構
第一步 - 確定應包含塊的元素。為簡單起見,我們只包括最必要的:先前塊的指數(指數),時間標記(時間戳),數據(數據),散列和散列,要錄制,以保持電路的結構完整性。
class Block { constructor(index, previousHash, timestamp, data, hash) { this.index = index; this.previousHash = previousHash.toString(); this.timestamp = timestamp; this.data = data; this.hash = hash.toString(); } }
散列單元
哈希塊需要保持數據的完整性。在我們的例子,這適用于算法SHA-256。這種類型的散列是不相關的開采,因為在這種情況下,我們并沒有用表現證明實施保護。
var calculateHash = (index, previousHash, timestamp, data) => { return CryptoJS.SHA256(index + previousHash + timestamp + data).toString(); };
產生單元
要生成塊,我們需要知道前一個塊的哈希,使我們在結構已經確定了元素的其余部分。數據由最終用戶提供。
var generateNextBlock = (blockData) => { var previousBlock = getLatestBlock(); var nextIndex = previousBlock.index + 1; var nextTimestamp = new Date().getTime() / 1000; var nextHash = calculateHash(nextIndex, previousBlock.hash, nextTimestamp, blockData); return new Block(nextIndex, previousBlock.hash, nextTimestamp, blockData, nextHash); };
存儲單元
使用blockchain 存儲陣列。第一個塊總是硬編碼“創世紀塊”。
var getGenesisBlock = () => { return new Block(0, "0", 1465154705, "my genesis block!!", "816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7"); }; var blockchain = [getGenesisBlock()];
確認塊的完整性
我們必須始終能夠確認單元或電路的完整性。尤其是當你從其他單位新的單位,必須決定是否接受它們。
var isValidNewBlock = (newBlock, previousBlock) => { if (previousBlock.index + 1 !== newBlock.index) { console.log('invalid index'); return false; } else if (previousBlock.hash !== newBlock.previousHash) { console.log('invalid previoushash'); return false; } else if (calculateHashForBlock(newBlock) !== newBlock.hash) { console.log(typeof (newBlock.hash) + ' ' + typeof calculateHashForBlock(newBlock)); console.log('invalid hash: ' + calculateHashForBlock(newBlock) + ' ' + newBlock.hash); return false; } return true; };
選擇鏈最長的
在電路塊的順序必須被明確指定,但是在發生沖突的情況下(例如,兩個節點同時在同一生成的塊和相同數量),我們選擇電路,其中包含的塊的數量較多。
var replaceChain = (newBlocks) => { if (isValidChain(newBlocks) && newBlocks.length > blockchain.length) { console.log('Received blockchain is valid. Replacing current blockchain with received blockchain'); blockchain = newBlocks; broadcast(responseLatestMsg()); } else { console.log('Received blockchain invalid'); } };
消息到其它網絡節點
該網站的一個組成部分 - 與其他節點的數據交換。下列規則用于維護網絡同步:
當一個節點產生新的單元,它會報告給網絡;
當本機連接到新的盛宴,他要求有關最后生成的塊信息;
當一個節點正面臨著一個塊,其中有一個指標比他還大,他增加了一個塊到電路或請求的完整鏈條的信息。
自動搜索同齡人不執行,所有環節都手動添加。
單元的控制
用戶應該能夠以某種方式控制節點,通過將HTTP服務器解決。當與節點相互作用有以下功能:
打印所有單元的列表;
創建用戶生成內容的新單元;
打印列表,或添加的節日。
互動的最直接的方式 - 通過卷曲:
一個節點上的所有塊#名單
curl http://localhost:3001/blocks
架構
值得注意的是,該網站是指兩個Web服務器:HTTP進行用戶控制的裝置和向所述的WebSocket HTTP來安裝節點之間的P2P連接。
如下為js 200行代碼
<span >'use strict';</span> var CryptoJS = require("crypto-js"); var express = require("express"); var bodyParser = require('body-parser'); var WebSocket = require("ws"); var http_port = process.env.HTTP_PORT || 3001; var p2p_port = process.env.P2P_PORT || 6001; var initialPeers = process.env.PEERS ? process.env.PEERS.split(',') : []; class Block { constructor(index, previousHash, timestamp, data, hash) { this.index = index; this.previousHash = previousHash.toString(); this.timestamp = timestamp; this.data = data; this.hash = hash.toString(); } } var sockets = []; var MessageType = { QUERY_LATEST: 0, QUERY_ALL: 1, RESPONSE_BLOCKCHAIN: 2 }; var getGenesisBlock = () => { return new Block(0, "0", 1465154705, "my genesis block!!", "816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7"); }; var blockchain = [getGenesisBlock()]; var initHttpServer = () => { var app = express(); app.use(bodyParser.json()); app.get('/blocks', (req, res) => res.send(JSON.stringify(blockchain))); app.post('/mineBlock', (req, res) => { var newBlock = generateNextBlock(req.body.data); addBlock(newBlock); broadcast(responseLatestMsg()); console.log('block added: ' + JSON.stringify(newBlock)); res.send(); }); app.get('/peers', (req, res) => { res.send(sockets.map(s => s._socket.remoteAddress + ':' + s._socket.remotePort)); }); app.post('/addPeer', (req, res) => { connectToPeers([req.body.peer]); res.send(); }); app.listen(http_port, () => console.log('Listening http on port: ' + http_port)); }; var initP2PServer = () => { var server = new WebSocket.Server({port: p2p_port}); server.on('connection', ws => initConnection(ws)); console.log('listening websocket p2p port on: ' + p2p_port); }; var initConnection = (ws) => { sockets.push(ws); initMessageHandler(ws); initErrorHandler(ws); write(ws, queryChainLengthMsg()); }; var initMessageHandler = (ws) => { ws.on('message', (data) => { var message = JSON.parse(data); console.log('Received message' + JSON.stringify(message)); switch (message.type) { case MessageType.QUERY_LATEST: write(ws, responseLatestMsg()); break; case MessageType.QUERY_ALL: write(ws, responseChainMsg()); break; case MessageType.RESPONSE_BLOCKCHAIN: handleBlockchainResponse(message); break; } }); }; var initErrorHandler = (ws) => { var closeConnection = (ws) => { console.log('connection failed to peer: ' + ws.url); sockets.splice(sockets.indexOf(ws), 1); }; ws.on('close', () => closeConnection(ws)); ws.on('error', () => closeConnection(ws)); }; var generateNextBlock = (blockData) => { var previousBlock = getLatestBlock(); var nextIndex = previousBlock.index + 1; var nextTimestamp = new Date().getTime() / 1000; var nextHash = calculateHash(nextIndex, previousBlock.hash, nextTimestamp, blockData); return new Block(nextIndex, previousBlock.hash, nextTimestamp, blockData, nextHash); }; var calculateHashForBlock = (block) => { return calculateHash(block.index, block.previousHash, block.timestamp, block.data); }; var calculateHash = (index, previousHash, timestamp, data) => { return CryptoJS.SHA256(index + previousHash + timestamp + data).toString(); }; var addBlock = (newBlock) => { if (isValidNewBlock(newBlock, getLatestBlock())) { blockchain.push(newBlock); } }; var isValidNewBlock = (newBlock, previousBlock) => { if (previousBlock.index + 1 !== newBlock.index) { console.log('invalid index'); return false; } else if (previousBlock.hash !== newBlock.previousHash) { console.log('invalid previoushash'); return false; } else if (calculateHashForBlock(newBlock) !== newBlock.hash) { console.log(typeof (newBlock.hash) + ' ' + typeof calculateHashForBlock(newBlock)); console.log('invalid hash: ' + calculateHashForBlock(newBlock) + ' ' + newBlock.hash); return false; } return true; }; var connectToPeers = (newPeers) => { newPeers.forEach((peer) => { var ws = new WebSocket(peer); ws.on('open', () => initConnection(ws)); ws.on('error', () => { console.log('connection failed') }); }); }; var handleBlockchainResponse = (message) => { var receivedBlocks = JSON.parse(message.data).sort((b1, b2) => (b1.index - b2.index)); var latestBlockReceived = receivedBlocks[receivedBlocks.length - 1]; var latestBlockHeld = getLatestBlock(); if (latestBlockReceived.index > latestBlockHeld.index) { console.log('blockchain possibly behind. We got: ' + latestBlockHeld.index + ' Peer got: ' + latestBlockReceived.index); if (latestBlockHeld.hash === latestBlockReceived.previousHash) { console.log("We can append the received block to our chain"); blockchain.push(latestBlockReceived); broadcast(responseLatestMsg()); } else if (receivedBlocks.length === 1) { console.log("We have to query the chain from our peer"); broadcast(queryAllMsg()); } else { console.log("Received blockchain is longer than current blockchain"); replaceChain(receivedBlocks); } } else { console.log('received blockchain is not longer than received blockchain. Do nothing'); } }; var replaceChain = (newBlocks) => { if (isValidChain(newBlocks) && newBlocks.length > blockchain.length) { console.log('Received blockchain is valid. Replacing current blockchain with received blockchain'); blockchain = newBlocks; broadcast(responseLatestMsg()); } else { console.log('Received blockchain invalid'); } }; var isValidChain = (blockchainToValidate) => { if (JSON.stringify(blockchainToValidate[0]) !== JSON.stringify(getGenesisBlock())) { return false; } var tempBlocks = [blockchainToValidate[0]]; for (var i = 1; i < blockchainToValidate.length; i++) { if (isValidNewBlock(blockchainToValidate[i], tempBlocks[i - 1])) { tempBlocks.push(blockchainToValidate[i]); } else { return false; } } return true; }; var getLatestBlock = () => blockchain[blockchain.length - 1]; var queryChainLengthMsg = () => ({'type': MessageType.QUERY_LATEST}); var queryAllMsg = () => ({'type': MessageType.QUERY_ALL}); var responseChainMsg = () =>({ 'type': MessageType.RESPONSE_BLOCKCHAIN, 'data': JSON.stringify(blockchain) }); var responseLatestMsg = () => ({ 'type': MessageType.RESPONSE_BLOCKCHAIN, 'data': JSON.stringify([getLatestBlock()]) }); var write = (ws, message) => ws.send(JSON.stringify(message)); var broadcast = (message) => sockets.forEach(socket => write(socket, message)); connectToPeers(initialPeers); initHttpServer(); initP2PServer();
感謝各位的閱讀!關于“js如何實現blockchain區塊鏈”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。