Test singleWorker._doPOWDefaults(), make it a classmethod (closes: #1834)

This commit is contained in:
Lee Miller 2023-12-07 03:22:03 +02:00
parent 338c006bb1
commit 73119af407
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
2 changed files with 28 additions and 4 deletions

View File

@ -217,8 +217,9 @@ class singleWorker(StoppableThread):
return privSigningKeyHex, privEncryptionKeyHex, \
pubSigningKey, pubEncryptionKey
@classmethod
def _doPOWDefaults(
self, payload, TTL,
cls, payload, TTL,
nonceTrialsPerByte=None, payloadLengthExtraBytes=None,
log_prefix='', log_time=False
):
@ -228,19 +229,19 @@ class singleWorker(StoppableThread):
if not payloadLengthExtraBytes:
payloadLengthExtraBytes = \
defaults.networkDefaultPayloadLengthExtraBytes
self.logger.info(
cls.logger.info(
'%s Doing proof of work... TTL set to %s', log_prefix, TTL)
if log_time:
start_time = time.time()
trialValue, nonce = proofofwork.calculate(
payload, TTL, nonceTrialsPerByte, payloadLengthExtraBytes)
self.logger.info(
cls.logger.info(
'%s Found proof of work %s Nonce: %s',
log_prefix, trialValue, nonce
)
try:
delta = time.time() - start_time
self.logger.info(
cls.logger.info(
'PoW took %.1f seconds, speed %s.',
delta, sizeof_fmt(nonce / delta)
)

View File

@ -42,6 +42,12 @@ class TestProofofworkBase(TestPartialRun):
self.assertTrue(
protocol.isProofOfWorkSufficient(pack('>Q', nonce) + payload))
# pylint: disable=import-outside-toplevel
from class_singleWorker import singleWorker
self.assertTrue(protocol.isProofOfWorkSufficient(
singleWorker._doPOWDefaults(payload, default_ttl)))
@unittest.skipUnless(
os.getenv('BITMESSAGE_TEST_POW'), "BITMESSAGE_TEST_POW is not set")
@ -67,6 +73,23 @@ class TestProofofwork(TestProofofworkBase):
pack('>Q', nonce) + payload, 2000, 2000,
int(time.time()) + TTL - 3600))
# pylint: disable=import-outside-toplevel
from class_singleWorker import singleWorker
with self.assertLogs('default') as cm:
self.assertTrue(protocol.isProofOfWorkSufficient(
singleWorker._doPOWDefaults(payload, TTL, log_prefix='+')))
self.assertEqual(
cm.output[0],
'INFO:default:+ Doing proof of work... TTL set to %s' % TTL)
self.assertEqual(
cm.output[1][:34], 'INFO:default:+ Found proof of work')
with self.assertLogs('default') as cm:
self.assertTrue(protocol.isProofOfWorkSufficient(
singleWorker._doPOWDefaults(payload, TTL, log_time=True)))
self.assertEqual(cm.output[2][:22], 'INFO:default:PoW took ')
with self.assertRaises(StopIteration):
self.state.shutdown = 1
proofofwork.calculate(payload, TTL)