Skip to content

Commit d770fa5

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 28df0f3 commit d770fa5

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

blockchain/proof_of_stake.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
import random
22
from typing import List
33

4+
45
class Validator:
56
"""
67
Represents a validator in a Proof of Stake system.
7-
8+
89
Attributes:
910
name (str): The name of the validator.
1011
stake (int): The amount of stake (coins) the validator holds.
1112
"""
13+
1214
def __init__(self, name: str, stake: int):
1315
"""
1416
Initializes a new validator with a given name and stake.
15-
17+
1618
Args:
1719
name (str): The name of the validator.
1820
stake (int): The amount of stake the validator has.
1921
"""
2022
self.name = name
2123
self.stake = stake
2224

25+
2326
def choose_validator(validators: List[Validator]) -> Validator:
2427
"""
2528
Selects a validator to create the next block based on the weight of their stake.
26-
29+
2730
The higher the stake, the greater the chance to be selected.
28-
31+
2932
Args:
3033
validators (List[Validator]): A list of Validator objects.
31-
34+
3235
Returns:
3336
Validator: The selected validator based on weighted random selection.
34-
37+
3538
Example:
3639
>>> validators = [Validator("Alice", 50), Validator("Bob", 30), Validator("Charlie", 20)]
3740
>>> chosen = choose_validator(validators)
@@ -40,6 +43,7 @@ def choose_validator(validators: List[Validator]) -> Validator:
4043
"""
4144
total_stake = sum(v.stake for v in validators)
4245
weighted_validators = [(v, v.stake / total_stake) for v in validators]
43-
selected = random.choices([v[0] for v in weighted_validators],
44-
weights=[v[1] for v in weighted_validators])
46+
selected = random.choices(
47+
[v[0] for v in weighted_validators], weights=[v[1] for v in weighted_validators]
48+
)
4549
return selected[0]

0 commit comments

Comments
 (0)