electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 44a83c240120aca7bf35ea518cec393cb9232956
parent 40e13224f7cf485c051a07943306f0c99ab17272
Author: ThomasV <thomasv@electrum.org>
Date:   Tue, 12 Dec 2017 11:10:50 +0100

read checkpoints file in NetworkConstants, add it to setup.py

Diffstat:
Mcontrib/build-wine/deterministic.spec | 1+
Mcontrib/osx.spec | 1+
Mlib/bitcoin.py | 12++++++------
Mlib/blockchain.py | 12++++++------
Mlib/network.py | 24++++++------------------
Msetup.py | 1+
6 files changed, 21 insertions(+), 30 deletions(-)

diff --git a/contrib/build-wine/deterministic.spec b/contrib/build-wine/deterministic.spec @@ -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'), diff --git a/contrib/osx.spec b/contrib/osx.spec @@ -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'), diff --git a/lib/bitcoin.py b/lib/bitcoin.py @@ -37,13 +37,13 @@ from . import version 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 @@ -78,10 +78,10 @@ class NetworkConstants: 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): @@ -90,10 +90,10 @@ class NetworkConstants: 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() diff --git a/lib/blockchain.py b/lib/blockchain.py @@ -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) @@ -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 @@ -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: @@ -128,7 +128,7 @@ class Blockchain(util.PrintError): 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 diff --git a/lib/network.py b/lib/network.py @@ -165,8 +165,7 @@ class Network(util.DaemonThread): 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(): @@ -951,7 +950,7 @@ class Network(util.DaemonThread): 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) @@ -1065,22 +1064,11 @@ class Network(util.DaemonThread): 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) diff --git a/setup.py b/setup.py @@ -74,6 +74,7 @@ setup( 'servers.json', 'servers_testnet.json', 'currencies.json', + 'checkpoints.json', 'www/index.html', 'wordlist/*.txt', 'locale/*/LC_MESSAGES/electrum.mo',