-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for browser bundle for lightclient (#6673)
* Reorganize the code so it is accessible from one package * Add support for browser build for lightclient * Update the build config * Improve the bls vite plugin * Restructure the vite and vitest scripts * Simplify vite config * Remove unused polyfill * Fix the doc lint error * Add support for bundle test * Update the package json files * Add dist build to default build task * Fix spelling in the docs * Fix the lint error * Fix type error * Disable eslint errors * Increase the timeout for bundle test * Fix eslint bundle * Fix lint warning * Remove the unused config * Add the default export to bundle * Enable compression on th build * Update packages/light-client/README.md Co-authored-by: Nico Flaig <nflaig@protonmail.com> * Increase timeout for one test * Optimize package build task * Update the readme --------- Co-authored-by: Nico Flaig <nflaig@protonmail.com>
- Loading branch information
1 parent
e1bc926
commit 02e28cf
Showing
28 changed files
with
483 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This file exists to have proper namespace for the web bundle | ||
export * from "./transport/index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This file exists to have proper namespace for the web bundle | ||
export * from "./utils/index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {getClient, Api} from "@lodestar/api"; | ||
import {ChainForkConfig, createChainForkConfig} from "@lodestar/config"; | ||
import {NetworkName, networksChainConfig} from "@lodestar/config/networks"; | ||
|
||
export function getApiFromUrl(url: string, network: NetworkName): Api { | ||
if (!(network in networksChainConfig)) { | ||
throw Error(`Invalid network name "${network}". Valid options are: ${Object.keys(networksChainConfig).join()}`); | ||
} | ||
|
||
return getClient({urls: [url]}, {config: createChainForkConfig(networksChainConfig[network])}); | ||
} | ||
|
||
export function getChainForkConfigFromNetwork(network: NetworkName): ChainForkConfig { | ||
if (!(network in networksChainConfig)) { | ||
throw Error(`Invalid network name "${network}". Valid options are: ${Object.keys(networksChainConfig).join()}`); | ||
} | ||
|
||
return createChainForkConfig(networksChainConfig[network]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/light-client/test/unit/webEsmBundle.browser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call */ | ||
import {expect, describe, it, beforeEach, vi} from "vitest"; | ||
import "../../dist/lightclient.min.mjs"; | ||
|
||
describe("web bundle for lightclient", () => { | ||
vi.setConfig({testTimeout: 20_000}); | ||
|
||
let lightclient: any; | ||
|
||
beforeEach(() => { | ||
lightclient = (window as any)["lodestar"]["lightclient"]; | ||
}); | ||
|
||
it("should have a global interface", () => { | ||
expect(lightclient).toBeDefined(); | ||
}); | ||
|
||
it("should have all relevant exports", () => { | ||
expect(lightclient).toHaveProperty("Lightclient"); | ||
expect(lightclient).toHaveProperty("LightclientEvent"); | ||
expect(lightclient).toHaveProperty("RunStatusCode"); | ||
expect(lightclient).toHaveProperty("upgradeLightClientFinalityUpdate"); | ||
expect(lightclient).toHaveProperty("upgradeLightClientOptimisticUpdate"); | ||
expect(lightclient).toHaveProperty("utils"); | ||
expect(lightclient).toHaveProperty("transport"); | ||
expect(lightclient).toHaveProperty("validation"); | ||
|
||
expect(lightclient.Lightclient).toBeTypeOf("function"); | ||
}); | ||
|
||
it("should start the lightclient and sync", async () => { | ||
const {Lightclient, LightclientEvent, transport, utils} = lightclient; | ||
|
||
const logger = utils.getConsoleLogger({logDebug: true}); | ||
const config = utils.getChainForkConfigFromNetwork("mainnet"); | ||
|
||
// TODO: Decide to check which node to use in testing | ||
// We have one node in CI, but that only starts with e2e tests | ||
const api = utils.getApiFromUrl("https://lodestar-mainnet.chainsafe.io", "mainnet"); | ||
|
||
const lc = await Lightclient.initializeFromCheckpointRoot({ | ||
config, | ||
logger, | ||
transport: new transport.LightClientRestTransport(api), | ||
genesisData: await utils.getGenesisData(api), | ||
checkpointRoot: await utils.getFinalizedSyncCheckpoint(api), | ||
opts: { | ||
allowForcedUpdates: true, | ||
updateHeadersOnForcedUpdate: true, | ||
}, | ||
}); | ||
|
||
await expect(lc.start()).resolves.toBeUndefined(); | ||
|
||
await expect( | ||
new Promise((resolve) => { | ||
lc.emitter.on(LightclientEvent.lightClientOptimisticHeader, async (optimisticUpdate: unknown) => { | ||
resolve(optimisticUpdate); | ||
}); | ||
}) | ||
).resolves.toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"exclude": ["src/index.browser.ts"], | ||
"compilerOptions": {} | ||
} |
Oops, something went wrong.