move max_checkpoint from network to constants
This commit is contained in:
@@ -28,6 +28,7 @@ from .bitcoin import Hash, hash_encode, int_to_hex, rev_hex
|
|||||||
from . import constants
|
from . import constants
|
||||||
from .util import bfh, bh2u
|
from .util import bfh, bh2u
|
||||||
|
|
||||||
|
|
||||||
MAX_TARGET = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
MAX_TARGET = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
||||||
|
|
||||||
|
|
||||||
@@ -315,7 +316,7 @@ class Blockchain(util.PrintError):
|
|||||||
|
|
||||||
def get_hash(self, height):
|
def get_hash(self, height):
|
||||||
def is_height_checkpoint():
|
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
|
at_chunk_boundary = (height+1) % 2016 == 0
|
||||||
return within_cp_range and at_chunk_boundary
|
return within_cp_range and at_chunk_boundary
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,14 @@ def read_json(filename, default):
|
|||||||
return r
|
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
|
TESTNET = False
|
||||||
WIF_PREFIX = 0x80
|
WIF_PREFIX = 0x80
|
||||||
@@ -66,7 +73,7 @@ class BitcoinMainnet:
|
|||||||
BIP44_COIN_TYPE = 0
|
BIP44_COIN_TYPE = 0
|
||||||
|
|
||||||
|
|
||||||
class BitcoinTestnet:
|
class BitcoinTestnet(AbstractNet):
|
||||||
|
|
||||||
TESTNET = True
|
TESTNET = True
|
||||||
WIF_PREFIX = 0xef
|
WIF_PREFIX = 0xef
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ from . import pem
|
|||||||
from .version import ELECTRUM_VERSION, PROTOCOL_VERSION
|
from .version import ELECTRUM_VERSION, PROTOCOL_VERSION
|
||||||
from . import blockchain
|
from . import blockchain
|
||||||
from .blockchain import deserialize_header
|
from .blockchain import deserialize_header
|
||||||
|
from . import constants
|
||||||
|
|
||||||
|
|
||||||
class NotificationSession(ClientSession):
|
class NotificationSession(ClientSession):
|
||||||
@@ -267,7 +268,7 @@ class Interface(PrintError):
|
|||||||
asyncio.get_event_loop().create_task(self.group.cancel_remaining())
|
asyncio.get_event_loop().create_task(self.group.cancel_remaining())
|
||||||
|
|
||||||
async def run_fetch_blocks(self, sub_reply, replies):
|
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')
|
raise GracefulDisconnect('server tip below max checkpoint')
|
||||||
|
|
||||||
async with self.network.bhi_lock:
|
async with self.network.bhi_lock:
|
||||||
@@ -298,7 +299,7 @@ class Interface(PrintError):
|
|||||||
could_connect, num_headers = await self.request_chunk(height, next_height)
|
could_connect, num_headers = await self.request_chunk(height, next_height)
|
||||||
self.tip = max(height + num_headers, self.tip)
|
self.tip = max(height + num_headers, self.tip)
|
||||||
if not could_connect:
|
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')
|
raise Exception('server chain conflicts with checkpoints or genesis')
|
||||||
last, height = await self.step(height)
|
last, height = await self.step(height)
|
||||||
self.tip = max(height, self.tip)
|
self.tip = max(height, self.tip)
|
||||||
@@ -328,8 +329,8 @@ class Interface(PrintError):
|
|||||||
bad_header = header
|
bad_header = header
|
||||||
height -= 1
|
height -= 1
|
||||||
checkp = False
|
checkp = False
|
||||||
if height <= self.network.max_checkpoint():
|
if height <= constants.net.max_checkpoint():
|
||||||
height = self.network.max_checkpoint() + 1
|
height = constants.net.max_checkpoint() + 1
|
||||||
checkp = True
|
checkp = True
|
||||||
|
|
||||||
header = await self.get_block_header(height, 'backward')
|
header = await self.get_block_header(height, 'backward')
|
||||||
@@ -343,8 +344,8 @@ class Interface(PrintError):
|
|||||||
delta = self.tip - height
|
delta = self.tip - height
|
||||||
next_height = self.tip - 2 * delta
|
next_height = self.tip - 2 * delta
|
||||||
checkp = False
|
checkp = False
|
||||||
if next_height <= self.network.max_checkpoint():
|
if next_height <= constants.net.max_checkpoint():
|
||||||
next_height = self.network.max_checkpoint() + 1
|
next_height = constants.net.max_checkpoint() + 1
|
||||||
checkp = True
|
checkp = True
|
||||||
height = next_height
|
height = next_height
|
||||||
|
|
||||||
|
|||||||
@@ -818,10 +818,6 @@ class Network(PrintError):
|
|||||||
with open(path, 'w', encoding='utf-8') as f:
|
with open(path, 'w', encoding='utf-8') as f:
|
||||||
f.write(json.dumps(cp, indent=4))
|
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):
|
def start(self, fx=None):
|
||||||
self.fut = threading.Thread(target=self._run, args=(fx,))
|
self.fut = threading.Thread(target=self._run, args=(fx,))
|
||||||
self.fut.start()
|
self.fut.start()
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import asyncio
|
|||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from electrum.constants import set_regtest
|
from electrum import constants
|
||||||
from electrum.simple_config import SimpleConfig
|
from electrum.simple_config import SimpleConfig
|
||||||
from electrum import blockchain
|
from electrum import blockchain
|
||||||
from electrum.interface import Interface
|
from electrum.interface import Interface
|
||||||
@@ -11,9 +11,6 @@ class MockInterface(Interface):
|
|||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.config = config
|
self.config = config
|
||||||
super().__init__(None, 'mock-server:50000:t', self.config.electrum_path(), None)
|
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.q = asyncio.Queue()
|
||||||
self.blockchain = blockchain.Blockchain(self.config, 2002, None)
|
self.blockchain = blockchain.Blockchain(self.config, 2002, None)
|
||||||
self.tip = 12
|
self.tip = 12
|
||||||
@@ -26,6 +23,17 @@ class MockInterface(Interface):
|
|||||||
return item
|
return item
|
||||||
|
|
||||||
class TestNetwork(unittest.TestCase):
|
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):
|
def setUp(self):
|
||||||
self.config = SimpleConfig({'electrum_path': tempfile.mkdtemp(prefix="test_network")})
|
self.config = SimpleConfig({'electrum_path': tempfile.mkdtemp(prefix="test_network")})
|
||||||
self.interface = MockInterface(self.config)
|
self.interface = MockInterface(self.config)
|
||||||
@@ -107,5 +115,5 @@ class TestNetwork(unittest.TestCase):
|
|||||||
self.assertEqual(times, 2)
|
self.assertEqual(times, 2)
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
set_regtest()
|
constants.set_regtest()
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user