align style when calling LC libp2p API (#5104)

The `UpdatesByRange` API takes `startPeriod / count`, but is internally
called by `Slice`. Move the logic that converts from the `Slice` to the
caller to reduce complexity inside the used `doRequest` function.
This commit is contained in:
Etan Kissling 2023-06-20 22:21:32 +02:00 committed by GitHub
parent e2f7065432
commit d670af5bf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,7 +27,9 @@ type
Bootstrap = Bootstrap =
Endpoint[Eth2Digest, ForkedLightClientBootstrap] Endpoint[Eth2Digest, ForkedLightClientBootstrap]
UpdatesByRange = UpdatesByRange =
Endpoint[Slice[SyncCommitteePeriod], ForkedLightClientUpdate] Endpoint[
tuple[startPeriod: SyncCommitteePeriod, count: uint64],
ForkedLightClientUpdate]
FinalityUpdate = FinalityUpdate =
Endpoint[Nothing, ForkedLightClientFinalityUpdate] Endpoint[Nothing, ForkedLightClientFinalityUpdate]
OptimisticUpdate = OptimisticUpdate =
@ -128,17 +130,15 @@ type LightClientUpdatesByRangeResponse =
proc doRequest( proc doRequest(
e: typedesc[UpdatesByRange], e: typedesc[UpdatesByRange],
peer: Peer, peer: Peer,
periods: Slice[SyncCommitteePeriod] key: tuple[startPeriod: SyncCommitteePeriod, count: uint64]
): Future[LightClientUpdatesByRangeResponse] {. ): Future[LightClientUpdatesByRangeResponse] {.
async, raises: [Defect, IOError].} = async, raises: [Defect, IOError].} =
let let (startPeriod, count) = key
startPeriod = periods.a doAssert count > 0 and count <= MAX_REQUEST_LIGHT_CLIENT_UPDATES
lastPeriod = periods.b let response = await peer.lightClientUpdatesByRange(startPeriod, count)
reqCount = min(periods.len, MAX_REQUEST_LIGHT_CLIENT_UPDATES).uint64
let response = await peer.lightClientUpdatesByRange(startPeriod, reqCount)
if response.isOk: if response.isOk:
let e = distinctBase(response.get) let e = distinctBase(response.get)
.checkLightClientUpdates(startPeriod, reqCount) .checkLightClientUpdates(startPeriod, count)
if e.isErr: if e.isErr:
raise newException(ResponseError, e.error) raise newException(ResponseError, e.error)
return response return response
@ -334,13 +334,6 @@ proc query[E](
progressFut.cancel() progressFut.cancel()
return progressFut.completed return progressFut.completed
template query(
self: LightClientManager,
e: typedesc[UpdatesByRange],
key: SyncCommitteePeriod
): Future[bool] =
self.query(e, key .. key)
template query[E]( template query[E](
self: LightClientManager, self: LightClientManager,
e: typedesc[E] e: typedesc[E]
@ -410,11 +403,19 @@ proc loop(self: LightClientManager) {.async.} =
didProgress = didProgress =
if finalized == optimistic and not isNextSyncCommitteeKnown: if finalized == optimistic and not isNextSyncCommitteeKnown:
if finalized >= current: if finalized >= current:
await self.query(UpdatesByRange, finalized) await self.query(UpdatesByRange, (
startPeriod: finalized,
count: 1'u64))
else: else:
await self.query(UpdatesByRange, finalized ..< current) await self.query(UpdatesByRange, (
startPeriod: finalized,
count: min(current - finalized,
MAX_REQUEST_LIGHT_CLIENT_UPDATES)))
elif finalized + 1 < current: elif finalized + 1 < current:
await self.query(UpdatesByRange, finalized + 1 ..< current) await self.query(UpdatesByRange, (
startPeriod: finalized + 1,
count: min(current - (finalized + 1),
MAX_REQUEST_LIGHT_CLIENT_UPDATES)))
elif finalized != optimistic: elif finalized != optimistic:
await self.query(FinalityUpdate) await self.query(FinalityUpdate)
else: else: