|
4 | 4 |
|
5 | 5 | ## Usage |
6 | 6 |
|
7 | | -See usage in the `lodestar` package here: |
| 7 | +This package provides functionality to read and write [era files](https://github.com/eth-clients/e2store-format-specs/blob/main/formats/era.md), which are based on the [e2store format](https://github.com/status-im/nimbus-eth2/blob/stable/docs/e2store.md#introduction). |
| 8 | + |
| 9 | +### Reading/Writing e2s files |
| 10 | + |
| 11 | +```ts |
| 12 | +import {open} from "node:fs/promises"; |
| 13 | +import {e2s} from "@lodestar/era"; |
| 14 | + |
| 15 | +const fh = await open("mainnet-xxxxxx-xxxxxxxx.era"); |
| 16 | +const entry = await e2s.readEntry(fh, 0); |
| 17 | +entry.type == e2s.EntryType.Version; |
| 18 | +``` |
| 19 | + |
| 20 | +### Reading era files |
| 21 | + |
| 22 | +```ts |
| 23 | +import {era} from "@lodestar/era"; |
| 24 | +import {config} from "@lodestar/config/default"; |
| 25 | + |
| 26 | +// open reader |
| 27 | +const reader = await era.EraReader.open(config, "mainnet-xxxxx-xxxxxxxx.era"); |
| 28 | + |
| 29 | +// check number of groups |
| 30 | +reader.groups.length === 1; |
| 31 | + |
| 32 | +// read blocks |
| 33 | +const slot = reader.groups[0].startSlot; |
| 34 | + |
| 35 | +// return snappy-frame compressed, ssz-serialized block at slot or null if a skip slot |
| 36 | +// throws if out of range |
| 37 | +await reader.readCompressedBlock(slot); |
| 38 | +// same, but for ssz-serialized block |
| 39 | +await reader.readSerializedBlock(slot); |
| 40 | +// same but for deserialized block |
| 41 | +await reader.readBlock(slot); |
| 42 | + |
| 43 | +// read state(s), one per group |
| 44 | +// similar api to blocks, but with an optional eraNumber param for specifying which group's state to read |
| 45 | +await reader.readCompressedState(); |
| 46 | +await reader.readSerializedState(); |
| 47 | +await reader.readState(); |
| 48 | +``` |
| 49 | + |
| 50 | +### Writing era files |
| 51 | + |
| 52 | +```ts |
| 53 | +import {era} from "@lodestar/era"; |
| 54 | +import {config} from "@lodestar/config/default"; |
| 55 | + |
| 56 | +const writer = await era.EraWriter.create(config, "path/to/era"); |
| 57 | + |
| 58 | +// similar api to reader, can write compressed, serialized, or deserialized items |
| 59 | +// first write all blocks for the era |
| 60 | +await writer.writeBlock(block); |
| 61 | +// ... |
| 62 | +// then write the state |
| 63 | +await writer.writeState(state); |
| 64 | +// if applicable, continue writing eras of blocks and state (an era file can contain multiple eras, or "groups" as the spec states) |
| 65 | +// when finished, must call `finish`, which will close the file handler and rename the file to the spec-compliant name |
| 66 | +await writer.finish(); |
| 67 | +``` |
8 | 68 |
|
9 | 69 | ## License |
10 | 70 |
|
|
0 commit comments