|
|
@ -38,7 +38,7 @@ class TestNetwork(unittest.TestCase): |
|
|
|
self.config = SimpleConfig({'electrum_path': tempfile.mkdtemp(prefix="test_network")}) |
|
|
|
self.interface = MockInterface(self.config) |
|
|
|
|
|
|
|
def test_new_fork(self): |
|
|
|
def test_fork_noconflict(self): |
|
|
|
blockchain.blockchains = {} |
|
|
|
self.interface.q.put_nowait({'block_height': 8, 'mock': {'catchup':1, 'check': lambda x: False, 'connect': lambda x: False}}) |
|
|
|
def mock_connect(height): |
|
|
@ -49,10 +49,24 @@ class TestNetwork(unittest.TestCase): |
|
|
|
self.interface.q.put_nowait({'block_height': 5, 'mock': {'binary':1,'check':lambda x: True, 'connect': lambda x: True}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 6, 'mock': {'binary':1,'check':lambda x: True, 'connect': lambda x: True}}) |
|
|
|
ifa = self.interface |
|
|
|
self.assertEqual(('fork', 8), asyncio.get_event_loop().run_until_complete(ifa.sync_until(8, next_height=7))) |
|
|
|
self.assertEqual(('fork_noconflict', 8), asyncio.get_event_loop().run_until_complete(ifa.sync_until(8, next_height=7))) |
|
|
|
self.assertEqual(self.interface.q.qsize(), 0) |
|
|
|
|
|
|
|
def test_new_can_connect_during_backward(self): |
|
|
|
def test_fork_conflict(self): |
|
|
|
blockchain.blockchains = {7: {'check': lambda bad_header: False}} |
|
|
|
self.interface.q.put_nowait({'block_height': 8, 'mock': {'catchup':1, 'check': lambda x: False, 'connect': lambda x: False}}) |
|
|
|
def mock_connect(height): |
|
|
|
return height == 6 |
|
|
|
self.interface.q.put_nowait({'block_height': 7, 'mock': {'backward':1,'check': lambda x: False, 'connect': mock_connect, 'fork': self.mock_fork}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 2, 'mock': {'backward':1,'check':lambda x: True, 'connect': lambda x: False}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 4, 'mock': {'binary':1,'check':lambda x: True, 'connect': lambda x: True}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 5, 'mock': {'binary':1,'check':lambda x: True, 'connect': lambda x: True}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 6, 'mock': {'binary':1,'check':lambda x: True, 'connect': lambda x: True}}) |
|
|
|
ifa = self.interface |
|
|
|
self.assertEqual(('fork_conflict', 8), asyncio.get_event_loop().run_until_complete(ifa.sync_until(8, next_height=7))) |
|
|
|
self.assertEqual(self.interface.q.qsize(), 0) |
|
|
|
|
|
|
|
def test_can_connect_during_backward(self): |
|
|
|
blockchain.blockchains = {} |
|
|
|
self.interface.q.put_nowait({'block_height': 8, 'mock': {'catchup':1, 'check': lambda x: False, 'connect': lambda x: False}}) |
|
|
|
def mock_connect(height): |
|
|
@ -68,7 +82,7 @@ class TestNetwork(unittest.TestCase): |
|
|
|
def mock_fork(self, bad_header): |
|
|
|
return blockchain.Blockchain(self.config, bad_header['block_height'], None) |
|
|
|
|
|
|
|
def test_new_chain_false_during_binary(self): |
|
|
|
def test_chain_false_during_binary(self): |
|
|
|
blockchain.blockchains = {} |
|
|
|
self.interface.q.put_nowait({'block_height': 8, 'mock': {'catchup':1, 'check': lambda x: False, 'connect': lambda x: False}}) |
|
|
|
mock_connect = lambda height: height == 3 |
|
|
@ -82,38 +96,6 @@ class TestNetwork(unittest.TestCase): |
|
|
|
self.assertEqual(('catchup', 7), asyncio.get_event_loop().run_until_complete(ifa.sync_until(8, next_height=6))) |
|
|
|
self.assertEqual(self.interface.q.qsize(), 0) |
|
|
|
|
|
|
|
@unittest.skip # FIXME test is broken |
|
|
|
def test_new_join(self): |
|
|
|
blockchain.blockchains = {7: {'check': lambda bad_header: True}} |
|
|
|
self.interface.q.put_nowait({'block_height': 8, 'mock': {'catchup':1, 'check': lambda x: False, 'connect': lambda x: False}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 7, 'mock': {'backward':1, 'check': lambda x: False, 'connect': lambda height: height == 6}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 2, 'mock': {'backward':1, 'check': lambda x: True, 'connect': lambda x: False}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 4, 'mock': {'binary':1, 'check': lambda x: True, 'connect': lambda x: False}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 5, 'mock': {'binary':1, 'check': lambda x: True, 'connect': lambda x: False}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 6, 'mock': {'binary':1, 'check': lambda x: True, 'connect': lambda x: True}}) |
|
|
|
ifa = self.interface |
|
|
|
self.assertEqual(('join', 7), asyncio.get_event_loop().run_until_complete(ifa.sync_until(8, next_height=6))) |
|
|
|
self.assertEqual(self.interface.q.qsize(), 0) |
|
|
|
|
|
|
|
@unittest.skip # FIXME test is broken |
|
|
|
def test_new_reorg(self): |
|
|
|
times = 0 |
|
|
|
def check(header): |
|
|
|
nonlocal times |
|
|
|
self.assertEqual(header['block_height'], 7) |
|
|
|
times += 1 |
|
|
|
return False |
|
|
|
blockchain.blockchains = {7: {'check': check, 'parent': {'check': lambda x: True}}} |
|
|
|
self.interface.q.put_nowait({'block_height': 8, 'mock': {'catchup':1, 'check': lambda x: False, 'connect': lambda x: False}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 7, 'mock': {'backward':1, 'check': lambda x: False, 'connect': lambda height: height == 6}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 2, 'mock': {'backward':1, 'check': lambda x: 1, 'connect': lambda x: False}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 4, 'mock': {'binary':1, 'check': lambda x: 1, 'connect': lambda x: False}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 5, 'mock': {'binary':1, 'check': lambda x: 1, 'connect': lambda x: False}}) |
|
|
|
self.interface.q.put_nowait({'block_height': 6, 'mock': {'binary':1, 'check': lambda x: 1, 'connect': lambda x: True}}) |
|
|
|
ifa = self.interface |
|
|
|
self.assertEqual(('conflict', 8), asyncio.get_event_loop().run_until_complete(ifa.sync_until(8, next_height=7))) |
|
|
|
self.assertEqual(self.interface.q.qsize(), 0) |
|
|
|
self.assertEqual(times, 1) |
|
|
|
|
|
|
|
if __name__=="__main__": |
|
|
|
constants.set_regtest() |
|
|
|