Skip to content

Commit

Permalink
review changes 9
Browse files Browse the repository at this point in the history
  • Loading branch information
jansegre committed Apr 20, 2023
1 parent 17f19b6 commit 0b30594
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 34 deletions.
1 change: 1 addition & 0 deletions hathor/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ def _initialize_components(self) -> None:
assert self.tx_storage.indexes is not None
if self.tx_storage.indexes.mempool_tips:
self.tx_storage.indexes.mempool_tips.update(tx)
# XXX: refactor this in the future so the index manager decies whether to update each index
if tx_meta.validation.is_fully_connected():
self.tx_storage.add_to_indexes(tx)
if tx.is_transaction and tx_meta.voided_by:
Expand Down
8 changes: 2 additions & 6 deletions hathor/p2p/node_sync_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class PeerState(Enum):
UNKNOWN = 'unknown'
SYNCING_CHECKPOINTS = 'syncing-checkpoints'
SYNCING_BLOCKS = 'syncing-blocks'
# XXX: maybe use a SYNCING_MEMPOOL and SYNCING_REALTIME too


class StreamEnd(IntFlag):
Expand Down Expand Up @@ -785,8 +786,7 @@ def handle_blocks(self, payload: str) -> None:
else:
self.log.debug('block received', blk_id=blk.hash.hex())
self.manager.on_new_tx(blk, propagate_to_peers=False, quiet=True, partial=True,
sync_checkpoints=is_syncing_checkpoints,
reject_locked_reward=not is_syncing_checkpoints)
sync_checkpoints=is_syncing_checkpoints)
except HathorError:
self.handle_invalid_block(exc_info=True)
return
Expand Down Expand Up @@ -923,10 +923,6 @@ def handle_transaction(self, payload: str) -> None:
"""Handle a received TRANSACTION message."""
assert self.protocol.connections is not None

# if self.state != PeerState.SYNCING_TXS:
# self.protocol.send_error_and_close_connection('Not expecting to receive transactions')
# return

# tx_bytes = bytes.fromhex(payload)
tx_bytes = base64.b64decode(payload)
tx = tx_or_block_from_bytes(tx_bytes)
Expand Down
1 change: 1 addition & 0 deletions hathor/p2p/sync_checkpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(self, manager: 'HathorManager'):
self.peer_syncing = None

# If set to true next run_sync_transactions will be skipped
# XXX: review how this is implemented
self.should_skip_sync_tx = False

# Create logger with context
Expand Down
2 changes: 1 addition & 1 deletion hathor/p2p/sync_mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, sync_manager: 'NodeBlockSync'):
# Maximum number of items in the DFS.
self.MAX_STACK_LENGTH: int = 1000

# Looping call of the main method
# Whether the mempool algorithm is running
self._is_running = False

def is_running(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion hathor/transaction/storage/transaction_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def pre_save_validation(self, tx: BaseTransaction, tx_meta: TransactionMetadata)
assert tx_meta.hash is not None
assert tx.hash == tx_meta.hash, f'{tx.hash.hex()} != {tx_meta.hash.hex()}'
voided_by = tx_meta.voided_by or set()
# XXX: PARTIALLY_VALIDATED_ID must be included if the tx is fully connected and must not included otherwise
# XXX: PARTIALLY_VALIDATED_ID must be included if the tx is fully connected and must not be included otherwise
has_partially_validated_marker = settings.PARTIALLY_VALIDATED_ID in voided_by
validation_is_fully_connected = tx_meta.validation.is_fully_connected()
assert (not has_partially_validated_marker) == validation_is_fully_connected, \
Expand Down
2 changes: 1 addition & 1 deletion tests/p2p/test_split_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_split_brain_plain(self):
dot2.render('dot2-post')

node_sync = conn.proto1.state.sync_manager
self.assertSynced(node_sync)
self.assertSyncedProgress(node_sync)
self.assertTipsEqual(manager1, manager2)
self.assertConsensusEqual(manager1, manager2)
self.assertConsensusValid(manager1)
Expand Down
4 changes: 2 additions & 2 deletions tests/p2p/test_split_brain2.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def test_split_brain(self):
dot2 = GraphvizVisualizer(manager2.tx_storage, include_verifications=True).dot()
dot2.render('dot2-post')

self.assertSynced(conn12.proto1.state.sync_manager)
self.assertSynced(conn12.proto2.state.sync_manager)
self.assertSyncedProgress(conn12.proto1.state.sync_manager)
self.assertSyncedProgress(conn12.proto2.state.sync_manager)
self.assertTipsEqual(manager1, manager2)
self.assertConsensusEqual(manager1, manager2)
self.assertConsensusValid(manager1)
Expand Down
13 changes: 13 additions & 0 deletions tests/tx/test_indexes3.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,20 @@ class SyncV1SimulatorIndexesTestCase(unittest.SyncV1Params, BaseSimulatorIndexes
class SyncV2SimulatorIndexesTestCase(unittest.SyncV2Params, BaseSimulatorIndexesTestCase):
__test__ = True

@pytest.mark.flaky(max_runs=5, min_passes=1)
def test_topological_iterators(self):
super().test_topological_iterators()

# XXX: disable this on sync-v2 because it's flaky and it won't make sense when tips indexes are removed
def test_tips_index_initialization(self):
pass


# sync-bridge should behave like sync-v2
class SyncBridgeSimulatorIndexesTestCase(unittest.SyncBridgeParams, SyncV2SimulatorIndexesTestCase):
__test__ = True

# XXX: re-enable this on sync-bridge with higher flakiness, sync-bridge has tips indexes so it makes sense
@pytest.mark.flaky(max_runs=5, min_passes=1)
def test_tips_index_initialization(self):
SyncV1SimulatorIndexesTestCase.test_tips_index_initialization(self)
33 changes: 10 additions & 23 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,28 +272,15 @@ def assertTipsNotEqual(self, manager1, manager2):
self.assertNotEqual(s1, s2)

def assertTipsEqualSyncV1(self, manager1, manager2):
# tx tips
tips1 = {tx.hash for tx in manager1.tx_storage.iter_mempool_tips_from_tx_tips()}
tips2 = {tx.hash for tx in manager2.tx_storage.iter_mempool_tips_from_tx_tips()}
self.log.debug('tx tips1', len=len(tips1), list=shorten_hash(tips1))
self.log.debug('tx tips2', len=len(tips2), list=shorten_hash(tips2))
self.assertEqual(tips1, tips2)

# best block
s1 = set(manager1.tx_storage.get_best_block_tips())
s2 = set(manager2.tx_storage.get_best_block_tips())
self.assertEqual(s1, s2)

# best block (from height index)
b1 = manager1.tx_storage.indexes.height.get_tip()
b2 = manager2.tx_storage.indexes.height.get_tip()
self.assertEqual(b1, b2)

# all tips must be equal (this check should be removed together with the index)
# XXX: this is the original implementation of assertTipsEqual
s1 = set(manager1.tx_storage.get_all_tips())
s2 = set(manager2.tx_storage.get_all_tips())
self.assertEqual(s1, s2)

s1 = set(manager1.tx_storage.get_tx_tips())
s2 = set(manager2.tx_storage.get_tx_tips())
self.assertEqual(s1, s2)

def assertTipsEqualSyncV2(self, manager1, manager2, *, strict_sync_v2_indexes=True):
# tx tips
if strict_sync_v2_indexes:
Expand Down Expand Up @@ -433,18 +420,18 @@ def assertTransactionConsensusValid(self, tx):
self.assertTrue(meta.voided_by)
self.assertTrue(parent_meta.voided_by.issubset(meta.voided_by))

def assertSynced(self, node_sync):
def assertSyncedProgress(self, node_sync):
"""Check "synced" status of p2p-manager, uses self._enable_sync_vX to choose which check to run."""
enable_sync_v1, enable_sync_v2 = self._syncVersionFlags()
if enable_sync_v2:
self.assertV2Synced(node_sync)
self.assertV2SyncedProgress(node_sync)
elif enable_sync_v1:
self.assertV1Synced(node_sync)
self.assertV1SyncedProgress(node_sync)

def assertV1Synced(self, node_sync):
def assertV1SyncedProgress(self, node_sync):
self.assertEqual(node_sync.synced_timestamp, node_sync.peer_timestamp)

def assertV2Synced(self, node_sync):
def assertV2SyncedProgress(self, node_sync):
self.assertEqual(node_sync.synced_height, node_sync.peer_height)

def clean_tmpdirs(self):
Expand Down

0 comments on commit 0b30594

Please sign in to comment.