Compare commits

...

28 Commits

Author SHA1 Message Date
DIVYASREE S
1bb9b2fb7e
Merge 9adea0af2e into a19bede190 2024-11-01 13:50:04 +01:00
pre-commit-ci[bot]
9adea0af2e [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 17:16:47 +00:00
DIVYASREE S
9403de6894
Update proof_of_work.py 2024-10-10 22:46:12 +05:30
pre-commit-ci[bot]
5d87a84ca0 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 17:01:05 +00:00
DIVYASREE S
38904a32f7
Update proof_of_work.py 2024-10-10 22:30:29 +05:30
DIVYASREE S
1e3b382c5d
Update proof_of_work.py 2024-10-10 22:28:24 +05:30
pre-commit-ci[bot]
6ee3d9e1e6 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:50:29 +00:00
DIVYASREE S
d6ab438d90
Updated proof_of_work.py 2024-10-10 22:20:05 +05:30
pre-commit-ci[bot]
5e70415ced [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:45:32 +00:00
DIVYASREE S
cd42352303
Added proof_of_work.py 2024-10-10 22:14:01 +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
2 changed files with 69 additions and 0 deletions

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]

View File

@ -0,0 +1,29 @@
import hashlib
def proof_of_work(difficulty: int) -> int:
"""
Simulates a Proof of Work mining process.
The miner must find a nonce such that the hash of the nonce starts
with a specific number of leading zeros (difficulty).
Args:
difficulty (int): The number of leading zeros required in the hash.
Returns:
int: The nonce value that solves the puzzle.
Example:
>>> result = proof_of_work(2) # Difficulty of 2 should be fast
>>> isinstance(result, int)
True
"""
prefix = "0" * difficulty
nonce = 0
while True:
hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest()
if hash_result.startswith(prefix):
return nonce
nonce += 1