Testing proofofwork #2276
|
@ -19,6 +19,10 @@ import state
|
||||||
import tr
|
import tr
|
||||||
from bmconfigparser import config
|
from bmconfigparser import config
|
||||||
from debug import logger
|
from debug import logger
|
||||||
|
from defaults import (
|
||||||
|
networkDefaultProofOfWorkNonceTrialsPerByte,
|
||||||
|
networkDefaultPayloadLengthExtraBytes)
|
||||||
|
|
||||||
|
|
||||||
bitmsglib = 'bitmsghash.so'
|
bitmsglib = 'bitmsghash.so'
|
||||||
bmpow = None
|
bmpow = None
|
||||||
|
@ -341,6 +345,28 @@ def run(target, initialHash):
|
||||||
pass # fallback
|
pass # fallback
|
||||||
|
|
||||||
|
|
||||||
|
def getTarget(payloadLength, ttl, nonceTrialsPerByte, payloadLengthExtraBytes):
|
||||||
|
"""Get PoW target for given length, ttl and difficulty params"""
|
||||||
|
return 2 ** 64 / (
|
||||||
|
nonceTrialsPerByte * (
|
||||||
|
payloadLength + 8 + payloadLengthExtraBytes + ((
|
||||||
|
ttl * (
|
||||||
|
payloadLength + 8 + payloadLengthExtraBytes
|
||||||
|
)) / (2 ** 16))
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
|
def calculate(
|
||||||
|
payload, ttl,
|
||||||
|
nonceTrialsPerByte=networkDefaultProofOfWorkNonceTrialsPerByte,
|
||||||
|
payloadLengthExtraBytes=networkDefaultPayloadLengthExtraBytes
|
||||||
|
):
|
||||||
|
"""Do the PoW for the payload and TTL with optional difficulty params"""
|
||||||
|
return run(getTarget(
|
||||||
|
len(payload), ttl, nonceTrialsPerByte, payloadLengthExtraBytes),
|
||||||
|
hashlib.sha512(payload).digest())
|
||||||
|
|
||||||
|
|
||||||
def resetPoW():
|
def resetPoW():
|
||||||
"""Initialise the OpenCL PoW"""
|
"""Initialise the OpenCL PoW"""
|
||||||
openclpow.initCL()
|
openclpow.initCL()
|
||||||
|
|
|
@ -91,3 +91,12 @@ sample_privsigningkey_wif = \
|
||||||
b'5K42shDERM5g7Kbi3JT5vsAWpXMqRhWZpX835M2pdSoqQQpJMYm'
|
b'5K42shDERM5g7Kbi3JT5vsAWpXMqRhWZpX835M2pdSoqQQpJMYm'
|
||||||
sample_privencryptionkey_wif = \
|
sample_privencryptionkey_wif = \
|
||||||
b'5HwugVWm31gnxtoYcvcK7oywH2ezYTh6Y4tzRxsndAeMi6NHqpA'
|
b'5HwugVWm31gnxtoYcvcK7oywH2ezYTh6Y4tzRxsndAeMi6NHqpA'
|
||||||
|
|
||||||
|
|
||||||
|
# PoW
|
||||||
|
|
||||||
|
sample_pow_target = 54227212183
|
||||||
|
sample_pow_initial_hash = unhexlify(
|
||||||
|
'3758f55b5a8d902fd3597e4ce6a2d3f23daff735f65d9698c270987f4e67ad590'
|
||||||
|
'b93f3ffeba0ef2fd08a8dc2f87b68ae5a0dc819ab57f22ad2c4c9c8618a43b3'
|
||||||
|
)
|
||||||
|
|
|
@ -3,9 +3,12 @@ Tests for openclpow module
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
from binascii import hexlify
|
||||||
|
|
||||||
from pybitmessage import openclpow, proofofwork
|
from pybitmessage import openclpow, proofofwork
|
||||||
|
|
||||||
|
from .samples import sample_pow_target, sample_pow_initial_hash
|
||||||
|
|
||||||
|
|
||||||
class TestOpenClPow(unittest.TestCase):
|
class TestOpenClPow(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
|
@ -19,11 +22,8 @@ class TestOpenClPow(unittest.TestCase):
|
||||||
@unittest.skipUnless(openclpow.enabledGpus, "No GPUs found / enabled")
|
@unittest.skipUnless(openclpow.enabledGpus, "No GPUs found / enabled")
|
||||||
def test_openclpow(self):
|
def test_openclpow(self):
|
||||||
"""Check the working of openclpow module"""
|
"""Check the working of openclpow module"""
|
||||||
target_ = 54227212183
|
nonce = openclpow.do_opencl_pow(
|
||||||
initialHash = (
|
hexlify(sample_pow_initial_hash), sample_pow_target)
|
||||||
"3758f55b5a8d902fd3597e4ce6a2d3f23daff735f65d9698c270987f4e67ad590"
|
|
||||||
"b93f3ffeba0ef2fd08a8dc2f87b68ae5a0dc819ab57f22ad2c4c9c8618a43b3"
|
|
||||||
).decode("hex")
|
|
||||||
nonce = openclpow.do_opencl_pow(initialHash.encode("hex"), target_)
|
|
||||||
self.assertLess(
|
self.assertLess(
|
||||||
nonce - proofofwork.trial_value(nonce, initialHash), target_)
|
nonce - proofofwork.trial_value(nonce, sample_pow_initial_hash),
|
||||||
|
sample_pow_target)
|
||||||
|
|
|
@ -1,13 +1,17 @@
|
||||||
"""
|
"""
|
||||||
Tests for proofofwork module
|
Tests for proofofwork module
|
||||||
"""
|
"""
|
||||||
|
# pylint: disable=protected-access
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import os
|
||||||
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
from binascii import unhexlify
|
|
||||||
from struct import pack, unpack
|
from struct import pack, unpack
|
||||||
|
|
||||||
from pybitmessage import proofofwork
|
from pybitmessage import proofofwork, protocol
|
||||||
|
|
||||||
|
from .samples import sample_pow_target, sample_pow_initial_hash
|
||||||
|
|
||||||
|
|
||||||
class TestProofofwork(unittest.TestCase):
|
class TestProofofwork(unittest.TestCase):
|
||||||
|
@ -17,20 +21,26 @@ class TestProofofwork(unittest.TestCase):
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
proofofwork.init()
|
proofofwork.init()
|
||||||
|
|
||||||
def test_empty(self):
|
def test_calculate(self):
|
||||||
"""just reproducing the empty test from proofofwork.init()"""
|
"""Ensure a calculated nonce has sufficient work for the protocol"""
|
||||||
self.assertEqual(
|
TTL = 24 * 60 * 60
|
||||||
proofofwork._doCPoW(2**63, ""), [6485065370652060397, 4])
|
payload = pack('>Q', int(time.time() + TTL)) + os.urandom(166)
|
||||||
|
nonce = proofofwork.calculate(payload, TTL)[1]
|
||||||
|
self.assertTrue(
|
||||||
|
protocol.isProofOfWorkSufficient(pack('>Q', nonce) + payload))
|
||||||
|
# raise difficulty
|
||||||
|
nonce = proofofwork.calculate(payload, TTL, 2000, 2000)[1]
|
||||||
|
self.assertTrue(
|
||||||
|
protocol.isProofOfWorkSufficient(
|
||||||
|
pack('>Q', nonce) + payload, 2000, 2000,
|
||||||
|
int(time.time()) + TTL - 3600))
|
||||||
|
|
||||||
def test_with_target(self):
|
def test_with_target(self):
|
||||||
"""Do PoW with parameters from test_openclpow and check the result"""
|
"""Do PoW with parameters from test_openclpow and check the result"""
|
||||||
target = 54227212183
|
nonce = proofofwork._doCPoW(
|
||||||
initialHash = unhexlify(
|
sample_pow_target, sample_pow_initial_hash)[0]
|
||||||
'3758f55b5a8d902fd3597e4ce6a2d3f23daff735f65d9698c270987f4e67ad590'
|
trial_value, = unpack(
|
||||||
'b93f3ffeba0ef2fd08a8dc2f87b68ae5a0dc819ab57f22ad2c4c9c8618a43b3'
|
|
||||||
)
|
|
||||||
nonce = proofofwork._doCPoW(target, initialHash)[0]
|
|
||||||
trialValue, = unpack(
|
|
||||||
'>Q', hashlib.sha512(hashlib.sha512(
|
'>Q', hashlib.sha512(hashlib.sha512(
|
||||||
pack('>Q', nonce) + initialHash).digest()).digest()[0:8])
|
pack('>Q', nonce) + sample_pow_initial_hash
|
||||||
self.assertLess((nonce - trialValue), target)
|
).digest()).digest()[0:8])
|
||||||
|
self.assertLess((nonce - trial_value), sample_pow_target)
|
||||||
|
|
Reference in New Issue
Block a user