JavaScript 如何评估实现的区块链

JavaScript 如何评估实现的区块链

区块链 是包含信息的一系列区块。在2009年,这项技术后来被 Satoshi Nakamoto 改编以创建数字加密货币比特币。任何想要开发或分析的人都可以完全开放地使用这项技术。这项技术具有一项特性,即一旦某些数据被记录在区块链的区块中,很难进行更改。

以下是在区块链程序评估中使用的一些术语。

  • 区块 - 区块链中的区块包含像数据、哈希值和前一个区块哈希值等信息。

  • 数据 - 这个数据完全取决于区块的类型,比如加密货币包含的信息有从哪个人转账、转账给哪个人以及转账的金额。

  • 哈希 - 这是一个唯一的字符串ID,就像身份证号码用于定位一个人的详细信息一样,这个哈希用于标识区块的详细信息。一旦创建了一个区块,它的哈希值就会被创建。改变区块的哈希可以很容易地被识别出来。一旦一个区块的哈希值被改变,它将不再是同一个区块。

  • 前一个哈希 - 这是用于连接或组成区块链的前一个区块的哈希值。

在上面的图片中,您可以观察到前一个哈希具有前一个区块的哈希值。第一个区块也被称为创世区块,因为它不能指向前一个区块。如果您更改哈希值,那么具有前一个哈希值的下一个区块将由于更改而无效。

我们将使用的包是 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();
   }
}

这个函数使用工作量证明来找到以一定数量的零开头的哈希值。零的数量由传递给该方法的难度参数确定。直到找到一个有效的哈希值,pf_work属性将被递增。

class Blockchain{
   constructor(){
      let genesisBlock=new Block("0", {isGenesisBlock: true});
      this.chain=[genesisBlock];
   }
}

chain − 这是一个由块对象组成的数组,用于构建区块链。

区块链类有两个方法−

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;
}

这个方法通过迭代链数组中的每个块并验证其哈希属性是否与计算的哈希匹配,来检查区块链的有效性。

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

输出

JavaScript 如何评估实现的区块链

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程