欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

200行代码实现blockchain 区块链实例详解

程序员文章站 2023-12-02 16:20:34
了解blockchain的概念很简单(区块链,交易链块):它是分布式的(即不是放置在同一台机器上,不同的网络设备上的)数据库支持主办记录日益增长的名单。但是,这也是容易混淆...

了解blockchain的概念很简单(区块链,交易链块):它是分布式的(即不是放置在同一台机器上,不同的网络设备上的)数据库支持主办记录日益增长的名单。但是,这也是容易混淆blockchain与我们试图帮他解决了目标 - 在人们心中的那一刻,这个词是相当强烈的交易,合同或智能cryptocurrency的概念有关。

只有在这里blockchain - 是不是一回事比特币,并理解链块的基本知识比它似乎更容易,尤其是在,它是基于源代码的情况下。在本文中,我们提出了建立与在javascript中200行代码的简单模型。这个项目,我们称之为naivechain的源代码,可以在github上找到。第1部分和第2部分:如果您需要刷上它的功能,使用我们的备忘单,我们将使用标准的ecmascript 6。
块结构

第一步 - 确定应包含块的元素。为简单起见,我们只包括最必要的:先前块的指数(指数),时间标记(时间戳),数据(数据),散列和散列,要录制,以保持电路的结构完整性。

200行代码实现blockchain 区块链实例详解

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服务器解决。当与节点相互作用有以下功能:
打印所有单元的列表;
创建用户生成内容的新单元;
打印列表,或添加的节日。
互动的最直接的方式 - 通过卷曲:

200行代码实现blockchain 区块链实例详解

一个节点上的所有块#名单

curl

架构

值得注意的是,该网站是指两个web服务器:http进行用户控制的装置和向所述的websocket http来安装节点之间的p2p连接。

如下为js 200行代码

<span style="font-family:arial, helvetica, sans-serif;">'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(); 

总结

以上所述是小编给大家介绍的200行代码实现blockchain 区块链实例详解,希望对大家有所帮助