refactor: handle case where returned items are less than the total requested and loop until we have them all

This commit is contained in:
Derrick Hammer 2023-07-13 07:10:48 -04:00
parent 6eaf874e44
commit cfa1462505
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 30 additions and 8 deletions

View File

@ -44,16 +44,34 @@ export default class Prover implements IProver {
} }
} }
const res = await this.callback({ const existingUpdates: LightClientUpdate[] = [];
start: trueStart, const results: Uint8Array[][] = [];
count: trueCount,
});
const updates: LightClientUpdate[] = []; let batchedStart = trueStart;
let batchedCount = trueCount;
while (true) {
const res = await this.callback({
start: batchedStart,
count: batchedCount,
});
if (res.length <= batchedCount) {
if (res.length > 0) {
results.push(res);
batchedStart += res.length;
batchedCount -= res.length;
}
}
if (batchedCount == 0) {
break;
}
}
if (trueStart != startPeriod) { if (trueStart != startPeriod) {
for (let i = 0; i < trueStart - startPeriod; i++) { for (let i = 0; i < trueStart - startPeriod; i++) {
updates.push( existingUpdates.push(
capella.ssz.LightClientUpdate.deserialize( capella.ssz.LightClientUpdate.deserialize(
this.client.store.getUpdate(startPeriod + i), this.client.store.getUpdate(startPeriod + i),
), ),
@ -61,8 +79,12 @@ export default class Prover implements IProver {
} }
} }
return updates.concat( return existingUpdates.concat(
res.map((u: any) => capella.ssz.LightClientUpdate.fromJson(u.data)), results
.reduce((prev, cur) => {
return prev.concat(cur);
}, [])
.map((u: any) => capella.ssz.LightClientUpdate.fromJson(u.data)),
); );
} }
} }