Bug in a simple BlockChain program [closed]

I started with a working Python program https://www.youtube.com/watch?v=b81Ib_oYbFk https://github.com/howCodeORG/Simple-Python-Blockchain and attempted to convert it to JavaScript. My code, less then 100 lines, is here: https://github.com/kurtfriedrichhotmail/blockchain/tree/main

Program "seems" to work correctly, as it correctly builds a linked list of 10 blocks, however in the Blockchain class is a function (mine) that "mines" the chain to come up with the new hash for the new block. It loops a number of times, using a counter called the nonce. The program's output display this count, which is how many times thru the mining code it took to come up with an acceptable new hash. This counter would just about never be the same for any 2 successive blocks, yet my code is often getting the same value for for 2 or 3 successive new blocks, and that has to be a bug. If I adjust the "diff" (difficulty) to 10, I get many repeat loop counts. If I make it 20, I don't get repeats. Here is an example, note how blocks 7 and 8 have same number of hash attempts counts. Some runs will have have 4 or more with same value.

Block Hash: 86551dd2379a1f301de85ea414ce3192f1ffa4cd0e371aa971476f1460f7d986
BlockNo: 0
Block Data: 12345
Name: Genesis
Hashes: 0
--------------
Block Hash: bc41df5ed5d814ea26a9221c563706498089c69ed629f011247cc1f9ee6f49e1
BlockNo: 1
Block Data: 12345
Name: Block 1
Hashes: 4727
--------------
Block Hash: 99a80b397f735b1111782f0b17f937c11b684347171f4fdb051006eac6363ac0
BlockNo: 2
Block Data: 12345
Name: Block 2
Hashes: 41508
--------------
Block Hash: da223650287dfccdd37ebf5a07029384a0d4e3fddcd2e5bd6278856a75956a43
BlockNo: 3
Block Data: 12345
Name: Block 3
Hashes: 24577
--------------
Block Hash: 46e1366567def92708a63028f7c71362a2b234b9b807f888f2ae6b7a6cfeb432
BlockNo: 4
Block Data: 12345
Name: Block 4
Hashes: 43848
--------------
Block Hash: 2ea297e26a29f0ebe7ee9989eb0d1df355288b46321b6d7dd1d80f1101208daa
BlockNo: 5
Block Data: 12345
Name: Block 5
Hashes: 9447
--------------
Block Hash: e6b13fb1e0907748a3df941348cf4803cd1cd4ff6399c9a43d2aa5f58cda2f5e
BlockNo: 6
Block Data: 12345
Name: Block 6
Hashes: 31961
--------------
Block Hash: 1e20d80ba44c13ef883d081452b55b5bb7623b3a946cd6dea71b71031879730a
BlockNo: 7
Block Data: 12345
Name: Block 7
Hashes: 7775
--------------
Block Hash: fbf7ac0bc090c566809a1140a475a26d47593c4188e8565aeb4777a1cc07e297
BlockNo: 8
Block Data: 12345
Name: Block 8
Hashes: 7775
--------------
Block Hash: e9f4243b06f0684afd15106d0b40edb02fc1f8f818cc80371dd03029219e1753
BlockNo: 9
Block Data: 12345
Name: Block 9
Hashes: 196730
--------------
Block Hash: 3774e57c56e50ee3f7f2750f214f6da7205af45998a6ddeb11c6a2c64f17754b
BlockNo: 10
Block Data: 12345
Name: Block 10
Hashes: 3884
--------------

Here is the mine loop in question:

this.mine = function(newBlock){
    let n = 0
    newBlock.nonce = 1;
    while (n < this.maxNonce){
        let hashBase10 = new BigNumber(newBlock.hash(), 16);
        if (hashBase10.toExponential(10) <= this.target){
            this.add(newBlock);
            break;
        }
        else{
            newBlock.nonce += 1;
            n++;
        }
  }
}


Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)