httphttps区块链
Ⅰ 如何用JavaScript实现区块链
<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!!", "");};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();
Ⅱ 目前比较火热的区块链交易所有哪些
目前中心化的交易所比较火热的有币安、GATE、火币,GATE出入金比较方便。但去中心化交易是未来的一个趋势,现在在去中心化交易这一块国内做得比较好的是路印协议。
Ⅲ http://gg.mzh1.com这个网站:叫谜知网 是区块链技术的吗
是一个区块链技术的综合门户网站
Ⅳ 国内最大的区块链交易平台是哪里
国盾区块链交易中心是世界最权威的工信部颁发通用积 分应用试点单位工信部颁发互联 网金融试点单位
Ⅳ 比特币和区块链是一样的吗有没有什么网站可以学习区块链知识,也可以了解比特币的。
比特币是区块链技术的一个应用,比特币属于数字货币,而数字货币仅仅是区块链技术在金融领域的一个应用,归属于区块链1.0领域。现在是处于区块链2.0时代属于智能合约的时代,据说区块链3.0是底层公链开发。平时关注这些区块链的知识是从币众里面看到的,有很多区块链的内容知识,学起来挺方便的,可以关注一下。
Ⅵ 小白适合什么区块链交易平台
币易交易平台还不错,我就在这上面玩.支持法币交易,提现速度快,币种优质,现在还有分红送币活动.
Ⅶ 区块链如何在现实中起作用
区块链不是万能的,目前落地的基本没有多少,你非想要和超市挂钩,我只能想到现在支付用的是支付宝或者微信,硬要用区块链手段支付的话唯一可能的好处是不需要第三方机构做担保(阿里巴巴和腾讯)节省了成本,并且阿里巴巴和腾讯有跑路风险(虽然目前看几乎不可能),用区块链不会担心程序叛变
Ⅷ 区块链如何开发
分享区视网:
区块链技术的应用范围还是很广的,基于去中心化,去信任,集体维护,可靠数据库等特点,其在金融行业的应用是先行一步的。
区块链是比特币的底层技术,区块链在数字货币的应用开发已经是成熟的了。
像英唐众创提供的基于区块链的交易系统开发方案,开发出来的软件系统有很大的安全系数和透明度。