forked from stallingerl/electrum-doi
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ef04fb
commit f4bc3b2
Showing
2 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env python | ||
from json import loads, dumps | ||
from sys import exit, argv | ||
import base64 | ||
import urllib.request, urllib.error, urllib.parse | ||
|
||
|
||
# From electrum. | ||
def bits_to_target(bits): | ||
bitsN = (bits >> 24) & 0xff | ||
if not (bitsN >= 0x03 and bitsN <= 0x1f): | ||
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 = "admin" | ||
password = "changeMe" | ||
port = 8339 | ||
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'] | ||
bits = "0x" + block['bits'] | ||
print(bits) | ||
|
||
checkpoints.append([ | ||
block['hash'], | ||
bits_to_target(int(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=(',', ':'))) |