Skip to content

Commit 44a4a37

Browse files
author
Lee Miller
committed
Use defaults while doing PoW for a preencrypted msg
1 parent e1ae333 commit 44a4a37

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

src/api.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
import six
7272
from six.moves import configparser, http_client, xmlrpc_server
7373

74-
import defaults
7574
import helper_inbox
7675
import helper_sent
7776
import protocol
@@ -89,6 +88,9 @@
8988
)
9089
from bmconfigparser import config
9190
from debug import logger
91+
from defaults import (
92+
networkDefaultProofOfWorkNonceTrialsPerByte,
93+
networkDefaultPayloadLengthExtraBytes)
9294
from helper_sql import (
9395
SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure, sql_ready)
9496
from highlevelcrypto import calculateInventoryHash
@@ -657,13 +659,11 @@ def HandleCreateRandomAddress(
657659
nonceTrialsPerByte = self.config.get(
658660
'bitmessagesettings', 'defaultnoncetrialsperbyte'
659661
) if not totalDifficulty else int(
660-
defaults.networkDefaultProofOfWorkNonceTrialsPerByte
661-
* totalDifficulty)
662+
networkDefaultProofOfWorkNonceTrialsPerByte * totalDifficulty)
662663
payloadLengthExtraBytes = self.config.get(
663664
'bitmessagesettings', 'defaultpayloadlengthextrabytes'
664665
) if not smallMessageDifficulty else int(
665-
defaults.networkDefaultPayloadLengthExtraBytes
666-
* smallMessageDifficulty)
666+
networkDefaultPayloadLengthExtraBytes * smallMessageDifficulty)
667667

668668
if not isinstance(eighteenByteRipe, bool):
669669
raise APIError(
@@ -705,13 +705,11 @@ def HandleCreateDeterministicAddresses(
705705
nonceTrialsPerByte = self.config.get(
706706
'bitmessagesettings', 'defaultnoncetrialsperbyte'
707707
) if not totalDifficulty else int(
708-
defaults.networkDefaultProofOfWorkNonceTrialsPerByte
709-
* totalDifficulty)
708+
networkDefaultProofOfWorkNonceTrialsPerByte * totalDifficulty)
710709
payloadLengthExtraBytes = self.config.get(
711710
'bitmessagesettings', 'defaultpayloadlengthextrabytes'
712711
) if not smallMessageDifficulty else int(
713-
defaults.networkDefaultPayloadLengthExtraBytes
714-
* smallMessageDifficulty)
712+
networkDefaultPayloadLengthExtraBytes * smallMessageDifficulty)
715713

716714
if not passphrase:
717715
raise APIError(1, 'The specified passphrase is blank.')
@@ -1286,15 +1284,21 @@ def ListSubscriptions(self):
12861284

12871285
@command('disseminatePreEncryptedMsg')
12881286
def HandleDisseminatePreEncryptedMsg(
1289-
self, encryptedPayload, requiredAverageProofOfWorkNonceTrialsPerByte,
1290-
requiredPayloadLengthExtraBytes):
1291-
"""Handle a request to disseminate an encrypted message"""
1292-
1293-
# The device issuing this command to PyBitmessage supplies a msg
1294-
# object that has already been encrypted but which still needs the POW
1295-
# to be done. PyBitmessage accepts this msg object and sends it out
1296-
# to the rest of the Bitmessage network as if it had generated
1297-
# the message itself. Please do not yet add this to the api doc.
1287+
self, encryptedPayload,
1288+
nonceTrialsPerByte=networkDefaultProofOfWorkNonceTrialsPerByte,
1289+
payloadLengthExtraBytes=networkDefaultPayloadLengthExtraBytes
1290+
):
1291+
"""
1292+
Handle a request to disseminate an encrypted message.
1293+
1294+
The device issuing this command to PyBitmessage supplies a msg object
1295+
that has already been encrypted but which still needs the PoW
1296+
to be done. PyBitmessage accepts this msg object and sends it out
1297+
to the rest of the Bitmessage network as if it had generated
1298+
the message itself.
1299+
1300+
*encryptedPayload* is a hex encoded string
1301+
"""
12981302
encryptedPayload = b'\x00' * 8 + self._decode(encryptedPayload, "hex")
12991303
# compatibility stub ^, since disseminatePreEncryptedMsg
13001304
# still expects the encryptedPayload without a nonce
@@ -1304,12 +1308,10 @@ def HandleDisseminatePreEncryptedMsg(
13041308
TTL = expiresTime - time.time() + 300 # a bit of extra padding
13051309
# Let us do the POW and attach it to the front
13061310
target = 2**64 / (
1307-
requiredAverageProofOfWorkNonceTrialsPerByte * (
1308-
len(encryptedPayload) + 8
1309-
+ requiredPayloadLengthExtraBytes + ((
1311+
nonceTrialsPerByte * (
1312+
len(encryptedPayload) + 8 + payloadLengthExtraBytes + ((
13101313
TTL * (
1311-
len(encryptedPayload) + 8
1312-
+ requiredPayloadLengthExtraBytes
1314+
len(encryptedPayload) + 8 + payloadLengthExtraBytes
13131315
)) / (2 ** 16))
13141316
))
13151317
logger.debug("expiresTime: %s", expiresTime)
@@ -1318,10 +1320,10 @@ def HandleDisseminatePreEncryptedMsg(
13181320
logger.info(
13191321
'(For msg message via API) Doing proof of work. Total required'
13201322
' difficulty: %s\nRequired small message difficulty: %s',
1321-
float(requiredAverageProofOfWorkNonceTrialsPerByte)
1322-
/ defaults.networkDefaultProofOfWorkNonceTrialsPerByte,
1323-
float(requiredPayloadLengthExtraBytes)
1324-
/ defaults.networkDefaultPayloadLengthExtraBytes,
1323+
float(nonceTrialsPerByte)
1324+
/ networkDefaultProofOfWorkNonceTrialsPerByte,
1325+
float(payloadLengthExtraBytes)
1326+
/ networkDefaultPayloadLengthExtraBytes,
13251327
)
13261328
powStartTime = time.time()
13271329
initialHash = hashlib.sha512(encryptedPayload).digest()
@@ -1365,8 +1367,8 @@ def HandleDissimatePubKey(self, payload):
13651367

13661368
# Let us do the POW
13671369
target = 2 ** 64 / ((
1368-
len(payload) + defaults.networkDefaultPayloadLengthExtraBytes + 8
1369-
) * defaults.networkDefaultProofOfWorkNonceTrialsPerByte)
1370+
len(payload) + networkDefaultPayloadLengthExtraBytes + 8
1371+
) * networkDefaultProofOfWorkNonceTrialsPerByte)
13701372
logger.info('(For pubkey message via API) Doing proof of work...')
13711373
initialHash = hashlib.sha512(payload).digest()
13721374
trialValue, nonce = proofofwork.run(target, initialHash)

0 commit comments

Comments
 (0)