blockchain.client package

Submodules

blockchain.client.miner module

class blockchain.client.miner.Miner(path_to_chain: str, json_format: bool, port: int, difficulty: int, neighbours: list, force_new_chain: bool)[source]

Bases: object

_backup_local_chain() → None[source]

Periodical thread to backup the local chain to disc.

_check_for_longest_chain() → None[source]

Consensus Algorithm:

Ask each neighbour for that neighbours. Add all unknown miner to neighbours set until maximum amount of neighbours is reached.
_communicate() → None[source]

Periodical thread to communicate with server process.

_fetch_unprocessed_data() → None[source]

Periodical thread to get unprocessed data form neighbours. => Broadcasts unprocessed data around the network.

static _hash(block: blockchain.blockchain.block.Block) → str[source]

Hash a Block object with SHA-256.

Parameters:block (Block) – Object of class Block to hash.
Returns:Hex representation of block hash.
Return type:str
Raises:ValueError – Will be raised if no Block object is passed.
_is_chain_valid(chain: list = None) → bool[source]
Checks if the given chain satisfies the following rules:
  1. The first (genesis) block:
    • index = 0
    • previous_hash = None
    • proof = None
  2. each and every following block:
    • index: step size 1 and monotonically increasing (1, 2, 3, 4, …)
    • previous_hash: SHA-256 of the string representation of the preceding block
    • proof: has to be valid -> see: is_proof_of_work_valid()
    • timestamp: higher than the timestamp of of preceding block
Parameters:chain (list) – Optional chain if None internal representation is used.
Returns:True if chain is valid, False otherwise.
Return type:bool
_is_data_processed(data: blockchain.blockchain.data.Data) → bool[source]

Checks if data is already in local chain.

Parameters:data (Data) – Data object to check if it exists in the actual chain.
Returns:True if unprocessed.
Return type:bool
static _is_proof_of_work_valid(last_proof: int, proof: int, difficulty: int) → bool[source]

Checks if the proof of work was correct. The hash value of last_proof concatenated with proof has to be difficulty trailing 0s.

Parameters:
  • last_proof (int) – Value of the proof of the preceding block.
  • proof (int) – proof of the actual block.
  • difficulty (int) – Amount of trailing 0s.
Returns:

True if proof of work is correct, False otherwise.

Return type:

bool

Raises:

ValueError – Will be raised if difficulty is not a positive integer value.

_mine() → None[source]

Blocking Mining loop.

If not_processed_messages are available it uses a random message an mines a new block.

_new_message(message: str) → None[source]
Adds the new message to its local cache.
Parameters:message (str) –
_proof_of_work(last_proof: int, difficulty: int) → int[source]

Simple proof of work:

Find a number p that when hashed with the previous block’s solution a hash with difficulty trailing 0s is produced.
Parameters:
  • last_proof (int) – Solution of the last blocks’ proof of work
  • difficulty (int) – Amount of trailing 0s for a valid proof of work.
Returns:

Solution for this proof of work quiz.

Return type:

int

Raises:

ValueError – Will be raised if difficulty is not a positive integer value.

_update_neighbours() → None[source]

Periodical thread to update neighbours if limit is not exceeded.

blockchain
difficulty
jobs
neighbours
port
queue
server_process
start() → None[source]

Starts some background Job s for the Gossip Protocol, Chain syncing, Data syncing, communication thread as well as the server functionalities as process. Starts the blocking function mine().

stop() → None[source]

Function that gets called when Python was killed. Takes care to shutting down all threads/process and saves the chain to disc.

unprocessed_data

blockchain.client.server module

blockchain.client.server.start_server(queue: multiprocessing.context.BaseContext.Queue, port: int)[source]

Module contents