如何评估用 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 和数据,并使用 CryptoJS 库将其传递到 SHA256 哈希函数来计算块的哈希值。
mine(difficulty){
while(!this.hash.startsWith("0".repeat(difficulty))){
this.pf_work++;
this.hash=this.calculateHash();
}
}
此函数使用工作量证明来查找以一定数量的零开头的哈希值。零的数量由传递给该方法的难度参数确定。 pf_work 属性会递增,直到找到有效的哈希值。
class Blockchain{
constructor(){
let genesisBlock=new Block("0", {isGenesisBlock: true});
this.chain=[genesisBlock];
}
}
chain - 这是一个 Block 对象数组,它构成了一个块的链。
区块链类有两个方法 -
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用于查找有效的哈希值,并将其添加到链数组中。
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;
}
此方法通过迭代链数组中的每个块并验证其哈希属性与计算的哈希值匹配来检查区块链的有效性。
let blockchain=new Blockchain();
blockchain.addNewBlock({
from: "joe",
to:"Juhi",
amount: 100,
});
blockchain.addNewBlock({
from: "martin",
to: "Genny",
amount: 150,
});
这里将使用两个块创建一个对象,这两个块将具有区块链类的属性。
此实现可以用作构建需要安全且不可变数据存储的更复杂区块链应用程序的起点。但需要注意的是,这只是一个基本的实现,一个功能齐全的区块链系统还需要许多附加功能,例如交易验证、共识机制和安全措施。
示例:完整代码
区块链.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程序文件。这里,文件名是区块链。
node blockchain.js
输出

javascript