Browse Source

move max_checkpoint from network to constants

3.3.3.1
SomberNight 6 years ago
parent
commit
cdca74aa39
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 3
      electrum/blockchain.py
  2. 11
      electrum/constants.py
  3. 13
      electrum/interface.py
  4. 4
      electrum/network.py
  5. 18
      electrum/tests/test_network.py

3
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

11
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

13
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

4
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()

18
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()

Loading…
Cancel
Save