-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
|
||
|
||
class Validator: | ||
Check failure on line 4 in blockchain/proof_of_stake.py
|
||
""" | ||
Represents a validator in a Proof of Stake system. | ||
|
||
Check failure on line 7 in blockchain/proof_of_stake.py
|
||
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): | ||
""" | ||
Initializes a new validator with a given name and stake. | ||
|
||
Check failure on line 15 in blockchain/proof_of_stake.py
|
||
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
|
||
""" | ||
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
|
||
The higher the stake, the greater the chance to be selected. | ||
|
||
Check failure on line 28 in blockchain/proof_of_stake.py
|
||
Args: | ||
validators (List[Validator]): A list of Validator objects. | ||
|
||
Check failure on line 31 in blockchain/proof_of_stake.py
|
||
Returns: | ||
Validator: The selected validator based on weighted random selection. | ||
|
||
Check failure on line 34 in blockchain/proof_of_stake.py
|
||
Example: | ||
>>> validators = [Validator("Alice", 50), Validator("Bob", 30), Validator("Charlie", 20)] | ||
Check failure on line 36 in blockchain/proof_of_stake.py
|
||
>>> 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] |
There was a problem hiding this comment.
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: