Fix difficulty calculator for new blocks

master
0Gitnick 2019-08-13 16:54:03 -05:00
parent 7c7058a691
commit 34c00069f5
2 changed files with 8 additions and 14 deletions

View File

@ -19,7 +19,7 @@ def verify_POW(blockContent):
except AttributeError: except AttributeError:
pass pass
difficulty = onionrproofs.getDifficultyForNewBlock(blockContent, ourBlock=False) difficulty = onionrproofs.getDifficultyForNewBlock(blockContent)
if difficulty < int(config.get('general.minimum_block_pow')): if difficulty < int(config.get('general.minimum_block_pow')):
difficulty = int(config.get('general.minimum_block_pow')) difficulty = int(config.get('general.minimum_block_pow'))

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
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 import config, logger, onionrblockapi, storagecounter
from onionrutils import bytesconverter from onionrutils import bytesconverter
from onionrcrypto import hashers from onionrcrypto import hashers
@ -32,25 +32,19 @@ def getDifficultyModifier():
return difficultyIncrease 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 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): if isinstance(data, onionrblockapi.Block):
dataSize = len(data.getRaw().encode('utf-8')) dataSizeInBytes = len(bytesconverter.str_to_bytes(data.getRaw()))
else: 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)
minDifficulty = config.get('general.minimum_send_pow', 4) totalDifficulty = max(minDifficulty, math.floor(dataSizeInBytes / 1000000.0)) + getDifficultyModifier()
else:
minDifficulty = config.get('general.minimum_block_pow', 4)
retData = max(minDifficulty, math.floor(dataSize / 100000)) + getDifficultyModifier() return totalDifficulty
return retData
def getHashDifficulty(hexHash): def getHashDifficulty(hexHash):
''' '''