@lodestar/era 1.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # `@lodestar/era`
2
+
3
+ > This package is part of [ChainSafe's Lodestar](https://lodestar.chainsafe.io) project
4
+
5
+ ## Usage
6
+
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].blocksIndex?.startSlot ?? 0;
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
+ import {SignedBeaconBlock, BeaconState} from "@lodestar/types";
56
+
57
+ const writer = await era.EraWriter.create(config, "path/to/era", 0);
58
+
59
+ // similar api to reader, can write compressed, serialized, or deserialized items
60
+ // first write all blocks for the era
61
+ // Assuming `block` is a SignedBeaconBlock
62
+ declare const block: SignedBeaconBlock;
63
+ await writer.writeBlock(block);
64
+ // ...
65
+ // then write the state
66
+ // Assuming `state` is a BeaconState
67
+ declare const state: BeaconState;
68
+ await writer.writeState(state);
69
+ // if applicable, continue writing eras of blocks and state (an era file can contain multiple eras, or "groups" as the spec states)
70
+ // when finished, must call `finish`, which will close the file handler and rename the file to the spec-compliant name
71
+ await writer.finish();
72
+ ```
73
+
74
+ ## License
75
+
76
+ Apache-2.0 [ChainSafe Systems](https://chainsafe.io)
package/lib/e2s.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ import type { FileHandle } from "node:fs/promises";
2
+ import { Slot } from "@lodestar/types";
3
+ /**
4
+ * Known entry types in an E2Store (.e2s) file along with their exact 2-byte codes.
5
+ */
6
+ export declare enum EntryType {
7
+ Empty = 0,
8
+ CompressedSignedBeaconBlock = 1,
9
+ CompressedBeaconState = 2,
10
+ Version = 12901,// "e2" in ASCII
11
+ SlotIndex = 12905
12
+ }
13
+ /**
14
+ * Logical, parsed entry from an E2Store file.
15
+ */
16
+ export interface Entry {
17
+ type: EntryType;
18
+ data: Uint8Array;
19
+ }
20
+ /**
21
+ * Maps slots to file positions in an era file.
22
+ * - Block index: count = SLOTS_PER_HISTORICAL_ROOT, maps slots to blocks
23
+ * - State index: count = 1, points to the era state
24
+ * - Zero offset = empty slot (no block)
25
+ */
26
+ export interface SlotIndex {
27
+ type: EntryType.SlotIndex;
28
+ /** First slot covered by this index (era * SLOTS_PER_HISTORICAL_ROOT) */
29
+ startSlot: Slot;
30
+ /** File positions where data can be found. Length varies by index type. */
31
+ offsets: number[];
32
+ /** File position where this index record starts */
33
+ recordStart: number;
34
+ }
35
+ /**
36
+ * The complete version record (8 bytes total).
37
+ */
38
+ export declare const VERSION_RECORD_BYTES: Uint8Array<ArrayBuffer>;
39
+ /**
40
+ * E2Store header size in bytes
41
+ */
42
+ export declare const E2STORE_HEADER_SIZE = 8;
43
+ /**
44
+ * Helper to read entry at a specific offset from an open file handle.
45
+ * Reads header first to determine data length, then reads the complete entry.
46
+ */
47
+ export declare function readEntry(fh: FileHandle, offset: number): Promise<Entry>;
48
+ /**
49
+ * Read an e2Store entry (header + data)
50
+ * Header: 2 bytes type + 4 bytes length (LE) + 2 bytes reserved (must be 0)
51
+ */
52
+ export declare function parseEntryHeader(header: Uint8Array): {
53
+ type: EntryType;
54
+ length: number;
55
+ };
56
+ export declare function readVersion(fh: FileHandle, offset: number): Promise<void>;
57
+ /**
58
+ * Read a SlotIndex from a file handle.
59
+ */
60
+ export declare function readSlotIndex(fh: FileHandle, offset: number): Promise<SlotIndex>;
61
+ /**
62
+ * Write a single E2Store TLV entry (header + payload)
63
+ * Header layout: type[2] | length u32 LE | reserved u16(=0)
64
+ */
65
+ export declare function writeEntry(fh: FileHandle, offset: number, type: EntryType, payload: Uint8Array): Promise<void>;
66
+ export declare function writeVersion(fh: FileHandle, offset: number): Promise<void>;
67
+ export declare function serializeSlotIndex(slotIndex: SlotIndex): Uint8Array;
68
+ //# sourceMappingURL=e2s.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"e2s.d.ts","sourceRoot":"","sources":["../src/e2s.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAC,IAAI,EAAC,MAAM,iBAAiB,CAAC;AAGrC;;GAEG;AACH,oBAAY,SAAS;IACnB,KAAK,IAAI;IACT,2BAA2B,IAAI;IAC/B,qBAAqB,IAAI;IACzB,OAAO,QAAqB,CAAE,gBAAgB;IAC9C,SAAS,QAAqB;CAC/B;AACD;;GAEG;AACH,MAAM,WAAW,K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.js ADDED
@@ -0,0 +1,129 @@
1
+ import { readInt48, readUint16, readUint32, writeInt48, writeUint16, writeUint32 } from "./util.js";
2
+ /**
3
+ * Known entry types in an E2Store (.e2s) file along with their exact 2-byte codes.
4
+ */
5
+ export var EntryType;
6
+ (function (EntryType) {
7
+ EntryType[EntryType["Empty"] = 0] = "Empty";
8
+ EntryType[EntryType["CompressedSignedBeaconBlock"] = 1] = "CompressedSignedBeaconBlock";
9
+ EntryType[EntryType["CompressedBeaconState"] = 2] = "CompressedBeaconState";
10
+ EntryType[EntryType["Version"] = 12901] = "Version";
11
+ EntryType[EntryType["SlotIndex"] = 12905] = "SlotIndex";
12
+ })(EntryType || (EntryType = {}));
13
+ /**
14
+ * The complete version record (8 bytes total).
15
+ */
16
+ export const VERSION_RECORD_BYTES = new Uint8Array([0x65, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
17
+ /**
18
+ * E2Store header size in bytes
19
+ */
20
+ export const E2STORE_HEADER_SIZE = 8;
21
+ /**
22
+ * Helper to read entry at a specific offset from an open file handle.
23
+ * Reads header first to determine data length, then reads the complete entry.
24
+ */
25
+ export async function readEntry(fh, offset) {
26
+ // Read header (8 bytes)
27
+ const header = new Uint8Array(E2STORE_HEADER_SIZE);
28
+ await fh.read(header, 0, E2STORE_HEADER_SIZE, offset);
29
+ const { type, length } = parseEntryHeader(header);
30
+ // Read entry payload/data
31
+ const data = new Uint8Array(length);
32
+ await fh.read(data, 0, data.length, offset + E2STORE_HEADER_SIZE);
33
+ return { type, data };
34
+ }
35
+ /**
36
+ * Read an e2Store entry (header + data)
37
+ * Header: 2 bytes type + 4 bytes length (LE) + 2 bytes reserved (must be 0)
38
+ */
39
+ export function parseEntryHeader(header) {
40
+ if (header.length < E2STORE_HEADER_SIZE) {
41
+ throw new Error(`Buffer too small for E2Store header: need ${E2STORE_HEADER_SIZE} bytes, got ${header.length}`);
42
+ }
43
+ // validate entry type from first 2 bytes
44
+ const typeCode = readUint16(header, 0);
45
+ if (!(typeCode in EntryType)) {
46
+ throw new Error(`Unknown E2Store entry type: 0x${typeCode.toString(16)}`);
47
+ }
48
+ const type = typeCode;
49
+ // Parse data length from next 4 bytes (offset 2, little endian)
50
+ const length = readUint32(header, 2);
51
+ // Validate reserved bytes are zero (offset 6-7)
52
+ const reserved = readUint16(header, 6);
53
+ if (reserved !== 0) {
54
+ throw new Error(`E2Store reserved bytes must be zero, got: ${reserved}`);
55
+ }
56
+ return { type, length };
57
+ }
58
+ export async function readVersion(fh, offset) {
59
+ const versionHeader = new Uint8Array(E2STORE_HEADER_SIZE);
60
+ await fh.read(versionHeader, 0, E2STORE_HEADER_SIZE, offset);
61
+ if (Buffer.compare(versionHeader, VERSION_RECORD_BYTES) !== 0) {
62
+ throw new Error("Invalid E2Store version record");
63
+ }
64
+ }
65
+ /**
66
+ * Read a SlotIndex from a file handle.
67
+ */
68
+ export async function readSlotIndex(fh, offset) {
69
+ const recordEnd = offset;
70
+ const countBuffer = new Uint8Array(8);
71
+ await fh.read(countBuffer, 0, 8, recordEnd - 8);
72
+ const count = readInt48(countBuffer, 0);
73
+ const recordStart = recordEnd - (8 * count + 24);
74
+ // Validate index position is within file bounds
75
+ if (recordStart < 0) {
76
+ throw new Error(`SlotIndex position ${recordStart} is invalid - file too small for count=${count}`);
77
+ }
78
+ // Read and validate the slot index entry
79
+ const entry = await readEntry(fh, recordStart);
80
+ if (entry.type !== EntryType.SlotIndex) {
81
+ throw new Error(`Expected SlotIndex entry, got ${entry.type}`);
82
+ }
83
+ // Size: startSlot(8) + offsets(count*8) + count(8) = count*8 + 16
84
+ const expectedSize = 8 * count + 16;
85
+ if (entry.data.length !== expectedSize) {
86
+ throw new Error(`SlotIndex payload size must be exactly ${expectedSize} bytes, got ${entry.data.length}`);
87
+ }
88
+ // Parse start slot from payload
89
+ const startSlot = readInt48(entry.data, 0);
90
+ const offsets = [];
91
+ for (let i = 0; i < count; i++) {
92
+ offsets.push(readInt48(entry.data, 8 * i + 8));
93
+ }
94
+ return {
95
+ type: EntryType.SlotIndex,
96
+ startSlot,
97
+ offsets,
98
+ recordStart,
99
+ };
100
+ }
101
+ /**
102
+ * Write a single E2Store TLV entry (header + payload)
103
+ * Header layout: type[2] | length u32 LE | reserved u16(=0)
104
+ */
105
+ export async function writeEntry(fh, offset, type, payload) {
106
+ const header = new Uint8Array(E2STORE_HEADER_SIZE);
107
+ writeUint16(header, 0, type); // type (2 bytes)
108
+ writeUint32(header, 2, payload.length); // length (4 bytes)
109
+ // reserved bytes (6-7) remain 0
110
+ await fh.writev([header, payload], offset);
111
+ }
112
+ export async function writeVersion(fh, offset) {
113
+ await fh.write(VERSION_RECORD_BYTES, 0, VERSION_RECORD_BYTES.length, offset);
114
+ }
115
+ export function serializeSlotIndex(slotIndex) {
116
+ const count = slotIndex.offsets.length;
117
+ const payload = new Uint8Array(count * 8 + 16);
118
+ // startSlot
119
+ writeInt48(payload, 0, slotIndex.startSlot);
120
+ // offsets
121
+ let off = 8;
122
+ for (let i = 0; i < count; i++, off += 8) {
123
+ writeInt48(payload, off, slotIndex.offsets[i]);
124
+ }
125
+ // trailing count
126
+ writeInt48(payload, 8 + count * 8, count);
127
+ return payload;
128
+ }
129
+ //# sourceMappingURL=e2s.js.map
package/lib/e2s.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"e2s.js","sourceRoot":"","sources":["../src/e2s.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,WAAW,EAAC,MAAM,WAAW,CAAC;AAElG;;GAEG;AACH,MAAM,CAAN,IAAY,SAMX;AAND,WAAY,SAAS;IACnB,2CAAS,CAAA;IACT,uFAA+B,CAAA;IAC/B,2EAAyB,CAAA;IACzB,mDAA4B,CAAA;IAC5B,uDAA8B,CAAA;AAChC,CAAC,EANW,SAAS,KAAT,SAAS,QAMpB;AAyBD;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG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"index.d.ts","sourceRoot":"","sources":["../../src/era/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from "./reader.js";
2
+ export * from "./util.js";
3
+ export * from "./writer.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/era/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,43 @@
1
+ import { type FileHandle } from "node:fs/promises";
2
+ import { ChainForkConfig } from "@lodestar/config";
3
+ import { BeaconState, SignedBeaconBlock, Slot } from "@lodestar/types";
4
+ import { EraIndices } from "./util.ts";
5
+ /**
6
+ * EraReader is responsible for reading and validating ERA files.
7
+ *
8
+ * See https://github.com/eth-clients/e2store-format-specs/blob/main/formats/era.md
9
+ */
10
+ export declare class EraReader {
11
+ readonly config: ChainForkConfig;
12
+ /** The underlying file handle */
13
+ readonly fh: FileHandle;
14
+ /** The era number retrieved from the file name */
15
+ readonly eraNumber: number;
16
+ /** The short historical root retrieved from the file name */
17
+ readonly shortHistoricalRoot: string;
18
+ /** An array of state and block indices, one per group */
19
+ readonly groups: EraIndices[];
20
+ constructor(config: ChainForkConfig, fh: FileHandle, eraNumber: number, shortHistoricalRoot: string, indices: EraIndices[]);
21
+ static open(config: ChainForkConfig, path: string): Promise<EraReader>;
22
+ /**
23
+ * Close the underlying file descriptor
24
+ *
25
+ * No further actions can be taken after this operation
26
+ */
27
+ close(): Promise<void>;
28
+ readCompressedState(eraNumber?: number): Promise<Uint8Array>;
29
+ readSerializedState(eraNumber?: number): Promise<Uint8Array>;
30
+ readState(eraNumber?: number): Promise<BeaconState>;
31
+ readCompressedBlock(slot: Slot): Promise<Uint8Array | null>;
32
+ readSerializedBlock(slot: Slot): Promise<Uint8Array | null>;
33
+ readBlock(slot: Slot): Promise<SignedBeaconBlock | null>;
34
+ /**
35
+ * Validate the era file.
36
+ * - e2s format correctness
37
+ * - era range correctness
38
+ * - network correctness for state and blocks
39
+ * - block root and signature matches
40
+ */
41
+ validate(): Promise<void>;
42
+ }
43
+ //# sourceMappingURL=reader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reader.d.ts","sourceRoot":"","sources":["../../src/era/reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,UAAU,EAAO,MAAM,kBAAkB,CAAC;AAGvD,OAAO,EAAC,eAAe,EAAsB,MAAM,kBAAkB,CAAC;AAEtE,OAAO,EAAC,WAAW,EAAE,iBAAiB,EAAE,IAAI,EAAM,MAAM,iBAAiB,CAAC;AAG1E,OAAO,EACL,UAAU,EAKX,MAAM,WAAW,CAAC;AAEnB;;;;GAIG;AACH,qBAAa,SAAS;IACpB,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IACjC,iCAAiC;IACjC,QAAQ,CAAC,EAAE,EAAE,UAAU,CAAC;IACxB,kDAAkD;IAClD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,6DAA6D;IAC7D,QAAQ,CAAC,mBAAmB,EAAE,MAAM,CAAC;IACrC,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,UAAU,EAAE,CAAC;gBAG5B,MAAM,EAAE,eAAe,EACvB,EAAE,EAAE,UAAU,EACd,SAAS,EAAE,MAAM,EACjB,mBAAmB,EAAE,MAAM,EAC3B,OAAO,EAAE,UAAU,EAAE;WASV,IAAI,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAW5E;;;;OAIG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,mBAAmB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAe5D,mBAAmB,CAAC,S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