Skip to content

Commit 73119af

Browse files
author
Lee Miller
committed
Test singleWorker._doPOWDefaults(), make it a classmethod (closes: #1834)
1 parent 338c006 commit 73119af

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/class_singleWorker.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ def _getKeysForAddress(self, address):
217217
return privSigningKeyHex, privEncryptionKeyHex, \
218218
pubSigningKey, pubEncryptionKey
219219

220+
@classmethod
220221
def _doPOWDefaults(
221-
self, payload, TTL,
222+
cls, payload, TTL,
222223
nonceTrialsPerByte=None, payloadLengthExtraBytes=None,
223224
log_prefix='', log_time=False
224225
):
@@ -228,19 +229,19 @@ def _doPOWDefaults(
228229
if not payloadLengthExtraBytes:
229230
payloadLengthExtraBytes = \
230231
defaults.networkDefaultPayloadLengthExtraBytes
231-
self.logger.info(
232+
cls.logger.info(
232233
'%s Doing proof of work... TTL set to %s', log_prefix, TTL)
233234
if log_time:
234235
start_time = time.time()
235236
trialValue, nonce = proofofwork.calculate(
236237
payload, TTL, nonceTrialsPerByte, payloadLengthExtraBytes)
237-
self.logger.info(
238+
cls.logger.info(
238239
'%s Found proof of work %s Nonce: %s',
239240
log_prefix, trialValue, nonce
240241
)
241242
try:
242243
delta = time.time() - start_time
243-
self.logger.info(
244+
cls.logger.info(
244245
'PoW took %.1f seconds, speed %s.',
245246
delta, sizeof_fmt(nonce / delta)
246247
)

src/tests/test_proofofwork.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ def test_calculate(self):
4242
self.assertTrue(
4343
protocol.isProofOfWorkSufficient(pack('>Q', nonce) + payload))
4444

45+
# pylint: disable=import-outside-toplevel
46+
from class_singleWorker import singleWorker
47+
48+
self.assertTrue(protocol.isProofOfWorkSufficient(
49+
singleWorker._doPOWDefaults(payload, default_ttl)))
50+
4551

4652
@unittest.skipUnless(
4753
os.getenv('BITMESSAGE_TEST_POW'), "BITMESSAGE_TEST_POW is not set")
@@ -67,6 +73,23 @@ def test_calculate(self):
6773
pack('>Q', nonce) + payload, 2000, 2000,
6874
int(time.time()) + TTL - 3600))
6975

76+
# pylint: disable=import-outside-toplevel
77+
from class_singleWorker import singleWorker
78+
79+
with self.assertLogs('default') as cm:
80+
self.assertTrue(protocol.isProofOfWorkSufficient(
81+
singleWorker._doPOWDefaults(payload, TTL, log_prefix='+')))
82+
self.assertEqual(
83+
cm.output[0],
84+
'INFO:default:+ Doing proof of work... TTL set to %s' % TTL)
85+
self.assertEqual(
86+
cm.output[1][:34], 'INFO:default:+ Found proof of work')
87+
88+
with self.assertLogs('default') as cm:
89+
self.assertTrue(protocol.isProofOfWorkSufficient(
90+
singleWorker._doPOWDefaults(payload, TTL, log_time=True)))
91+
self.assertEqual(cm.output[2][:22], 'INFO:default:PoW took ')
92+
7093
with self.assertRaises(StopIteration):
7194
self.state.shutdown = 1
7295
proofofwork.calculate(payload, TTL)

0 commit comments

Comments
 (0)