Skip to content

Commit

Permalink
dict() and from_dict() for entries and blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Emojigit committed Dec 26, 2023
1 parent 3585caa commit 6d84fda
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 12 deletions.
95 changes: 93 additions & 2 deletions bchosttrust/internal/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"])
)
60 changes: 59 additions & 1 deletion tests/BCHTBlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
26 changes: 25 additions & 1 deletion tests/BCHTEntry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
5 changes: 4 additions & 1 deletion tests/analysis_horizontal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion tests/analysis_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion tests/analysis_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion tests/consensus_limitations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion tests/consensus_powc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tests/import_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion tests/storage_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion tests/storage_leveldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6d84fda

Please sign in to comment.