mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
df4c444095
commit
8b9ff8d4f3
|
@ -1,5 +1,6 @@
|
|||
import random
|
||||
|
||||
|
||||
class Validator:
|
||||
def __init__(self, name: str, stake: int) -> None:
|
||||
"""
|
||||
|
@ -12,6 +13,7 @@ class Validator:
|
|||
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.
|
||||
|
@ -31,11 +33,8 @@ def choose_validator(validators: list[Validator]) -> Validator:
|
|||
True
|
||||
"""
|
||||
total_stake = sum(v.stake for v in validators)
|
||||
weighted_validators = [
|
||||
(v, v.stake / total_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]
|
||||
[v[0] for v in weighted_validators], weights=[v[1] for v in weighted_validators]
|
||||
)
|
||||
return selected[0]
|
||||
|
|
Loading…
Reference in New Issue
Block a user