forked from jackschultz/jbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.py
53 lines (46 loc) · 1.55 KB
/
sync.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
from chain import Chain
from config import *
from block import Block
import os
import json
import requests
import glob
def sync_local():
blocks = []
#We're assuming that the folder and at least initial block exists
if os.path.exists(CHAINDATA_DIR):
for filepath in glob.glob(os.path.join(CHAINDATA_DIR, '*.json')):
with open(filepath, 'r') as block_file:
try:
block_info = json.load(block_file)
except:
print filepath
local_block = Block(block_info)
blocks.append(local_block)
blocks.sort(key=lambda block: block.index)
local_chain = Chain(blocks)
return local_chain
def sync_overall(save=False):
best_chain = sync_local()
for peer in PEERS:
#try to connect to peer
peer_blockchain_url = peer + 'blockchain.json'
try:
r = requests.get(peer_blockchain_url)
peer_blockchain_dict = r.json()
peer_blocks = [Block(bdict) for bdict in peer_blockchain_dict]
peer_chain = Chain(peer_blocks)
print peer_chain.is_valid()
if peer_chain.is_valid() and len(peer_chain) > len(best_chain):
best_chain = peer_chain
except requests.exceptions.ConnectionError:
print "Peer at %s not running. Continuing to next peer." % peer
else:
print "Peer at %s is running. Gathered their blochchain for analysis." % peer
print "Longest blockchain is %s blocks" % len(best_chain)
#for now, save the new blockchain over whatever was there
if save:
best_chain.self_save()
return best_chain
def sync(save=False):
return sync_overall(save=save)