Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sync-v2): sync-v2 implemented, sync-v1 still default #275

Merged
merged 1 commit into from
Jul 25, 2023

Conversation

jansegre
Copy link
Member

@jansegre jansegre commented Jul 21, 2021

These changes were separated from #236, it should now have mostly the sync-v2 base code itself. RFC

Acceptance Criteria

  1. Fix DepsIndex and its both implementations (memory and rocksdb), allowing voided vertices and using the correct scope when retrieving vertices from the storage.
  2. Change SyncVersion.V2 to 'v2' (previously 'v2-fake').
  3. Add SyncV2Factory and use it on ConnectionsManager._sync_factories.
  4. Add NodeBlockSync that sync blocks and its transactions.
  5. Add BlockchainStreaming and TransactionsStreaming to stream vertices as requested by peers. They are used by NodeBlockSync.
  6. Add TransactionStorage.iter_mempool_tips_from_best_index().
  7. Refactor some tests to run with both sync-v1 and sync-v2.

Current issues

  • Sync checkpoint seems to hang when syncing from zero (happens sometimes and not always at the same height);
  • Sync checkpoint seems to not resume properly sometimes after restarting (happens sometimes and not always at the same height);
  • Stats counters aren't being update (they might have to be redesigned since they were originally made for sync-v1 and some counters can't have the same semantics);
  • When using rocksdb-indexes, the memory-deps index is not re-initialized correctly because the iterator will ignore partially validated transactions;
  • Deal with syncing soft-voided txs, which didn't exist when sync-v2 was designed, and since sync-v2 doesn't sync voided txs/blocks they need special treatment;
  • Some tx-reward-lock validation could fail during checkpoint-sync;
  • Sync blocks seems to hang requesting blocks from the same height: with checkpoints disabled the node was only able to sync about 30% of the mainnet;

hathor/consensus.py Outdated Show resolved Hide resolved
@jansegre jansegre force-pushed the feat/sync-v2-mvp2 branch 2 times, most recently from 67cd607 to 92c4036 Compare July 28, 2021 01:06
@jansegre jansegre force-pushed the feat/sync-v2-mvp2 branch from 92c4036 to 7648a4b Compare July 28, 2021 03:04
@jansegre jansegre force-pushed the feat/sync-v2-mvp branch 2 times, most recently from 1584396 to 6abbfed Compare July 28, 2021 04:20
@jansegre jansegre force-pushed the feat/sync-v2-mvp2 branch from 7648a4b to 768047f Compare July 28, 2021 04:27
Base automatically changed from feat/sync-v2-mvp to dev July 29, 2021 05:42
@msbrogli msbrogli force-pushed the feat/sync-v2-mvp2 branch from 768047f to b1d6f5a Compare July 29, 2021 06:04
@jansegre jansegre changed the title feat(sync-v2): minimal sync-v2 implemented, sync-v1 still default feat(sync-v2): sync-v2 implemented, sync-v1 still default Jul 29, 2021
@jansegre jansegre force-pushed the feat/sync-v2-mvp2 branch 2 times, most recently from 4aaec0f to e11ff9d Compare July 30, 2021 01:02
hathor/p2p/manager.py Outdated Show resolved Hide resolved
hathor/p2p/manager.py Outdated Show resolved Hide resolved
hathor/p2p/manager.py Outdated Show resolved Hide resolved
hathor/p2p/manager.py Outdated Show resolved Hide resolved
hathor/p2p/manager.py Outdated Show resolved Hide resolved
hathor/transaction/storage/block_height_index.py Outdated Show resolved Hide resolved
hathor/transaction/storage/block_height_index.py Outdated Show resolved Hide resolved
hathor/transaction/storage/block_height_index.py Outdated Show resolved Hide resolved
hathor/transaction/transaction.py Outdated Show resolved Hide resolved
hathor/transaction/transaction.py Outdated Show resolved Hide resolved
hathor/manager.py Outdated Show resolved Hide resolved
hathor/manager.py Outdated Show resolved Hide resolved
hathor/transaction/base_transaction.py Outdated Show resolved Hide resolved
hathor/transaction/base_transaction.py Outdated Show resolved Hide resolved
hathor/transaction/base_transaction.py Outdated Show resolved Hide resolved
hathor/transaction/base_transaction.py Outdated Show resolved Hide resolved
hathor/transaction/storage/transaction_storage.py Outdated Show resolved Hide resolved
hathor/transaction/storage/transaction_storage.py Outdated Show resolved Hide resolved
tests/p2p/test_capabilities.py Show resolved Hide resolved
tests/simulation/test_simulator.py Outdated Show resolved Hide resolved
tests/simulation/test_simulator.py Outdated Show resolved Hide resolved
tests/simulation/test_simulator.py Outdated Show resolved Hide resolved
tests/unittest.py Outdated Show resolved Hide resolved
tests/unittest.py Show resolved Hide resolved
tests/unittest.py Outdated Show resolved Hide resolved
tests/unittest.py Outdated Show resolved Hide resolved
tests/utils.py Outdated Show resolved Hide resolved
tests/utils.py Outdated Show resolved Hide resolved
hathor/consensus.py Outdated Show resolved Hide resolved
tests/p2p/test_protocol.py Outdated Show resolved Hide resolved
hathor/p2p/sync_checkpoints.py Outdated Show resolved Hide resolved
hathor/p2p/sync_checkpoints.py Outdated Show resolved Hide resolved
hathor/p2p/sync_checkpoints.py Outdated Show resolved Hide resolved
hathor/p2p/sync_checkpoints.py Outdated Show resolved Hide resolved
hathor/p2p/sync_checkpoints.py Outdated Show resolved Hide resolved
hathor/p2p/sync_checkpoints.py Outdated Show resolved Hide resolved
hathor/p2p/sync_checkpoints.py Outdated Show resolved Hide resolved
hathor/p2p/sync_checkpoints.py Outdated Show resolved Hide resolved
hathor/p2p/sync_checkpoints.py Outdated Show resolved Hide resolved
hathor/p2p/sync_checkpoints.py Outdated Show resolved Hide resolved
hathor/p2p/sync_mempool.py Outdated Show resolved Hide resolved
hathor/p2p/sync_mempool.py Outdated Show resolved Hide resolved
hathor/p2p/sync_mempool.py Outdated Show resolved Hide resolved
hathor/p2p/sync_mempool.py Outdated Show resolved Hide resolved
hathor/p2p/sync_mempool.py Outdated Show resolved Hide resolved
hathor/p2p/sync_mempool.py Outdated Show resolved Hide resolved
@jansegre jansegre force-pushed the feat/sync-v2-mvp2 branch from e11ff9d to 4245bb4 Compare July 31, 2021 01:50
hathor/p2p/node_sync_v2.py Outdated Show resolved Hide resolved
hathor/p2p/node_sync_v2.py Outdated Show resolved Hide resolved
hathor/p2p/node_sync_v2.py Outdated Show resolved Hide resolved
hathor/p2p/node_sync_v2.py Outdated Show resolved Hide resolved
hathor/p2p/node_sync_v2.py Outdated Show resolved Hide resolved
hathor/p2p/node_sync_v2.py Outdated Show resolved Hide resolved
hathor/manager.py Outdated Show resolved Hide resolved
hathor/transaction/storage/transaction_storage.py Outdated Show resolved Hide resolved
hathor/transaction/transaction.py Outdated Show resolved Hide resolved
tests/p2p/test_capabilities.py Show resolved Hide resolved
if self._started:
raise Exception('NodeSyncBlock is already running')
self._started = True
self._lc_run.start(5)
Copy link
Collaborator

@luislhl luislhl Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be any penalty for us if we decrease this time from 5s to maybe 1s or 2s?

I have the impression we could be losing some seconds between the end of the current streaming and the start of the next one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might just move it to a variable and let us change it through syscall.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, I'll move it to sysctl.

hathor/p2p/sync_v2/manager.py Show resolved Hide resolved
hathor/p2p/sync_v2/manager.py Show resolved Hide resolved
hathor/p2p/sync_v2/manager.py Show resolved Hide resolved
hathor/p2p/sync_v2/manager.py Show resolved Hide resolved
hathor/p2p/sync_v2/manager.py Show resolved Hide resolved
Comment on lines +961 to +972
tx_bytes = base64.b64decode(payload)
tx = tx_or_block_from_bytes(tx_bytes)
assert tx.hash is not None
if not isinstance(tx, Transaction):
self.log.warn('not a transaction', hash=tx.hash_hex)
# Not a transaction. Punish peer?
return

self._tx_received += 1
if self._tx_received > self._tx_max_quantity + 1:
self.log.warn('too many txs received')
self.state = PeerState.ERROR
return

try:
# this methods takes care of checking if the tx already exists, it will take care of doing at least
# a basic validation
# self.log.debug('add new tx', tx=tx.hash_hex)
if self.partial_vertex_exists(tx.hash):
# XXX: early terminate?
self.log.debug('tx early terminate?', tx_id=tx.hash.hex())
else:
self.log.debug('tx received', tx_id=tx.hash.hex())
self.on_new_tx(tx, propagate_to_peers=False, quiet=True, reject_locked_reward=True)
except HathorError:
self.log.warn('invalid new tx', exc_info=True)
# Invalid block?!
# Invalid transaction?!
# Maybe stop syncing and punish peer.
self.state = PeerState.ERROR
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are stopping sync and/or punishing a peer for sending invalid txs, we shouldn't forget about handling invalid payloads on lines 961 and 962. The same for handle_blocks()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe refactor the following lines to a _parse_tx_bytes(tx_bytes: bytes) -> BaseTransaction method.

tx_bytes = base64.b64decode(payload)
tx = tx_or_block_from_bytes(tx_bytes)

So, handlers can check vertex types only.

Comment on lines +1012 to +996
if tx is None:
self.log.error('failed to get tx', tx_id=tx_id.hex())
self.protocol.send_error_and_close_connection(f'DATA mempool {tx_id.hex()} not found')
raise
if tx.hash != tx_id:
self.protocol.send_error_and_close_connection(f'DATA mempool {tx_id.hex()} hash mismatch')
raise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are multiple places where we call send_error_and_close_connection() but do not raise. Considering consistency, is this expected? Should we raise in the other places too, or not raise here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes more sense to return instead.

raise
return tx

def get_data(self, tx_id: bytes, origin: str) -> Deferred:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should origin be an enum?

Copy link
Member

@msbrogli msbrogli Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. Not sure whether it has the peer id or its more related to the type of origin (sync-v2-mempool, sync-v2-blocks, sync-v2-mempool).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to use an enum, I'll also refactor the methods a little bit.

hathor/manager.py Show resolved Hide resolved
@jansegre jansegre force-pushed the feat/sync-v2-mvp2 branch from a9dc05b to a7581ef Compare July 25, 2023 22:32
Co-authored-by: Marcelo Salhab Brogliato <[email protected]>
Co-authored-by: Pedro Ferreira <[email protected]>
@jansegre jansegre force-pushed the feat/sync-v2-mvp2 branch from a7581ef to f63f3b8 Compare July 25, 2023 22:43
@jansegre jansegre merged commit bdfc5fc into master Jul 25, 2023
@jansegre jansegre deleted the feat/sync-v2-mvp2 branch July 25, 2023 22:44
jansegre added a commit that referenced this pull request Sep 5, 2023
jansegre added a commit that referenced this pull request Sep 5, 2023
jansegre added a commit that referenced this pull request Sep 5, 2023
jansegre added a commit that referenced this pull request Sep 8, 2023
jansegre added a commit that referenced this pull request Sep 8, 2023
jansegre added a commit that referenced this pull request Sep 8, 2023
jansegre added a commit that referenced this pull request Sep 8, 2023
jansegre added a commit that referenced this pull request Sep 15, 2023
jansegre added a commit that referenced this pull request Sep 15, 2023
jansegre added a commit that referenced this pull request Sep 15, 2023
jansegre added a commit that referenced this pull request Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants