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
1 changed files with 19 additions and 18 deletions

View File

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