refactor: handle case where returned items are less than the total requested and loop until we have them all
This commit is contained in:
parent
6eaf874e44
commit
cfa1462505
|
@ -44,16 +44,34 @@ export default class Prover implements IProver {
|
|||
}
|
||||
}
|
||||
|
||||
const existingUpdates: LightClientUpdate[] = [];
|
||||
const results: Uint8Array[][] = [];
|
||||
|
||||
let batchedStart = trueStart;
|
||||
let batchedCount = trueCount;
|
||||
|
||||
while (true) {
|
||||
const res = await this.callback({
|
||||
start: trueStart,
|
||||
count: trueCount,
|
||||
start: batchedStart,
|
||||
count: batchedCount,
|
||||
});
|
||||
|
||||
const updates: LightClientUpdate[] = [];
|
||||
if (res.length <= batchedCount) {
|
||||
if (res.length > 0) {
|
||||
results.push(res);
|
||||
batchedStart += res.length;
|
||||
batchedCount -= res.length;
|
||||
}
|
||||
}
|
||||
|
||||
if (batchedCount == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (trueStart != startPeriod) {
|
||||
for (let i = 0; i < trueStart - startPeriod; i++) {
|
||||
updates.push(
|
||||
existingUpdates.push(
|
||||
capella.ssz.LightClientUpdate.deserialize(
|
||||
this.client.store.getUpdate(startPeriod + i),
|
||||
),
|
||||
|
@ -61,8 +79,12 @@ export default class Prover implements IProver {
|
|||
}
|
||||
}
|
||||
|
||||
return updates.concat(
|
||||
res.map((u: any) => capella.ssz.LightClientUpdate.fromJson(u.data)),
|
||||
return existingUpdates.concat(
|
||||
results
|
||||
.reduce((prev, cur) => {
|
||||
return prev.concat(cur);
|
||||
}, [])
|
||||
.map((u: any) => capella.ssz.LightClientUpdate.fromJson(u.data)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue