Skip to content

Commit 7836538

Browse files
author
Lee Miller
committed
Make PoW optional in disseminatePreEncryptedMsg
1 parent 44a4a37 commit 7836538

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

src/api.py

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
import subprocess # nosec B404
6767
import time
6868
from binascii import hexlify, unhexlify
69-
from struct import pack
69+
from struct import pack, unpack
7070

7171
import six
7272
from six.moves import configparser, http_client, xmlrpc_server
@@ -1292,49 +1292,53 @@ def HandleDisseminatePreEncryptedMsg(
12921292
Handle a request to disseminate an encrypted message.
12931293
12941294
The device issuing this command to PyBitmessage supplies a msg object
1295-
that has already been encrypted but which still needs the PoW
1295+
that has already been encrypted but which may still need the PoW
12961296
to be done. PyBitmessage accepts this msg object and sends it out
12971297
to the rest of the Bitmessage network as if it had generated
12981298
the message itself.
12991299
1300-
*encryptedPayload* is a hex encoded string
1300+
*encryptedPayload* is a hex encoded string starting with the nonce,
1301+
8 zero bytes in case of no PoW done.
13011302
"""
1302-
encryptedPayload = b'\x00' * 8 + self._decode(encryptedPayload, "hex")
1303-
# compatibility stub ^, since disseminatePreEncryptedMsg
1304-
# still expects the encryptedPayload without a nonce
1303+
encryptedPayload = self._decode(encryptedPayload, "hex")
1304+
1305+
nonce, = unpack('>Q', encryptedPayload[:8])
13051306
objectType, toStreamNumber, expiresTime = \
13061307
protocol.decodeObjectParameters(encryptedPayload)
1307-
encryptedPayload = encryptedPayload[8:]
1308-
TTL = expiresTime - time.time() + 300 # a bit of extra padding
1309-
# Let us do the POW and attach it to the front
1310-
target = 2**64 / (
1311-
nonceTrialsPerByte * (
1312-
len(encryptedPayload) + 8 + payloadLengthExtraBytes + ((
1313-
TTL * (
1314-
len(encryptedPayload) + 8 + payloadLengthExtraBytes
1315-
)) / (2 ** 16))
1316-
))
1317-
logger.debug("expiresTime: %s", expiresTime)
1318-
logger.debug("TTL: %s", TTL)
1319-
logger.debug("objectType: %s", objectType)
1320-
logger.info(
1321-
'(For msg message via API) Doing proof of work. Total required'
1322-
' difficulty: %s\nRequired small message difficulty: %s',
1323-
float(nonceTrialsPerByte)
1324-
/ networkDefaultProofOfWorkNonceTrialsPerByte,
1325-
float(payloadLengthExtraBytes)
1326-
/ networkDefaultPayloadLengthExtraBytes,
1327-
)
1328-
powStartTime = time.time()
1329-
initialHash = hashlib.sha512(encryptedPayload).digest()
1330-
trialValue, nonce = proofofwork.run(target, initialHash)
1331-
logger.info(
1332-
'(For msg message via API) Found proof of work %s\nNonce: %s\n'
1333-
'POW took %s seconds. %s nonce trials per second.',
1334-
trialValue, nonce, int(time.time() - powStartTime),
1335-
nonce / (time.time() - powStartTime)
1336-
)
1337-
encryptedPayload = pack('>Q', nonce) + encryptedPayload
1308+
1309+
if nonce == 0: # Let us do the POW and attach it to the front
1310+
encryptedPayload = encryptedPayload[8:]
1311+
TTL = expiresTime - time.time() + 300 # a bit of extra padding
1312+
# Let us do the POW and attach it to the front
1313+
logger.debug("expiresTime: %s", expiresTime)
1314+
logger.debug("TTL: %s", TTL)
1315+
logger.debug("objectType: %s", objectType)
1316+
logger.info(
1317+
'(For msg message via API) Doing proof of work. Total required'
1318+
' difficulty: %s\nRequired small message difficulty: %s',
1319+
float(nonceTrialsPerByte)
1320+
/ networkDefaultProofOfWorkNonceTrialsPerByte,
1321+
float(payloadLengthExtraBytes)
1322+
/ networkDefaultPayloadLengthExtraBytes,
1323+
)
1324+
powStartTime = time.time()
1325+
target = 2**64 / (
1326+
nonceTrialsPerByte * (
1327+
len(encryptedPayload) + 8 + payloadLengthExtraBytes + ((
1328+
TTL * (
1329+
len(encryptedPayload) + 8 + payloadLengthExtraBytes
1330+
)) / (2 ** 16))
1331+
))
1332+
initialHash = hashlib.sha512(encryptedPayload).digest()
1333+
trialValue, nonce = proofofwork.run(target, initialHash)
1334+
logger.info(
1335+
'(For msg message via API) Found proof of work %s\nNonce: %s\n'
1336+
'POW took %s seconds. %s nonce trials per second.',
1337+
trialValue, nonce, int(time.time() - powStartTime),
1338+
nonce / (time.time() - powStartTime)
1339+
)
1340+
encryptedPayload = pack('>Q', nonce) + encryptedPayload
1341+
13381342
inventoryHash = calculateInventoryHash(encryptedPayload)
13391343
Inventory()[inventoryHash] = (
13401344
objectType, toStreamNumber, encryptedPayload,

0 commit comments

Comments
 (0)