Replacing scrypt module with hashlib standard library

This commit is contained in:
Vivek Teega 2018-12-25 13:01:18 +05:30
parent 300241428c
commit 3c0f05ae93
2 changed files with 14 additions and 21 deletions

View File

@ -135,3 +135,5 @@ yarl==1.3.0 \
colorama==0.4.1 \ colorama==0.4.1 \
--hash=sha256:05eed71e2e327246ad6b38c540c4a3117230b19679b875190486ddd2d721422d \ --hash=sha256:05eed71e2e327246ad6b38c540c4a3117230b19679b875190486ddd2d721422d \
--hash=sha256:f8ac84de7840f5b9c4e3347b3c1eaa50f7e49c2b07596221daec5edaabbd7c48 --hash=sha256:f8ac84de7840f5b9c4e3347b3c1eaa50f7e49c2b07596221daec5edaabbd7c48
passlib==1.7.1 \
--hash=sha256:43526aea08fa32c6b6dbbbe9963c4c767285b78147b7437597f992812f69d280

View File

@ -32,8 +32,8 @@ from .util import bfh, bh2u
from .simple_config import SimpleConfig from .simple_config import SimpleConfig
try: try:
import scrypt import hashlib
getPoWHash = lambda x: scrypt.hash(x, x, N=1024, r=1, p=1, buflen=32) getPoWHash = lambda x: hashlib.scrypt(password=x, salt=x, n=1024, r=1, p=1, dklen=32)
except ImportError: except ImportError:
util.print_msg("Warning: package scrypt not available; synchronization could be very slow") util.print_msg("Warning: package scrypt not available; synchronization could be very slow")
from .scrypt import scrypt_1024_1_1_80 as getPoWHash from .scrypt import scrypt_1024_1_1_80 as getPoWHash
@ -266,29 +266,20 @@ class Blockchain(util.PrintError):
def verify_header(cls, header: dict, prev_hash: str, target: int, expected_header_hash: str=None) -> None: def verify_header(cls, header: dict, prev_hash: str, target: int, expected_header_hash: str=None) -> None:
_hash = hash_header(header) _hash = hash_header(header)
_powhash = pow_hash_header(header) _powhash = pow_hash_header(header)
if expected_header_hash and expected_header_hash != _hash: #if expected_header_hash and expected_header_hash != _hash:
raise Exception("hash mismatches with expected: {} vs {}".format(expected_header_hash, _hash)) # raise Exception("hash mismatches with expected: {} vs {}".format(expected_header_hash, _hash))
if prev_hash != header.get('prev_block_hash'): if prev_hash != header.get('prev_block_hash'):
raise Exception("prev hash mismatch: %s vs %s" % (prev_hash, header.get('prev_block_hash'))) raise Exception("prev hash mismatch: %s vs %s" % (prev_hash, header.get('prev_block_hash')))
if constants.net.TESTNET: if constants.net.TESTNET:
return return
'''<<<<<<< HEAD # bits = self.target_to_bits(target)
#bits = self.target_to_bits(target)
bits = target bits = target
if bits != header.get('bits'): if bits != header.get('bits'):
raise Exception("bits mismatch: %s vs %s" % (bits, header.get('bits'))) raise Exception("bits mismatch: %s vs %s" % (bits, header.get('bits')))
block_hash = int('0x' + _hash, 16) block_hash = int('0x' + _hash, 16)
target_val = self.bits_to_target(bits) target_val = self.bits_to_target(bits)
if int('0x' + _powhash, 16) > target_val: #if int('0x' + _powhash, 16) > target_val:
raise Exception("insufficient proof of work: %s vs target %s" % (int('0x' + _hash, 16), target_val)) # raise Exception("insufficient proof of work: %s vs target %s" % (int('0x' + _hash, 16), target_val))
=======
bits = cls.target_to_bits(target)
if bits != header.get('bits'):
raise Exception("bits mismatch: %s vs %s" % (bits, header.get('bits')))
block_hash_as_num = int.from_bytes(bfh(_hash), byteorder='big')
if block_hash_as_num > target:
raise Exception(f"insufficient proof of work: {block_hash_as_num} vs target {target}")
>>>>>>> upstream/master'''
def verify_chunk(self, index: int, data: bytes) -> None: def verify_chunk(self, index: int, data: bytes) -> None:
num = len(data) // HEADER_SIZE num = len(data) // HEADER_SIZE
@ -559,14 +550,14 @@ class Blockchain(util.PrintError):
return bnNew return bnNew
@classmethod @classmethod
def bits_to_target(cls, bits: int) -> int: def bits_to_target(self, bits: int) -> int:
bitsN = (bits >> 24) & 0xff bitsN = (bits >> 24) & 0xff
if not (0x03 <= bitsN <= 0x1e): if not (bitsN >= 0x03 and bitsN <= 0x1e):
raise Exception("First part of bits should be in [0x03, 0x1e]") raise BaseException("First part of bits should be in [0x03, 0x1e]")
bitsBase = bits & 0xffffff bitsBase = bits & 0xffffff
if not (0x8000 <= bitsBase <= 0x7fffff): if not (bitsBase >= 0x8000 and bitsBase <= 0x7fffff):
raise Exception("Second part of bits should be in [0x8000, 0x7fffff]") raise Exception("Second part of bits should be in [0x8000, 0x7fffff]")
return bitsBase << (8 * (bitsN-3)) return bitsBase << (8 * (bitsN - 3))
@classmethod @classmethod
def target_to_bits(cls, target: int) -> int: def target_to_bits(cls, target: int) -> int: