diff --git a/duniterpy/documents/block.py b/duniterpy/documents/block.py index a711716fe5c563b67768657df60efb7528a17ab3..7793cb5882248b1e6820a4d3a5ce315346ab97ed 100644 --- a/duniterpy/documents/block.py +++ b/duniterpy/documents/block.py @@ -5,6 +5,7 @@ from .transaction import Transaction from .constants import pubkey_regex, block_id_regex, block_hash_regex import re +import hashlib def block_uid(value): @@ -255,7 +256,7 @@ The class Block handles Block documents. @property def blockUID(self): - return BlockUID(self.number, self.sha_hash) + return BlockUID(self.number, self.proof_of_work()) @classmethod def from_signed_raw(cls, signed_raw): @@ -501,4 +502,11 @@ PreviousIssuer: {1}\n".format(self.prev_hash, self.prev_issuer) doc += "Nonce: {0}\n".format(self.noonce) - return doc \ No newline at end of file + return doc + + def proof_of_work(self): + doc_str = """InnerHash: {inner_hash} +Nonce: {nonce} +{signature} +""".format(inner_hash=self.inner_hash, nonce=self.noonce, signature=self.signatures[0]) + return hashlib.sha256(doc_str.encode('ascii')).hexdigest().upper() diff --git a/tests/documents/test_block.py b/tests/documents/test_block.py index e8c0be792dca8c30a1037e5e565b5ae6fe5a4847..3a48bc249a6c2fe48c5e8260264d07ee97ed308b 100644 --- a/tests/documents/test_block.py +++ b/tests/documents/test_block.py @@ -539,6 +539,37 @@ class Test_Block(unittest.TestCase): elif BlockUID.empty(): self.fail("Empty blockuid __nonzero__ comparison failed") + def test_proof_of_work(self): + block = """Version: 5 +Type: Block +Currency: test_net +Number: 60803 +PoWMin: 80 +Time: 1480979125 +MedianTime: 1480975879 +UnitBase: 7 +Issuer: 9bZEATXBGPUSsk8oAYi4KAChg3rHKwNt67hVdErbNGCW +IssuersFrame: 120 +IssuersFrameVar: 0 +DifferentIssuersCount: 18 +PreviousHash: 0000083FB6E3435ADCDF0F86B0A1BCA108B6B47D4B4BA61D0B4FDC21A262CF4C +PreviousIssuer: BnSRjMjJ7gWy13asCRz9rQ6G5Njytdf3pvR1GMkJgtu6 +MembersCount: 187 +Identities: +Joiners: +Actives: +Leavers: +Revoked: +Excluded: +Certifications: +Transactions: +InnerHash: 310F57575EA865EF47BFA236108B2B1CAEBFBF8EF70960E32E214E413E9C836B +Nonce: 10200000037440 +AywstQpC0S5iaA/YQvbz2alpP6zTYG3tjkWpxy1jgeCo028Te2V327bBZbfDGDzsjxOrF4UVmEBiGsgbqhL9CA== +""" + block_doc = Block.from_signed_raw(block) + self.assertEqual(block_doc.proof_of_work(), "00000A84839226046082E2B1AD49664E382D98C845644945D133D4A90408813A") + if __name__ == '__main__': unittest.main()