forked from rajat-babel/blockchainSimulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.py
46 lines (41 loc) · 1.03 KB
/
node.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
from block import Block
from flask import Flask
import sync
import os
import json
node = Flask(__name__)
node_blocks = sync.sync()
@node.route('/blockchain.json', methods=['GET'])
def blockchain():
'''
Shoots back the blockchain, which in our case, is a json list of hashes
with the block information which is:
index
timestamp
data
hash
prev_hash
'''
node_blocks = sync.sync() #update if they've changed
# Convert our blocks into dictionaries
# so we can send them as json objects later
python_blocks = []
for block in node_blocks:
'''
block_index = str(block.index)
block_timestamp = str(block.timestamp)
block_data = str(block.data)
block_hash = block.hash
block = {
"index": block.index,
"timestamp": block.timestamp,
"data": block.data,
"hash": block.hash,
"prev_hash": block.prev_hash
}
'''
python_blocks.append(block.__dict__())
json_blocks = json.dumps(python_blocks)
return json_blocks
if __name__ == '__main__':
node.run()