Compare commits

..

No commits in common. "v0.1.0-develop.29" and "v0.1.0-develop.28" have entirely different histories.

4 changed files with 55 additions and 73 deletions

View File

@ -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) # [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)

4
npm-shrinkwrap.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@lumeweb/libethclient", "name": "@lumeweb/libethclient",
"version": "0.1.0-develop.29", "version": "0.1.0-develop.28",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@lumeweb/libethclient", "name": "@lumeweb/libethclient",
"version": "0.1.0-develop.29", "version": "0.1.0-develop.28",
"dependencies": { "dependencies": {
"@chainsafe/as-sha256": "^0.3.1", "@chainsafe/as-sha256": "^0.3.1",
"@chainsafe/bls": "7.1.1", "@chainsafe/bls": "7.1.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@lumeweb/libethsync", "name": "@lumeweb/libethsync",
"version": "0.1.0-develop.29", "version": "0.1.0-develop.28",
"type": "module", "type": "module",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -15,7 +15,6 @@ import {
} from "#util.js"; } from "#util.js";
import { LightClientUpdate, OptimisticUpdateCallback } from "#types.js"; import { LightClientUpdate, OptimisticUpdateCallback } from "#types.js";
import { assertValidLightClientUpdate } from "@lodestar/light-client/validation"; import { assertValidLightClientUpdate } from "@lodestar/light-client/validation";
import * as capella from "@lodestar/types/capella";
export interface BaseClientOptions { export interface BaseClientOptions {
prover: IProver; prover: IProver;
@ -40,12 +39,6 @@ export default abstract class BaseClient {
this.options = options; this.options = options;
} }
private _latestOptimisticUpdate?: Uint8Array;
get latestOptimisticUpdate(): Uint8Array {
return this._latestOptimisticUpdate as Uint8Array;
}
private _latestPeriod: number = -1; private _latestPeriod: number = -1;
get latestPeriod(): number { get latestPeriod(): number {
@ -86,6 +79,58 @@ export default abstract class BaseClient {
return this.getNextValidExecutionInfo(retry - 1); 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( async syncProver(
startPeriod: number, startPeriod: number,
currentPeriod: 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( protected async syncUpdateVerifyGetCommittee(
prevCommittee: Uint8Array[], prevCommittee: Uint8Array[],
period: number, period: number,