-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiblock2.py
147 lines (92 loc) · 2.8 KB
/
iblock2.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import hashlib
import json
import time
reward = 10.0
time_stamp=time.ctime()
genesis_block = {
'previous_hash': '',
'time_stamp':time_stamp,
'index': 0,
'transaction': [],
'nonce': 23
}
blockchain = [genesis_block]
open_transactions = []
owner = ''
def hash_block(block):
return hashlib.sha256(json.dumps(block).encode()).hexdigest()
def valid_proof(transactions, last_hash, nonce):
guess = (str(transactions) + str(last_hash) + str(nonce)).encode()
guess_hash = hashlib.sha256(guess).hexdigest()
print(guess_hash)
return guess_hash[0:1] == '0' #condition digital signature of the block
def pow():
last_block = blockchain[-1]
last_hash = hash_block(last_block)
nonce = 0
while not valid_proof(open_transactions, last_hash, nonce):
nonce += 1
return nonce
def get_last_value():
""" extracting the last element of the blockchain list """
return(blockchain[-1])
def add_value(recipient, sender=owner, amount=1.0):
time_stamp=time.ctime()
transaction = {
'sender': sender,
'time_stamp':time_stamp,
'recipient': recipient,
'amount': amount
}
open_transactions.append(transaction)
def mine_block():
time_stamp=time.ctime()
last_block = blockchain[-1]
hashed_block = hash_block(last_block)
nonce = pow()
print(nonce)
reward_transaction = {
'sender': 'MINING',
'time_stamp':time_stamp,
'recipient': owner,
'amount': reward
}
open_transactions.append(reward_transaction)
block = {
'previous_hash': hashed_block,
'index': len(blockchain),
'time_stamp':time_stamp,
'transaction': open_transactions,
'nonce': nonce
}
blockchain.append(block)
def get_transaction_value():
tx_sender=input('Enter the sender of the transaction: ')
tx_recipient = input('Enter the recipient of the transaction: ')
tx_amount = float(input('Enter your transaction amount '))
return tx_sender,tx_recipient, tx_amount
def get_user_choice():
user_input = float(input("Please give your choice here: "))
return user_input
def print_block():
for block in blockchain:
print("Here is your block")
print(block[-1])
while True:
print("Choose an option")
print('Choose 1 for adding a new transaction')
print('Choose 2 for mining a new block')
print('Choose 3 for printing the blockchain')
print('Choose any other number if you want to quit')
user_choice = get_user_choice()
if user_choice == 1:
tx_data = get_transaction_value()
sender ,recipient, amount = tx_data
add_value(recipient, sender ,amount=amount)
print(open_transactions)
elif user_choice == 2:
mine_block()
elif user_choice == 3:
print_block()
else:
break