Skip to content

Commit

Permalink
read checkpoints file in NetworkConstants, add it to setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ecdsa committed Dec 12, 2017
1 parent 40e1322 commit 44a83c2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 30 deletions.
1 change: 1 addition & 0 deletions contrib/build-wine/deterministic.spec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ hiddenimports += collect_submodules('keepkeylib')
datas = [
(home+'lib/currencies.json', 'electrum'),
(home+'lib/servers.json', 'electrum'),
(home+'lib/checkpoints.json', 'electrum'),
(home+'lib/servers_testnet.json', 'electrum'),
(home+'lib/wordlist/english.txt', 'electrum/wordlist'),
(home+'lib/locale', 'electrum/locale'),
Expand Down
1 change: 1 addition & 0 deletions contrib/osx.spec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ hiddenimports += collect_submodules('keepkeylib')
datas = [
(home+'lib/currencies.json', 'electrum'),
(home+'lib/servers.json', 'electrum'),
(home+'lib/checkpoints.json', 'electrum'),
(home+'lib/wordlist/english.txt', 'electrum/wordlist'),
(home+'lib/locale', 'electrum/locale'),
(home+'plugins', 'electrum_plugins'),
Expand Down
12 changes: 6 additions & 6 deletions lib/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
from .util import print_error, InvalidPassword, assert_bytes, to_bytes, inv_dict
from . import segwit_addr

def read_json_dict(filename):
def read_json(filename, default):
path = os.path.join(os.path.dirname(__file__), filename)
try:
with open(path, 'r') as f:
r = json.loads(f.read())
except:
r = {}
r = default
return r


Expand Down Expand Up @@ -78,10 +78,10 @@ def set_mainnet(cls):
cls.ADDRTYPE_P2PKH = 0
cls.ADDRTYPE_P2SH = 5
cls.SEGWIT_HRP = "bc"
cls.HEADERS_URL = "https://headers.electrum.org/blockchain_headers"
cls.GENESIS = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
cls.DEFAULT_PORTS = {'t': '50001', 's': '50002'}
cls.DEFAULT_SERVERS = read_json_dict('servers.json')
cls.DEFAULT_SERVERS = read_json('servers.json', {})
cls.CHECKPOINTS = read_json('checkpoints.json', [])

@classmethod
def set_testnet(cls):
Expand All @@ -90,10 +90,10 @@ def set_testnet(cls):
cls.ADDRTYPE_P2PKH = 111
cls.ADDRTYPE_P2SH = 196
cls.SEGWIT_HRP = "tb"
cls.HEADERS_URL = "https://headers.electrum.org/testnet_headers"
cls.GENESIS = "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"
cls.DEFAULT_PORTS = {'t':'51001', 's':'51002'}
cls.DEFAULT_SERVERS = read_json_dict('servers_testnet.json')
cls.DEFAULT_SERVERS = read_json('servers_testnet.json', {})
cls.CHECKPOINTS = read_json('checkpoints_testnet.json', [])


NetworkConstants.set_mainnet()
Expand Down
12 changes: 6 additions & 6 deletions lib/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def hash_header(header):

blockchains = {}

def read_blockchains(config, checkpoints):
blockchains[0] = Blockchain(config, checkpoints, 0, None)
def read_blockchains(config):
blockchains[0] = Blockchain(config, 0, None)
fdir = os.path.join(util.get_headers_dir(config), 'forks')
if not os.path.exists(fdir):
os.mkdir(fdir)
Expand All @@ -70,7 +70,7 @@ def read_blockchains(config, checkpoints):
for filename in l:
checkpoint = int(filename.split('_')[2])
parent_id = int(filename.split('_')[1])
b = Blockchain(config, checkpoints, checkpoint, parent_id)
b = Blockchain(config, checkpoint, parent_id)
blockchains[b.checkpoint] = b
return blockchains

Expand All @@ -94,11 +94,11 @@ class Blockchain(util.PrintError):
Manages blockchain headers and their verification
"""

def __init__(self, config, checkpoints, checkpoint, parent_id):
def __init__(self, config, checkpoint, parent_id):
self.config = config
self.catch_up = None # interface catching up
self.checkpoint = checkpoint
self.checkpoints = checkpoints
self.checkpoints = bitcoin.NetworkConstants.CHECKPOINTS
self.parent_id = parent_id
self.lock = threading.Lock()
with self.lock:
Expand Down Expand Up @@ -128,7 +128,7 @@ def check_header(self, header):

def fork(parent, header):
checkpoint = header.get('block_height')
self = Blockchain(parent.config, parent.checkpoints, checkpoint, parent.checkpoint)
self = Blockchain(parent.config, checkpoint, parent.checkpoint)
open(self.path(), 'w+').close()
self.save_header(header)
return self
Expand Down
24 changes: 6 additions & 18 deletions lib/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ def __init__(self, config=None):
util.DaemonThread.__init__(self)
self.config = SimpleConfig(config) if isinstance(config, dict) else config
self.num_server = 10 if not self.config.get('oneserver') else 0
self.read_checkpoints()
self.blockchains = blockchain.read_blockchains(self.config, self.checkpoints)
self.blockchains = blockchain.read_blockchains(self.config)
self.print_error("blockchains", self.blockchains.keys())
self.blockchain_index = config.get('blockchain_index', 0)
if self.blockchain_index not in self.blockchains.keys():
Expand Down Expand Up @@ -951,7 +950,7 @@ def init_headers_file(self):
b = self.blockchains[0]
filename = b.path()
if not os.path.exists(filename):
length = 80 * len(self.checkpoints) * 2016
length = 80 * len(bitcoin.NetworkConstants.CHECKPOINTS) * 2016
with open(filename, 'wb') as f:
if length>0:
f.seek(length-1)
Expand Down Expand Up @@ -1065,22 +1064,11 @@ def broadcast(self, tx, timeout=30):
return False, "error: " + out
return True, out

def checkpoints_path(self):
return os.path.join(os.path.dirname(__file__), 'checkpoints.json')

def export_checkpoints(self):
# for each chunk, store the hash of the last block and the target after the chunk
def export_checkpoints(self, path):
# run manually from the console to generate checkpoints
cp = self.blockchain().get_checkpoints()
with open(self.checkpoints_path(), 'w') as f:
with open(path, 'w') as f:
f.write(json.dumps(cp, indent=4))

def read_checkpoints(self):
try:
with open(self.checkpoints_path(), 'r') as f:
self.checkpoints = json.loads(f.read())
self.print_error("Read %d checkpoints" % len(self.checkpoints))
except:
self.checkpoints = []

def max_checkpoint(self):
return max(0, len(self.checkpoints) * 2016 - 1)
return max(0, len(bitcoin.NetworkConstants.CHECKPOINTS) * 2016 - 1)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
'servers.json',
'servers_testnet.json',
'currencies.json',
'checkpoints.json',
'www/index.html',
'wordlist/*.txt',
'locale/*/LC_MESSAGES/electrum.mo',
Expand Down

0 comments on commit 44a83c2

Please sign in to comment.