From ae845940d98a589acb237ece6c93bf9c6fd1a1af Mon Sep 17 00:00:00 2001 From: 0Gitnick <36289298+0Gitnick@users.noreply.github.com> Date: Tue, 13 Aug 2019 08:13:52 -0500 Subject: [PATCH 1/9] Increase random bits mainly for preventing duplicate nonces for the same block --- onionr/onionrproofs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index fb6cafda..8402f727 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -221,7 +221,7 @@ class POW: iFound = False # if current thread is the one that found the answer answer = '' hbCount = 0 - nonce = int(binascii.hexlify(nacl.utils.random(2)), 16) + nonce = int(binascii.hexlify(nacl.utils.random(64)), 16) startNonce = nonce while self.hashing: #token = nacl.hash.blake2b(rand + self.data).decode() From 8a36144675f99eef0e91b0b396de81e2dc1c5f9e Mon Sep 17 00:00:00 2001 From: 0Gitnick Date: Tue, 13 Aug 2019 14:51:46 -0500 Subject: [PATCH 2/9] Fix difficultyModifier --- onionr/onionrproofs.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index 8402f727..8ccc376d 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -22,23 +22,15 @@ import config, logger, onionrblockapi, storagecounter from onionrutils import bytesconverter from onionrcrypto import hashers config.reload() + def getDifficultyModifier(): '''returns the difficulty modifier for block storage based on a variety of factors, currently only disk use. ''' - retData = 0 - useFunc = storagecounter.StorageCounter().getPercent + percentUse = storagecounter.StorageCounter().getPercent() + difficultyIncrease = math.floor(4 * percentUse) - percentUse = useFunc() - - if percentUse >= 0.50: - retData += 1 - elif percentUse >= 0.75: - retData += 2 - elif percentUse >= 0.95: - retData += 3 - - return retData + return difficultyIncrease def getDifficultyForNewBlock(data, ourBlock=True): ''' From 60c9a395f68b65a7f07564b8dcb0e50488930c8a Mon Sep 17 00:00:00 2001 From: 0Gitnick Date: Tue, 13 Aug 2019 15:04:20 -0500 Subject: [PATCH 3/9] Delegate finding leading zeroes to library functions --- onionr/onionrproofs.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index 8ccc376d..f4f8b540 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -52,17 +52,15 @@ def getDifficultyForNewBlock(data, ourBlock=True): return retData -def getHashDifficulty(h): +def getHashDifficulty(hexHash): ''' - Return the amount of leading zeroes in a hex hash string (h) + Return the amount of leading zeroes in a hex hash string (hexHash) ''' difficulty = 0 - assert type(h) is str - for character in h: - if character == '0': - difficulty += 1 - else: - break + assert type(hexHash) is str + + difficulty = len(hexHash) - len(hexHash.lstrip('0')) + return difficulty def hashMeetsDifficulty(h): From 401723f5abac937d881d3b8ec7d33415082b9ab4 Mon Sep 17 00:00:00 2001 From: 0Gitnick Date: Tue, 13 Aug 2019 15:10:58 -0500 Subject: [PATCH 4/9] Comment step function --- onionr/onionrproofs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index f4f8b540..860da578 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -28,7 +28,7 @@ def getDifficultyModifier(): on a variety of factors, currently only disk use. ''' percentUse = storagecounter.StorageCounter().getPercent() - difficultyIncrease = math.floor(4 * percentUse) + difficultyIncrease = math.floor(4 * percentUse) # difficulty increase is a step function return difficultyIncrease From 6c091acd34dd73f327ebda86075340f4c394acf6 Mon Sep 17 00:00:00 2001 From: 0Gitnick Date: Tue, 13 Aug 2019 15:18:43 -0500 Subject: [PATCH 5/9] Shorten hashMeetsDifficulty function --- onionr/onionrproofs.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index 860da578..ede7b50d 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -63,19 +63,18 @@ def getHashDifficulty(hexHash): return difficulty -def hashMeetsDifficulty(h): +def hashMeetsDifficulty(hexHash): ''' Return bool for a hash string to see if it meets pow difficulty defined in config ''' - hashDifficulty = getHashDifficulty(h) + hashDifficulty = getHashDifficulty(hexHash) + try: expected = int(config.get('general.minimum_block_pow')) except TypeError: raise ValueError('Missing general.minimum_block_pow config') - if hashDifficulty >= expected: - return True - else: - return False + + return hashDifficulty >= expected class DataPOW: def __init__(self, data, forceDifficulty=0, threadCount = 1): From 7c7058a6910de3d80c57a7df328b11873411d0e9 Mon Sep 17 00:00:00 2001 From: 0Gitnick Date: Tue, 13 Aug 2019 15:26:20 -0500 Subject: [PATCH 6/9] Remove unused variables in onionrproofs.py --- onionr/onionrproofs.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index ede7b50d..216c0879 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -114,9 +114,6 @@ class DataPOW: self.hashing = True self.reporting = reporting iFound = False # if current thread is the one that found the answer - answer = '' - heartbeat = 200000 - hbCount = 0 while self.hashing: rand = nacl.utils.random() @@ -208,10 +205,7 @@ class POW: self.hashing = True self.reporting = reporting iFound = False # if current thread is the one that found the answer - answer = '' - hbCount = 0 nonce = int(binascii.hexlify(nacl.utils.random(64)), 16) - startNonce = nonce while self.hashing: #token = nacl.hash.blake2b(rand + self.data).decode() self.metadata['pow'] = nonce From 34c00069f5f421e6442a4b4f4c926dae448325f2 Mon Sep 17 00:00:00 2001 From: 0Gitnick Date: Tue, 13 Aug 2019 16:54:03 -0500 Subject: [PATCH 7/9] 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): ''' From 26b1cc5d87ecbe72a1f7b47bdacc1008563e8936 Mon Sep 17 00:00:00 2001 From: 0Gitnick Date: Tue, 13 Aug 2019 19:12:45 -0500 Subject: [PATCH 8/9] Delegate code in onionrproofs --- onionr/communicatorutils/announcenode.py | 2 +- onionr/onionrproofs.py | 20 ++++++-------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/onionr/communicatorutils/announcenode.py b/onionr/communicatorutils/announcenode.py index aa391c62..c4475d52 100755 --- a/onionr/communicatorutils/announcenode.py +++ b/onionr/communicatorutils/announcenode.py @@ -65,7 +65,7 @@ def announce_node(daemon): data['random'] = existingRand else: daemon.announceProgress[peer] = True - proof = onionrproofs.DataPOW(combinedNodes, forceDifficulty=onionrvalues.ANNOUNCE_POW) + proof = onionrproofs.DataPOW(combinedNodes, minDifficulty=onionrvalues.ANNOUNCE_POW) del daemon.announceProgress[peer] try: data['random'] = base64.b64encode(proof.waitForResult()[1]) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index db1d3ebc..f7bfc1f9 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -71,21 +71,13 @@ def hashMeetsDifficulty(hexHash): return hashDifficulty >= expected class DataPOW: - def __init__(self, data, forceDifficulty=0, threadCount = 1): - self.foundHash = False - self.difficulty = 0 + def __init__(self, data, minDifficulty = 0, threadCount = 1): self.data = data self.threadCount = threadCount + self.difficulty = max(minDifficulty, getDifficultyForNewBlock(data)) self.rounds = 0 self.hashing = False - - if forceDifficulty == 0: - dataLen = sys.getsizeof(data) - self.difficulty = math.floor(dataLen / 1000000) - if self.difficulty <= 2: - self.difficulty = 4 - else: - self.difficulty = forceDifficulty + self.foundHash = False try: self.data = self.data.encode() @@ -164,7 +156,7 @@ class DataPOW: return result class POW: - def __init__(self, metadata, data, threadCount = 1, forceDifficulty=0): + def __init__(self, metadata, data, threadCount = 1, minDifficulty=0): self.foundHash = False self.difficulty = 0 self.data = data @@ -179,8 +171,8 @@ class POW: except AttributeError: pass - if forceDifficulty > 0: - self.difficulty = forceDifficulty + if minDifficulty > 0: + self.difficulty = minDifficulty else: # Calculate difficulty. Dumb for now, may use good algorithm in the future. self.difficulty = getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data)) From 00ad409737eeb77a72d5f584c41952d6a31439d8 Mon Sep 17 00:00:00 2001 From: 0Gitnick Date: Tue, 13 Aug 2019 20:06:14 -0500 Subject: [PATCH 9/9] Some microimprovements --- onionr/onionrproofs.py | 6 +----- onionr/utils/detectoptimization.py | 2 +- onionr/utils/hastor.py | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index f7bfc1f9..88329fb8 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -50,12 +50,8 @@ def getHashDifficulty(hexHash): ''' Return the amount of leading zeroes in a hex hash string (hexHash) ''' - difficulty = 0 assert type(hexHash) is str - - difficulty = len(hexHash) - len(hexHash.lstrip('0')) - - return difficulty + return len(hexHash) - len(hexHash.lstrip('0')) def hashMeetsDifficulty(hexHash): ''' diff --git a/onionr/utils/detectoptimization.py b/onionr/utils/detectoptimization.py index 20570242..eb208ff3 100755 --- a/onionr/utils/detectoptimization.py +++ b/onionr/utils/detectoptimization.py @@ -21,7 +21,7 @@ def detect_optimization(): '''Returns true if Python is run in optimized mode (-o), based on optimization ignoring assert statements''' try: - assert True is False + assert False except AssertionError: return False return True \ No newline at end of file diff --git a/onionr/utils/hastor.py b/onionr/utils/hastor.py index 94a33be1..f94298e8 100644 --- a/onionr/utils/hastor.py +++ b/onionr/utils/hastor.py @@ -1,6 +1,4 @@ import netcontroller def has_tor(): - if netcontroller.tor_binary() is None: - return False - return True \ No newline at end of file + return netcontroller.tor_binary() is not None \ No newline at end of file