2020-04-15 03:40:31 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2018-02-22 22:55:42 +00:00
|
|
|
|
2020-04-15 03:40:31 +00:00
|
|
|
Proof of work module
|
|
|
|
"""
|
|
|
|
import multiprocessing, time, math, threading, binascii, sys, json
|
|
|
|
import nacl.encoding, nacl.hash, nacl.utils
|
|
|
|
|
|
|
|
import config
|
|
|
|
import logger
|
|
|
|
from onionrblocks import onionrblockapi, storagecounter
|
|
|
|
from onionrutils import bytesconverter
|
|
|
|
from onionrcrypto import hashers
|
|
|
|
|
|
|
|
from .blocknoncestart import BLOCK_NONCE_START_INT
|
|
|
|
"""
|
2018-02-22 22:55:42 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-04-15 03:40:31 +00:00
|
|
|
"""
|
2019-06-16 06:06:32 +00:00
|
|
|
config.reload()
|
2019-08-13 19:51:46 +00:00
|
|
|
|
2019-07-19 19:49:56 +00:00
|
|
|
def getDifficultyModifier():
|
2020-04-15 03:40:31 +00:00
|
|
|
"""returns the difficulty modifier for block storage based
|
2018-12-24 06:12:46 +00:00
|
|
|
on a variety of factors, currently only disk use.
|
2020-04-15 03:40:31 +00:00
|
|
|
"""
|
2019-09-11 02:14:15 +00:00
|
|
|
percentUse = storagecounter.StorageCounter().get_percent()
|
2019-08-13 20:10:58 +00:00
|
|
|
difficultyIncrease = math.floor(4 * percentUse) # difficulty increase is a step function
|
2018-12-24 06:12:46 +00:00
|
|
|
|
2019-08-13 19:51:46 +00:00
|
|
|
return difficultyIncrease
|
2018-12-24 06:12:46 +00:00
|
|
|
|
2019-08-13 21:54:03 +00:00
|
|
|
def getDifficultyForNewBlock(data):
|
2020-04-15 03:40:31 +00:00
|
|
|
"""
|
2018-12-24 06:12:46 +00:00
|
|
|
Get difficulty for block. Accepts size in integer, Block instance, or str/bytes full block contents
|
2020-04-15 03:40:31 +00:00
|
|
|
"""
|
2018-12-24 06:12:46 +00:00
|
|
|
if isinstance(data, onionrblockapi.Block):
|
2019-08-13 21:54:03 +00:00
|
|
|
dataSizeInBytes = len(bytesconverter.str_to_bytes(data.getRaw()))
|
2018-12-24 06:12:46 +00:00
|
|
|
else:
|
2019-08-13 21:54:03 +00:00
|
|
|
dataSizeInBytes = len(bytesconverter.str_to_bytes(data))
|
2019-06-07 05:09:14 +00:00
|
|
|
|
2019-08-13 21:54:03 +00:00
|
|
|
minDifficulty = config.get('general.minimum_send_pow', 4)
|
|
|
|
totalDifficulty = max(minDifficulty, math.floor(dataSizeInBytes / 1000000.0)) + getDifficultyModifier()
|
2018-12-24 06:12:46 +00:00
|
|
|
|
2019-08-13 21:54:03 +00:00
|
|
|
return totalDifficulty
|
2019-02-17 05:20:47 +00:00
|
|
|
|
2018-12-24 06:12:46 +00:00
|
|
|
return retData
|
2018-11-11 02:10:58 +00:00
|
|
|
|
2019-09-09 00:21:36 +00:00
|
|
|
def getHashDifficulty(h: str):
|
2020-04-15 03:40:31 +00:00
|
|
|
"""
|
2019-08-13 20:04:20 +00:00
|
|
|
Return the amount of leading zeroes in a hex hash string (hexHash)
|
2020-04-15 03:40:31 +00:00
|
|
|
"""
|
2019-09-11 02:08:35 +00:00
|
|
|
return len(h) - len(h.lstrip('0'))
|
2018-11-11 02:10:58 +00:00
|
|
|
|
2019-08-13 20:18:43 +00:00
|
|
|
def hashMeetsDifficulty(hexHash):
|
2020-04-15 03:40:31 +00:00
|
|
|
"""
|
2018-11-11 02:10:58 +00:00
|
|
|
Return bool for a hash string to see if it meets pow difficulty defined in config
|
2020-04-15 03:40:31 +00:00
|
|
|
"""
|
2019-08-13 20:18:43 +00:00
|
|
|
hashDifficulty = getHashDifficulty(hexHash)
|
|
|
|
|
2018-12-09 17:29:39 +00:00
|
|
|
try:
|
|
|
|
expected = int(config.get('general.minimum_block_pow'))
|
|
|
|
except TypeError:
|
|
|
|
raise ValueError('Missing general.minimum_block_pow config')
|
2019-08-13 20:18:43 +00:00
|
|
|
|
|
|
|
return hashDifficulty >= expected
|
2018-04-23 03:49:53 +00:00
|
|
|
|