在推送新块时无法获取块的先前哈希值

问题描述 投票:1回答:1

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

而不是获得一个新的链,其中第二个块具有来自前一个链的最新块的数据,它丢弃了一个错误,我真的不明白这意味着什么。为什么“1”?它甚至不是哈希,而是新链中块的索引。这是错误:

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>
javascript oop frontend blockchain cryptojs
1个回答
1
投票

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

CHAIN.addBlock(new Block(1,'Hi'));
© www.soinside.com 2019 - 2024. All rights reserved.