forked from pharesim/hz-explorer-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_fork.py
67 lines (51 loc) · 2.51 KB
/
check_fork.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
#!/usr/bin/env python
import json
import urllib2
import sqlite3
import time
import sys
import ConfigParser
import nhz_exp
config = ConfigParser.RawConfigParser()
config.read('config.ini')
conn = sqlite3.connect(config.get("database", "blockexplorer_db"))
c = conn.cursor()
def validate_block():
counter = 0
while True:
nextblock = nhz_exp.last_dbblockdata()
if nextblock != False and nextblock[0][1] != '' and nextblock[1][1] != '' and nextblock[2][1] != '':
if (config.get("general", "debug")) == "on":
print "Checking for blockchain forks."
prevblock_data = (config.get("api", "getblock_url") + '%s') % nextblock[0][1]
res_next_block = urllib2.urlopen(prevblock_data).read()
prevblock_dict = json.loads(res_next_block)
if 'nextBlock' not in prevblock_dict and 'previousBlock' in prevblock_dict:
if (config.get("general", "debug")) == "on":
print "No new block generated. Exiting."
sys.exit()
if 'errorDescription' in prevblock_dict and prevblock_dict['errorDescription'] == 'Unknown block':
if (config.get("general", "debug")) == "on":
print "Block: %s does not exist in blockchain, deleting from database." % (nextblock[0][1])
c.execute("DELETE FROM blockdata WHERE timestamp >= ?", (nextblock[1][0],))
conn.commit()
elif 'previousBlock' in prevblock_dict and nextblock[1][1] == prevblock_dict['previousBlock']:
if (config.get("general", "debug")) == "on":
print "The previous nextblock field is equal to the current previousblock field."
break
else:
print "Block not found, rolling back";
c.execute("DELETE FROM blockdata WHERE timestamp >= ?", (nextblock[1][0],))
c.execute("DELETE FROM transactions WHERE timestamp >= ?", (nextblock[1][0],))
#c.execute("DELETE FROM aliases WHERE timestamp >= ?", (nextblock[1][0],))
conn.commit()
counter += 1
if counter > 9:
if (config.get("general", "debug")) == "on":
print "Current block might be a fork, exiting."
sys.exit()
else:
if (config.get("general", "debug")) == "on":
print "We need at least two previous block entries in database. Not checking fork."
break
return True