Skip to content

Commit 2cf71e3

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 33474e4 commit 2cf71e3

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

blockchain/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ So this is all about introduction to blockchain technology. To learn more about
4545
* <https://www.geeksforgeeks.org/modular-division/>
4646

4747
## about blockchain_main file:
48-
This python file contains the code for basic implementaion of blockchain in python with proof of work algorithm.
48+
This python file contains the code for basic implementaion of blockchain in python with proof of work algorithm.

blockchain/blockchain_main.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
from time import time
44
from uuid import uuid4
55

6+
67
class Blockchain:
78
def __init__(self):
89
# Initialize blockchain as an empty list and pending transactions
910
self.chain = []
1011
self.current_transactions = []
1112

1213
# Create the genesis block
13-
self.new_block(previous_hash='1', proof=100)
14+
self.new_block(previous_hash="1", proof=100)
1415

1516
def new_block(self, proof, previous_hash=None):
1617
"""
@@ -20,11 +21,11 @@ def new_block(self, proof, previous_hash=None):
2021
:return: New Block
2122
"""
2223
block = {
23-
'index': len(self.chain) + 1,
24-
'timestamp': time(),
25-
'transactions': self.current_transactions,
26-
'proof': proof,
27-
'previous_hash': previous_hash or self.hash(self.chain[-1])
24+
"index": len(self.chain) + 1,
25+
"timestamp": time(),
26+
"transactions": self.current_transactions,
27+
"proof": proof,
28+
"previous_hash": previous_hash or self.hash(self.chain[-1]),
2829
}
2930

3031
# Reset the current list of transactions
@@ -41,13 +42,11 @@ def new_transaction(self, sender, recipient, amount):
4142
:param amount: Amount of the transaction
4243
:return: The index of the block that will hold this transaction
4344
"""
44-
self.current_transactions.append({
45-
'sender': sender,
46-
'recipient': recipient,
47-
'amount': amount
48-
})
45+
self.current_transactions.append(
46+
{"sender": sender, "recipient": recipient, "amount": amount}
47+
)
4948

50-
return self.last_block['index'] + 1
49+
return self.last_block["index"] + 1
5150

5251
@staticmethod
5352
def hash(block):
@@ -79,12 +78,12 @@ def proof_of_work(self, last_proof):
7978
proof = 0
8079
while self.valid_proof(last_proof, proof) is False:
8180
proof += 1
82-
81+
8382
# Add the mining reward here
8483
self.new_transaction(
8584
sender="0", # System or network
8685
recipient="Miner1", # Replace with the miner's address
87-
amount=1 # Reward amount
86+
amount=1, # Reward amount
8887
)
8988
return proof
9089

@@ -96,16 +95,17 @@ def valid_proof(last_proof, proof):
9695
:param proof: Current proof
9796
:return: True if correct, False if not.
9897
"""
99-
guess = f'{last_proof}{proof}'.encode()
98+
guess = f"{last_proof}{proof}".encode()
10099
guess_hash = hashlib.sha256(guess).hexdigest()
101100
return guess_hash[:4] == "0000"
102101

102+
103103
# Create a new blockchain instance
104104
blockchain = Blockchain()
105105

106106
# Example usage: Add a new transaction and mine a new block
107107
blockchain.new_transaction(sender="Alice", recipient="Bob", amount=50)
108-
last_proof = blockchain.last_block['proof']
108+
last_proof = blockchain.last_block["proof"]
109109
proof = blockchain.proof_of_work(last_proof)
110110
block = blockchain.new_block(proof)
111111

0 commit comments

Comments
 (0)