Onionr/src/onionrproofs/__init__.py

68 lines
2.1 KiB
Python
Raw Normal View History

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
2020-04-15 03:40:31 +00:00
from onionrutils import bytesconverter
from onionrcrypto import hashers
from .blocknoncestart import BLOCK_NONCE_START_INT
2022-01-06 20:48:22 +00:00
from .vdf import create_vdf
2020-04-15 03:40:31 +00:00
"""
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
2018-12-24 06:12:46 +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):
dataSizeInBytes = len(bytesconverter.str_to_bytes(data.getRaw()))
2018-12-24 06:12:46 +00:00
else:
dataSizeInBytes = len(bytesconverter.str_to_bytes(data))
minDifficulty = config.get('general.minimum_send_pow', 4)
totalDifficulty = max(minDifficulty, math.floor(dataSizeInBytes / 1000000.0))
2018-12-24 06:12:46 +00:00
return totalDifficulty
2019-02-17 05:20:47 +00:00
def getHashDifficulty(h: str):
2020-04-15 03:40:31 +00:00
"""
Return the amount of leading zeroes in a hex hash string (hexHash)
2020-04-15 03:40:31 +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