Use proofofwork.calculate() everywhere
This commit is contained in:
parent
120b044e2e
commit
c6ae9f6843
17
src/api.py
17
src/api.py
|
@ -1410,15 +1410,10 @@ class BMRPCDispatcher(object):
|
||||||
/ networkDefaultPayloadLengthExtraBytes,
|
/ networkDefaultPayloadLengthExtraBytes,
|
||||||
)
|
)
|
||||||
powStartTime = time.time()
|
powStartTime = time.time()
|
||||||
target = 2**64 / (
|
trialValue, nonce = proofofwork.calculate(
|
||||||
nonceTrialsPerByte * (
|
encryptedPayload, TTL,
|
||||||
len(encryptedPayload) + 8 + payloadLengthExtraBytes + ((
|
nonceTrialsPerByte, payloadLengthExtraBytes
|
||||||
TTL * (
|
)
|
||||||
len(encryptedPayload) + 8 + payloadLengthExtraBytes
|
|
||||||
)) / (2 ** 16))
|
|
||||||
))
|
|
||||||
initialHash = hashlib.sha512(encryptedPayload).digest()
|
|
||||||
trialValue, nonce = proofofwork.run(target, initialHash)
|
|
||||||
logger.info(
|
logger.info(
|
||||||
'(For msg message via API) Found proof of work %s\nNonce: %s\n'
|
'(For msg message via API) Found proof of work %s\nNonce: %s\n'
|
||||||
'POW took %s seconds. %s nonce trials per second.',
|
'POW took %s seconds. %s nonce trials per second.',
|
||||||
|
@ -1429,9 +1424,7 @@ class BMRPCDispatcher(object):
|
||||||
|
|
||||||
inventoryHash = calculateInventoryHash(encryptedPayload)
|
inventoryHash = calculateInventoryHash(encryptedPayload)
|
||||||
state.Inventory[inventoryHash] = (
|
state.Inventory[inventoryHash] = (
|
||||||
objectType, toStreamNumber, encryptedPayload,
|
objectType, toStreamNumber, encryptedPayload, expiresTime, b'')
|
||||||
expiresTime, b''
|
|
||||||
)
|
|
||||||
logger.info(
|
logger.info(
|
||||||
'Broadcasting inv for msg(API disseminatePreEncryptedMsg'
|
'Broadcasting inv for msg(API disseminatePreEncryptedMsg'
|
||||||
' command): %s', hexlify(inventoryHash))
|
' command): %s', hexlify(inventoryHash))
|
||||||
|
|
|
@ -217,24 +217,23 @@ class singleWorker(StoppableThread):
|
||||||
return privSigningKeyHex, privEncryptionKeyHex, \
|
return privSigningKeyHex, privEncryptionKeyHex, \
|
||||||
pubSigningKey, pubEncryptionKey
|
pubSigningKey, pubEncryptionKey
|
||||||
|
|
||||||
def _doPOWDefaults(self, payload, TTL,
|
def _doPOWDefaults(
|
||||||
log_prefix='',
|
self, payload, TTL,
|
||||||
log_time=False):
|
nonceTrialsPerByte=None, payloadLengthExtraBytes=None,
|
||||||
target = 2 ** 64 / (
|
log_prefix='', log_time=False
|
||||||
defaults.networkDefaultProofOfWorkNonceTrialsPerByte * (
|
):
|
||||||
len(payload) + 8
|
if not nonceTrialsPerByte:
|
||||||
+ defaults.networkDefaultPayloadLengthExtraBytes + ((
|
nonceTrialsPerByte = \
|
||||||
TTL * (
|
defaults.networkDefaultProofOfWorkNonceTrialsPerByte
|
||||||
len(payload) + 8
|
if not payloadLengthExtraBytes:
|
||||||
+ defaults.networkDefaultPayloadLengthExtraBytes
|
payloadLengthExtraBytes = \
|
||||||
)) / (2 ** 16))
|
defaults.networkDefaultPayloadLengthExtraBytes
|
||||||
))
|
|
||||||
initialHash = hashlib.sha512(payload).digest()
|
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
'%s Doing proof of work... TTL set to %s', log_prefix, TTL)
|
'%s Doing proof of work... TTL set to %s', log_prefix, TTL)
|
||||||
if log_time:
|
if log_time:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
trialValue, nonce = proofofwork.run(target, initialHash)
|
trialValue, nonce = proofofwork.calculate(
|
||||||
|
payload, TTL, nonceTrialsPerByte, payloadLengthExtraBytes)
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
'%s Found proof of work %s Nonce: %s',
|
'%s Found proof of work %s Nonce: %s',
|
||||||
log_prefix, trialValue, nonce
|
log_prefix, trialValue, nonce
|
||||||
|
@ -245,8 +244,8 @@ class singleWorker(StoppableThread):
|
||||||
'PoW took %.1f seconds, speed %s.',
|
'PoW took %.1f seconds, speed %s.',
|
||||||
delta, sizeof_fmt(nonce / delta)
|
delta, sizeof_fmt(nonce / delta)
|
||||||
)
|
)
|
||||||
except: # noqa:E722 # NameError
|
except NameError: # no start_time - no logging
|
||||||
self.logger.warning("Proof of Work exception")
|
pass
|
||||||
payload = pack('>Q', nonce) + payload
|
payload = pack('>Q', nonce) + payload
|
||||||
return payload
|
return payload
|
||||||
|
|
||||||
|
@ -1250,41 +1249,13 @@ class singleWorker(StoppableThread):
|
||||||
encryptedPayload += '\x00\x00\x00\x02' # object type: msg
|
encryptedPayload += '\x00\x00\x00\x02' # object type: msg
|
||||||
encryptedPayload += encodeVarint(1) # msg version
|
encryptedPayload += encodeVarint(1) # msg version
|
||||||
encryptedPayload += encodeVarint(toStreamNumber) + encrypted
|
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()
|
encryptedPayload = self._doPOWDefaults(
|
||||||
initialHash = hashlib.sha512(encryptedPayload).digest()
|
encryptedPayload, TTL,
|
||||||
trialValue, nonce = proofofwork.run(target, initialHash)
|
requiredAverageProofOfWorkNonceTrialsPerByte,
|
||||||
self.logger.info(
|
requiredPayloadLengthExtraBytes,
|
||||||
'(For msg message) Found proof of work %s Nonce: %s',
|
log_prefix='(For msg message)', log_time=True
|
||||||
trialValue, nonce
|
|
||||||
)
|
)
|
||||||
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
|
# Sanity check. The encryptedPayload size should never be
|
||||||
# larger than 256 KiB. There should be checks elsewhere
|
# larger than 256 KiB. There should be checks elsewhere
|
||||||
|
|
Reference in New Issue
Block a user