mirror of
https://github.com/yzernik/squeaknode.git
synced 2026-08-17 13:07:32 +02:00
Make peer sync efficient (#475)
This commit is contained in:
parent
f35451977b
commit
32cdff9a85
4 changed files with 19 additions and 18 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue