-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockchain.py
69 lines (56 loc) · 2.21 KB
/
blockchain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import hashlib
import time
class Transaction:
def __init__(self, sender, receiver, amount):
self.sender = sender
self.receiver = receiver
self.amount = amount
self.timestamp = time.time()
class Block:
def __init__(self, index, transactions, timestamp, previous_hash):
self.index = index
self.transactions = transactions
self.timestamp = timestamp
self.previous_hash = previous_hash
self.nonce = 0
self.hash = self.calculate_hash()
def calculate_hash(self):
block_string = f"{self.index}{self.previous_hash}{self.timestamp}{self.transactions}{self.nonce}"
return hashlib.sha256(block_string.encode()).hexdigest()
def mine_block(self, difficulty):
while self.hash[:difficulty] != '0' * difficulty:
self.nonce += 1
self.hash = self.calculate_hash()
class Blockchain:
def __init__(self, difficulty=4):
self.difficulty = difficulty
self.unconfirmed_transactions = []
self.chain = []
self.create_genesis_block()
def create_genesis_block(self):
genesis_block = Block(0, [], time.time(), "0")
genesis_block.mine_block(self.difficulty)
self.chain.append(genesis_block)
def get_last_block(self):
return self.chain[-1]
def add_transaction(self, transaction):
self.unconfirmed_transactions.append(transaction)
def mine_unconfirmed_transactions(self):
if not self.unconfirmed_transactions:
return False
last_block = self.get_last_block()
new_block = Block(last_block.index + 1, self.unconfirmed_transactions, time.time(), last_block.hash)
new_block.mine_block(self.difficulty)
self.chain.append(new_block)
self.unconfirmed_transactions = []
return True
def is_chain_valid(self):
for i in range(1, len(self.chain)):
current = self.chain[i]
previous = self.chain[i - 1]
if current.hash != current.calculate_hash():
return False
if current.previous_hash != previous.hash:
return False
return True
# Additional methods can be added here for more functionalities