Compare commits
No commits in common. "v0.1.0-develop.29" and "v0.1.0-develop.28" have entirely different histories.
v0.1.0-dev
...
v0.1.0-dev
|
@ -1,5 +1,3 @@
|
|||
# [0.1.0-develop.29](https://git.lumeweb.com/LumeWeb/libethsync/compare/v0.1.0-develop.28...v0.1.0-develop.29) (2023-07-13)
|
||||
|
||||
# [0.1.0-develop.28](https://git.lumeweb.com/LumeWeb/libethsync/compare/v0.1.0-develop.27...v0.1.0-develop.28) (2023-07-13)
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@lumeweb/libethclient",
|
||||
"version": "0.1.0-develop.29",
|
||||
"version": "0.1.0-develop.28",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@lumeweb/libethclient",
|
||||
"version": "0.1.0-develop.29",
|
||||
"version": "0.1.0-develop.28",
|
||||
"dependencies": {
|
||||
"@chainsafe/as-sha256": "^0.3.1",
|
||||
"@chainsafe/bls": "7.1.1",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@lumeweb/libethsync",
|
||||
"version": "0.1.0-develop.29",
|
||||
"version": "0.1.0-develop.28",
|
||||
"type": "module",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
|
|
@ -15,7 +15,6 @@ import {
|
|||
} from "#util.js";
|
||||
import { LightClientUpdate, OptimisticUpdateCallback } from "#types.js";
|
||||
import { assertValidLightClientUpdate } from "@lodestar/light-client/validation";
|
||||
import * as capella from "@lodestar/types/capella";
|
||||
|
||||
export interface BaseClientOptions {
|
||||
prover: IProver;
|
||||
|
@ -40,12 +39,6 @@ export default abstract class BaseClient {
|
|||
this.options = options;
|
||||
}
|
||||
|
||||
private _latestOptimisticUpdate?: Uint8Array;
|
||||
|
||||
get latestOptimisticUpdate(): Uint8Array {
|
||||
return this._latestOptimisticUpdate as Uint8Array;
|
||||
}
|
||||
|
||||
private _latestPeriod: number = -1;
|
||||
|
||||
get latestPeriod(): number {
|
||||
|
@ -86,6 +79,58 @@ export default abstract class BaseClient {
|
|||
return this.getNextValidExecutionInfo(retry - 1);
|
||||
}
|
||||
|
||||
protected async _sync() {
|
||||
await this.syncMutex.acquire();
|
||||
|
||||
const currentPeriod = this.getCurrentPeriod();
|
||||
if (currentPeriod > this._latestPeriod) {
|
||||
if (!this.booted) {
|
||||
this.latestCommittee = await this.syncFromGenesis();
|
||||
} else {
|
||||
this.latestCommittee = await this.syncFromLastUpdate();
|
||||
}
|
||||
this._latestPeriod = currentPeriod;
|
||||
}
|
||||
|
||||
this.syncMutex.release();
|
||||
}
|
||||
|
||||
protected async subscribe(callback?: (ei: ExecutionInfo) => void) {
|
||||
setInterval(async () => {
|
||||
try {
|
||||
const ei = await this.getLatestExecution();
|
||||
if (ei && ei.blockHash !== this.latestBlockHash) {
|
||||
this.latestBlockHash = ei.blockHash;
|
||||
return await callback?.(ei);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}, POLLING_DELAY);
|
||||
}
|
||||
|
||||
protected async getLatestExecution(): Promise<ExecutionInfo | null> {
|
||||
await this._sync();
|
||||
const update = await this.options.optimisticUpdateCallback();
|
||||
|
||||
const verify = await optimisticUpdateVerify(
|
||||
this.latestCommittee as Uint8Array[],
|
||||
update,
|
||||
);
|
||||
// TODO: check the update agains the latest sync commttee
|
||||
if (!verify.correct) {
|
||||
console.error(`Invalid Optimistic Update: ${verify.reason}`);
|
||||
return null;
|
||||
}
|
||||
console.log(
|
||||
`Optimistic update verified for slot ${update.attestedHeader.beacon.slot}`,
|
||||
);
|
||||
|
||||
return {
|
||||
blockHash: toHexString(update.attestedHeader.execution.blockHash),
|
||||
blockNumber: update.attestedHeader.execution.blockNumber,
|
||||
};
|
||||
}
|
||||
async syncProver(
|
||||
startPeriod: number,
|
||||
currentPeriod: number,
|
||||
|
@ -136,67 +181,6 @@ export default abstract class BaseClient {
|
|||
};
|
||||
}
|
||||
|
||||
protected async _sync() {
|
||||
await this.syncMutex.acquire();
|
||||
|
||||
const currentPeriod = this.getCurrentPeriod();
|
||||
if (currentPeriod > this._latestPeriod) {
|
||||
if (!this.booted) {
|
||||
this.latestCommittee = await this.syncFromGenesis();
|
||||
} else {
|
||||
this.latestCommittee = await this.syncFromLastUpdate();
|
||||
}
|
||||
this._latestPeriod = currentPeriod;
|
||||
}
|
||||
|
||||
this.syncMutex.release();
|
||||
}
|
||||
|
||||
protected async subscribe(callback?: (ei: ExecutionInfo) => void) {
|
||||
setInterval(async () => {
|
||||
await this.syncToLatestBlock(callback);
|
||||
}, POLLING_DELAY);
|
||||
}
|
||||
|
||||
public async syncToLatestBlock(callback?: (ei: ExecutionInfo) => void) {
|
||||
try {
|
||||
const ei = await this.getLatestExecution();
|
||||
if (ei && ei.blockHash !== this.latestBlockHash) {
|
||||
this.latestBlockHash = ei.blockHash;
|
||||
return await callback?.(ei);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected async getLatestExecution(): Promise<ExecutionInfo | null> {
|
||||
await this._sync();
|
||||
const update = await this.options.optimisticUpdateCallback();
|
||||
|
||||
const verify = await optimisticUpdateVerify(
|
||||
this.latestCommittee as Uint8Array[],
|
||||
update,
|
||||
);
|
||||
// TODO: check the update against the latest sync committee
|
||||
if (!verify.correct) {
|
||||
console.error(`Invalid Optimistic Update: ${verify.reason}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
this._latestOptimisticUpdate =
|
||||
capella.ssz.LightClientOptimisticUpdate.serialize(update);
|
||||
|
||||
console.log(
|
||||
`Optimistic update verified for slot ${update.attestedHeader.beacon.slot}`,
|
||||
);
|
||||
|
||||
return {
|
||||
blockHash: toHexString(update.attestedHeader.execution.blockHash),
|
||||
blockNumber: update.attestedHeader.execution.blockNumber,
|
||||
};
|
||||
}
|
||||
|
||||
protected async syncUpdateVerifyGetCommittee(
|
||||
prevCommittee: Uint8Array[],
|
||||
period: number,
|
||||
|
|
Loading…
Reference in New Issue