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,28 +1292,24 @@ 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)
if nonce == 0: # Let us do the POW and attach it to the front
encryptedPayload = encryptedPayload[8:] encryptedPayload = encryptedPayload[8:]
TTL = expiresTime - time.time() + 300 # a bit of extra padding TTL = expiresTime - time.time() + 300 # a bit of extra padding
# Let us do the POW and attach it to the front # Let us do the POW and attach it to the front
target = 2**64 / (
nonceTrialsPerByte * (
len(encryptedPayload) + 8 + payloadLengthExtraBytes + ((
TTL * (
len(encryptedPayload) + 8 + payloadLengthExtraBytes
)) / (2 ** 16))
))
logger.debug("expiresTime: %s", expiresTime) logger.debug("expiresTime: %s", expiresTime)
logger.debug("TTL: %s", TTL) logger.debug("TTL: %s", TTL)
logger.debug("objectType: %s", objectType) logger.debug("objectType: %s", objectType)
@ -1326,6 +1322,13 @@ class BMRPCDispatcher(object):
/ networkDefaultPayloadLengthExtraBytes, / networkDefaultPayloadLengthExtraBytes,
) )
powStartTime = time.time() powStartTime = time.time()
target = 2**64 / (
nonceTrialsPerByte * (
len(encryptedPayload) + 8 + payloadLengthExtraBytes + ((
TTL * (
len(encryptedPayload) + 8 + payloadLengthExtraBytes
)) / (2 ** 16))
))
initialHash = hashlib.sha512(encryptedPayload).digest() initialHash = hashlib.sha512(encryptedPayload).digest()
trialValue, nonce = proofofwork.run(target, initialHash) trialValue, nonce = proofofwork.run(target, initialHash)
logger.info( logger.info(
@ -1335,6 +1338,7 @@ class BMRPCDispatcher(object):
nonce / (time.time() - powStartTime) nonce / (time.time() - powStartTime)
) )
encryptedPayload = pack('>Q', nonce) + encryptedPayload encryptedPayload = pack('>Q', nonce) + encryptedPayload
inventoryHash = calculateInventoryHash(encryptedPayload) inventoryHash = calculateInventoryHash(encryptedPayload)
Inventory()[inventoryHash] = ( Inventory()[inventoryHash] = (
objectType, toStreamNumber, encryptedPayload, objectType, toStreamNumber, encryptedPayload,