Skip to content

Commit 82064e3

Browse files
authored
Changes to proof_of_stake.py
1 parent d770fa5 commit 82064e3

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

blockchain/proof_of_stake.py

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
import random
2-
from typing import List
3-
42

53
class Validator:
6-
"""
7-
Represents a validator in a Proof of Stake system.
8-
9-
Attributes:
10-
name (str): The name of the validator.
11-
stake (int): The amount of stake (coins) the validator holds.
12-
"""
13-
14-
def __init__(self, name: str, stake: int):
4+
def __init__(self, name: str, stake: int) -> None:
155
"""
166
Initializes a new validator with a given name and stake.
177
@@ -22,15 +12,14 @@ def __init__(self, name: str, stake: int):
2212
self.name = name
2313
self.stake = stake
2414

25-
26-
def choose_validator(validators: List[Validator]) -> Validator:
15+
def choose_validator(validators: list[Validator]) -> Validator:
2716
"""
2817
Selects a validator to create the next block based on the weight of their stake.
2918
3019
The higher the stake, the greater the chance to be selected.
3120
3221
Args:
33-
validators (List[Validator]): A list of Validator objects.
22+
validators (list[Validator]): A list of Validator objects.
3423
3524
Returns:
3625
Validator: The selected validator based on weighted random selection.
@@ -43,7 +32,6 @@ def choose_validator(validators: List[Validator]) -> Validator:
4332
"""
4433
total_stake = sum(v.stake for v in validators)
4534
weighted_validators = [(v, v.stake / total_stake) for v in validators]
46-
selected = random.choices(
47-
[v[0] for v in weighted_validators], weights=[v[1] for v in weighted_validators]
48-
)
35+
selected = random.choices([v[0] for v in weighted_validators],
36+
weights=[v[1] for v in weighted_validators])
4937
return selected[0]

0 commit comments

Comments
 (0)