forked from kyuupichan/electrumx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckpoints.py
68 lines (56 loc) · 1.94 KB
/
checkpoints.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
#!/usr/bin/env python
from json import loads, dumps
from sys import exit, argv
import base64
import urllib.request, urllib.error, urllib.parse
if len(argv) < 3:
print('Arguments: <rpc_username> <rpc_password> [<rpc_port>]')
sys.exit(1)
# From electrum.
def bits_to_target(bits):
bitsN = (bits >> 24) & 0xff
if not (bitsN >= 0x03 and bitsN <= 0x1e):
raise BaseException("First part of bits should be in [0x03, 0x1e]")
bitsBase = bits & 0xffffff
if not (bitsBase >= 0x8000 and bitsBase <= 0x7fffff):
raise BaseException("Second part of bits should be in [0x8000, 0x7fffff]")
return bitsBase << (8 * (bitsN-3))
def rpc(method, params):
data = {
"jsonrpc": "1.0",
"id":"1",
"method": method,
"params": params
}
data_json = dumps(data)
username = argv[1]
password = argv[2]
port = 4561
if len(argv) > 3:
port = argv[3]
url = "http://127.0.0.1:{}/".format(port)
req = urllib.request.Request(url, data_json.encode("utf-8"), {'content-type': 'application/json'})
base64string = base64.encodestring(('%s:%s' % (username, password)).encode()).decode().replace('\n', '')
req.add_header("Authorization", "Basic %s" % base64string)
response_stream = urllib.request.urlopen(req)
json_response = response_stream.read()
return loads(json_response)
# Electrum checkpoints are blocks 2015, 2015 + 2016, 2015 + 2016*2, ...
i = 2015
INTERVAL = 2016
checkpoints = []
block_count = int(rpc('getblockcount', [])['result'])
print(('Blocks: {}'.format(block_count)))
while True:
h = rpc('getblockhash', [i])['result']
block = rpc('getblock', [h])['result']
checkpoints.append([
block['hash'],
bits_to_target(int(block['bits'], 16))
])
i += INTERVAL
if i > block_count:
print('Done.')
break
with open('checkpoints_output.json', 'w+') as f:
f.write(dumps(checkpoints, indent=4, separators=(',', ':')))