Use proofofwork.calculate() everywhere

This commit is contained in:
Lee Miller 2023-01-16 02:23:36 +02:00
parent 120b044e2e
commit c6ae9f6843
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
2 changed files with 25 additions and 61 deletions

View File

@ -1410,15 +1410,10 @@ class BMRPCDispatcher(object):
/ networkDefaultPayloadLengthExtraBytes,
)
powStartTime = time.time()
target = 2**64 / (
nonceTrialsPerByte * (
len(encryptedPayload) + 8 + payloadLengthExtraBytes + ((
TTL * (
len(encryptedPayload) + 8 + payloadLengthExtraBytes
)) / (2 ** 16))
))
initialHash = hashlib.sha512(encryptedPayload).digest()
trialValue, nonce = proofofwork.run(target, initialHash)
trialValue, nonce = proofofwork.calculate(
encryptedPayload, TTL,
nonceTrialsPerByte, payloadLengthExtraBytes
)
logger.info(
'(For msg message via API) Found proof of work %s\nNonce: %s\n'
'POW took %s seconds. %s nonce trials per second.',
@ -1429,9 +1424,7 @@ class BMRPCDispatcher(object):
inventoryHash = calculateInventoryHash(encryptedPayload)
state.Inventory[inventoryHash] = (
objectType, toStreamNumber, encryptedPayload,
expiresTime, b''
)
objectType, toStreamNumber, encryptedPayload, expiresTime, b'')
logger.info(
'Broadcasting inv for msg(API disseminatePreEncryptedMsg'
' command): %s', hexlify(inventoryHash))

View File

@ -217,24 +217,23 @@ class singleWorker(StoppableThread):
return privSigningKeyHex, privEncryptionKeyHex, \
pubSigningKey, pubEncryptionKey
def _doPOWDefaults(self, payload, TTL,
log_prefix='',
log_time=False):
target = 2 ** 64 / (
defaults.networkDefaultProofOfWorkNonceTrialsPerByte * (
len(payload) + 8
+ defaults.networkDefaultPayloadLengthExtraBytes + ((
TTL * (
len(payload) + 8
+ defaults.networkDefaultPayloadLengthExtraBytes
)) / (2 ** 16))
))
initialHash = hashlib.sha512(payload).digest()
def _doPOWDefaults(
self, payload, TTL,
nonceTrialsPerByte=None, payloadLengthExtraBytes=None,
log_prefix='', log_time=False
):
if not nonceTrialsPerByte:
nonceTrialsPerByte = \
defaults.networkDefaultProofOfWorkNonceTrialsPerByte
if not payloadLengthExtraBytes:
payloadLengthExtraBytes = \
defaults.networkDefaultPayloadLengthExtraBytes
self.logger.info(
'%s Doing proof of work... TTL set to %s', log_prefix, TTL)
if log_time:
start_time = time.time()
trialValue, nonce = proofofwork.run(target, initialHash)
trialValue, nonce = proofofwork.calculate(
payload, TTL, nonceTrialsPerByte, payloadLengthExtraBytes)
self.logger.info(
'%s Found proof of work %s Nonce: %s',
log_prefix, trialValue, nonce
@ -245,8 +244,8 @@ class singleWorker(StoppableThread):
'PoW took %.1f seconds, speed %s.',
delta, sizeof_fmt(nonce / delta)
)
except: # noqa:E722 # NameError
self.logger.warning("Proof of Work exception")
except NameError: # no start_time - no logging
pass
payload = pack('>Q', nonce) + payload
return payload
@ -1250,41 +1249,13 @@ class singleWorker(StoppableThread):
encryptedPayload += '\x00\x00\x00\x02' # object type: msg
encryptedPayload += encodeVarint(1) # msg version
encryptedPayload += encodeVarint(toStreamNumber) + encrypted
target = 2 ** 64 / (
requiredAverageProofOfWorkNonceTrialsPerByte * (
len(encryptedPayload) + 8
+ requiredPayloadLengthExtraBytes + ((
TTL * (
len(encryptedPayload) + 8
+ requiredPayloadLengthExtraBytes
)) / (2 ** 16))
))
self.logger.info(
'(For msg message) Doing proof of work. Total required'
' difficulty: %f. Required small message difficulty: %f.',
float(requiredAverageProofOfWorkNonceTrialsPerByte)
/ defaults.networkDefaultProofOfWorkNonceTrialsPerByte,
float(requiredPayloadLengthExtraBytes)
/ defaults.networkDefaultPayloadLengthExtraBytes
)
powStartTime = time.time()
initialHash = hashlib.sha512(encryptedPayload).digest()
trialValue, nonce = proofofwork.run(target, initialHash)
self.logger.info(
'(For msg message) Found proof of work %s Nonce: %s',
trialValue, nonce
encryptedPayload = self._doPOWDefaults(
encryptedPayload, TTL,
requiredAverageProofOfWorkNonceTrialsPerByte,
requiredPayloadLengthExtraBytes,
log_prefix='(For msg message)', log_time=True
)
try:
self.logger.info(
'PoW took %.1f seconds, speed %s.',
time.time() - powStartTime,
sizeof_fmt(nonce / (time.time() - powStartTime))
)
except: # noqa:E722
self.logger.warning("Proof of Work exception")
encryptedPayload = pack('>Q', nonce) + encryptedPayload
# Sanity check. The encryptedPayload size should never be
# larger than 256 KiB. There should be checks elsewhere