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 44a4a370a6 - Show all commits

View File

@ -71,7 +71,6 @@ from struct import pack
import six import six
from six.moves import configparser, http_client, xmlrpc_server from six.moves import configparser, http_client, xmlrpc_server
import defaults
import helper_inbox import helper_inbox
import helper_sent import helper_sent
import protocol import protocol
@ -89,6 +88,9 @@ from addresses import (
) )
from bmconfigparser import config from bmconfigparser import config
from debug import logger from debug import logger
from defaults import (
networkDefaultProofOfWorkNonceTrialsPerByte,
networkDefaultPayloadLengthExtraBytes)
from helper_sql import ( from helper_sql import (
SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure, sql_ready) SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure, sql_ready)
from highlevelcrypto import calculateInventoryHash from highlevelcrypto import calculateInventoryHash
@ -657,13 +659,11 @@ class BMRPCDispatcher(object):
nonceTrialsPerByte = self.config.get( nonceTrialsPerByte = self.config.get(
'bitmessagesettings', 'defaultnoncetrialsperbyte' 'bitmessagesettings', 'defaultnoncetrialsperbyte'
) if not totalDifficulty else int( ) if not totalDifficulty else int(
defaults.networkDefaultProofOfWorkNonceTrialsPerByte networkDefaultProofOfWorkNonceTrialsPerByte * totalDifficulty)
* totalDifficulty)
payloadLengthExtraBytes = self.config.get( payloadLengthExtraBytes = self.config.get(
'bitmessagesettings', 'defaultpayloadlengthextrabytes' 'bitmessagesettings', 'defaultpayloadlengthextrabytes'
) if not smallMessageDifficulty else int( ) if not smallMessageDifficulty else int(
defaults.networkDefaultPayloadLengthExtraBytes networkDefaultPayloadLengthExtraBytes * smallMessageDifficulty)
* smallMessageDifficulty)
if not isinstance(eighteenByteRipe, bool): if not isinstance(eighteenByteRipe, bool):
raise APIError( raise APIError(
@ -705,13 +705,11 @@ class BMRPCDispatcher(object):
nonceTrialsPerByte = self.config.get( nonceTrialsPerByte = self.config.get(
'bitmessagesettings', 'defaultnoncetrialsperbyte' 'bitmessagesettings', 'defaultnoncetrialsperbyte'
) if not totalDifficulty else int( ) if not totalDifficulty else int(
defaults.networkDefaultProofOfWorkNonceTrialsPerByte networkDefaultProofOfWorkNonceTrialsPerByte * totalDifficulty)
* totalDifficulty)
payloadLengthExtraBytes = self.config.get( payloadLengthExtraBytes = self.config.get(
'bitmessagesettings', 'defaultpayloadlengthextrabytes' 'bitmessagesettings', 'defaultpayloadlengthextrabytes'
) if not smallMessageDifficulty else int( ) if not smallMessageDifficulty else int(
defaults.networkDefaultPayloadLengthExtraBytes networkDefaultPayloadLengthExtraBytes * smallMessageDifficulty)
* smallMessageDifficulty)
if not passphrase: if not passphrase:
raise APIError(1, 'The specified passphrase is blank.') raise APIError(1, 'The specified passphrase is blank.')
@ -1286,15 +1284,21 @@ class BMRPCDispatcher(object):
@command('disseminatePreEncryptedMsg') @command('disseminatePreEncryptedMsg')
def HandleDisseminatePreEncryptedMsg( def HandleDisseminatePreEncryptedMsg(
self, encryptedPayload, requiredAverageProofOfWorkNonceTrialsPerByte, self, encryptedPayload,
requiredPayloadLengthExtraBytes): nonceTrialsPerByte=networkDefaultProofOfWorkNonceTrialsPerByte,
"""Handle a request to disseminate an encrypted message""" payloadLengthExtraBytes=networkDefaultPayloadLengthExtraBytes
):
"""
Handle a request to disseminate an encrypted message.
# The device issuing this command to PyBitmessage supplies a msg The device issuing this command to PyBitmessage supplies a msg object
# object that has already been encrypted but which still needs the POW that has already been encrypted but which still needs 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. Please do not yet add this to the api doc. the message itself.
*encryptedPayload* is a hex encoded string
"""
encryptedPayload = b'\x00' * 8 + self._decode(encryptedPayload, "hex") encryptedPayload = b'\x00' * 8 + self._decode(encryptedPayload, "hex")
# compatibility stub ^, since disseminatePreEncryptedMsg # compatibility stub ^, since disseminatePreEncryptedMsg
# still expects the encryptedPayload without a nonce # still expects the encryptedPayload without a nonce
@ -1304,12 +1308,10 @@ class BMRPCDispatcher(object):
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 / ( target = 2**64 / (
requiredAverageProofOfWorkNonceTrialsPerByte * ( nonceTrialsPerByte * (
len(encryptedPayload) + 8 len(encryptedPayload) + 8 + payloadLengthExtraBytes + ((
+ requiredPayloadLengthExtraBytes + ((
TTL * ( TTL * (
len(encryptedPayload) + 8 len(encryptedPayload) + 8 + payloadLengthExtraBytes
+ requiredPayloadLengthExtraBytes
)) / (2 ** 16)) )) / (2 ** 16))
)) ))
logger.debug("expiresTime: %s", expiresTime) logger.debug("expiresTime: %s", expiresTime)
@ -1318,10 +1320,10 @@ class BMRPCDispatcher(object):
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',
float(requiredAverageProofOfWorkNonceTrialsPerByte) float(nonceTrialsPerByte)
/ defaults.networkDefaultProofOfWorkNonceTrialsPerByte, / networkDefaultProofOfWorkNonceTrialsPerByte,
float(requiredPayloadLengthExtraBytes) float(payloadLengthExtraBytes)
/ defaults.networkDefaultPayloadLengthExtraBytes, / networkDefaultPayloadLengthExtraBytes,
) )
powStartTime = time.time() powStartTime = time.time()
initialHash = hashlib.sha512(encryptedPayload).digest() initialHash = hashlib.sha512(encryptedPayload).digest()
@ -1365,8 +1367,8 @@ class BMRPCDispatcher(object):
# Let us do the POW # Let us do the POW
target = 2 ** 64 / (( target = 2 ** 64 / ((
len(payload) + defaults.networkDefaultPayloadLengthExtraBytes + 8 len(payload) + networkDefaultPayloadLengthExtraBytes + 8
) * defaults.networkDefaultProofOfWorkNonceTrialsPerByte) ) * networkDefaultProofOfWorkNonceTrialsPerByte)
logger.info('(For pubkey message via API) Doing proof of work...') logger.info('(For pubkey message via API) Doing proof of work...')
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
trialValue, nonce = proofofwork.run(target, initialHash) trialValue, nonce = proofofwork.run(target, initialHash)