初识区块链——用JS构建你自己的区块链
前言
认识区块链

calculateHash() {
return SHA256(this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();
}
Block的数据结构
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();
}
}
BlockChain的数据结构
class BlockChain {
constructor() {
this.chain = [];
}
}
创世区块
createGenesisBlock() {
return new Block("2018-11-11 00:00:00", "Genesis block of simple chain", "");
}
class BlockChain {
constructor() {
this.chain = [this.createGenesisBlock()];
}
}
添加区块
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;
}
Just run it
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
篡改下试试
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"
}
]
}
再篡改下试试
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"
}
]
}
是真的无法篡改吗
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
原文链接
本文为云栖社区原创内容,未经允许不得转载。
初识区块链——用JS构建你自己的区块链的更多相关文章
- 用Hyperledger Fabric(超级账本)来构建Java语言开发区块链的环境
面向 Java 开发人员的链代码简介 您或许听说过区块链,但可能不确定它对 Java™ 开发人员有何用.本教程将帮助大家解惑.我将分步展示如何使用 Hyperledger Fabric v0.6 来构 ...
- 用 Python 构建一个极小的区块链
虽然有些人认为区块链是一个早晚会出现问题的解决方案,但是毫无疑问,这个创新技术是一个计算机技术上的奇迹.那么,究竟什么是区块链呢? 区块链 以比特币(Bitcoin)或其它加密货币按时间顺序公开地记录 ...
- js构建ui的统一异常处理方案(一)
从早期从事基于java的服务器端开发,再到之后从事基于web和js的ui开发,总体感觉基于web页面的ui开发远不如服务器端健壮.主要是早期ie浏览器功能太弱小,很多业务被迫放到服务器端去实现,浏览器 ...
- js基础篇——原型与原型链的详细理解
js中的对象分为两种:普通对象object和函数对象function. function fn1(){}; var fn2 = function(){}; var fn3 = new Function ...
- 全国11省市出台区块链专项政策,Panda Global发现 "区块链+政务"被寄予厚望!
2020年已经过半,回顾2020年的上半年,不难发现其实区块链的变化非常大,今天Panda Global就给大家回顾下上半年全国关于区块链政策的发布情况.今年上半年,全国已有11个省市出台区块链专项政 ...
- 区块链开发学习第三章:私有链上部署helloBlockchain简单合约
前面讲了部署私有链以及新增账户,现在进行到了部署合约了,此操作真是踩了无数无数无数的坑,到写文章为止确实是已经部署好了,但是还有些坑是还没有解决的! 一.Solidity编译器 开始的时候用的http ...
- NativeScript - JS 构建跨平台的原生 APP
使用 NativeScript,你可以用现有的 JavaScript 和 CSS 技术来编写 iOS.Android 和 Windows Phone 原生移动应用程序.由原生平台的呈现引擎呈现界面而不 ...
- 通过Web Api 和 Angular.js 构建单页面的web 程序
通过Web Api 和 Angular.js 构建单页面的web 程序 在传统的web 应用程序中,浏览器端通过向服务器端发送请求,然后服务器端根据这个请求发送HTML到浏览器,这个响应将会影响整个的 ...
- 第十九篇 js高级知识---词法分析和AO 链
上面一篇文章说了js的作用域链,这一节算是对上面的延申,有一个典型的例子,首先看原来的一段代码: var name = "test"; function t() { var b = ...
随机推荐
- windows server 远程桌面连接问题。
远程桌面连接相当于 linux 服务器root权限连接 mstsc /admin /v:目标IP mstsc /admin /
- tf.contrib.slim.data数据加载(1) reader
reader: 适用于原始数据数据形式的Tensorflow Reader 在库中parallel_reader.py是与reader相关的,它使用多个reader并行处理来提高速度,但文件中定义的类 ...
- 使用curl上传图片的方法
关键:当参数名为"@绝对路径",这时 CURL 會幫你做 multipart/form-data 編碼 实现方法: $params = array( 'file' => '@ ...
- hadoop2-elasticsearch的安装
本文主要讲elasticsearch-2.2.1的安装过程. 准备工作: 1.搭建虚拟机 你需要先参考 hadoop2集群环境搭建 把你的虚拟机搭建起来-hadoop环境可以先不用搭建(完成步骤1到步 ...
- php获取当前时间的毫秒数
floor(microtime()*1000); 用microtime能输出当前的秒的后面8位小数 乘以1000取整数就行了
- cadence钻孔文件及光绘文件的生成
完成PCB布线之后,需要生成钻孔文件和光绘文件交给PCB厂家制作PCB板,下面总结详细方法!
- swust oj 1014
交换排序算法的设计与实现——冒泡排序 1000(ms) 10000(kb) 2640 / 7503 编程实现冒泡排序,按照非递减排序,测试数据为整数. 输入 第一行是待排序数据元素的个数: 第二行是待 ...
- emWin实现ATM机界面设计,含uCOS-III和FreeRTOS两个版本
第1期:ATM机配套例子:V6-900_STemWin提高篇实验_ATM机(uCOS-III)V6-901_STemWin提高篇实验_ATM机(FreeRTOS) 例程下载地址:http://foru ...
- Spring Cloud 微服务架构的五脏六腑,统统晒一晒!
Spring Cloud 是一个基于 Spring Boot 实现的微服务框架,它包含了实现微服务架构所需的各种组件. 注:Spring Boot 简单理解就是简化 Spring 项目的搭建.配置.组 ...
- Go语言单元测试与基准测试
目录 单元测试 概述 go test参数解读 单元测试日志 基准测试 基础测试基本使用 基准测试原理 自定义测试时间 测试内存 控制计时器 Go语言拥有一套单元测试和性能测试系统,仅需要添加很少的代码 ...