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
-
_check_for_longest_chain
() → None[source]¶ Consensus Algorithm:
Ask eachneighbour
for thatneighbours
. Add all unknown miner toneighbours
set until maximum amount of neighbours is reached.
-
_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 noBlock
object is passed.
-
_is_chain_valid
(chain: list = None) → bool[source]¶ - Checks if the given
chain
satisfies the following rules: - The first (genesis) block:
index
= 0previous_hash
= Noneproof
= None
- 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 blockproof
: 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
ifchain
is valid,False
otherwise.Return type: bool - Checks if the given
-
_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 withproof
has to bedifficulty
trailing 0s.Parameters: Returns: True
if proof of work is correct,False
otherwise.Return type: Raises: ValueError
– Will be raised ifdifficulty
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 numberp
that when hashed with the previousblock
’s solution a hash withdifficulty
trailing 0s is produced.Parameters: Returns: Solution for this proof of work quiz.
Return type: Raises: ValueError
– Will be raised ifdifficulty
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 functionmine()
.
-
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
¶
-