From f467cec98a1678f7eabd99d1f8f5fc8df5fb9578 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 21:12:51 +0530 Subject: [PATCH 01/27] Created proof_of_stake.py --- blockchain/proof_of_stake.py | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 blockchain/proof_of_stake.py diff --git a/blockchain/proof_of_stake.py b/blockchain/proof_of_stake.py new file mode 100644 index 000000000..8cc451112 --- /dev/null +++ b/blockchain/proof_of_stake.py @@ -0,0 +1,50 @@ +import random + +class Validator: + """ + Represents a validator in a Proof of Stake system. + + Attributes: + name (str): The name of the validator. + stake (int): The amount of stake (coins) the validator holds. + """ + def __init__(self, name, stake): + """ + 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 # Stake defines how much weight the validator has. + +def choose_validator(validators): + """ + 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): A list of Validator objects. + + Returns: + Validator: The selected validator based on weighted random selection. + """ + # Total stake of all validators + total_stake = sum(v.stake for v in validators) + + # Create a list of validators with weights (probability of being chosen) + weighted_validators = [(v, v.stake / total_stake) for v in validators] + + # Randomly select a validator based on their stake weight + selected = random.choices([v[0] for v in weighted_validators], + weights=[v[1] for v in weighted_validators]) + return selected[0] + +# Example of validators with different stakes +validators = [Validator("Alice", 50), Validator("Bob", 30), Validator("Charlie", 20)] + +# Select a validator based on their stake +chosen_validator = choose_validator(validators) +print(f"Chosen validator: {chosen_validator.name}") From 28df0f3c9b0af3b5fd1b7737a575668c3ad786e2 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 21:17:13 +0530 Subject: [PATCH 02/27] Updated proof_of_stake.py --- blockchain/proof_of_stake.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/blockchain/proof_of_stake.py b/blockchain/proof_of_stake.py index 8cc451112..946d7218e 100644 --- a/blockchain/proof_of_stake.py +++ b/blockchain/proof_of_stake.py @@ -1,14 +1,15 @@ import random +from typing import List class Validator: """ Represents a validator in a Proof of Stake system. - + Attributes: name (str): The name of the validator. stake (int): The amount of stake (coins) the validator holds. """ - def __init__(self, name, stake): + def __init__(self, name: str, stake: int): """ Initializes a new validator with a given name and stake. @@ -17,34 +18,28 @@ class Validator: stake (int): The amount of stake the validator has. """ self.name = name - self.stake = stake # Stake defines how much weight the validator has. + self.stake = stake -def choose_validator(validators): +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): A list of Validator objects. + 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), Validator("Charlie", 20)] + >>> chosen = choose_validator(validators) + >>> isinstance(chosen, Validator) + True """ - # Total stake of all validators total_stake = sum(v.stake for v in validators) - - # Create a list of validators with weights (probability of being chosen) weighted_validators = [(v, v.stake / total_stake) for v in validators] - - # Randomly select a validator based on their stake weight selected = random.choices([v[0] for v in weighted_validators], weights=[v[1] for v in weighted_validators]) return selected[0] - -# Example of validators with different stakes -validators = [Validator("Alice", 50), Validator("Bob", 30), Validator("Charlie", 20)] - -# Select a validator based on their stake -chosen_validator = choose_validator(validators) -print(f"Chosen validator: {chosen_validator.name}") From d770fa5bf55a8cfeb632301fdc3d09ad484efa36 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 15:54:30 +0000 Subject: [PATCH 03/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/proof_of_stake.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/blockchain/proof_of_stake.py b/blockchain/proof_of_stake.py index 946d7218e..78dc45c6f 100644 --- a/blockchain/proof_of_stake.py +++ b/blockchain/proof_of_stake.py @@ -1,18 +1,20 @@ import random from typing import List + class Validator: """ Represents a validator in a Proof of Stake system. - + Attributes: name (str): The name of the validator. stake (int): The amount of stake (coins) the validator holds. """ + def __init__(self, name: str, stake: int): """ 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. @@ -20,18 +22,19 @@ 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. - + 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), Validator("Charlie", 20)] >>> chosen = choose_validator(validators) @@ -40,6 +43,7 @@ def choose_validator(validators: List[Validator]) -> Validator: """ 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]) + selected = random.choices( + [v[0] for v in weighted_validators], weights=[v[1] for v in weighted_validators] + ) return selected[0] From 82064e3a23bbb1fd7dcb115ae255bbc011de17cf Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 21:32:04 +0530 Subject: [PATCH 04/27] Changes to proof_of_stake.py --- blockchain/proof_of_stake.py | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/blockchain/proof_of_stake.py b/blockchain/proof_of_stake.py index 78dc45c6f..1cb29ce1e 100644 --- a/blockchain/proof_of_stake.py +++ b/blockchain/proof_of_stake.py @@ -1,17 +1,7 @@ import random -from typing import List - class Validator: - """ - Represents a validator in a Proof of Stake system. - - Attributes: - name (str): The name of the validator. - stake (int): The amount of stake (coins) the validator holds. - """ - - def __init__(self, name: str, stake: int): + def __init__(self, name: str, stake: int) -> None: """ Initializes a new validator with a given name and stake. @@ -22,15 +12,14 @@ class Validator: self.name = name 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. The higher the stake, the greater the chance to be selected. Args: - validators (List[Validator]): A list of Validator objects. + validators (list[Validator]): A list of Validator objects. Returns: Validator: The selected validator based on weighted random selection. @@ -43,7 +32,6 @@ def choose_validator(validators: List[Validator]) -> Validator: """ 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] - ) + selected = random.choices([v[0] for v in weighted_validators], + weights=[v[1] for v in weighted_validators]) return selected[0] From 2046ea45bcc55f7e76822984f0a3288ecc546a5b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 16:03:50 +0000 Subject: [PATCH 05/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/proof_of_stake.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/blockchain/proof_of_stake.py b/blockchain/proof_of_stake.py index 1cb29ce1e..b988bfe2f 100644 --- a/blockchain/proof_of_stake.py +++ b/blockchain/proof_of_stake.py @@ -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. @@ -32,6 +34,7 @@ def choose_validator(validators: list[Validator]) -> Validator: """ 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]) + selected = random.choices( + [v[0] for v in weighted_validators], weights=[v[1] for v in weighted_validators] + ) return selected[0] From b1c7d37812fb6b6ce36d8ab04d9fe684e7ec8690 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 21:39:40 +0530 Subject: [PATCH 06/27] Delete blockchain/proof_of_stake.py --- blockchain/proof_of_stake.py | 40 ------------------------------------ 1 file changed, 40 deletions(-) delete mode 100644 blockchain/proof_of_stake.py diff --git a/blockchain/proof_of_stake.py b/blockchain/proof_of_stake.py deleted file mode 100644 index b988bfe2f..000000000 --- a/blockchain/proof_of_stake.py +++ /dev/null @@ -1,40 +0,0 @@ -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), Validator("Charlie", 20)] - >>> 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] From df4c4440958f930b064fd4b10d2a882c4281bb90 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 21:40:33 +0530 Subject: [PATCH 07/27] Created and updated proof_of_stake.py --- blockchain/proof_of_stake.py | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 blockchain/proof_of_stake.py diff --git a/blockchain/proof_of_stake.py b/blockchain/proof_of_stake.py new file mode 100644 index 000000000..568824921 --- /dev/null +++ b/blockchain/proof_of_stake.py @@ -0,0 +1,41 @@ +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), Validator("Charlie", 20)] + >>> 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] From 8b9ff8d4f358562e5d2677542e00428cd5591c5a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 16:12:08 +0000 Subject: [PATCH 08/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/proof_of_stake.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/blockchain/proof_of_stake.py b/blockchain/proof_of_stake.py index 568824921..b988bfe2f 100644 --- a/blockchain/proof_of_stake.py +++ b/blockchain/proof_of_stake.py @@ -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] From e14f6d5122a0d08332c188db4b4274c5ca20ab60 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 21:46:12 +0530 Subject: [PATCH 09/27] Updated proof_of_stake.py --- blockchain/proof_of_stake.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain/proof_of_stake.py b/blockchain/proof_of_stake.py index b988bfe2f..7eea9ded2 100644 --- a/blockchain/proof_of_stake.py +++ b/blockchain/proof_of_stake.py @@ -27,7 +27,7 @@ def choose_validator(validators: list[Validator]) -> Validator: Validator: The selected validator based on weighted random selection. Example: - >>> validators = [Validator("Alice", 50), Validator("Bob", 30), Validator("Charlie", 20)] + >>> validators = [Validator("Alice", 50), Validator("Bob", 30)] >>> chosen = choose_validator(validators) >>> isinstance(chosen, Validator) True From 374cbc2bf99406315c40e02f9dc3f7ecf6699f5e Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 21:50:01 +0530 Subject: [PATCH 10/27] Created proof_of_work.py --- blockchain/proof_of_work.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 blockchain/proof_of_work.py diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py new file mode 100644 index 000000000..3055eaf67 --- /dev/null +++ b/blockchain/proof_of_work.py @@ -0,0 +1,32 @@ +import hashlib +import time + +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 + start = time.time() + + while True: + hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest() + if hash_result.startswith(prefix): + end = time.time() + print(f"Nonce: {nonce}, Hash: {hash_result}, Time: {end - start:.2f}s") + return nonce + nonce += 1 From bd89e9705f2830f04231b63bc61c294a1173a4d7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 16:20:57 +0000 Subject: [PATCH 11/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/proof_of_work.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 3055eaf67..425050b1d 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -1,11 +1,12 @@ import hashlib import time + 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 + The miner must find a nonce such that the hash of the nonce starts with a specific number of leading zeros (difficulty). Args: @@ -19,10 +20,10 @@ def proof_of_work(difficulty: int) -> int: >>> isinstance(result, int) True """ - prefix = '0' * difficulty + prefix = "0" * difficulty nonce = 0 start = time.time() - + while True: hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest() if hash_result.startswith(prefix): From 5a7b2ffbbadb499bbd8eda75341a9eff6272b631 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 21:52:14 +0530 Subject: [PATCH 12/27] Updated proof_of_work.py --- blockchain/proof_of_work.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 425050b1d..12f2cec5a 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -6,7 +6,7 @@ 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 + The miner must find a nonce such that the hash of the nonce starts with a specific number of leading zeros (difficulty). Args: @@ -20,7 +20,7 @@ def proof_of_work(difficulty: int) -> int: >>> isinstance(result, int) True """ - prefix = "0" * difficulty + prefix = '0' * difficulty nonce = 0 start = time.time() From bbe09d7c4cbb957450ad486bec37bb91e4b04f4f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 16:22:36 +0000 Subject: [PATCH 13/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/proof_of_work.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 12f2cec5a..425050b1d 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -6,7 +6,7 @@ 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 + The miner must find a nonce such that the hash of the nonce starts with a specific number of leading zeros (difficulty). Args: @@ -20,7 +20,7 @@ def proof_of_work(difficulty: int) -> int: >>> isinstance(result, int) True """ - prefix = '0' * difficulty + prefix = "0" * difficulty nonce = 0 start = time.time() From 390cd7e12e60f10db481825234075bcb9af83a40 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 22:00:17 +0530 Subject: [PATCH 14/27] Updated proof_of_work.py --- blockchain/proof_of_work.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 425050b1d..4bb61ea03 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -6,7 +6,7 @@ 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 + The miner must find a nonce such that the hash of the nonce starts with a specific number of leading zeros (difficulty). Args: @@ -20,7 +20,7 @@ def proof_of_work(difficulty: int) -> int: >>> isinstance(result, int) True """ - prefix = "0" * difficulty + prefix = '0' * difficulty nonce = 0 start = time.time() @@ -28,6 +28,6 @@ def proof_of_work(difficulty: int) -> int: hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest() if hash_result.startswith(prefix): end = time.time() - print(f"Nonce: {nonce}, Hash: {hash_result}, Time: {end - start:.2f}s") + # Removed the print statement return nonce nonce += 1 From 9aa315441bcc6dc35c04cd822ad4140b3780e049 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 22:01:48 +0530 Subject: [PATCH 15/27] Updated proof_of_work.py --- blockchain/proof_of_work.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 4bb61ea03..94cfe765a 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -22,12 +22,12 @@ def proof_of_work(difficulty: int) -> int: """ prefix = '0' * difficulty nonce = 0 - start = time.time() + start = time.time() # Timing starts while True: hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest() if hash_result.startswith(prefix): - end = time.time() - # Removed the print statement + end = time.time() # Timing ends + print(f"Time taken: {end - start:.2f}s") # Print time taken return nonce nonce += 1 From 44cb9a30f7aa1a4def95b961c46b92f859155647 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 16:33:39 +0000 Subject: [PATCH 17/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/proof_of_work.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 94cfe765a..df4bdbd86 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -6,7 +6,7 @@ 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 + The miner must find a nonce such that the hash of the nonce starts with a specific number of leading zeros (difficulty). Args: @@ -20,7 +20,7 @@ def proof_of_work(difficulty: int) -> int: >>> isinstance(result, int) True """ - prefix = '0' * difficulty + prefix = "0" * difficulty nonce = 0 start = time.time() # Timing starts From 8ad73811e5c54930fcb2881a3bec69f139347fdc Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 22:04:33 +0530 Subject: [PATCH 18/27] Delete blockchain/proof_of_work.py --- blockchain/proof_of_work.py | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 blockchain/proof_of_work.py diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py deleted file mode 100644 index df4bdbd86..000000000 --- a/blockchain/proof_of_work.py +++ /dev/null @@ -1,33 +0,0 @@ -import hashlib -import time - - -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 - start = time.time() # Timing starts - - while True: - hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest() - if hash_result.startswith(prefix): - end = time.time() # Timing ends - print(f"Time taken: {end - start:.2f}s") # Print time taken - return nonce - nonce += 1 From cd42352303043d30bc462961036e7d9056860ee1 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 22:14:01 +0530 Subject: [PATCH 19/27] Added proof_of_work.py --- blockchain/proof_of_work.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 blockchain/proof_of_work.py diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py new file mode 100644 index 000000000..4bf14106b --- /dev/null +++ b/blockchain/proof_of_work.py @@ -0,0 +1,31 @@ +import hashlib +import time + +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 + start = time.time() # Timing starts + + while True: + hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest() + if hash_result.startswith(prefix): + end = time.time() # Timing ends + print(f"Time taken: {end - start:.2f}s") # Print time taken + return nonce + nonce += 1 From 5e70415cedb7abd364e8603ae6f4fbd7c8a44fe7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 16:45:31 +0000 Subject: [PATCH 20/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/proof_of_work.py | 55 +++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 4bf14106b..f0e8c3bfa 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -1,31 +1,32 @@ import hashlib import time -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 - start = time.time() # Timing starts - while True: - hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest() - if hash_result.startswith(prefix): - end = time.time() # Timing ends - print(f"Time taken: {end - start:.2f}s") # Print time taken - return nonce - nonce += 1 +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 + start = time.time() # Timing starts + + while True: + hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest() + if hash_result.startswith(prefix): + end = time.time() # Timing ends + print(f"Time taken: {end - start:.2f}s") # Print time taken + return nonce + nonce += 1 From d6ab438d902dbfea406a957dc304b4fd4bb42e21 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 22:20:05 +0530 Subject: [PATCH 21/27] Updated proof_of_work.py --- blockchain/proof_of_work.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index f0e8c3bfa..84e918656 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -1,25 +1,25 @@ import hashlib import time - 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) + 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. + difficulty (int): The number of leading zeros required in the hash. + Returns: - int: The nonce value that solves the puzzle. + int: The nonce value that solves the puzzle. Example: - >>> result = proof_of_work(2) # Difficulty of 2 should be fast - >>> isinstance(result, int) - True + >>> result = proof_of_work(2) # Difficulty of 2 should be fast + >>> isinstance(result, int) + True """ - prefix = "0" * difficulty + prefix = '0' * difficulty nonce = 0 start = time.time() # Timing starts From 6ee3d9e1e6fea02b119d7c3ed7cb1d34f9940c71 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 16:50:28 +0000 Subject: [PATCH 22/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/proof_of_work.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 84e918656..df4bdbd86 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -1,11 +1,12 @@ import hashlib import time + 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 + The miner must find a nonce such that the hash of the nonce starts with a specific number of leading zeros (difficulty). Args: @@ -19,7 +20,7 @@ def proof_of_work(difficulty: int) -> int: >>> isinstance(result, int) True """ - prefix = '0' * difficulty + prefix = "0" * difficulty nonce = 0 start = time.time() # Timing starts From 1e3b382c5d13fb443f13303287975019d4dfaf8d Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 22:28:24 +0530 Subject: [PATCH 23/27] Update proof_of_work.py --- blockchain/proof_of_work.py | 1 - 1 file changed, 1 deletion(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index df4bdbd86..2aa1e8c5a 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -28,6 +28,5 @@ def proof_of_work(difficulty: int) -> int: hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest() if hash_result.startswith(prefix): end = time.time() # Timing ends - print(f"Time taken: {end - start:.2f}s") # Print time taken return nonce nonce += 1 From 38904a32f7cc512e447989830d283c9cf2d2d7c9 Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 22:30:29 +0530 Subject: [PATCH 24/27] Update proof_of_work.py --- blockchain/proof_of_work.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 2aa1e8c5a..d273b94d6 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -6,7 +6,7 @@ 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 + The miner must find a nonce such that the hash of the nonce starts with a specific number of leading zeros (difficulty). Args: @@ -20,7 +20,7 @@ def proof_of_work(difficulty: int) -> int: >>> isinstance(result, int) True """ - prefix = "0" * difficulty + prefix = '0' * difficulty nonce = 0 start = time.time() # Timing starts From 5d87a84ca076af531188d685bc276d6e1c4ac1f3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:01:04 +0000 Subject: [PATCH 25/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/proof_of_work.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index d273b94d6..2aa1e8c5a 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -6,7 +6,7 @@ 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 + The miner must find a nonce such that the hash of the nonce starts with a specific number of leading zeros (difficulty). Args: @@ -20,7 +20,7 @@ def proof_of_work(difficulty: int) -> int: >>> isinstance(result, int) True """ - prefix = '0' * difficulty + prefix = "0" * difficulty nonce = 0 start = time.time() # Timing starts From 9403de6894bee8e1cefd78ee87e8ea537a8bb8ae Mon Sep 17 00:00:00 2001 From: DIVYASREE S Date: Thu, 10 Oct 2024 22:46:12 +0530 Subject: [PATCH 26/27] Update proof_of_work.py --- blockchain/proof_of_work.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 2aa1e8c5a..64ad96efa 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -1,12 +1,11 @@ import hashlib -import time 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 + The miner must find a nonce such that the hash of the nonce starts with a specific number of leading zeros (difficulty). Args: @@ -20,13 +19,11 @@ def proof_of_work(difficulty: int) -> int: >>> isinstance(result, int) True """ - prefix = "0" * difficulty + prefix = '0' * difficulty nonce = 0 - start = time.time() # Timing starts while True: hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest() if hash_result.startswith(prefix): - end = time.time() # Timing ends return nonce nonce += 1 From 9adea0af2ec1e9e030c0161f876788af43ac1880 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:16:46 +0000 Subject: [PATCH 27/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/proof_of_work.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain/proof_of_work.py b/blockchain/proof_of_work.py index 64ad96efa..66d50d752 100644 --- a/blockchain/proof_of_work.py +++ b/blockchain/proof_of_work.py @@ -5,7 +5,7 @@ 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 + The miner must find a nonce such that the hash of the nonce starts with a specific number of leading zeros (difficulty). Args: @@ -19,7 +19,7 @@ def proof_of_work(difficulty: int) -> int: >>> isinstance(result, int) True """ - prefix = '0' * difficulty + prefix = "0" * difficulty nonce = 0 while True: