Compare commits

...

19 Commits

Author SHA1 Message Date
DIVYASREE S
cea18ff69b
Merge 8ad73811e5 into e3bd7721c8 2024-11-18 17:13:04 +05:30
DIVYASREE S
8ad73811e5
Delete blockchain/proof_of_work.py 2024-10-10 22:04:33 +05:30
pre-commit-ci[bot]
44cb9a30f7 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:33:40 +00:00
DIVYASREE S
f55f9aba89
Updated proof_of_work.py 2024-10-10 22:03:33 +05:30
DIVYASREE S
9aa315441b
Updated proof_of_work.py 2024-10-10 22:01:48 +05:30
DIVYASREE S
390cd7e12e
Updated proof_of_work.py 2024-10-10 22:00:17 +05:30
pre-commit-ci[bot]
bbe09d7c4c [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:22:37 +00:00
DIVYASREE S
5a7b2ffbba
Updated proof_of_work.py 2024-10-10 21:52:14 +05:30
pre-commit-ci[bot]
bd89e9705f [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:20:58 +00:00
DIVYASREE S
374cbc2bf9
Created proof_of_work.py 2024-10-10 21:50:01 +05:30
DIVYASREE S
e14f6d5122
Updated proof_of_stake.py 2024-10-10 21:46:12 +05:30
pre-commit-ci[bot]
8b9ff8d4f3 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:12:09 +00:00
DIVYASREE S
df4c444095
Created and updated proof_of_stake.py 2024-10-10 21:40:33 +05:30
DIVYASREE S
b1c7d37812
Delete blockchain/proof_of_stake.py 2024-10-10 21:39:40 +05:30
pre-commit-ci[bot]
2046ea45bc [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:03:51 +00:00
DIVYASREE S
82064e3a23
Changes to proof_of_stake.py 2024-10-10 21:32:04 +05:30
pre-commit-ci[bot]
d770fa5bf5 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 15:54:31 +00:00
DIVYASREE S
28df0f3c9b
Updated proof_of_stake.py 2024-10-10 21:17:13 +05:30
DIVYASREE S
f467cec98a
Created proof_of_stake.py 2024-10-10 21:12:51 +05:30

View File

@ -0,0 +1,40 @@
import random
class Validator:
def __init__(self, name: str, stake: int) -> None:
"""
Initializes a new validator with a given name and stake.
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:
"""
Selects a validator to create the next block based on the weight of their stake.
The higher the stake, the greater the chance to be selected.
Args:
validators (list[Validator]): A list of Validator objects.
Returns:
Validator: The selected validator based on weighted random selection.
Example:
>>> validators = [Validator("Alice", 50), Validator("Bob", 30)]
>>> 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]