Skip to content

Added Proof of Stake(PoS) algorithm #11943

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

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
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
45 changes: 45 additions & 0 deletions blockchain/proof_of_stake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import random
from typing import List

Check failure on line 2 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

blockchain/proof_of_stake.py:2:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 2 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

blockchain/proof_of_stake.py:2:1: UP035 `typing.List` is deprecated, use `list` instead

class Validator:

Check failure on line 4 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

blockchain/proof_of_stake.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 4 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

blockchain/proof_of_stake.py:1:1: I001 Import block is un-sorted or un-formatted
"""
Represents a validator in a Proof of Stake system.

Check failure on line 7 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:7:1: W293 Blank line contains whitespace

Check failure on line 7 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:7:1: W293 Blank line contains whitespace
Attributes:
name (str): The name of the validator.
stake (int): The amount of stake (coins) the validator holds.
"""
def __init__(self, name: str, stake: int):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

"""
Initializes a new validator with a given name and stake.

Check failure on line 15 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:15:1: W293 Blank line contains whitespace

Check failure on line 15 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:15:1: W293 Blank line contains whitespace
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:

Check failure on line 23 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

blockchain/proof_of_stake.py:23:34: UP006 Use `list` instead of `List` for type annotation

Check failure on line 23 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

blockchain/proof_of_stake.py:23:34: UP006 Use `list` instead of `List` for type annotation
"""
Selects a validator to create the next block based on the weight of their stake.

Check failure on line 26 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:26:1: W293 Blank line contains whitespace

Check failure on line 26 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:26:1: W293 Blank line contains whitespace
The higher the stake, the greater the chance to be selected.

Check failure on line 28 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:28:1: W293 Blank line contains whitespace

Check failure on line 28 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:28:1: W293 Blank line contains whitespace
Args:
validators (List[Validator]): A list of Validator objects.

Check failure on line 31 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:31:1: W293 Blank line contains whitespace

Check failure on line 31 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:31:1: W293 Blank line contains whitespace
Returns:
Validator: The selected validator based on weighted random selection.

Check failure on line 34 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:34:1: W293 Blank line contains whitespace

Check failure on line 34 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_stake.py:34:1: W293 Blank line contains whitespace
Example:
>>> validators = [Validator("Alice", 50), Validator("Bob", 30), Validator("Charlie", 20)]

Check failure on line 36 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

blockchain/proof_of_stake.py:36:89: E501 Line too long (97 > 88)

Check failure on line 36 in blockchain/proof_of_stake.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

blockchain/proof_of_stake.py:36:89: E501 Line too long (97 > 88)
>>> 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]