Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lodestar prover for execution api #5222

Merged
merged 30 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5a91f58
Add package skeleton
nazarhussain Feb 27, 2023
c5cdd6b
Add implementation for the verification
nazarhussain Mar 1, 2023
9485021
Update package versions
nazarhussain Mar 1, 2023
6cf67ee
Update teh provdier structure to store full execution payload
nazarhussain Mar 6, 2023
ef3036e
Add a test script
nazarhussain Mar 6, 2023
e1e5332
Split the utils to scoped files
nazarhussain Mar 6, 2023
632d99a
Add multiple web3 providers
nazarhussain Mar 6, 2023
cfdffbe
Add a proxy factory method
nazarhussain Mar 8, 2023
36302e0
Add the CLI for the prover proxy
nazarhussain Mar 8, 2023
d3babc1
Rename few functions to make those consistent
nazarhussain Mar 8, 2023
b31b7ab
Add some required unit tests
nazarhussain Mar 8, 2023
4962a28
Add unit tests
nazarhussain Mar 8, 2023
f929a0e
Add e2e tests
nazarhussain Mar 8, 2023
41faea2
Fix duplicate Buffer error
nazarhussain Mar 9, 2023
7472405
Fix lint error
nazarhussain Mar 9, 2023
1e558fc
Fix lint errors
nazarhussain Mar 9, 2023
585e447
Update the lightclient to sync in background
nazarhussain Mar 10, 2023
8c038cf
Validate the execution payload
nazarhussain Mar 10, 2023
c1acac4
Update initWithRest to init
nazarhussain Mar 13, 2023
25f62e6
Update the max limit for the payloads to not cross finalized slot
nazarhussain Mar 13, 2023
be64793
Remove the usage for finalizedPayloadHeaders tracking
nazarhussain Mar 13, 2023
021cb97
Rename update to lcHeader
nazarhussain Mar 13, 2023
4f7dfd3
Update the code as per feedback
nazarhussain Mar 16, 2023
f193c08
Fix readme
nazarhussain Mar 17, 2023
8d14640
Update the payload store logic
nazarhussain Mar 17, 2023
ec20372
Add the cleanup logic to payload store
nazarhussain Mar 17, 2023
07fd574
Update the code as per feedback
nazarhussain Mar 25, 2023
1a7e951
Fix few types in the tests
nazarhussain Mar 26, 2023
6f15c64
Move the usage to isForkWithdrawls
nazarhussain Mar 26, 2023
621363b
Fix a unit test
nazarhussain Mar 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/light-client/src/events.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {allForks} from "@lodestar/types";
import {RunStatusCode} from "./index.js";

export enum LightclientEvent {
lightClientOptimisticUpdate = "light_client_optimistic_update",
lightClientFinalityUpdate = "light_client_finality_update",
lightClientOptimisticHeader = "light_client_optimistic_header",
lightClientFinalityHeader = "light_client_finality_header",
statusChange = "light_client_status_change",
}

export type LightclientEmitterEvents = {
[LightclientEvent.lightClientOptimisticUpdate]: (newHeader: allForks.LightClientHeader) => void;
[LightclientEvent.lightClientFinalityUpdate]: (newHeader: allForks.LightClientHeader) => void;
[LightclientEvent.lightClientOptimisticHeader]: (newHeader: allForks.LightClientHeader) => void;
[LightclientEvent.lightClientFinalityHeader]: (newHeader: allForks.LightClientHeader) => void;
[LightclientEvent.statusChange]: (code: RunStatusCode) => void;
};

export type LightclientEmitter = MittEmitter<LightclientEmitterEvents>;
Expand Down
44 changes: 30 additions & 14 deletions packages/light-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ const ON_ERROR_RETRY_MS = 1000;
// TODO: Customize with option
const ALLOW_FORCED_UPDATES = true;

enum RunStatusCode {
export enum RunStatusCode {
uninitialized,
started,
syncing,
stopped,
}
type RunStatus =
| {code: RunStatusCode.uninitialized}
| {code: RunStatusCode.started; controller: AbortController}
| {code: RunStatusCode.syncing}
| {code: RunStatusCode.stopped};
Expand Down Expand Up @@ -104,7 +106,7 @@ export class Lightclient {

private readonly lightclientSpec: LightclientSpec;

private status: RunStatus = {code: RunStatusCode.stopped};
private runStatus: RunStatus = {code: RunStatusCode.stopped};

constructor({config, logger, genesisData, bootstrap, transport}: LightclientInitArgs) {
this.genesisTime = genesisData.genesisTime;
Expand All @@ -116,24 +118,29 @@ export class Lightclient {
this.config = createBeaconConfig(config, this.genesisValidatorsRoot);
this.logger = logger ?? getLcLoggerConsole();
this.transport = transport;
this.runStatus = {code: RunStatusCode.uninitialized};

this.lightclientSpec = new LightclientSpec(
this.config,
{
allowForcedUpdates: ALLOW_FORCED_UPDATES,
onSetFinalizedHeader: (header) => {
this.emitter.emit(LightclientEvent.lightClientFinalityUpdate, header);
this.emitter.emit(LightclientEvent.lightClientFinalityHeader, header);
this.logger.debug("Updated state.finalizedHeader", {slot: header.beacon.slot});
},
onSetOptimisticHeader: (header) => {
this.emitter.emit(LightclientEvent.lightClientOptimisticUpdate, header);
this.emitter.emit(LightclientEvent.lightClientOptimisticHeader, header);
this.logger.debug("Updated state.optimisticHeader", {slot: header.beacon.slot});
},
},
bootstrap
);
}

get status(): RunStatusCode {
return this.runStatus.code;
}

// Embed lightweight clock. The epoch cycles are handled with `this.runLoop()`
get currentSlot(): number {
return getCurrentSlot(this.config, this.genesisTime);
Expand Down Expand Up @@ -167,16 +174,20 @@ export class Lightclient {
}

stop(): void {
if (this.status.code !== RunStatusCode.started) return;
if (this.runStatus.code !== RunStatusCode.started) return;

this.status.controller.abort();
this.status = {code: RunStatusCode.stopped};
this.runStatus.controller.abort();
this.updateRunStatus({code: RunStatusCode.stopped});
}

getHead(): allForks.LightClientHeader {
return this.lightclientSpec.store.optimisticHeader;
}

getFinalized(): allForks.LightClientHeader {
return this.lightclientSpec.store.finalizedHeader;
}

async sync(fromPeriod: SyncPeriod, toPeriod: SyncPeriod): Promise<void> {
// Initialize the BLS implementation. This may requires initializing the WebAssembly instance
// so why it's a an async process. This should be initialized once before any bls operations.
Expand Down Expand Up @@ -212,12 +223,12 @@ export class Lightclient {
// Check if we have a sync committee for the current clock period
if (!this.lightclientSpec.store.syncCommittees.has(currentPeriod)) {
// Stop head tracking
if (this.status.code === RunStatusCode.started) {
this.status.controller.abort();
if (this.runStatus.code === RunStatusCode.started) {
this.runStatus.controller.abort();
}

// Go into sync mode
this.status = {code: RunStatusCode.syncing};
this.updateRunStatus({code: RunStatusCode.syncing});
const headPeriod = computeSyncPeriodAtSlot(this.getHead().beacon.slot);
this.logger.debug("Syncing", {lastPeriod: headPeriod, currentPeriod});

Expand All @@ -243,9 +254,9 @@ export class Lightclient {
}

// After successfully syncing, track head if not already
if (this.status.code !== RunStatusCode.started) {
if (this.runStatus.code !== RunStatusCode.started) {
const controller = new AbortController();
this.status = {code: RunStatusCode.started, controller};
this.updateRunStatus({code: RunStatusCode.started, controller});
this.logger.debug("Started tracking the head");

this.transport.onOptimisticUpdate(this.processOptimisticUpdate.bind(this));
Expand All @@ -268,11 +279,11 @@ export class Lightclient {
}

// Wait for the next epoch
if (this.status.code !== RunStatusCode.started) {
if (this.runStatus.code !== RunStatusCode.started) {
return;
} else {
try {
await sleep(timeUntilNextEpoch(this.config, this.genesisTime), this.status.controller.signal);
await sleep(timeUntilNextEpoch(this.config, this.genesisTime), this.runStatus.controller.signal);
} catch (e) {
if (isErrorAborted(e)) {
return;
Expand Down Expand Up @@ -306,4 +317,9 @@ export class Lightclient {
private currentSlotWithTolerance(): Slot {
return slotWithFutureTolerance(this.config, this.genesisTime, MAX_CLOCK_DISPARITY_SEC);
}

private updateRunStatus(runStatus: RunStatus): void {
this.runStatus = runStatus;
this.emitter.emit(LightclientEvent.statusChange, this.runStatus.code);
}
}
4 changes: 2 additions & 2 deletions packages/light-client/test/unit/sync.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe("sync", () => {

// Sync periods to current
await new Promise<void>((resolve) => {
lightclient.emitter.on(LightclientEvent.lightClientFinalityUpdate, (header) => {
lightclient.emitter.on(LightclientEvent.lightClientFinalityHeader, (header) => {
if (computeSyncPeriodAtSlot(header.beacon.slot) >= targetPeriod) {
resolve();
}
Expand All @@ -128,7 +128,7 @@ describe("sync", () => {
// Track head + reference states with some known data
const syncCommittee = getInteropSyncCommittee(targetPeriod);
await new Promise<void>((resolve) => {
lightclient.emitter.on(LightclientEvent.lightClientOptimisticUpdate, (header) => {
lightclient.emitter.on(LightclientEvent.lightClientOptimisticHeader, (header) => {
if (header.beacon.slot === targetSlot) {
resolve();
}
Expand Down
1 change: 1 addition & 0 deletions packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
ForkExecution,
ForkBlobs,
isForkExecution,
isForkWithdrawals,
isForkBlobs,
isForkLightClient,
} from "./forkName.js";
Expand Down
10 changes: 10 additions & 0 deletions packages/prover/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules/
lib
.nyc_output/
coverage/**
.DS_Store
*.swp
.idea
yarn-error.log
package-lock.json
dist*
8 changes: 8 additions & 0 deletions packages/prover/.mocharc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
colors: true
timeout: 2000
exit: true
extension: ["ts"]
require:
- ./test/setup.ts
node-option:
- "loader=ts-node/esm"
3 changes: 3 additions & 0 deletions packages/prover/.nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../.nycrc.json"
}
Loading