lhartikk / naivechain

A blockchain implementation in 200 lines of code
Apache License 2.0
5.28k stars 1.15k forks source link

blockchain invalid error when syncing a longer blockchain #29

Open koshikraj opened 6 years ago

koshikraj commented 6 years ago

Created 2 nodes, node1 has 41 blocks created using mineBlock API node2 has only genesis block when node 2 is connected to peer node1, it gives following error and fails to replace the blockchain

blockchain possibly behind. We got: 0 Peer got: 41 Received blockchain is longer than current blockchain Received blockchain invalid

The error is likely to be because of the first condition in isValidChain when called from replacechain.


var isValidChain = (blockchainToValidate) => {
    if (JSON.stringify(blockchainToValidate[0]) !== JSON.stringify(getGenesisBlock())) {
        return false;

Performing a JSON.stringify on an object will not always give the same value as objects are unordered.

koshikraj commented 6 years ago

Can someone verify this issue?

gentleKun commented 6 years ago

I have no such issue.

koshikraj commented 6 years ago

@gentleKun , This issue is due to stringifying the unordered dictionary. May not be reproducible every time. It is always safer to compare the hash value instead which proves the integrity.

gentleKun commented 6 years ago

I know the question where it is , and i have modified it in my file main.js file.