From cdca74aa399402546af0ab86ed645da327fe0410 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sun, 9 Sep 2018 05:00:09 +0200 Subject: [PATCH] move max_checkpoint from network to constants --- electrum/blockchain.py | 3 ++- electrum/constants.py | 11 +++++++++-- electrum/interface.py | 13 +++++++------ electrum/network.py | 4 ---- electrum/tests/test_network.py | 18 +++++++++++++----- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/electrum/blockchain.py b/electrum/blockchain.py index a7befad25..4936f0bec 100644 --- a/electrum/blockchain.py +++ b/electrum/blockchain.py @@ -28,6 +28,7 @@ from .bitcoin import Hash, hash_encode, int_to_hex, rev_hex from . import constants from .util import bfh, bh2u + MAX_TARGET = 0x00000000FFFF0000000000000000000000000000000000000000000000000000 @@ -315,7 +316,7 @@ class Blockchain(util.PrintError): def get_hash(self, height): def is_height_checkpoint(): - within_cp_range = height < len(self.checkpoints) * 2016 + within_cp_range = height <= constants.net.max_checkpoint() at_chunk_boundary = (height+1) % 2016 == 0 return within_cp_range and at_chunk_boundary diff --git a/electrum/constants.py b/electrum/constants.py index 12cb4b1de..af3f7205a 100644 --- a/electrum/constants.py +++ b/electrum/constants.py @@ -37,7 +37,14 @@ def read_json(filename, default): return r -class BitcoinMainnet: +class AbstractNet: + + @classmethod + def max_checkpoint(cls) -> int: + return max(0, len(cls.CHECKPOINTS) * 2016 - 1) + + +class BitcoinMainnet(AbstractNet): TESTNET = False WIF_PREFIX = 0x80 @@ -66,7 +73,7 @@ class BitcoinMainnet: BIP44_COIN_TYPE = 0 -class BitcoinTestnet: +class BitcoinTestnet(AbstractNet): TESTNET = True WIF_PREFIX = 0xef diff --git a/electrum/interface.py b/electrum/interface.py index 4c02191e4..d612ca9d4 100644 --- a/electrum/interface.py +++ b/electrum/interface.py @@ -41,6 +41,7 @@ from . import pem from .version import ELECTRUM_VERSION, PROTOCOL_VERSION from . import blockchain from .blockchain import deserialize_header +from . import constants class NotificationSession(ClientSession): @@ -267,7 +268,7 @@ class Interface(PrintError): asyncio.get_event_loop().create_task(self.group.cancel_remaining()) async def run_fetch_blocks(self, sub_reply, replies): - if self.tip < self.network.max_checkpoint(): + if self.tip < constants.net.max_checkpoint(): raise GracefulDisconnect('server tip below max checkpoint') async with self.network.bhi_lock: @@ -298,7 +299,7 @@ class Interface(PrintError): could_connect, num_headers = await self.request_chunk(height, next_height) self.tip = max(height + num_headers, self.tip) if not could_connect: - if height <= self.network.max_checkpoint(): + if height <= constants.net.max_checkpoint(): raise Exception('server chain conflicts with checkpoints or genesis') last, height = await self.step(height) self.tip = max(height, self.tip) @@ -328,8 +329,8 @@ class Interface(PrintError): bad_header = header height -= 1 checkp = False - if height <= self.network.max_checkpoint(): - height = self.network.max_checkpoint() + 1 + if height <= constants.net.max_checkpoint(): + height = constants.net.max_checkpoint() + 1 checkp = True header = await self.get_block_header(height, 'backward') @@ -343,8 +344,8 @@ class Interface(PrintError): delta = self.tip - height next_height = self.tip - 2 * delta checkp = False - if next_height <= self.network.max_checkpoint(): - next_height = self.network.max_checkpoint() + 1 + if next_height <= constants.net.max_checkpoint(): + next_height = constants.net.max_checkpoint() + 1 checkp = True height = next_height diff --git a/electrum/network.py b/electrum/network.py index d51b8ab15..52ae10652 100644 --- a/electrum/network.py +++ b/electrum/network.py @@ -818,10 +818,6 @@ class Network(PrintError): with open(path, 'w', encoding='utf-8') as f: f.write(json.dumps(cp, indent=4)) - @classmethod - def max_checkpoint(cls): - return max(0, len(constants.net.CHECKPOINTS) * 2016 - 1) - def start(self, fx=None): self.fut = threading.Thread(target=self._run, args=(fx,)) self.fut.start() diff --git a/electrum/tests/test_network.py b/electrum/tests/test_network.py index 07b5996c9..b28dff770 100644 --- a/electrum/tests/test_network.py +++ b/electrum/tests/test_network.py @@ -2,7 +2,7 @@ import asyncio import tempfile import unittest -from electrum.constants import set_regtest +from electrum import constants from electrum.simple_config import SimpleConfig from electrum import blockchain from electrum.interface import Interface @@ -11,9 +11,6 @@ class MockInterface(Interface): def __init__(self, config): self.config = config super().__init__(None, 'mock-server:50000:t', self.config.electrum_path(), None) - class FakeNetwork: - max_checkpoint = lambda: 0 - self.network = FakeNetwork self.q = asyncio.Queue() self.blockchain = blockchain.Blockchain(self.config, 2002, None) self.tip = 12 @@ -26,6 +23,17 @@ class MockInterface(Interface): return item class TestNetwork(unittest.TestCase): + + @classmethod + def setUpClass(cls): + super().setUpClass() + constants.set_regtest() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() + constants.set_mainnet() + def setUp(self): self.config = SimpleConfig({'electrum_path': tempfile.mkdtemp(prefix="test_network")}) self.interface = MockInterface(self.config) @@ -107,5 +115,5 @@ class TestNetwork(unittest.TestCase): self.assertEqual(times, 2) if __name__=="__main__": - set_regtest() + constants.set_regtest() unittest.main()