1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
import asyncio
import tempfile
import unittest
from typing import List
from electrum import constants
from electrum.simple_config import SimpleConfig
from electrum import blockchain
from electrum.interface import Interface, ServerAddr, ChainResolutionMode
from electrum.crypto import sha256
from electrum.util import OldTaskGroup
from electrum import util
from . import ElectrumTestCase
CRM = ChainResolutionMode
class MockBlockchain:
def __init__(self, headers: List[str]):
self._headers = headers
self.forkpoint = len(headers)
def height(self) -> int:
return len(self._headers) - 1
def save_header(self, header: dict) -> None:
assert header['block_height'] == self.height()+1, f"new {header['block_height']=}, cur {self.height()=}"
self._headers.append(header['mock']['id'])
def check_header(self, header: dict) -> bool:
return header['mock']['id'] in self._headers
def can_connect(self, header: dict, *, check_height: bool = True) -> bool:
height = header['block_height']
if check_height and self.height() != height - 1:
return False
if self.check_header(header):
return True
return header['mock']['prev_id'] in self._headers
def fork(parent, header: dict) -> 'MockBlockchain':
if not parent.can_connect(header, check_height=False):
raise Exception("forking header does not connect to parent chain")
forkpoint = header.get('block_height')
self = MockBlockchain(parent._headers[:forkpoint])
self.save_header(header)
chain_id = header['mock']['id']
with blockchain.blockchains_lock:
blockchain.blockchains[chain_id] = self
return self
class MockNetwork:
def __init__(self, config: SimpleConfig):
self.config = config
self.asyncio_loop = util.get_asyncio_loop()
self.taskgroup = OldTaskGroup()
self.proxy = None
class MockInterface(Interface):
def __init__(self, config: SimpleConfig):
self.config = config
network = MockNetwork(config)
super().__init__(network=network, server=ServerAddr.from_str('mock-server:50000:t'))
self.q = asyncio.Queue()
async def get_block_header(self, height: int, *, mode: ChainResolutionMode) -> dict:
assert self.q.qsize() > 0, (height, mode)
item = await self.q.get()
self.logger.debug(f"step with {height=}. {mode=}. will get {item=}")
assert item['block_height'] == height, (item['block_height'], height)
assert mode in item['mock'], (mode, item)
return item
async def run(self):
return
async def _maybe_warm_headers_cache(self, *args, **kwargs):
return
class TestHeaderChainResolution(ElectrumTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
constants.BitcoinRegtest.set_as_network()
@classmethod
def tearDownClass(cls):
super().tearDownClass()
constants.BitcoinMainnet.set_as_network()
def tearDown(self):
blockchain.blockchains = {}
super().tearDown()
async def asyncSetUp(self):
await super().asyncSetUp()
self.config = SimpleConfig({'electrum_path': self.electrum_path})
self.interface = MockInterface(self.config)
async def test_catchup_one_block_behind(self):
"""Single chain, but client is behind. The client's height is 5, server is on block 6.
- first missing block found during *catchup* phase
"""
ifa = self.interface
ifa.tip = 6
ifa.blockchain = MockBlockchain(["00a", "01a", "02a", "03a", "04a", "05a"])
blockchain.blockchains = {
"00a": ifa.blockchain,
}
ifa.q.put_nowait({'block_height': 6, 'mock': {CRM.CATCHUP:1, 'id': '06a', 'prev_id': '05a'}})
res = await ifa.sync_until(ifa.tip)
self.assertEqual((CRM.CATCHUP, 7), res)
self.assertEqual(ifa.q.qsize(), 0)
self.assertEqual(len(blockchain.blockchains), 1)
async def test_catchup_already_up_to_date(self):
"""Single chain, local chain tip already matches server tip."""
ifa = self.interface
ifa.tip = 5
ifa.blockchain = MockBlockchain(["00a", "01a", "02a", "03a", "04a", "05a"])
blockchain.blockchains = {
"00a": ifa.blockchain,
}
ifa.q.put_nowait({'block_height': 5, 'mock': {CRM.CATCHUP:1, 'id': '05a', 'prev_id': '04a'}})
res = await ifa.sync_until(ifa.tip)
self.assertEqual((CRM.CATCHUP, 6), res)
self.assertEqual(ifa.q.qsize(), 0)
self.assertEqual(len(blockchain.blockchains), 1)
async def test_catchup_client_ahead_of_lagging_server(self):
"""Single chain, server is lagging."""
ifa = self.interface
ifa.tip = 3
ifa.blockchain = MockBlockchain(["00a", "01a", "02a", "03a", "04a", "05a"])
blockchain.blockchains = {
"00a": ifa.blockchain,
}
ifa.q.put_nowait({'block_height': 3, 'mock': {CRM.CATCHUP:1, 'id': '03a', 'prev_id': '02a'}})
res = await ifa.sync_until(ifa.tip)
self.assertEqual((CRM.CATCHUP, 4), res)
self.assertEqual(ifa.q.qsize(), 0)
self.assertEqual(len(blockchain.blockchains), 1)
async def test_catchup_fast_forward(self):
"""Single chain, but client is behind. The client's height is 5, server is already on block 12.
- first missing block found during *backward* phase
"""
ifa = self.interface
ifa.tip = 12
ifa.blockchain = MockBlockchain(["00a", "01a", "02a", "03a", "04a", "05a"])
blockchain.blockchains = {
"00a": ifa.blockchain,
}
ifa.q.put_nowait({'block_height': 12, 'mock': {CRM.CATCHUP:1, 'id': '12a', 'prev_id': '11a'}})
ifa.q.put_nowait({'block_height': 6, 'mock': {CRM.BACKWARD:1, 'id': '06a', 'prev_id': '05a'}})
ifa.q.put_nowait({'block_height': 7, 'mock': {CRM.CATCHUP: 1, 'id': '07a', 'prev_id': '06a'}})
ifa.q.put_nowait({'block_height': 8, 'mock': {CRM.CATCHUP: 1, 'id': '08a', 'prev_id': '07a'}})
ifa.q.put_nowait({'block_height': 9, 'mock': {CRM.CATCHUP: 1, 'id': '09a', 'prev_id': '08a'}})
res = await ifa.sync_until(ifa.tip, next_height=9)
self.assertEqual((CRM.CATCHUP, 10), res)
self.assertEqual(ifa.q.qsize(), 0)
self.assertEqual(len(blockchain.blockchains), 1)
async def test_fork(self):
"""client starts on main chain, has no knowledge of any fork.
server is on other side of chain split, the last common block is height 6.
- first missing block found during *binary* phase
- is *new* fork
"""
ifa = self.interface
ifa.tip = 8
ifa.blockchain = MockBlockchain(["00a", "01a", "02a", "03a", "04a", "05a", "06a", "07a", "08a", "09a", "10a", "11a", "12a"])
blockchain.blockchains = {
"00a": ifa.blockchain,
}
ifa.q.put_nowait({'block_height': 8, 'mock': {CRM.CATCHUP:1, 'id': '08b', 'prev_id': '07b'}})
ifa.q.put_nowait({'block_height': 7, 'mock': {CRM.BACKWARD:1, 'id': '07b', 'prev_id': '06a'}})
ifa.q.put_nowait({'block_height': 5, 'mock': {CRM.BACKWARD:1, 'id': '05a', 'prev_id': '04a'}})
ifa.q.put_nowait({'block_height': 6, 'mock': {CRM.BINARY:1, 'id': '06a', 'prev_id': '05a'}})
res = await ifa.sync_until(ifa.tip, next_height=7)
self.assertEqual((CRM.FORK, 8), res)
self.assertEqual(ifa.q.qsize(), 0)
self.assertEqual(len(blockchain.blockchains), 2)
async def test_can_connect_during_backward(self):
"""client starts on main chain. client already knows about another fork, which has local height 4.
server is on that fork but has more blocks.
- first missing block found during *backward* phase
- is *existing* fork
"""
ifa = self.interface
ifa.tip = 8
ifa.blockchain = MockBlockchain(["00a", "01a", "02a", "03a", "04a", "05a", "06a", "07a", "08a", "09a", "10a", "11a", "12a"])
blockchain.blockchains = {
"00a": ifa.blockchain,
"03b": MockBlockchain(["00a", "01a", "02a", "03b", "04b"]),
}
ifa.q.put_nowait({'block_height': 8, 'mock': {CRM.CATCHUP:1, 'id': '08b', 'prev_id': '07b'}})
ifa.q.put_nowait({'block_height': 7, 'mock': {CRM.BACKWARD:1, 'id': '07b', 'prev_id': '06b'}})
ifa.q.put_nowait({'block_height': 5, 'mock': {CRM.BACKWARD:1, 'id': '05b', 'prev_id': '04b'}})
ifa.q.put_nowait({'block_height': 6, 'mock': {CRM.CATCHUP:1, 'id': '06b', 'prev_id': '05b'}})
res = await ifa.sync_until(ifa.tip, next_height=6)
self.assertEqual((CRM.CATCHUP, 7), res)
self.assertEqual(ifa.q.qsize(), 0)
self.assertEqual(len(blockchain.blockchains), 2)
async def test_chain_false_during_binary(self):
"""client starts on main chain, has no knowledge of any fork.
server is on other side of chain split, the last common block is height 3.
- first missing block found during *binary* phase
- is *new* fork
"""
ifa = self.interface
ifa.tip = 8
ifa.blockchain = MockBlockchain(["00a", "01a", "02a", "03a", "04a", "05a", "06a", "07a", "08a", "09a", "10a", "11a", "12a"])
blockchain.blockchains = {
"00a": ifa.blockchain,
}
ifa.q.put_nowait({'block_height': 8, 'mock': {CRM.CATCHUP:1, 'id': '08b', 'prev_id': '07b'}})
ifa.q.put_nowait({'block_height': 7, 'mock': {CRM.BACKWARD:1, 'id': '07b', 'prev_id': '06b'}})
ifa.q.put_nowait({'block_height': 5, 'mock': {CRM.BACKWARD:1, 'id': '05b', 'prev_id': '04b'}})
ifa.q.put_nowait({'block_height': 1, 'mock': {CRM.BACKWARD:1, 'id': '01a', 'prev_id': '00a'}})
ifa.q.put_nowait({'block_height': 3, 'mock': {CRM.BINARY:1, 'id': '03a', 'prev_id': '02a'}})
ifa.q.put_nowait({'block_height': 4, 'mock': {CRM.BINARY:1, 'id': '04b', 'prev_id': '03a'}})
ifa.q.put_nowait({'block_height': 5, 'mock': {CRM.CATCHUP:1, 'id': '05b', 'prev_id': '04b'}})
ifa.q.put_nowait({'block_height': 6, 'mock': {CRM.CATCHUP:1, 'id': '06b', 'prev_id': '05b'}})
res = await ifa.sync_until(ifa.tip, next_height=6)
self.assertEqual((CRM.CATCHUP, 7), res)
self.assertEqual(ifa.q.qsize(), 0)
self.assertEqual(len(blockchain.blockchains), 2)
async def test_chain_true_during_binary(self):
"""client starts on main chain. client already knows about another fork, which has local height 10.
server is on that fork but has more blocks.
- first missing block found during *binary* phase
- is *existing* fork
"""
ifa = self.interface
ifa.tip = 20
ifa.blockchain = MockBlockchain(["00a", "01a", "02a", "03a", "04a", "05a", "06a", "07a", "08a", "09a", "10a", "11a", "12a", "13a", "14a"])
blockchain.blockchains = {
"00a": ifa.blockchain,
"07b": MockBlockchain(["00a", "01a", "02a", "03a", "04a", "05a", "06a", "07b", "08b", "09b", "10b"]),
}
ifa.q.put_nowait({'block_height': 20, 'mock': {CRM.CATCHUP:1, 'id': '20b', 'prev_id': '19b'}})
ifa.q.put_nowait({'block_height': 15, 'mock': {CRM.BACKWARD:1, 'id': '15b', 'prev_id': '14b'}})
ifa.q.put_nowait({'block_height': 13, 'mock': {CRM.BACKWARD:1, 'id': '13b', 'prev_id': '12b'}})
ifa.q.put_nowait({'block_height': 9, 'mock': {CRM.BACKWARD:1, 'id': '09b', 'prev_id': '08b'}})
ifa.q.put_nowait({'block_height': 11, 'mock': {CRM.BINARY:1, 'id': '11b', 'prev_id': '10b'}})
ifa.q.put_nowait({'block_height': 10, 'mock': {CRM.BINARY:1, 'id': '10b', 'prev_id': '09b'}})
ifa.q.put_nowait({'block_height': 11, 'mock': {CRM.CATCHUP:1, 'id': '11b', 'prev_id': '10b'}})
ifa.q.put_nowait({'block_height': 12, 'mock': {CRM.CATCHUP:1, 'id': '12b', 'prev_id': '11b'}})
ifa.q.put_nowait({'block_height': 13, 'mock': {CRM.CATCHUP:1, 'id': '13b', 'prev_id': '12b'}})
res = await ifa.sync_until(ifa.tip, next_height=13)
self.assertEqual((CRM.CATCHUP, 14), res)
self.assertEqual(ifa.q.qsize(), 0)
self.assertEqual(len(blockchain.blockchains), 2)
if __name__ == "__main__":
constants.BitcoinRegtest.set_as_network()
unittest.main()
|