ChainSafe / ChainSafe/lodestar

Prepare light-client for multi-environment support

Open
#6,486 0 comments 0 reactions 0 assignees View on GitHub
prio-low scope-devex scope-light-clients
Dominant language
TypeScript
Stars
1.4k
Forks
483
Avg merge
1d 16h
Merged PRs (30d)
150

Description

Lodestar relies heavily on `nodejs` specifics (e.g. `Buffer` or native modules). This allows to provide good performance for client artifacts (e.g. `beacon-node`, `validator`).

The `light-client` package can be used independently in a number of alternative environment (e.g. `browser`, `ReactNative`). While such usage can be made to [work](https://github.com/ChainSafe/eth2-light-client-demo/) it requires polyfilling and dark magic heavily tool dependent.

According to my tests, here is the current support of `light-client` for a number of well-known bundlers/platforms:

| Platform | Status | Issue |
| ------------- | ------------- |------------- |
| webpack5 | ✅ | works for `light-client`; doesn't support `node:*` syntax |
| docusaurus | ❓ | |
| AWS Lambda | ❓ | |
| vite/astro | ❌ | Buffer usage |
| parcel | ❌ | top-level-await (in `bls`) |
| bun | ❌ | napi issues with `bls` |
| ReactNative | ❌ | exports |
| CloudFlare workers | ❌ | wasm limitations |

Make sure we document supported platforms.

Different options should be pursued to improve this:

* reducing `lodestar` libs inter-dependency (only rely on env safe libs)
* reduce external dependencies; they should follow those same principles
* reduce usage of `node` modules; follow principled patterns easing their `polyfilling` when are mandatory

# Inter-packages dependencies

* [x] do not depend on `state-transition`
* [ ] https://github.com/ChainSafe/lodestar/issues/6542
* [ ] extract `cli` specific from `utils`
* [ ] extract `node` specific from `logger` (e.g. winston related)

# NodeJS modules

* [ ] document patterns detailing preferred usage per module

In general:

* reduce dependencies on nodejs modules, when possible:
* use global [crypto](https://nodejs.org/api/webcrypto.html, available by default starting `node20`, [1](https://github.com/libp2p/js-libp2p-crypto/issues/46))
* replace `events` with [mitt](https://github.com/developit/mitt) or [tseep](https://github.com/Morglod/tseep)
* consider creating helper function abstracting away node modules usage (similarly to what [uint8arrays](https://github.com/achingbrain/uint8arrays/blob/main/src/util/as-uint8array.ts#L5) does)

It makes it simpler for end users of no shim are required.

`light-client` directly or indirectly depends on the following NodeJS modules:

## From`@chainsafe/blst` https://github.com/ChainSafe/lodestar/pull/6519

* crypto
* path

## From `bls-eth-wasm`

* crypto https://github.com/ChainSafe/bls/pull/158

## From `@chainsafe/bls`

* crypto https://github.com/herumi/bls-eth-wasm/pull/23, https://github.com/ChainSafe/bls/pull/159

## From `eventsource`

* url
* https
* http
* util

# External dependencies

* [ ] use [bls switchable](https://github.com/ChainSafe/bls/tree/master?tab=readme-ov-file#switchable-singleton) pattern; alternatively create an isomorphic `blst` (with `wasm` port of `supranational`); drop `bls` dependency https://github.com/ChainSafe/bls/issues/157

# Weight analysis

Capture d’écran 2024-03-19 à 11 53 05

See [bundlejs](https://bundlejs.com/?q=%40lodestar%2Flight-client%401.17.0&treeshake=%5B*%5D&config=%7B%22analysis%22%3Atrue%7D)

## Heavy packages

* bls herumi
* js-yaml, from `utils`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.