gpt4 book ai didi

javascript - 推送新 block 时无法获取 block 的先前哈希

转载 作者:行者123 更新时间:2023-11-30 20:14:02 25 4
gpt4 key购买 nike

我创建了一个区 block 链的实现,它运行良好。然后我想写一个程序,在删除消息时创建一个新链。

而不是得到一个新的链,其中第二个 block 有来自前一个链的最新 block 的数据,它会抛出一个错误,我真的不明白那是什么意思。为什么是“1”?它甚至不是散列,而是新链中 block 的索引。这是错误:

Uncaught TypeError: Cannot create property 'prevHash' on number '1'
at Chain.addBlock (chain.js:24)
at newChain (test.js:26)
at HTMLButtonElement.onclick (index.html:1)

谁能解释一下为什么?我还附上了一个代码片段,用于展示它是否有效

// Chain.js
class Block {
constructor(id, data, prevHash = ''){
this.id = id;
this.prevHash = this.prevHash;
this.hash = this.calcHash();
this.data = data;
}
calcHash() {
return CryptoJS.SHA512(this.id + JSON.stringify(this.data)).toString();
}

}
class Chain {
constructor(){
this.chain = [this.genesisBlock()];
}
genesisBlock(){
return new Block(0,'Chain started.');
}
getLastBlock(){
return this.chain[this.chain.length - 1];
}
addBlock(block){
block.prevHash = this.getLastBlock().hash;
block.hash = block.calcHash();
this.chain.push(block)
}
isValid(){
for(let i = 1; i < this.chain.length; i++){
let prev = this.chain[i-1], current = this.chain[i];
if(current.hash !== prev.prevHash || current.hash !== current.calcHash())
return false;
}return true;
}

}

// Msg.js
class Msg {
constructor(msg, date){
this.msg = msg;
const D = new Date();
this.date = [D.getHours(), D.getMinutes(), D.getSeconds()].join(' : ');
}
}

// Test.js
FROZENCHAINS = [];
CHAIN = new Chain();
i = 0;

msg = () => {
let text = $('input').val();
i++;
CHAIN.addBlock(new Block(i, text));

let msg = JSON.stringify(CHAIN.chain,null, 4);

$('#log').text(msg);
let thisMSG = new Msg(text);

$('section').append('<div class="notification is-primary"><span class="tag">' + thisMSG.msg + '</span>'
+ '<span class="tag">Created at: ' + thisMSG.date + '</span><button onclick="$(this).parent().hide() && newChain()" align=center class="delete is-large"></button></div>')


}

newChain = () => {
FROZENCHAINS.push(CHAIN);
delete CHAIN;
CHAIN = new Chain();
CHAIN.addBlock(1,'Hi')
}
.input {
margin: 10px 0;
}
.tag {
font-size: 23px !important;
background-color: whitesmoke !important;
margin: 5px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" charset="utf-8"></script>
<title>Blockchain Chat</title>
</head>
<body>
<div class="tile is-parent">
<article class="tile is-child notification">
<p class="title">Blockchain Chat Part 1</p>
<div class="content">
<pre class="hero-body" id=log></pre>
<section class="hero-body"></section>
<input class="input" value="Hello World"/>
<button onclick="msg()" class="button">Send Message</button>
</div>
</body>
</html>

最佳答案

您的 addBlock 函数需要一个 block ,但您在 newChain() 中提供了 1。将行更改为

CHAIN.addBlock(new Block(1,'Hi'));

关于javascript - 推送新 block 时无法获取 block 的先前哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52101106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com