did an oo refactor

This commit is contained in:
Kjistóf 2017-12-01 21:40:45 +01:00
parent 7ea8082fd2
commit dcf203111c

View File

@ -3,10 +3,11 @@ from itertools import count
from hashlib import sha256 from hashlib import sha256
class proofow:
class pow_base:
def __init__(self, difficulty, base=None): def __init__(self, difficulty, base=None):
self._base = urandom(32) if not base else base self._base = urandom(self._bits//8) if not base else base
self._target = 2 ** (256 - difficulty) self._target = 2 ** (self._bits - difficulty)
def work(self): def work(self):
for nonce in count(): for nonce in count():
@ -14,5 +15,17 @@ class proofow:
return nonce return nonce
def verify(self, nonce): def verify(self, nonce):
hexresult = sha256(self._base + str(nonce).encode()).hexdigest() hexresult = self._hexdigest(self._base + str(nonce).encode())
return int(hexresult, 16) < self._target return int(hexresult, 16) < self._target
class pow_hashlib_base(pow_base):
def _hexdigest(self, data):
return self._hasher(data).hexdigest()
class pow256(pow_hashlib_base):
_bits = 256
_hasher = sha256