blockchain.simple_proof_of_work

Proof of Work (PoW) implementation. This algorithm is used in blockchain technology to reach consensus and secure the network

Attributes

example_tx

Functions

proof_of_work(→ tuple[int, str])

Finds a nonce such that the SHA-256 hash of the block starts with

Module Contents

blockchain.simple_proof_of_work.proof_of_work(block_number: int, transactions: str, previous_hash: str, difficulty: int) tuple[int, str]

Finds a nonce such that the SHA-256 hash of the block starts with a specific number of zeros (difficulty).

>>> proof_of_work(1, "test", "abc", 1)[1].startswith("0")
True
>>> # Consistency check: same input must produce same output
>>> res1 = proof_of_work(1, "data", "hash", 2)
>>> res2 = proof_of_work(1, "data", "hash", 2)
>>> res1 == res2
True
>>> # Difficulty 0 should return nonce 0 immediately
>>> proof_of_work(1, "data", "hash", 0)[0]
0
blockchain.simple_proof_of_work.example_tx = 'Alice sends 1 BTC to Bob'