Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockChain的src和demo中可能存在两个bug #5

Merged
merged 2 commits into from
Jun 21, 2020
Merged

blockChain的src和demo中可能存在两个bug #5

merged 2 commits into from
Jun 21, 2020

Conversation

fgh1999
Copy link
Contributor

@fgh1999 fgh1999 commented Jun 16, 2020

BLock.validateTransactions这个函数原来是酱紫的,您的注释里面写的是“验证这里的所有transactions”,但是实际上这么写好像只是验证了一个transactions……

  validateTransactions(){
    for(let transaction of this.transactions){
      if(!transaction.isValid()){
        return false
      }
      return true
    }
  }

所以大概这两个函数都要改为酱紫?

  validateTransactions(){
    for(let transaction of this.transactions){
      if(!transaction.isValid()){
        return false
      }
    }
    return true
  }

@fgh1999
Copy link
Contributor Author

fgh1999 commented Jun 16, 2020

经过这样的修改后,测试可能无法通过。原因可能是Chain.mineTransactionPool方法中的minerRewardTransaction在被加入到this.transactionPool之前未被签名(sign);而这在之前的validateTransactions方法中是无法被校验出来的。

  mineTransactionPool(minerRewardAddress) {
    // 发放矿工奖励
    const minerRewardTransaction = new Transaction(
      null,
      minerRewardAddress,
      this.minerReward
    );// 这里这里这里!!!
    this.transactionPool.push(minerRewardTransaction);

    // 挖矿
    const newBlock = new Block(
      this.transactionPool,
      this.getLatestBlock().hash
    );
    newBlock.mine(this.difficulty);

    // 添加区块到区块链
    // 清空 transaction Pool
    this.chain.push(newBlock);
    this.transactionPool = [];
  }

如果要进行签名(sign),必须让chain也有自己的pk和sk,但是我对js不是很熟,忘了怎么加入类的静态变量了。如果您有更改的意愿的话,只需添加Chain的静态变量pk和sk后,再在minerRewardTransaction构造后使用chain的sk对其签名(sign)即可。

@fgh1999 fgh1999 changed the title blockChain的src和demo中可能存在一个bug blockChain的src和demo中可能存在两个bug Jun 16, 2020
@ycraaron ycraaron merged commit 3336fd6 into ycraaron:master Jun 21, 2020
@ycraaron
Copy link
Owner

#6 Merged. 感谢!😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants