如何评估用 JavaScript 实现的区块链?
区块链是一串包含信息的区块。2009年,这项技术被中本聪用于创建比特币这种数字加密货币。它对任何想要开发或分析的人完全开放。这项技术的一个特点是,一旦一些数据被记录到区块链中,就很难更改。
以下是一些用于评估区块链程序的术语。
区块 - 区块链中的区块包含信息,例如数据、哈希值和前一个区块的哈希值。
数据 - 此数据完全取决于区块的类型,例如加密货币包含交易信息,例如交易的双方以及交易的货币数量。
哈希 - 这是一个唯一的字符串 ID,就像 Aadhar 号码可以用来查找一个人的详细信息一样,哈希值用于标识区块的详细信息。一旦创建了一个区块,它的哈希值就会被创建。更改区块哈希值很容易被识别。一旦区块哈希值被更改,它就不再是同一个区块了。
前一个哈希值 - 这是前一个区块的哈希值,用于连接或构成区块链。
在上图中,您可以看到前一个哈希值包含前一个区块的哈希值。第一个区块也称为创世区块,因为它无法指向前一个区块。如果您更改哈希值,则具有前一个哈希值的下个区块将由于更改而无效。
我们将使用的包是crypto.js。这是一个 JavaScript 库,提供加密算法和函数。它可用于执行各种加密操作,例如在 Web 浏览器或 Node.js 等服务器端 JavaScript 环境中进行哈希、加密、解密和密钥生成。
该库广泛用于 Web 应用程序,以提供安全通信、数据保护和用户身份验证。例如,它可用于在通过互联网发送敏感数据之前对其进行加密,或为用户身份验证生成安全的密码哈希。
让我们通过一个使用 Crypto.JS 库进行哈希和工作量证明的程序来理解。
这里有两个类:Block 和 Blockchain。
class Block{
constructor(prev_hashValue, data){
this.data=data;
this.hash=this.calculateHash();
this.prev_hashValue=prev_hashValue;
this.time_stamp= new Date();
this.pf_work=0;
}
}
Block 类有五个属性:
data - 这将存储区块中的数据。
hash - 通过调用 calculateHash 方法来存储区块的哈希值。
prev_hashValue - 这将存储前一个区块的哈希值。
time_stamp - 时间戳将包含创建区块的时间。
pf_work - 在挖掘过程中递增的数字。
Block 类包含两个方法:
calculateHash(){
return SHA256(this.pf_work + this.prev_hashValue + this.timestamp + JSON.stringify(this.data)).toString();
}
此函数将通过将 pf_work、prev_hashValue、time_stamp 和 data 连接起来,并通过使用 CryptoJS 库的SHA256哈希函数传递来计算区块的哈希值。
mine(difficulty){
while(!this.hash.startsWith("0".repeat(difficulty))){
this.pf_work++;
this.hash=this.calculateHash();
}
}
此函数使用工作量证明来查找以特定数量的零开头的哈希值。零的数量由传递给该方法的 difficulty 参数确定。pf_work 属性会递增,直到找到有效的哈希值。
class Blockchain{
constructor(){
let genesisBlock=new Block("0", {isGenesisBlock: true});
this.chain=[genesisBlock];
}
}
chain - 这是一个 Block 对象数组,构成区块链。
Blockchain 类有两个方法:
addNewBlock(data){
let lastBlock=this.chain[this.chain.length-1];
let newBlock=new Block(lastBlock.hash, data);
newBlock.mine(2); //find a hash for new block
this.chain.push(newBlock);
}
此方法创建一个新的 Block 对象,其中数据作为参数传递,使用 mines 查找有效的哈希值,并将其添加到 chain 数组。
isValid_hash(){
for(let i=1; i<this.chain.length; i++){
const currentBlock=this.chain[i];
const previousBlock=this.chain[i-1];
if(currentBlock.hash!=currentBlock.calculateHash()) return false;
if(currentBlock.prev_hashValue!=previousBlock.hash) return false;
}
return true;
}
此方法通过迭代 chain 数组中的每个区块并验证其 hash 属性是否与计算出的哈希值匹配来检查区块链的有效性。
let blockchain=new Blockchain();
blockchain.addNewBlock({
from: "joe",
to:"Juhi",
amount: 100,
});
blockchain.addNewBlock({
from: "martin",
to: "Genny",
amount: 150,
});
这里将创建一个包含两个区块的对象,这些区块将具有区块链类的属性。
此实现可以用作构建更复杂的区块链应用程序的起点,这些应用程序需要安全且不可变的数据存储。但是,需要注意的是,这只是一个基本的实现,完全功能的区块链系统还需要许多其他功能,例如交易验证、共识机制和安全措施。
示例:完整代码
Blockchain.js
const SHA256 = require('crypto-js/sha256');
class Block{
constructor(prev_hashValue, data){
this.data=data;
this.hash=this.calculateHash();
this.prev_hashValue=prev_hashValue;
this.time_stamp= new Date();
this.pf_work=0;
}
calculateHash(){
return SHA256(this.pf_work + this.prev_hashValue + this.time_stamp + JSON.stringify(this.data)).toString();
}
mine(difficulty){
while(!this.hash.startsWith("0".repeat(difficulty))){
this.pf_work++;
this.hash=this.calculateHash();
}
}
}
class Blockchain{
constructor(){
let genesisBlock=new Block("0", {isGenesisBlock: true});
this.chain=[genesisBlock];
}
addNewBlock(data){
let lastBlock=this.chain[this.chain.length-1];
let newBlock=new Block(lastBlock.hash, data);
newBlock.mine(2); //find a hash for new block
this.chain.push(newBlock);
}
isValid_hash(){
for(let i=1; i<this.chain.length; i++){
const currentBlock=this.chain[i];
const previousBlock=this.chain[i-1];
if(currentBlock.hash!=currentBlock.calculateHash()) return false;
if(currentBlock.prev_hashValue!=previousBlock.hash) return false;
}
return true;
}
}
//test
let blockchain=new Blockchain();
blockchain.addNewBlock({
from: "joe",
to:"Juhi",
amount: 100,
});
blockchain.addNewBlock({
from: "martin",
to: "Genny",
amount: 150,
});
console.log(blockchain);
console.log("Blockchain is valid: "+blockchain.isValid_hash());
要编译程序,您必须安装node.js。使用这篇文章 (Node.js - 环境设置 )安装 Node.js。然后使用以下命令安装 crypto.js 库。
npm install crypto-js
然后编译 JavaScript 程序文件。这里,文件名是 blockchain。
node blockchain.js
输出

数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP