From 32cdff9a854990abddfe5923c2390e108288286e Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Sun, 22 Nov 2020 22:24:09 -0500 Subject: [PATCH] Make peer sync efficient (#475) --- squeaknode/node/network_sync.py | 6 +++--- squeaknode/node/network_task.py | 8 +++++--- squeaknode/node/peer_task.py | 15 ++++----------- squeaknode/node/squeak_sync_status.py | 8 +++++++- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/squeaknode/node/network_sync.py b/squeaknode/node/network_sync.py index 8c6bf1a6..9f1631de 100644 --- a/squeaknode/node/network_sync.py +++ b/squeaknode/node/network_sync.py @@ -27,7 +27,7 @@ class NetworkSync: self.squeak_db = squeak_db self.lightning_client = lightning_client - def sync_timeline(self, peer, block_height): + def sync_timeline(self, peer, min_block, max_block): if not peer.downloading: return peer_connection = PeerConnection(peer) @@ -38,9 +38,9 @@ class NetworkSync: self.lightning_client, ) if peer.uploading: - peer_sync_task.upload(block_height) + peer_sync_task.upload(min_block, max_block) if peer.downloading: - peer_sync_task.download(block_height) + peer_sync_task.download(min_block, max_block) def sync_single_squeak(self, peer, squeak_hash): if not peer.downloading: diff --git a/squeaknode/node/network_task.py b/squeaknode/node/network_task.py index 753cf377..5f04399c 100644 --- a/squeaknode/node/network_task.py +++ b/squeaknode/node/network_task.py @@ -94,13 +94,15 @@ class TimelineNetworkSyncTask(NetworkSyncTask): def __init__( self, network_sync, - block_height, + min_block, + max_block, ): super().__init__(network_sync) - self.block_height = block_height + self.min_block = min_block + self.max_block = max_block def sync_peer(self, peer): - self.network_sync.sync_timeline(peer, self.block_height) + self.network_sync.sync_timeline(peer, self.min_block, self.max_block) class SingleSqueakNetworkSyncTask(NetworkSyncTask): diff --git a/squeaknode/node/peer_task.py b/squeaknode/node/peer_task.py index 95516798..bd5581cd 100644 --- a/squeaknode/node/peer_task.py +++ b/squeaknode/node/peer_task.py @@ -11,9 +11,6 @@ from squeaknode.server.util import get_hash, get_replyto logger = logging.getLogger(__name__) -LOOKUP_BLOCK_INTERVAL = 1008 # 1 week - - class PeerSyncTask: def __init__( self, @@ -37,14 +34,12 @@ class PeerSyncTask: def download( self, - block_height, - lookup_block_interval=LOOKUP_BLOCK_INTERVAL, + min_block, + max_block, ): # Get list of followed addresses. addresses = self._get_followed_addresses() logger.debug("Followed addresses: {}".format(addresses)) - min_block = block_height - lookup_block_interval - max_block = block_height # Get remote hashes lookup_result = self._get_remote_hashes(addresses, min_block, max_block) @@ -93,14 +88,12 @@ class PeerSyncTask: def upload( self, - block_height, - lookup_block_interval=LOOKUP_BLOCK_INTERVAL, + min_block, + max_block, ): # Get list of sharing addresses. addresses = self._get_sharing_addresses() logger.debug("Sharing addresses: {}".format(addresses)) - min_block = block_height - lookup_block_interval - max_block = block_height # Get remote hashes lookup_result = self._get_remote_hashes(addresses, min_block, max_block) diff --git a/squeaknode/node/squeak_sync_status.py b/squeaknode/node/squeak_sync_status.py index 65399352..b03c75a1 100644 --- a/squeaknode/node/squeak_sync_status.py +++ b/squeaknode/node/squeak_sync_status.py @@ -10,6 +10,9 @@ from squeaknode.node.network_sync import NetworkSync logger = logging.getLogger(__name__) +LOOKUP_BLOCK_INTERVAL = 1008 # 1 week + + class SqueakSyncController: def __init__(self, blockchain_client, squeak_store, squeak_db, lightning_client): self.blockchain_client = blockchain_client @@ -27,10 +30,13 @@ class SqueakSyncController: "Failed to sync because unable to get blockchain info.", exc_info=False ) return + min_block = block_height - LOOKUP_BLOCK_INTERVAL + max_block = block_height peers = self.squeak_db.get_peers() dowload_timeline_task = TimelineNetworkSyncTask( self.network_sync, - block_height, + min_block, + max_block, ) network_sync_result = dowload_timeline_task.sync(peers) logger.info("Upload network_sync_result: {}".format(network_sync_result))