blockchain.proof_of_work

Classes

Block

Blockchain

Functions

test_blockchain(→ None)

Test cases for the Blockchain proof of work algorithm.

Module Contents

class blockchain.proof_of_work.Block(index: int, previous_hash: str, transactions: str, timestamp: float, difficulty: int)
compute_hash() str

Generates the hash of the block content. Combines index, previous hash, transactions, timestamp, and nonce into a string, which is then hashed using SHA-256.

Returns: - str: The hash of the block.

mine_block() None

Performs Proof of Work by adjusting the nonce until a valid hash is found. A valid hash has the required number of leading zeros based on the difficulty level.

Returns: - None

difficulty
hash = ''
index
nonce = 0
previous_hash
timestamp
transactions
class blockchain.proof_of_work.Blockchain(difficulty: int)
add_block(transactions: str) None

Adds a new block to the blockchain after performing Proof of Work.

Parameters: - transactions (str): The list of transactions to be added in the new block.

Returns: - None

create_genesis_block() None

Creates the first block in the blockchain (the Genesis block).

Returns: - None

is_chain_valid() bool

Verifies the integrity of the blockchain by ensuring each block’s previous hash matches and that all blocks meet the Proof of Work requirement.

Returns: - bool: True if the blockchain is valid, False otherwise.

chain: list[Block] = []
difficulty
blockchain.proof_of_work.test_blockchain() None

Test cases for the Blockchain proof of work algorithm.

Returns: - None