mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11: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
82064e3a23
commit
2046ea45bc
|
@ -1,5 +1,6 @@
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
class Validator:
|
class Validator:
|
||||||
def __init__(self, name: str, stake: int) -> None:
|
def __init__(self, name: str, stake: int) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -12,6 +13,7 @@ class Validator:
|
||||||
self.name = name
|
self.name = name
|
||||||
self.stake = stake
|
self.stake = stake
|
||||||
|
|
||||||
|
|
||||||
def choose_validator(validators: list[Validator]) -> Validator:
|
def choose_validator(validators: list[Validator]) -> Validator:
|
||||||
"""
|
"""
|
||||||
Selects a validator to create the next block based on the weight of their stake.
|
Selects a validator to create the next block based on the weight of their stake.
|
||||||
|
@ -32,6 +34,7 @@ def choose_validator(validators: list[Validator]) -> Validator:
|
||||||
"""
|
"""
|
||||||
total_stake = sum(v.stake for v in validators)
|
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],
|
selected = random.choices(
|
||||||
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]
|
return selected[0]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user