Skip to content

New: Added proof of work(PoW) #11946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f467cec
Created proof_of_stake.py
divyaSree-S0 Oct 10, 2024
28df0f3
Updated proof_of_stake.py
divyaSree-S0 Oct 10, 2024
d770fa5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
82064e3
Changes to proof_of_stake.py
divyaSree-S0 Oct 10, 2024
2046ea4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
b1c7d37
Delete blockchain/proof_of_stake.py
divyaSree-S0 Oct 10, 2024
df4c444
Created and updated proof_of_stake.py
divyaSree-S0 Oct 10, 2024
8b9ff8d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
e14f6d5
Updated proof_of_stake.py
divyaSree-S0 Oct 10, 2024
374cbc2
Created proof_of_work.py
divyaSree-S0 Oct 10, 2024
bd89e97
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
5a7b2ff
Updated proof_of_work.py
divyaSree-S0 Oct 10, 2024
bbe09d7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
390cd7e
Updated proof_of_work.py
divyaSree-S0 Oct 10, 2024
9aa3154
Updated proof_of_work.py
divyaSree-S0 Oct 10, 2024
f55f9ab
Updated proof_of_work.py
divyaSree-S0 Oct 10, 2024
44cb9a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
8ad7381
Delete blockchain/proof_of_work.py
divyaSree-S0 Oct 10, 2024
cd42352
Added proof_of_work.py
divyaSree-S0 Oct 10, 2024
5e70415
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
d6ab438
Updated proof_of_work.py
divyaSree-S0 Oct 10, 2024
6ee3d9e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
1e3b382
Update proof_of_work.py
divyaSree-S0 Oct 10, 2024
38904a3
Update proof_of_work.py
divyaSree-S0 Oct 10, 2024
5d87a84
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
9403de6
Update proof_of_work.py
divyaSree-S0 Oct 10, 2024
9adea0a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions blockchain/proof_of_stake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import random


class Validator:
def __init__(self, name: str, stake: int) -> None:
"""
Initializes a new validator with a given name and stake.

Args:
name (str): The name of the validator.
stake (int): The amount of stake the validator has.
"""
self.name = name
self.stake = stake


def choose_validator(validators: list[Validator]) -> Validator:
"""
Selects a validator to create the next block based on the weight of their stake.

The higher the stake, the greater the chance to be selected.

Args:
validators (list[Validator]): A list of Validator objects.

Returns:
Validator: The selected validator based on weighted random selection.

Example:
>>> validators = [Validator("Alice", 50), Validator("Bob", 30)]
>>> chosen = choose_validator(validators)
>>> isinstance(chosen, Validator)
True
"""
total_stake = sum(v.stake for v in validators)
weighted_validators = [(v, v.stake / total_stake) for v in validators]
selected = random.choices(
[v[0] for v in weighted_validators], weights=[v[1] for v in weighted_validators]
)
return selected[0]
29 changes: 29 additions & 0 deletions blockchain/proof_of_work.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import hashlib


def proof_of_work(difficulty: int) -> int:
"""
Simulates a Proof of Work mining process.

The miner must find a nonce such that the hash of the nonce starts
with a specific number of leading zeros (difficulty).

Args:
difficulty (int): The number of leading zeros required in the hash.

Returns:
int: The nonce value that solves the puzzle.

Example:
>>> result = proof_of_work(2) # Difficulty of 2 should be fast
>>> isinstance(result, int)
True
"""
prefix = "0" * difficulty
nonce = 0

while True:
hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest()
if hash_result.startswith(prefix):
return nonce
nonce += 1