Make PoW optional in disseminatePreEncryptedMsg API command #2211

Merged
PeterSurda merged 5 commits from gitea-80 into v0.6 2024-04-14 03:40:55 +02:00
Showing only changes of commit 7836538290 - Show all commits

View File

@ -66,7 +66,7 @@ import socket
import subprocess # nosec B404 import subprocess # nosec B404
import time import time
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from struct import pack from struct import pack, unpack
import six import six
from six.moves import configparser, http_client, xmlrpc_server from six.moves import configparser, http_client, xmlrpc_server
@ -1292,49 +1292,53 @@ class BMRPCDispatcher(object):
Handle a request to disseminate an encrypted message. Handle a request to disseminate an encrypted message.
The device issuing this command to PyBitmessage supplies a msg object The device issuing this command to PyBitmessage supplies a msg object
that has already been encrypted but which still needs the PoW that has already been encrypted but which may still need the PoW
to be done. PyBitmessage accepts this msg object and sends it out to be done. PyBitmessage accepts this msg object and sends it out
to the rest of the Bitmessage network as if it had generated to the rest of the Bitmessage network as if it had generated
the message itself. the message itself.
*encryptedPayload* is a hex encoded string *encryptedPayload* is a hex encoded string starting with the nonce,
8 zero bytes in case of no PoW done.
""" """
encryptedPayload = b'\x00' * 8 + self._decode(encryptedPayload, "hex") encryptedPayload = self._decode(encryptedPayload, "hex")
# compatibility stub ^, since disseminatePreEncryptedMsg
# still expects the encryptedPayload without a nonce nonce, = unpack('>Q', encryptedPayload[:8])
objectType, toStreamNumber, expiresTime = \ objectType, toStreamNumber, expiresTime = \
protocol.decodeObjectParameters(encryptedPayload) protocol.decodeObjectParameters(encryptedPayload)
encryptedPayload = encryptedPayload[8:]
TTL = expiresTime - time.time() + 300 # a bit of extra padding if nonce == 0: # Let us do the POW and attach it to the front
# Let us do the POW and attach it to the front encryptedPayload = encryptedPayload[8:]
target = 2**64 / ( TTL = expiresTime - time.time() + 300 # a bit of extra padding
nonceTrialsPerByte * ( # Let us do the POW and attach it to the front
len(encryptedPayload) + 8 + payloadLengthExtraBytes + (( logger.debug("expiresTime: %s", expiresTime)
TTL * ( logger.debug("TTL: %s", TTL)
len(encryptedPayload) + 8 + payloadLengthExtraBytes logger.debug("objectType: %s", objectType)
)) / (2 ** 16)) logger.info(
)) '(For msg message via API) Doing proof of work. Total required'
logger.debug("expiresTime: %s", expiresTime) ' difficulty: %s\nRequired small message difficulty: %s',
logger.debug("TTL: %s", TTL) float(nonceTrialsPerByte)
logger.debug("objectType: %s", objectType) / networkDefaultProofOfWorkNonceTrialsPerByte,
logger.info( float(payloadLengthExtraBytes)
'(For msg message via API) Doing proof of work. Total required' / networkDefaultPayloadLengthExtraBytes,
' difficulty: %s\nRequired small message difficulty: %s', )
float(nonceTrialsPerByte) powStartTime = time.time()
/ networkDefaultProofOfWorkNonceTrialsPerByte, target = 2**64 / (
float(payloadLengthExtraBytes) nonceTrialsPerByte * (
/ networkDefaultPayloadLengthExtraBytes, len(encryptedPayload) + 8 + payloadLengthExtraBytes + ((
) TTL * (
powStartTime = time.time() len(encryptedPayload) + 8 + payloadLengthExtraBytes
initialHash = hashlib.sha512(encryptedPayload).digest() )) / (2 ** 16))
trialValue, nonce = proofofwork.run(target, initialHash) ))
logger.info( initialHash = hashlib.sha512(encryptedPayload).digest()
'(For msg message via API) Found proof of work %s\nNonce: %s\n' trialValue, nonce = proofofwork.run(target, initialHash)
'POW took %s seconds. %s nonce trials per second.', logger.info(
trialValue, nonce, int(time.time() - powStartTime), '(For msg message via API) Found proof of work %s\nNonce: %s\n'
nonce / (time.time() - powStartTime) 'POW took %s seconds. %s nonce trials per second.',
) trialValue, nonce, int(time.time() - powStartTime),
encryptedPayload = pack('>Q', nonce) + encryptedPayload nonce / (time.time() - powStartTime)
)
encryptedPayload = pack('>Q', nonce) + encryptedPayload
inventoryHash = calculateInventoryHash(encryptedPayload) inventoryHash = calculateInventoryHash(encryptedPayload)
Inventory()[inventoryHash] = ( Inventory()[inventoryHash] = (
objectType, toStreamNumber, encryptedPayload, objectType, toStreamNumber, encryptedPayload,