赞
踩
区块链是一种分布式数据库,它存储在节点上,每个节点都拥有一份完整的数据库副本。当一个节点更新数据库时,其他节点会收到通知并更新自己的数据库。
我们可以使用 CryptoJS 库来计算哈希值。
先简单的理解一下,区块链的每一个区块都包含三个字段:数据、前一个区块的哈希值和当前区块的哈希值。
代码实现如下:
// 如果是通过CDN或者全局引入CryptoJS,可以直接使用 const CryptoJS = require("crypto-js"); // 定义一个区块类 class Block { public data: string; public previousHash: string; public hash: string; constructor(data, previousHash = "") { this.data = data; this.previousHash = previousHash; this.hash = this.calculateHash(); } calculateHash() { return CryptoJS.SHA256(this.previousHash + this.data).toString(); } } // 创建一个区块链 const block1 = new Block("Hello World", "0"); console.log(block1);
我们可以看到,block1 为:
接下来,我们要理解一下链的存在.
你可以把它想成是一个列表,列表中的每一个元素都包含一个指向前一个元素的哈希指针,以及通过哈希函数计算自己的内容和前一个元素的哈希值计算出的哈希值。
这个区块链一开始就有一个初始的区块(创世区块),这个区块的哈希值为 0。
可以使用以下代码来实现:
// 定义一个区块链类 class Blockchain { public chain: Block[]; constructor() { this.chain = [this.createGenesisBlock()]; } //获取最后一个区块 getLastBlock(){ return this.chain[this.chain.length - 1]; }; // 区块的追加 pushLatestBlock(newBlock: Block){ newBlock.previousHash = this.getLastBlock().hash; newBlock.hash = newBlock.calculateHash(); this.chain.push(newBlock); } }
可以看到,我们创建了一个区块链类,其中包含一个链,这个链是一个数组,数组中的每一个元素都是区块。
我们可以创建一个区块链实例,然后向区块链中添加区块。
// 创建一个区块
const block1 = new Block("Hello World", "0");
// console.log(block1);
// 创建一个区块链,并将block1加入到区块链中
const blockchain = new Blockchain();
blockchain.pushLatestBlock(block1);
console.log(blockchain);
结果如下:
在区块链中,如果一个节点篡改了数据,那么其他节点就会收到通知,并更新自己的数据库。我们不希望这样的情况出现,因为这会导致整个区块链的 integrity 被破坏。
所以我们需要验证区块。
首先,我们先验证当前的区块的信息是否已经被篡改过。
// 如果是通过CDN或者全局引入CryptoJS,可以直接使用 const CryptoJS = require("crypto-js"); // 定义一个区块类 class Block{ public data: string; public previousHash: string; public hash: string; constructor( data: string, previousHash: string ){ this.data = data; this.previousHash = previousHash; this.hash = this.calculateHash(); } calculateHash(){ return CryptoJS.SHA256(this.previousHash + this.data).toString(); } } // 定义一个区块链类 class Blockchain{ public chain: Block[]; constructor(){ this.chain = [this.createGenesisBlock()]; } // 创世区块的创建 createGenesisBlock(){ return new Block('Genesis Block',''); } //获取最后一个区块 getLastBlock(){ return this.chain[this.chain.length - 1]; }; // 区块的追加 pushLatestBlock(newBlock: Block){ newBlock.previousHash = this.getLastBlock().hash; 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值 if(currentBlock.hash !== currentBlock.calculateHash()){ console.log('当前区块的哈希值与计算出的哈希值不一致'); return false; } return true; } } } // 创建一个区块 const block1 = new Block('Hello World',"1"); const blockchain1 = new Blockchain(); blockchain1.pushLatestBlock(block1); console.log(blockchain1.isChainValid()); // 修改已经上链的值 blockchain1.chain[1].data = 'Hello java'; console.log(blockchain1.isChainValid());
结果如下
// 如果是通过CDN或者全局引入CryptoJS,可以直接使用 const CryptoJS = require("crypto-js"); // 定义一个区块类 class Block{ public data: string; public previousHash: string; public hash: string; constructor( data: string, previousHash: string ){ this.data = data; this.previousHash = previousHash; this.hash = this.calculateHash(); } calculateHash(){ return CryptoJS.SHA256(this.previousHash + this.data).toString(); } } // 定义一个区块链类 class Blockchain{ public chain: Block[]; constructor(){ this.chain = [this.createGenesisBlock()]; } // 创世区块的创建 createGenesisBlock(){ return new Block('Genesis Block',''); } //获取最后一个区块 getLastBlock(){ return this.chain[this.chain.length - 1]; }; // 区块的追加 pushLatestBlock(newBlock: Block){ newBlock.previousHash = this.getLastBlock().hash; 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值 if(currentBlock.hash !== currentBlock.calculateHash()){ console.log('当前区块的哈希值与计算出的哈希值不一致'); return false; } // 校验区块链接是否正常 if(currentBlock.previousHash !== previousBlock.hash){ console.log('当前区块的previousHash与前一个区块的hash不一致'); return false; } return true; } } } // 创建一个区块 const block1 = new Block('Hello World',"1"); const blockchain1 = new Blockchain(); blockchain1.pushLatestBlock(block1); console.log(blockchain1.isChainValid()); // 修改已经上链的值 blockchain1.chain[1].data = 'Hello java'; console.log(blockchain1.isChainValid()); const blockchain2 = new Blockchain(); blockchain1.pushLatestBlock(block1); const block2 = new Block('Hello ts',"2"); blockchain2.pushLatestBlock(block2); // 修改已经上链的区块的值和哈希值 blockchain1.chain[1].data = 'Hello java'; blockchain1.chain[1].hash = blockchain1.chain[1].calculateHash(); console.log(blockchain1.isChainValid());
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。