From 34c00069f5f421e6442a4b4f4c926dae448325f2 Mon Sep 17 00:00:00 2001 From: 0Gitnick Date: Tue, 13 Aug 2019 16:54:03 -0500 Subject: [PATCH] Fix difficulty calculator for new blocks --- onionr/onionrcrypto/cryptoutils/verifypow.py | 2 +- onionr/onionrproofs.py | 20 +++++++------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/onionr/onionrcrypto/cryptoutils/verifypow.py b/onionr/onionrcrypto/cryptoutils/verifypow.py index cce69713..197281c2 100644 --- a/onionr/onionrcrypto/cryptoutils/verifypow.py +++ b/onionr/onionrcrypto/cryptoutils/verifypow.py @@ -19,7 +19,7 @@ def verify_POW(blockContent): except AttributeError: pass - difficulty = onionrproofs.getDifficultyForNewBlock(blockContent, ourBlock=False) + difficulty = onionrproofs.getDifficultyForNewBlock(blockContent) if difficulty < int(config.get('general.minimum_block_pow')): difficulty = int(config.get('general.minimum_block_pow')) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index 216c0879..db1d3ebc 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import multiprocessing, nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, sys, json +import multiprocessing, nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, sys, json, sys import config, logger, onionrblockapi, storagecounter from onionrutils import bytesconverter from onionrcrypto import hashers @@ -32,25 +32,19 @@ def getDifficultyModifier(): return difficultyIncrease -def getDifficultyForNewBlock(data, ourBlock=True): +def getDifficultyForNewBlock(data): ''' Get difficulty for block. Accepts size in integer, Block instance, or str/bytes full block contents ''' - retData = 0 - dataSize = 0 if isinstance(data, onionrblockapi.Block): - dataSize = len(data.getRaw().encode('utf-8')) + dataSizeInBytes = len(bytesconverter.str_to_bytes(data.getRaw())) else: - dataSize = len(bytesconverter.str_to_bytes(data)) + dataSizeInBytes = len(bytesconverter.str_to_bytes(data)) - if ourBlock: - minDifficulty = config.get('general.minimum_send_pow', 4) - else: - minDifficulty = config.get('general.minimum_block_pow', 4) + minDifficulty = config.get('general.minimum_send_pow', 4) + totalDifficulty = max(minDifficulty, math.floor(dataSizeInBytes / 1000000.0)) + getDifficultyModifier() - retData = max(minDifficulty, math.floor(dataSize / 100000)) + getDifficultyModifier() - - return retData + return totalDifficulty def getHashDifficulty(hexHash): '''