-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathnode.py
74 lines (59 loc) · 2.2 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
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
from block import Block
import mine
from flask import Flask, jsonify, request
import sync
import requests
import os
import json
import sys
import apscheduler
import argparse
import utils
from config import *
node = Flask(__name__)
sync.sync(save=True) #want to sync and save the overall "best" blockchain from peers
from apscheduler.schedulers.background import BackgroundScheduler
sched = BackgroundScheduler(standalone=True)
@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
'''
local_chain = sync.sync_local() #update if they've changed
# Convert our blocks into dictionaries
# so we can send them as json objects later
json_blocks = json.dumps(local_chain.block_list_dict())
return json_blocks
@node.route('/mined', methods=['POST'])
def mined():
possible_block_dict = request.get_json()
print possible_block_dict
print sched.get_jobs()
print sched
sched.add_job(mine.validate_possible_block, args=[possible_block_dict], id='validate_possible_block') #add the block again
return jsonify(received=True)
if __name__ == '__main__':
#args!
parser = argparse.ArgumentParser(description='JBC Node')
parser.add_argument('--port', '-p', default='5000',
help='what port we will run the node on')
parser.add_argument('--mine', '-m', dest='mine', action='store_true')
args = parser.parse_args()
filename = '%sdata.txt' % (CHAINDATA_DIR)
with open(filename, 'w') as data_file:
data_file.write("Mined by node on port %s" % args.port)
mine.sched = sched #to override the BlockingScheduler in the
#only mine if we want to
if args.mine:
#in this case, sched is the background sched
sched.add_job(mine.mine_for_block, kwargs={'rounds':STANDARD_ROUNDS, 'start_nonce':0}, id='mining') #add the block again
sched.add_listener(mine.mine_for_block_listener, apscheduler.events.EVENT_JOB_EXECUTED)#, args=sched)
sched.start() #want this to start so we can validate on the schedule and not rely on Flask
#now we know what port to use
node.run(host='127.0.0.1', port=args.port)