blockchain 區塊鏈200行代碼:在JavaScript實現的一個簡單的例子__PHP

來源:互聯網
上載者:User
瞭解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行代碼

 
'use strict';
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();


 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.