|
66 | 66 | import subprocess # nosec B404 |
67 | 67 | import time |
68 | 68 | from binascii import hexlify, unhexlify |
69 | | -from struct import pack |
| 69 | +from struct import pack, unpack |
70 | 70 |
|
71 | 71 | import six |
72 | 72 | from six.moves import configparser, http_client, xmlrpc_server |
@@ -1292,49 +1292,53 @@ def HandleDisseminatePreEncryptedMsg( |
1292 | 1292 | Handle a request to disseminate an encrypted message. |
1293 | 1293 |
|
1294 | 1294 | 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 |
1296 | 1296 | to be done. PyBitmessage accepts this msg object and sends it out |
1297 | 1297 | to the rest of the Bitmessage network as if it had generated |
1298 | 1298 | the message itself. |
1299 | 1299 |
|
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. |
1301 | 1302 | """ |
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]) |
1305 | 1306 | objectType, toStreamNumber, expiresTime = \ |
1306 | 1307 | 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 | + |
1338 | 1342 | inventoryHash = calculateInventoryHash(encryptedPayload) |
1339 | 1343 | Inventory()[inventoryHash] = ( |
1340 | 1344 | objectType, toStreamNumber, encryptedPayload, |
|
0 commit comments