参照
単純なブロックチェーンを構築するexp1-POWexp2-の単純な実装
鉱業
難易度の値があり、難易度の要件を満たすハッシュ文字列を計算します。たとえば、難易度の値nonce = 1の場合、0で始まるハッシュ文字列を見つける必要があります。
class Block {
constructor(data,previousHash){
this.data = data
this.previousHash = previousHash
this.hash = this.computHash()
this.nonce = 1
}
computHash(){
return sha256(this.data + this.previousHash + this.nonce).toString()
}
// 根据难度值确定哈希串前‘0’个数
getAnswer(difficulty){
// 开头前n位为0的hash
let answer = '';
for (let i = 0;i<difficulty;i++){
answer+='0';
}
return answer
}
// 计算符合区块链难度要求的hash,也就是挖矿
// 通过添加随机数nonce,改变哈希值
// 什么是 符合区块链难度要求的hash(通过getAnswer()可以得到)
mine(difficulty){
while(true){
this.hash = this.computHash()
if(this.hash.substring(0,difficulty) !== this.getAnswer(difficulty)){
this.nonce++
this.hash = this.computHash()
}else{
break
}
}
console.log('挖矿结束',this.hash)
}
}
class Chain {
constructor(){
this.chain = [this.bigBang()];
this.difficulty = 5;
}
// ………………
//添加区块到区块链上
addBlockToChain(newBlock){
// data
// 找到最近一个block的hash
// 这个hash就是新区块的previousHash
newBlock.previousHash = this.getLatestBlock().hash;
// 不能简单的计算哈希值就可以加入链,而是需要挖矿,即计算符合难度difficulty要求的哈希值
// newBlock.hash = newBlock.computHash();
newBlock.mine(this.difficulty);
// 这个hash需要满足一个区块链设置的条件,方可加入链中
this.chain.push(newBlock);
}
}
//生成一个新的区块
const block1 = new Block("转账十元","")
chainExp.addBlockToChain(block1)
const block2 = new Block("转账十个十元","")
chainExp.addBlockToChain(block2)
改ざん
// 篡改区块链数据以及数据的hash
chainExp.chain[1].data = "转账100个十元"
chainExp.chain[1].mine(4)
console.log(chainExp)
console.log(chainExp.validateChain())