-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.py
94 lines (76 loc) · 2.7 KB
/
block.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
from time import time
from flask import Flask, jsonify, request
import requests
import json
import threading
import data
import utilities
import transaction
class block:
def __init__(self,index, timestamp, transactions,nonce, current_hash=None, previous_hash=None):
self.index=index
self.timestamp=timestamp
self.transactions=transactions
self.nonce=nonce
self.current_hash=current_hash
self.previous_hash=previous_hash
def hash(self):
transactionsAsList=[]
for trans in self.transactions:
transactionsAsList.append(trans.asDictionary())
tempDict={
"index":self.index,
"transactions":transactionsAsList,
"timestamp":self.timestamp,
"previous_hash":self.previous_hash,
"nonce":self.nonce
}
asString=json.dumps(tempDict,sort_keys=True)
hash = utilities.hashStringToString(asString)
return hash
def asDictionary(self):
transactionsAsList=[]
for trans in self.transactions:
transactionsAsList.append(trans.asDictionary())
tempDict={
"index":self.index,
"timestamp":self.timestamp,
"transactions":transactionsAsList,
"nonce":self.nonce,
"current_hash":self.current_hash,
"previous_hash":self.previous_hash,
}
return tempDict
'''
def new_block(self, proof, previous_hash,transactions):
block = {
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': transactions,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.chain[-1]),
'current_hash':"string"
}
block['current_hash']=self.hash(block)
# Reset the current list of transactions
if(len(self.chain)==0 and data.myPort==data.adminPort):
self.chain.append(block)
return block
response=requests.get("http://localhost:"+str(data.myPort)+"/nodes/resolve")
#self.current_transactions = []
if(response.status_code==200):
self.chain.append(block)#mpainei sthn lista mas
broadcast.broadcast_a_block(block,self)
return block
'''
def createBlockFromDictionary(b):
temp_trans_list=[]
for trans_dict in b['transactions']:
temp_trans_list.append(transaction.createTranasactionFromDictionary(trans_dict))
tempBlock= block(b['index'], b['timestamp'],temp_trans_list ,b['nonce'], b['current_hash'], b['previous_hash'])
return tempBlock
def createGenesisBlock(transactions):#mono o admin to ektelei
timestamp=time()
genBlock= block(0,time(),transactions,0,"1",1)
genBlock.current_hash= genBlock.hash()
return genBlock