From e5ca4420ac4876e2ebdc35d61d9e3aeb861957de Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sat, 29 May 2021 22:42:46 -0500 Subject: [PATCH] Fix a bug in State reversion. Closes https://github.com/MerosCrypto/Meros/issues/315. Also has the Blockchain Test generate packets once again, as well as changes the Element setup to something fuzzier. --- src/Database/Merit/State.nim | 2 +- tests/Database/Merit/BlockchainTest.nim | 49 ++++++++++++------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/Database/Merit/State.nim b/src/Database/Merit/State.nim index 11770e863..c73785063 100644 --- a/src/Database/Merit/State.nim +++ b/src/Database/Merit/State.nim @@ -281,7 +281,7 @@ proc revert*( #Don't add Merit back if the miner has a MeritRemoval. var removed: bool = false try: - removed = state.db.loadRemovalHeight(nick) < height + removed = state.db.loadRemovalHeight(nick) <= height except DBReadError: discard if not removed: diff --git a/tests/Database/Merit/BlockchainTest.nim b/tests/Database/Merit/BlockchainTest.nim index 58b4f4008..fe093c79a 100644 --- a/tests/Database/Merit/BlockchainTest.nim +++ b/tests/Database/Merit/BlockchainTest.nim @@ -63,36 +63,35 @@ suite "Blockchain": if state.holders.len != removed.card: #Randomize the Packets. packets = @[] - for _ in 0 ..< rand(0):#300): + for _ in 0 ..< rand(300): packets.add(newValidVerificationPacket(state.holders, removed)) #Randomize the Elements/removals. elements = @[] toRemove = {} - if rand(1) == 0: - for _ in 0 ..< 3: - var - holder: uint16 = getNonMaliciousHolder() - elementNonce: int - try: - elementNonce = elementNonces[holder] - except KeyError: - elementNonce = 0 - elementNonces[holder] = elementNonce + 1 - - case rand(2): - of 0: - var sd: SignedSendDifficulty = newSignedSendDifficultyObj(elementNonce, uint16(rand(high(int16)))) - miners[holder].sign(sd) - elements.add(sd) - of 1: - var dd: SignedDataDifficulty = newSignedDataDifficultyObj(elementNonce, uint16(rand(high(int16)))) - miners[holder].sign(dd) - elements.add(dd) - of 2: - toRemove.incl(holder) - else: - panic("Generated a number outside of the provided range.") + for _ in 0 ..< rand(3): + var + holder: uint16 = getNonMaliciousHolder() + elementNonce: int + try: + elementNonce = elementNonces[holder] + except KeyError: + elementNonce = 0 + elementNonces[holder] = elementNonce + 1 + + case rand(2): + of 0: + var sd: SignedSendDifficulty = newSignedSendDifficultyObj(elementNonce, uint16(rand(high(int16)))) + miners[holder].sign(sd) + elements.add(sd) + of 1: + var dd: SignedDataDifficulty = newSignedDataDifficultyObj(elementNonce, uint16(rand(high(int16)))) + miners[holder].sign(dd) + elements.add(dd) + of 2: + toRemove.incl(holder) + else: + panic("Generated a number outside of the provided range.") #Decide if this is a nickname or new miner Block. if (miners.len == removed.card) or (rand(2) == 0):