diff --git a/bchosttrust/internal/block.py b/bchosttrust/internal/block.py index c6aeaa1..0575a04 100644 --- a/bchosttrust/internal/block.py +++ b/bchosttrust/internal/block.py @@ -29,6 +29,26 @@ from .. import exceptions +# Typed Dictionaries +class BCHTEntryDict(typing.TypedDict): + """Dictionary form of BCHTEntry""" + + domain_name: str + attitude: int + + +class BCHTBlockDict(typing.TypedDict): + """Dictionary form of BCHTBlock""" + + version: int + prev_hash: bytes + creation_time: int + nonce: int + entries: tuple[BCHTEntryDict, ...] + +# Object Classes + + @dataclass(frozen=True) @typechecked class BCHTEntry: @@ -194,6 +214,36 @@ def raw(self) -> bytes: return attitude_bytes + domain_name_len_bytes + domain_name_bytes + def dict(self) -> BCHTEntryDict: + """Return the dictionary form of BCHTEntry + + Returns + ------- + BCHTEntryDict + The dictionary form + """ + + return self.__dict__ + + @classmethod + def from_dict(cls, data_dict: BCHTEntryDict) -> typing.Self: + """Convert the dictionary form of BCHTEntry into object + + Parameters + ---------- + data_dict : BCHTEntryDict + The dictionary form + + Returns + ------- + BCHTEntry + The object form + """ + return cls( + domain_name=data_dict["domain_name"], + attitude=data_dict["attitude"] + ) + @dataclass(frozen=True) @typechecked @@ -210,7 +260,7 @@ class BCHTBlock: The creation time in Unix epoch. Must not exceed 18446744073709551615 nonce : int Increases on every attempt to the proof-of-work concensus. Must not exceed 4294967295 - entries : tuple[BCHTEntry] + entries : tuple[BCHTEntry, ...] A tuple of BCHTEntry objects. MAX_VERSION : int Maximum value accepted for the version field. @@ -238,7 +288,7 @@ class BCHTBlock: prev_hash: bytes # 32 bytes creation_time: int # u64, 8 bytes nonce: int # u32, 4 bytes - entries: tuple[BCHTEntry] + entries: tuple[BCHTEntry, ...] def __post_init__(self): if self.version > self.MAX_VERSION: @@ -341,3 +391,44 @@ def hexdigest(self) -> str: h = sha3_256() h.update(self.raw) return h.hexdigest() + + def dict(self) -> BCHTBlockDict: + """Return the dictionary form of BCHTBlock. + + Returns + ------- + BCHTBlockDict + The dictionary form. + """ + + return { + "version": self.version, + "prev_hash": self.prev_hash, + "creation_time": self.creation_time, + "nonce": self.nonce, + "entries": tuple(entry.dict() for entry in self.entries) + } + + @classmethod + def from_dict(cls, data_dict: BCHTBlockDict) -> typing.Self: + """Convert the dictionary form of BCHTBlock into object + + Parameters + ---------- + data_dict : BCHTBlockDict + The dictionary form + + Returns + ------- + BCHTBlock + The object form + """ + + return cls( + version=data_dict["version"], + prev_hash=data_dict["prev_hash"], + creation_time=data_dict["creation_time"], + nonce=data_dict["nonce"], + entries=tuple(BCHTEntry.from_dict(entry) + for entry in data_dict["entries"]) + ) diff --git a/tests/BCHTBlock.py b/tests/BCHTBlock.py index b8780c0..724c613 100644 --- a/tests/BCHTBlock.py +++ b/tests/BCHTBlock.py @@ -20,7 +20,11 @@ # The legal text of GPLv3 and LGPLv3 can be found at # bchosttrust/gpl-3.0.txt and bchosttrust/lgpl-3.0.txt respectively. -# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring +# pylint: disable=missing-module-docstring +# pylint: disable=invalid-name +# pylint: disable=line-too-long import unittest @@ -48,6 +52,60 @@ def test_from_raw(self): self.assertEqual(block, compare_target_block) + def testToDict(self): + entry_a = BCHTEntry("www.google.com", 2) + entry_b = BCHTEntry("www.example.net", 3) + entry_tuple = (entry_a, entry_b) + block_obj = BCHTBlock(0, b"\x00" * 32, 1, 4, entry_tuple) + block_dict = block_obj.dict() + + self.assertDictEqual(block_dict, { + "version": 0, + "prev_hash": b"\x00" * 32, + "creation_time": 1, + "nonce": 4, + "entries": ( + { + "domain_name": "www.google.com", + "attitude": 2 + }, + { + "domain_name": "www.example.net", + "attitude": 3 + }, + ) + }) + + def testFromDict(self): + block_dict = { + "version": 0, + "prev_hash": b"\x00" * 32, + "creation_time": 1, + "nonce": 4, + "entries": ( + { + "domain_name": "www.google.com", + "attitude": 2 + }, + { + "domain_name": "www.example.net", + "attitude": 3 + }, + ) + } + block_obj = BCHTBlock.from_dict(block_dict) + + self.assertEqual(block_obj.version, 0) + self.assertEqual(block_obj.prev_hash, b"\x00" * 32) + self.assertEqual(block_obj.creation_time, 1) + self.assertEqual(block_obj.nonce, 4) + + entry_a = BCHTEntry("www.google.com", 2) + entry_b = BCHTEntry("www.example.net", 3) + entry_tuple = (entry_a, entry_b) + + self.assertEqual(block_obj.entries, entry_tuple) + if __name__ == '__main__': unittest.main() diff --git a/tests/BCHTEntry.py b/tests/BCHTEntry.py index 91f6b68..1ce9fe5 100644 --- a/tests/BCHTEntry.py +++ b/tests/BCHTEntry.py @@ -20,7 +20,10 @@ # The legal text of GPLv3 and LGPLv3 can be found at # bchosttrust/gpl-3.0.txt and bchosttrust/lgpl-3.0.txt respectively. -# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring +# pylint: disable=missing-module-docstring +# pylint: disable=invalid-name import unittest from bchosttrust import BCHTEntry @@ -63,6 +66,27 @@ def testShortRawLength(self): with self.assertRaises(ValueError): BCHTEntry.from_raw(raw) + def testToDict(self): + domain_name = "www.example.com" + attitude = 1 + entry_obj = BCHTEntry(domain_name, attitude) + entry_dict = entry_obj.dict() + + self.assertEqual(entry_dict["domain_name"], domain_name) + self.assertEqual(entry_dict["attitude"], attitude) + + def testFromDict(self): + domain_name = "www.example.com" + attitude = 1 + entry_dict = { + "domain_name": domain_name, + "attitude": attitude + } + entry_obj = BCHTEntry.from_dict(entry_dict) + + self.assertEqual(entry_obj.domain_name, domain_name) + self.assertEqual(entry_obj.attitude, attitude) + if __name__ == '__main__': unittest.main() diff --git a/tests/analysis_horizontal.py b/tests/analysis_horizontal.py index 1885f06..b97a232 100644 --- a/tests/analysis_horizontal.py +++ b/tests/analysis_horizontal.py @@ -19,7 +19,10 @@ # The legal text of GPLv3 and LGPLv3 can be found at # bchosttrust/gpl-3.0.txt and bchosttrust/lgpl-3.0.txt respectively. -# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring +# pylint: disable=missing-module-docstring +# pylint: disable=invalid-name import unittest diff --git a/tests/analysis_search.py b/tests/analysis_search.py index c54a12b..81685ac 100644 --- a/tests/analysis_search.py +++ b/tests/analysis_search.py @@ -19,7 +19,10 @@ # The legal text of GPLv3 and LGPLv3 can be found at # bchosttrust/gpl-3.0.txt and bchosttrust/lgpl-3.0.txt respectively. -# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring +# pylint: disable=missing-module-docstring +# pylint: disable=invalid-name import unittest diff --git a/tests/analysis_tree.py b/tests/analysis_tree.py index 69f5ef6..715afb1 100644 --- a/tests/analysis_tree.py +++ b/tests/analysis_tree.py @@ -19,7 +19,10 @@ # The legal text of GPLv3 and LGPLv3 can be found at # bchosttrust/gpl-3.0.txt and bchosttrust/lgpl-3.0.txt respectively. -# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring +# pylint: disable=missing-module-docstring +# pylint: disable=invalid-name import unittest diff --git a/tests/consensus_limitations.py b/tests/consensus_limitations.py index c2f3c30..1400fcb 100644 --- a/tests/consensus_limitations.py +++ b/tests/consensus_limitations.py @@ -19,7 +19,10 @@ # The legal text of GPLv3 and LGPLv3 can be found at # bchosttrust/gpl-3.0.txt and bchosttrust/lgpl-3.0.txt respectively. -# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring +# pylint: disable=missing-module-docstring +# pylint: disable=invalid-name import unittest diff --git a/tests/consensus_powc.py b/tests/consensus_powc.py index 61df79d..daf925f 100644 --- a/tests/consensus_powc.py +++ b/tests/consensus_powc.py @@ -19,7 +19,10 @@ # The legal text of GPLv3 and LGPLv3 can be found at # bchosttrust/gpl-3.0.txt and bchosttrust/lgpl-3.0.txt respectively. -# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring +# pylint: disable=missing-module-docstring +# pylint: disable=invalid-name import unittest from bchosttrust.consensus import powc diff --git a/tests/import_block.py b/tests/import_block.py index 423b740..e19ca4a 100644 --- a/tests/import_block.py +++ b/tests/import_block.py @@ -19,7 +19,10 @@ # The legal text of GPLv3 and LGPLv3 can be found at # bchosttrust/gpl-3.0.txt and bchosttrust/lgpl-3.0.txt respectively. -# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring +# pylint: disable=missing-module-docstring +# pylint: disable=invalid-name import unittest diff --git a/tests/storage_dummy.py b/tests/storage_dummy.py index 7a77d3a..d5b57ff 100644 --- a/tests/storage_dummy.py +++ b/tests/storage_dummy.py @@ -20,7 +20,10 @@ # The legal text of GPLv3 and LGPLv3 can be found at # bchosttrust/gpl-3.0.txt and bchosttrust/lgpl-3.0.txt respectively. -# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring +# pylint: disable=missing-module-docstring +# pylint: disable=invalid-name import unittest diff --git a/tests/storage_leveldb.py b/tests/storage_leveldb.py index dc9e135..41e6546 100644 --- a/tests/storage_leveldb.py +++ b/tests/storage_leveldb.py @@ -20,7 +20,10 @@ # The legal text of GPLv3 and LGPLv3 can be found at # bchosttrust/gpl-3.0.txt and bchosttrust/lgpl-3.0.txt respectively. -# pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring +# pylint: disable=missing-class-docstring +# pylint: disable=missing-function-docstring +# pylint: disable=missing-module-docstring +# pylint: disable=invalid-name import unittest import tempfile