-
-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,14 +41,14 @@ It is possible to start up the light-client as a standalone process. | |
|
||
```bash | ||
lodestar lightclient \ | ||
--network mainnet \ | ||
--beacon-api-url https://beacon-node.your-domain.com \ | ||
--network sepolia \ | ||
--beacon-api-url https://lodestar-sepolia.chainsafe.io \ | ||
--checkpoint-root "0xccaff4b99986a7b05e06738f1828a32e40799b277fd9f9ff069be55341fe0229" | ||
``` | ||
|
||
## Light-Client Programmatic Example | ||
|
||
For this example we will assume there is a running beacon node at `https://beacon-node.your-domain.com` | ||
For this example we will assume there is a running beacon node at `https://lodestar-sepolia.chainsafe.io` | ||
|
||
If you are running light-client on a server/node environment there is a faster version of bls that can help with performance. It is a peerDependency and needs to be installed separately by the consumer of this package. This was done so that for browser situations there is not a hard requirement for node-only code that will cause bundling errors. On startup, if running in a node environment, and `@chainsafe/blst` is installed the LightClient will automatically use the faster bls bindings. | ||
|
||
|
@@ -57,16 +57,19 @@ npm i -S @chainsafe/blst | |
``` | ||
|
||
```ts | ||
import {getClient} from "@lodestar/api"; | ||
import {createChainForkConfig} from "@lodestar/config"; | ||
import {networksChainConfig} from "@lodestar/config/networks"; | ||
import {Lightclient, LightclientEvent} from "@lodestar/light-client"; | ||
import {LightClientRestTransport} from "@lodestar/light-client/transport"; | ||
import {getFinalizedSyncCheckpoint, getGenesisData, getLcLoggerConsole} from "@lodestar/light-client/utils"; | ||
|
||
const config = createChainForkConfig(networksChainConfig.mainnet); | ||
const logger = getLcLoggerConsole({logDebug: Boolean(process.env.DEBUG)}); | ||
const api = getClient({urls: ["https://beacon-node.your-domain.com"]}, {config}); | ||
import { | ||
getFinalizedSyncCheckpoint, | ||
getGenesisData, | ||
getConsoleLogger, | ||
getApiFromUrl, | ||
getChainForkConfigFromNetwork, | ||
} from "@lodestar/light-client/utils"; | ||
|
||
const config = getChainForkConfigFromNetwork("sepolia"); | ||
const logger = getConsoleLogger({logDebug: Boolean(process.env.DEBUG)}); | ||
const api = getApiFromUrl({urls: ["https://lodestar-sepolia.chainsafe.io"]}, {config}); | ||
|
||
const lightclient = await Lightclient.initializeFromCheckpointRoot({ | ||
config, | ||
|
@@ -94,6 +97,31 @@ lightclient.emitter.on(LightclientEvent.lightClientOptimisticHeader, async (opti | |
}); | ||
``` | ||
|
||
## Browser Integration | ||
|
||
If you want to use Lightclient in browser and facing some issues in building it with bundlers like webpack, vite. We suggest to use our distribution build. The support for single distribution build is started from `1.19.0` version. | ||
|
||
Directly link the dist build with the `<script />` tag with tools like unpkg or other. e.g. | ||
|
||
```html | ||
<script src="https://www.unpkg.com/@lodestar/[email protected]/dist/lightclient.es.min.js" type="module"> | ||
``` | ||
Then the lightclient package will be exposed to `globalThis`, in case of browser environment that will be `window`. You can access the package as `window.lodestar.lightclient`. All named exports will also be available from this interface. e.g. `window.lodestar.lightclient.transport`. | ||
NOTE: Due to `top-level-await` used in one of dependent library, the package will not be available right after the load. You have to use a hack to clear up that await from the event loop. | ||
```html | ||
<script> | ||
window.addEventListener("DOMContentLoaded", () => { | ||
setTimeout(function () { | ||
// here you can access the Lightclient | ||
// window.lodestar.lightclient | ||
}, 50); | ||
}); | ||
</script> | ||
``` | ||
|
||
## Contributors | ||
|
||
Read our [contribution documentation](https://chainsafe.github.io/lodestar/contribution/getting-started), [submit an issue](https://github.com/ChainSafe/lodestar/issues/new/choose) or talk to us on our [discord](https://discord.gg/yjyvFRP)! | ||
|
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.