Skip to content

Commit a4c1df1

Browse files
divyaSree-S0pre-commit-ci[bot]cclauss
authored
New: Added proof of work(PoW) (#11946)
* Created proof_of_stake.py * Updated proof_of_stake.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changes to proof_of_stake.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Delete blockchain/proof_of_stake.py * Created and updated proof_of_stake.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated proof_of_stake.py * Created proof_of_work.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated proof_of_work.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated proof_of_work.py * Updated proof_of_work.py * Updated proof_of_work.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Delete blockchain/proof_of_work.py * Added proof_of_work.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated proof_of_work.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update proof_of_work.py * Update proof_of_work.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update proof_of_work.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updating DIRECTORY.md --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 895d125 commit a4c1df1

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎DIRECTORY.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
## [Blockchain](blockchain)
111111
* [Diophantine Equation](blockchain/diophantine_equation.py)
112112
* [Merkle Tree](blockchain/merkle_tree.py)
113+
* [Proof Of Stake](blockchain/proof_of_stake.py)
114+
* [Proof Of Work](blockchain/proof_of_work.py)
113115
* [Simple Blockchain](blockchain/simple_blockchain.py)
114116
* [Simple Proof Of Work](blockchain/simple_proof_of_work.py)
115117

‎blockchain/proof_of_stake.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import random
2+
3+
4+
class Validator:
5+
def __init__(self, name: str, stake: int) -> None:
6+
"""
7+
Initializes a new validator with a given name and stake.
8+
9+
Args:
10+
name (str): The name of the validator.
11+
stake (int): The amount of stake the validator has.
12+
"""
13+
self.name = name
14+
self.stake = stake
15+
16+
17+
def choose_validator(validators: list[Validator]) -> Validator:
18+
"""
19+
Selects a validator to create the next block based on the weight of their stake.
20+
21+
The higher the stake, the greater the chance to be selected.
22+
23+
Args:
24+
validators (list[Validator]): A list of Validator objects.
25+
26+
Returns:
27+
Validator: The selected validator based on weighted random selection.
28+
29+
Example:
30+
>>> validators = [Validator("Alice", 50), Validator("Bob", 30)]
31+
>>> chosen = choose_validator(validators)
32+
>>> isinstance(chosen, Validator)
33+
True
34+
"""
35+
total_stake = sum(v.stake for v in validators)
36+
weighted_validators = [(v, v.stake / total_stake) for v in validators]
37+
selected = random.choices(
38+
[v[0] for v in weighted_validators], weights=[v[1] for v in weighted_validators]
39+
)
40+
return selected[0]

‎blockchain/proof_of_work.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import hashlib
2+
3+
4+
def proof_of_work(difficulty: int) -> int:
5+
"""
6+
Simulates a Proof of Work mining process.
7+
8+
The miner must find a nonce such that the hash of the nonce starts
9+
with a specific number of leading zeros (difficulty).
10+
11+
Args:
12+
difficulty (int): The number of leading zeros required in the hash.
13+
14+
Returns:
15+
int: The nonce value that solves the puzzle.
16+
17+
Example:
18+
>>> result = proof_of_work(2) # Difficulty of 2 should be fast
19+
>>> isinstance(result, int)
20+
True
21+
"""
22+
prefix = "0" * difficulty
23+
nonce = 0
24+
25+
while True:
26+
hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest()
27+
if hash_result.startswith(prefix):
28+
return nonce
29+
nonce += 1

0 commit comments

Comments
 (0)