From 757cd18af8928734569db18f92fedb47827dc5d0 Mon Sep 17 00:00:00 2001 From: zoelee <31149915+zoeleesss@users.noreply.github.com> Date: Sat, 17 Mar 2018 15:23:46 +0800 Subject: [PATCH] fix issue during mining The bug was caused when `timeTaken > timeExpected * 2` at first round. Originally, It would be -1, which is absolutely wrong. So in order to avoid this bug, test if `prevAdjustmentBlock.difficulty > 0`. if yes, then return prevAdjustmentBlock.difficulty - 1; if not , then return prevAdjustmentBlock.difficulty; --- src/blockchain.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/blockchain.ts b/src/blockchain.ts index 85aedc3..c81e1d2 100644 --- a/src/blockchain.ts +++ b/src/blockchain.ts @@ -82,7 +82,11 @@ const getAdjustedDifficulty = (latestBlock: Block, aBlockchain: Block[]) => { if (timeTaken < timeExpected / 2) { return prevAdjustmentBlock.difficulty + 1; } else if (timeTaken > timeExpected * 2) { - return prevAdjustmentBlock.difficulty - 1; + if (prevAdjustmentBlock.difficulty > 0) { + return prevAdjustmentBlock.difficulty - 1; + } else { + return 0; + } } else { return prevAdjustmentBlock.difficulty; }