From 1612f9c77825dd6d91c453feb4f1e343a6fe79ad Mon Sep 17 00:00:00 2001 From: navjot Date: Sun, 17 Jan 2021 00:28:40 +0530 Subject: [PATCH] test case for openclpow module --- src/openclpow.py | 14 +------------- src/tests/test_openclpow.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 src/tests/test_openclpow.py diff --git a/src/openclpow.py b/src/openclpow.py index 35bf46d2..a086c81f 100644 --- a/src/openclpow.py +++ b/src/openclpow.py @@ -2,9 +2,8 @@ """ Module for Proof of Work using OpenCL """ -import hashlib import os -from struct import pack, unpack +from struct import pack import paths from bmconfigparser import BMConfigParser @@ -109,14 +108,3 @@ def do_opencl_pow(hash_, target): raise Exception("Interrupted") # logger.debug("Took %d tries.", progress) return output[0][0] - - -if __name__ == "__main__": - initCL() - target_ = 54227212183 - initialHash = ("3758f55b5a8d902fd3597e4ce6a2d3f23daff735f65d9698c270987f4e67ad590" - "b93f3ffeba0ef2fd08a8dc2f87b68ae5a0dc819ab57f22ad2c4c9c8618a43b3").decode("hex") - nonce = do_opencl_pow(initialHash.encode("hex"), target_) - trialValue, = unpack( - '>Q', hashlib.sha512(hashlib.sha512(pack('>Q', nonce) + initialHash).digest()).digest()[0:8]) - print "{} - value {} < {}".format(nonce, trialValue, target_) diff --git a/src/tests/test_openclpow.py b/src/tests/test_openclpow.py new file mode 100644 index 00000000..384addb7 --- /dev/null +++ b/src/tests/test_openclpow.py @@ -0,0 +1,28 @@ +""" +Tests for openclpow module +""" +import hashlib +import unittest + +import pybitmessage.openclpow as openclpow + +from struct import pack, unpack + + +class TestOpenClPow(unittest.TestCase): + """ + Main opencl test case + """ + + @unittest.skipIf(not openclpow.libAvailable, + "pyopencl is not installed") + def test_openclpow(self): + """Check the working of openclpow module""" + openclpow.initCL() + target_ = 54227212183 + initialHash = ("3758f55b5a8d902fd3597e4ce6a2d3f23daff735f65d9698c270987f4e67ad590" + "b93f3ffeba0ef2fd08a8dc2f87b68ae5a0dc819ab57f22ad2c4c9c8618a43b3").decode("hex") + nonce = openclpow.do_opencl_pow(initialHash.encode("hex"), target_) + trialValue, = unpack( + '>Q', hashlib.sha512(hashlib.sha512(pack('>Q', nonce) + initialHash).digest()).digest()[0:8]) + self.assertLess((nonce - trialValue), target_)