Testing proofofwork #2276

Merged
PeterSurda merged 10 commits from gitea-63 into v0.6 2025-01-21 04:03:38 +01:00
2 changed files with 28 additions and 4 deletions
Showing only changes of commit 73119af407 - Show all commits

View File

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

View File

@ -42,6 +42,12 @@ class TestProofofworkBase(TestPartialRun):
self.assertTrue( self.assertTrue(
protocol.isProofOfWorkSufficient(pack('>Q', nonce) + payload)) 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( @unittest.skipUnless(
os.getenv('BITMESSAGE_TEST_POW'), "BITMESSAGE_TEST_POW is not set") os.getenv('BITMESSAGE_TEST_POW'), "BITMESSAGE_TEST_POW is not set")
@ -67,6 +73,23 @@ class TestProofofwork(TestProofofworkBase):
pack('>Q', nonce) + payload, 2000, 2000, pack('>Q', nonce) + payload, 2000, 2000,
int(time.time()) + TTL - 3600)) 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): with self.assertRaises(StopIteration):
self.state.shutdown = 1 self.state.shutdown = 1
proofofwork.calculate(payload, TTL) proofofwork.calculate(payload, TTL)