-
Notifications
You must be signed in to change notification settings - Fork 0
/
A2.py
96 lines (79 loc) · 2.81 KB
/
A2.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
import json, os
import pandas as pd
from pathlib import Path
path = str(Path(__file__).parent).split('Blockchain_System')[0]
def getGenesisBlock(chain):
all_txn = []
if len(chain) < 1:
print("Blockchain is Empty\n")
else:
print(f"Genesis Block hash : {chain[0]['block_hash']}")
def showAllTxns(chain):
all_txn = []
if len(chain) < 1:
print("Blockchain is empty\n")
else:
for block in chain:
txn_list = block['transactions']
for txn in txn_list:
all_txn.append(pd.Series({'Sender' : txn['sender'], 'Receiver' : txn['receiver'], 'Amount' : txn['amount'], 'Block Id' : txn['block_id']}))
print(f"{pd.DataFrame(all_txn)}\n")
def getBlockInfo(chain, hashval):
if len(chain) < 1:
print("Blockchain is empty\n")
else:
for block in chain:
flag=0
for i in range(len(hashval)):
if block['block_hash'][i] == hashval[i]:
continue
else:
flag=1
if flag==0:
print(f"Required Block : \n{block}\n")
break
def getLastBlockHeight(chain):
if len(chain) < 1:
print("Blockchain is empty\n")
else:
last_block = chain[-1]
print(f"Height of last stored block : {last_block['height']}\n")
def getLastBlock(chain):
if len(chain) < 1:
print("Blockchain is empty\n")
else:
print(f"Most Recent stored block : {chain[-1]}\n")
def getAvgTxn(chain):
if len(chain) < 1:
print("Blockchain is empty\n")
else:
total_txn = 0
for block in chain:
total_txn += len(block['transactions'])
print(f"Average number of Transactions : {total_txn/len(chain)}\n")
def getSummary(chain):
if len(chain) < 6:
print("There is no block with Height=6\n")
else:
block = chain[6]
total_btc = 0
for txn in block['transactions']:
total_btc += txn['amount']
info = pd.Series({'Block Id' : block['id'], 'No. of Txns' : len(block['transactions']), 'Total BTC' : total_btc})
print(f"{pd.DataFrame(info)}\n")
if __name__=="__main__":
with open(os.path.join(path, 'Blockchain_System', 'Miner_0', 'blockchain.json'), 'r') as f:
chain = json.load(f)
all_miners = [i for i in os.listdir(os.path.join(path, 'Blockchain_System')) if i.startswith('Miner')]
all_txn = []
for miner in all_miners:
with open(os.path.join(path, 'Blockchain_System', miner, 'transactions.json'), 'r') as f:
all_txn += json.load(f)
getGenesisBlock(chain)
showAllTxns(chain)
hashval = input("Enter a valid hash value : ")
getBlockInfo(chain, hashval)
getLastBlockHeight(chain)
getLastBlock(chain)
getAvgTxn(chain)
getSummary(chain)