This commit is contained in:
Youngjoon Lee 2025-02-28 10:37:09 +09:00
parent 113d3bb6d7
commit 781f762ecd
No known key found for this signature in database
GPG Key ID: D94003D91DE12141
2 changed files with 17 additions and 16 deletions

View File

@ -1,30 +1,31 @@
from collections import defaultdict
from typing import Generator
from dill.tests.test_diff import A
from cryptarchia.cryptarchia import BlockHeader, Follower, Id, Slot
SLOT_TOLERANCE = 2
SLOT_TOLERANCE = 1
def range_sync(local: Follower, remotes: list[Follower], start_slot: Slot):
while groups := {
tip: group
for tip, group in group_by_tip(remotes).items()
if group[0].tip().slot.absolute_slot - start_slot.absolute_slot > SLOT_TOLERANCE
}:
def full_sync(local: Follower, remotes: list[Follower], start_slot: Slot):
while groups := group_sync_targets(remotes, start_slot):
for _, group in groups.items():
remote = group[0]
for block in request_blocks_by_range(remote, start_slot, remote.tip().slot):
local.on_block(block)
range_sync(local, remote, start_slot, remote.tip().slot)
start_slot = Slot(local.tip().slot.absolute_slot + 1)
def group_by_tip(remotes: list[Follower]) -> dict[Id, list[Follower]]:
def range_sync(local: Follower, remote: Follower, from_slot: Slot, to_slot: Slot):
for block in request_blocks_by_range(remote, from_slot, to_slot):
local.on_block(block)
def group_sync_targets(
targets: list[Follower], start_slot: Slot
) -> dict[Id, list[Follower]]:
groups: dict[Id, list[Follower]] = defaultdict(list)
for remote in remotes:
groups[remote.tip_id()].append(remote)
for target in targets:
if target.tip().slot.absolute_slot - start_slot.absolute_slot > SLOT_TOLERANCE:
groups[target.tip_id()].append(target)
return groups

View File

@ -1,7 +1,7 @@
from unittest import TestCase
from cryptarchia.cryptarchia import Coin, Follower
from cryptarchia.sync.range_sync import range_sync
from cryptarchia.sync.full_sync import full_sync
from cryptarchia.test_common import mk_block, mk_config, mk_genesis_state
@ -21,6 +21,6 @@ class TestRangeSync(TestCase):
assert follower.forks == []
new_follower = Follower(genesis, config)
range_sync(new_follower, [follower], genesis.block.slot)
full_sync(new_follower, [follower], genesis.block.slot)
assert new_follower.tip() == b2
assert new_follower.forks == []