Skip to content

Commit

Permalink
sync: remove step from sync client implementation
Browse files Browse the repository at this point in the history
Deprecated in the spec:
ethereum/consensus-specs#2856 - future PR:s will
deprecate server support as well.
  • Loading branch information
arnetheduck committed May 31, 2022
1 parent 85d0234 commit 930568b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions beacon_chain/sync/sync_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ proc getBlocks*[A, B](man: SyncManager[A, B], peer: A,
try:
let res =
if peer.useSyncV2():
await beaconBlocksByRange_v2(peer, req.slot, req.count, req.step)
await beaconBlocksByRange_v2(peer, req.slot, req.count, 1)
else:
(await beaconBlocksByRange(peer, req.slot, req.count, req.step)).map(
(await beaconBlocksByRange(peer, req.slot, req.count, 1)).map(
proc(blcks: seq[phase0.SignedBeaconBlock]): auto =
blcks.mapIt(newClone(ForkedSignedBeaconBlock.init(it))))

Expand Down
4 changes: 4 additions & 0 deletions beacon_chain/sync/sync_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ p2pProtocol BeaconSync(version = 1,
# client call that returns `seq[ref SignedBeaconBlock]` will
# will be generated by the libp2p macro - we guarantee that seq items
# are `not-nil` in the implementation
# TODO reqStep is deprecated - future versions can remove support for
# values != 1: https://github.com/ethereum/consensus-specs/pull/2856
trace "got range request", peer, startSlot,
count = reqCount, step = reqStep
if reqCount == 0'u64 or reqStep == 0'u64:
Expand Down Expand Up @@ -404,6 +406,8 @@ p2pProtocol BeaconSync(version = 1,
# client call that returns `seq[ref ForkedSignedBeaconBlock]` will
# will be generated by the libp2p macro - we guarantee that seq items
# are `not-nil` in the implementation
# TODO reqStep is deprecated - future versions can remove support for
# values != 1: https://github.com/ethereum/consensus-specs/pull/2856

trace "got range request", peer, startSlot,
count = reqCount, step = reqStep
Expand Down
32 changes: 13 additions & 19 deletions beacon_chain/sync/sync_queue.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type
index*: uint64
slot*: Slot
count*: uint64
step*: uint64
item*: T

SyncResult*[T] = object
Expand Down Expand Up @@ -105,12 +104,12 @@ proc getShortMap*[T](req: SyncRequest[T],
break
else:
res.add('.')
slider = slider + req.step
slider = slider + 1
res

proc contains*[T](req: SyncRequest[T], slot: Slot): bool {.inline.} =
slot >= req.slot and slot < req.slot + req.count * req.step and
((slot - req.slot) mod req.step == 0)
slot >= req.slot and slot < req.slot + req.count and
((slot - req.slot) == 0)

proc cmp*[T](a, b: SyncRequest[T]): int =
cmp(uint64(a.slot), uint64(b.slot))
Expand All @@ -137,7 +136,7 @@ proc checkResponse*[T](req: SyncRequest[T],
inc(dindex)
else:
return false
slot = slot + req.step
slot = slot + 1
rindex = rindex + 1'u64

if dindex == len(data):
Expand All @@ -153,26 +152,26 @@ proc getFullMap*[T](req: SyncRequest[T],
proc init[T](t1: typedesc[SyncRequest], kind: SyncQueueKind, start: Slot,
finish: Slot, t2: typedesc[T]): SyncRequest[T] =
let count = finish - start + 1'u64
SyncRequest[T](kind: kind, slot: start, count: count, step: 1'u64)
SyncRequest[T](kind: kind, slot: start, count: count)

proc init[T](t1: typedesc[SyncRequest], kind: SyncQueueKind, slot: Slot,
count: uint64, item: T): SyncRequest[T] =
SyncRequest[T](kind: kind, slot: slot, count: count, item: item, step: 1'u64)
SyncRequest[T](kind: kind, slot: slot, count: count, item: item)

proc init[T](t1: typedesc[SyncRequest], kind: SyncQueueKind, start: Slot,
finish: Slot, item: T): SyncRequest[T] =
let count = finish - start + 1'u64
SyncRequest[T](kind: kind, slot: start, count: count, step: 1'u64, item: item)
SyncRequest[T](kind: kind, slot: start, count: count, item: item)

proc empty*[T](t: typedesc[SyncRequest], kind: SyncQueueKind,
t2: typedesc[T]): SyncRequest[T] {.inline.} =
SyncRequest[T](kind: kind, step: 0'u64, count: 0'u64)
SyncRequest[T](kind: kind, count: 0'u64)

proc setItem*[T](sr: var SyncRequest[T], item: T) =
sr.item = item

proc isEmpty*[T](sr: SyncRequest[T]): bool {.inline.} =
(sr.step == 0'u64) and (sr.count == 0'u64)
(sr.count == 0'u64)

proc init*[T](t1: typedesc[SyncQueue], t2: typedesc[T],
queueKind: SyncQueueKind,
Expand Down Expand Up @@ -263,8 +262,7 @@ proc `<`*[T](a, b: SyncResult[T]): bool =
a.request.slot > b.request.slot

proc `==`*[T](a, b: SyncRequest[T]): bool =
(a.kind == b.kind) and (a.slot == b.slot) and (a.count == b.count) and
(a.step == b.step)
(a.kind == b.kind) and (a.slot == b.slot) and (a.count == b.count)

proc lastSlot*[T](req: SyncRequest[T]): Slot =
## Returns last slot for request ``req``.
Expand Down Expand Up @@ -808,12 +806,10 @@ func updateRequestForNewSafeSlot[T](sq: SyncQueue[T], sr: var SyncRequest[T]) =
# Request is only partially relevant.
let
numSlotsDone = outSlot - lowSlot
numStepsDone = (numSlotsDone + sr.step - 1) div sr.step
sr.slot += numStepsDone * sr.step
sr.count -= numStepsDone
sr.slot += numSlotsDone
sr.count -= numSlotsDone
else:
# Entire request is no longer relevant.
sr.step = 0
sr.count = 0
of SyncQueueKind.Backward:
if outSlot >= highSlot:
Expand All @@ -823,11 +819,9 @@ func updateRequestForNewSafeSlot[T](sq: SyncQueue[T], sr: var SyncRequest[T]) =
# Request is only partially relevant.
let
numSlotsDone = highSlot - outSlot
numStepsDone = (numSlotsDone + sr.step - 1) div sr.step
sr.count -= numStepsDone
sr.count -= numSlotsDone
else:
# Entire request is no longer relevant.
sr.step = 0
sr.count = 0

proc pop*[T](sq: SyncQueue[T], maxslot: Slot, item: T): SyncRequest[T] =
Expand Down

0 comments on commit 930568b

Please sign in to comment.