DEV Community

Discussion on: Creating a blockchain in 60 lines of Python

theflanman profile image
Connor Flanigan

The consensus we've reached at work has mostly been that typing should really only be bothered to help users and devs where it's not clear what expected input and output is. Things like __init__ methods or other dunder methods are expected to return a certain type of object so they don't need it.

What I might recommend is typing your inputs a little more. As an example, explicitly use ints to represent timestamps, and use the function time.time_ns() for timestamps, then use structs to generate more accurate representations to hash.

class Block:

    def __init__(self, data: Optional[List[Any]] = None, timestamp: Optional[float]=None, prevHash: Optional[bytes] = None):
        Create a new block for the Blockchain

        :param timestamp: Timestamp of the block, defaults to the time the block object is created
        :param data: Data to store in the block, defaults to an empty list
        :param prevHash: Hash of the previous block, defaults to None.  Should always be specefied except for the genesis block.
        if datais None:

        if timestamp is None:
            self.timestamp = time_ns()
            self.timestamp = timestamp

        if prevHashis None:
            self.prevHash= b''
            self.prevHash= prevHash

        self._hash = None
        self.nonce = 0

    def hash(self) -> bytes:
        Return the (non-python) hash of the block

        :return: The bytes of the hash of this block
        if self._hash is None:
            hashFun = sha256()
            hashFun.update(struct.pack('@l', self.timestamp))
            hashFun.update(struct.pack('@l', self.nonce))
            self._hash = hashFun.hexdigest()
        return self._hash

Enter fullscreen mode Exit fullscreen mode

Here I've specified that data is a list of anything, timestamp is an int, and prevHash is bytes, and that Block.hash generates bytes. This informs a user/developer that's interfacing with it that this is what the methods are expecting to take or should be expected to return. The only thing I might also add for type hinting in the Block class is specifying the difficulty parameter in Block.mine() is an integer, as well as a check that it's greater than or equal to 1, raising an exception if it isn't.