Api fix HandleDisseminatePreEncryptedMsg

- PoW target formula correction
- correctly decode expiration time / TTL, object type and destination
  stream
This commit is contained in:
Peter Šurda 2022-11-10 18:25:53 +08:00 committed by Peter Šurda
parent 3fb6adad9b
commit 8a1835e3ad
Signed by: PeterSurda
GPG Key ID: 3E47497CF67ABB95

View File

@ -70,7 +70,7 @@ import time
import xmlrpclib import xmlrpclib
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
from struct import pack from struct import pack, unpack
import defaults import defaults
import helper_inbox import helper_inbox
@ -1275,7 +1275,7 @@ class BMRPCDispatcher(object):
return {'subscriptions': data} return {'subscriptions': data}
@command('disseminatePreEncryptedMsg') @command('disseminatePreEncryptedMsg')
def HandleDisseminatePreEncryptedMsg( def HandleDisseminatePreEncryptedMsg( # pylint: disable=too-many-locals
self, encryptedPayload, requiredAverageProofOfWorkNonceTrialsPerByte, self, encryptedPayload, requiredAverageProofOfWorkNonceTrialsPerByte,
requiredPayloadLengthExtraBytes): requiredPayloadLengthExtraBytes):
"""Handle a request to disseminate an encrypted message""" """Handle a request to disseminate an encrypted message"""
@ -1286,13 +1286,22 @@ class BMRPCDispatcher(object):
# 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. Please do not yet add this to the api doc. # the message itself. Please do not yet add this to the api doc.
encryptedPayload = self._decode(encryptedPayload, "hex") encryptedPayload = self._decode(encryptedPayload, "hex")
expiresTime = unpack('>Q', encryptedPayload[0:8])[0]
objectType = unpack('>I', encryptedPayload[8:12])[0]
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 / ( target = 2**64 / (
( requiredAverageProofOfWorkNonceTrialsPerByte * (
len(encryptedPayload) len(encryptedPayload) + 8
+ requiredPayloadLengthExtraBytes + ((
TTL * (
len(encryptedPayload) + 8
+ requiredPayloadLengthExtraBytes + requiredPayloadLengthExtraBytes
+ 8 )) / (2 ** 16))
) * requiredAverageProofOfWorkNonceTrialsPerByte) ))
logger.debug("expiresTime: %s", expiresTime)
logger.debug("TTL: %s", TTL)
logger.debug("objectType: %s", objectType)
logger.info( logger.info(
'(For msg message via API) Doing proof of work. Total required' '(For msg message via API) Doing proof of work. Total required'
' difficulty: %s\nRequired small message difficulty: %s', ' difficulty: %s\nRequired small message difficulty: %s',
@ -1311,18 +1320,22 @@ class BMRPCDispatcher(object):
nonce / (time.time() - powStartTime) nonce / (time.time() - powStartTime)
) )
encryptedPayload = pack('>Q', nonce) + encryptedPayload encryptedPayload = pack('>Q', nonce) + encryptedPayload
toStreamNumber = decodeVarint(encryptedPayload[16:26])[0] parserPos = 20
_, objectVersionLength = decodeVarint(
encryptedPayload[parserPos:parserPos + 10])
parserPos += objectVersionLength
toStreamNumber, _ = decodeVarint(
encryptedPayload[parserPos:parserPos + 10])
inventoryHash = calculateInventoryHash(encryptedPayload) inventoryHash = calculateInventoryHash(encryptedPayload)
objectType = 2
TTL = 2.5 * 24 * 60 * 60
Inventory()[inventoryHash] = ( Inventory()[inventoryHash] = (
objectType, toStreamNumber, encryptedPayload, objectType, toStreamNumber, encryptedPayload,
int(time.time()) + TTL, '' expiresTime, ''
) )
logger.info( logger.info(
'Broadcasting inv for msg(API disseminatePreEncryptedMsg' 'Broadcasting inv for msg(API disseminatePreEncryptedMsg'
' command): %s', hexlify(inventoryHash)) ' command): %s', hexlify(inventoryHash))
queues.invQueue.put((toStreamNumber, inventoryHash)) queues.invQueue.put((toStreamNumber, inventoryHash))
return hexlify(inventoryHash)
@command('trashSentMessageByAckData') @command('trashSentMessageByAckData')
def HandleTrashSentMessageByAckDAta(self, ackdata): def HandleTrashSentMessageByAckDAta(self, ackdata):