-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathminingNode.py
148 lines (90 loc) · 2.77 KB
/
miningNode.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
from duckchain import Blockchain
from flask import Flask, jsonify, request
import json
import sys
import time
import schedule,threading
# instantiate the full node
app = Flask(__name__)
# instantiate the blockchain
blockchain = Blockchain()
@app.route('/list_blocks',methods=['get'])
def list_blocks():
"""
list all the blocks saved on the node
"""
return jsonify(blockchain.blocks),200
@app.route('/mining/mine', methods=['get'])
def mine():
"""
as mining node,find a proof which solves pow puzzle,send proof to full node to validate
"""
new_block = blockchain.mine_new_block()
proof = new_block['proof']
check_url = blockchain.fullNodeUrl + "/full/validate_block/?proof="+ str(proof)
# send to full node to validate
data = blockchain.loadUrl(check_url)
if data == "wrong":
return ( "full node is dead: " + blockchain.fullNodeUrl),200
if data['reason'] == "ok":
# reward miner one coin
blockchain.new_transaction(0,blockchain.node_identifier,1,new_block)
# add new block to blockchain
blockchain.blocks.append(new_block)
response = new_block
else:
response = {
"wrong": data['reason']
}
return jsonify(response),200
def sync_blocks():
"""
sync blocks with other registered node,if any blocks on these nodes is longer,update local blockchain
"""
url = blockchain.fullNodeUrl + "/full/list_nodes"
address_list = blockchain.loadUrl(url)
if address_list == "wrong":
print "full node is dead: " + blockchain.fullNodeUrl
return None;
whether_update = False
for node_url in address_list:
full_url = node_url + "/list_blocks"
node_blocks = blockchain.loadUrl(full_url)
if node_blocks == "wrong":
print "died node :" + node_url
continue
if len(node_blocks) > len(blockchain.blocks):
whether_update = True
blockchain.blocks = node_blocks
if whether_update:
add_str = "chain updated to length: " + str(len(blockchain.blocks))
else:
add_str = "no changes"
response = {
"status": "compared :" + str(len(address_list)) + " nodes, " + add_str
}
print(response)
def run_app(port_num):
"""
start server
"""
app.run(host='0.0.0.0', port=port_num)
if __name__ == "__main__":
# register node
url = blockchain.fullNodeUrl+'/full/register_node/'
address = "http://localhost:"+str(sys.argv[1])
post_data = json.dumps({"address": address})
data = blockchain.postData(url,post_data)
if data != "wrong":
if data['back'] == "ok":
print "add address: " + address + "\n\n"
# run server
threading.Thread(target=run_app,args=(sys.argv[1],)).start()
# sync blockchain every 10 seconds
schedule.every(10).seconds.do(sync_blocks)
while True:
schedule.run_pending()
else:
print "address exists,please change other one !"
else:
print "full node is dead: " + blockchain.fullNodeUrl