*Add support to sync from the last committee

This commit is contained in:
Derrick Hammer 2023-03-29 05:32:39 -04:00
parent 4ac1621e3f
commit 191ef04b46
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 32 additions and 1 deletions

View File

@ -49,6 +49,8 @@ export default class Client {
private prover: IProver;
private rpcCallback: Function;
private boot = false;
constructor(prover: IProver, rpcCallback: Function) {
this.prover = prover;
this.rpcCallback = rpcCallback;
@ -84,6 +86,7 @@ export default class Client {
});
this._provider = provider;
this.boot = true;
}
return this._provider;
@ -156,7 +159,11 @@ export default class Client {
private async _sync() {
const currentPeriod = this.getCurrentPeriod();
if (currentPeriod > this.latestPeriod) {
this.latestCommittee = await this.syncFromGenesis();
if (!this.boot) {
this.latestCommittee = await this.syncFromGenesis();
} else {
this.latestCommittee = await this.syncFromLastUpdate();
}
this.latestPeriod = currentPeriod;
}
}
@ -186,6 +193,30 @@ export default class Client {
return this.getCommittee(currentPeriod, lastCommitteeHash);
}
private async syncFromLastUpdate() {
const currentPeriod = this.getCurrentPeriod();
let startPeriod = this.latestPeriod;
let lastCommitteeHash: Uint8Array = this.getCommitteeHash(
this.latestCommittee as Uint8Array[]
);
for (let period = startPeriod + 1; period <= currentPeriod; period++) {
try {
lastCommitteeHash = await this.prover.getCommitteeHash(
period,
currentPeriod,
DEFAULT_BATCH_SIZE
);
} catch (e: any) {
throw new Error(
`failed to fetch committee hash for prover at period(${period}): ${e.meessage}`
);
}
}
return this.getCommittee(currentPeriod, lastCommitteeHash);
}
async getCommittee(
period: number,
expectedCommitteeHash: Uint8Array | null