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

时间:2018-08-30 15:53:41

标签: javascript oop frontend blockchain cryptojs

我创建了一个区块链实现,效果很好。然后我想编写一个在删除消息时创建新链的程序。

并没有获得新的链,而是在第二个块包含来自前一个链的最新块的数据,而是丢弃了一个错误,我真的不明白这意味着什么。为什么是“ 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>

1 个答案:

答案 0 :(得分:1)

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

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