Skip to content

Commit 9896dfb

Browse files
committed
update e2store README
1 parent 1f8f8c7 commit 9896dfb

File tree

1 file changed

+87
-16
lines changed

1 file changed

+87
-16
lines changed

packages/e2store/README.md

+87-16
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@
66
[![Code Coverage][e2store-coverage-badge]][e2store-coverage-link]
77
[![Discord][discord-badge]][discord-link]
88

9-
| A collection of utility functions for Ethereum. |
10-
| ----------------------------------------------- |
9+
| A collection of utility functions for Ethereum data storage formats. |
10+
| ------------------------------------------------------------------- |
11+
12+
`@ethereumjs/e2store` provides utilities for working with Ethereum data storage formats, including E2HS, Era1, and Era files. These formats are commonly used for storing historical blockchain data, beacon states, and block data in an efficient, provable, and standardized way.
1113

1214
## Table of Contents
1315

1416
- [@ethereumjs/e2store `v10`](#ethereumjse2store-v10)
1517
- [Table of Contents](#table-of-contents)
1618
- [Installation](#installation)
1719
- [Usage](#usage)
18-
- [Export History as Era1](#export-history-as-era1)
20+
- [E2HS](#e2hs)
21+
- [Format E2HS](#format-e2hs)
22+
- [Read Tuples from E2HS](#read-tuples-from-e2hs)
23+
- [Read E2HS Tuple at Index](#read-e2hs-tuple-at-index)
24+
- [Era1](#era1)
25+
- [Export History as Era1](#export-history-as-era1)
1926
- [Read Era1 file](#read-era1-file)
20-
- [Read Era file](#read-era-file)
27+
- [Era](#era)
28+
- [Read Era file](#read-era-file)
29+
- [Common Use Cases](#common-use-cases)
2130
- [EthereumJS](#ethereumjs)
2231
- [License](#license)
2332

@@ -37,23 +46,69 @@ All helpers are re-exported from the root level and deep imports are not necessa
3746
import { formatEntry } from "@ethereumjs/e2store"
3847
```
3948

40-
### Export History as Era1
49+
### E2HS
50+
51+
E2HS is a format for storing historical blockchain data along with proofs. It provides efficient access to block headers, bodies, and receipts, and can be used to bootstrap a Portal History Network DB.
52+
53+
#### Format E2HS
54+
55+
```ts
56+
import { formatE2HS } from "@ethereumjs/e2store"
57+
58+
// Format data into E2HS format
59+
// data is an array of block tuple data and an epoch index
60+
const e2hs = await formatE2HS(data)
61+
```
62+
63+
#### Read Tuples from E2HS
64+
65+
```ts
66+
import { readTuplesFromE2HS, parseEH2SBlockTuple } from "@ethereumjs/e2store"
67+
68+
// Read all tuples from an E2HS file
69+
const tuples = await readTuplesFromE2HS(filePath)
70+
for await (const tuple of tuples) {
71+
const { headerWithProof, body, receipts } = parseEH2SBlockTuple(tuple)
72+
console.log(headerWithProof)
73+
console.log(body)
74+
console.log(receipts)
75+
}
76+
```
77+
78+
#### Read E2HS Tuple at Index
79+
80+
```ts
81+
import { readE2HSTupleAtIndex, parseEH2SBlockTuple } from "@ethereumjs/e2store"
82+
83+
// Read a specific tuple by index
84+
const tuple = await readE2HSTupleAtIndex(filePath, index)
85+
const { headerWithProof, body, receipts } = parseEH2SBlockTuple(tuple)
86+
console.log(headerWithProof)
87+
console.log(body)
88+
console.log(receipts)
89+
```
90+
91+
### Era1
92+
93+
Era1 files store historical data in epochs of 8192 blocks, making it efficient to access large ranges of historical data. Era1 block tuples contain a header, body, receipts, and total difficulty. The data can be verified by reconstructing the epoch accumulator, and validating again the accumulator root, also contained in the era1 file.
4194

42-
Export history in epochs of 8192 blocks as Era1 files
95+
#### Export History as Era1
96+
97+
Export history from an EthereumJS client DB in epochs of 8192 blocks as Era1 files:
4398

4499
```ts
45100
import { exportEpochAsEra1 } from "@ethereumjs/e2store"
46101

47102
const dataDir = PATH_TO_ETHEREUMJS_CLIENT_DB
48103
const epoch = 0
49104

50-
// generates ${dataDir}/era1/epoch-0.era1
105+
// Generates ${dataDir}/era1/epoch-0.era1
51106
await exportEpochAsEra1(epoch, dataDir)
52107
```
53108

54109
### Read Era1 file
55110

56-
`readERA1` returns an async iterator of block tuples (header + body + receipts + totalDifficulty)
111+
`readERA1` returns an async iterator of block tuples (header + body + receipts + totalDifficulty):
57112

58113
```ts
59114
import {
@@ -68,44 +123,60 @@ import {
68123

69124
const era1File = readBinaryFile(PATH_TO_ERA1_FILE)
70125

71-
// validate era1 file
126+
// Validate era1 file
127+
// Rebuilds epoch accumulator and validates the accumulator root
72128
const isValid = validateERA1(era1File)
129+
if (!isValid) {
130+
throw new Error('Invalid Era1 file')
131+
}
73132

74-
// read blocks from era1 file
133+
// Read blocks from era1 file
75134
const blocks = readERA1(era1File)
76135

77136
for await (const blockTuple of blocks) {
78137
const { header, body, receipts } = await parseBlockTuple(blockTuple)
79138
const block = blockFromTuple({ header, body })
80-
console.log(block.header.number)
139+
console.log(`Block number: ${block.header.number}`)
81140
}
82141

83-
// reconstruct epoch accumulator
142+
// Reconstruct epoch accumulator
84143
const headerRecords = await getHeaderRecords(era1File)
85144
const epochAccumulator = EpochAccumulator.encode(headerRecords)
86145
const epochAccumulatorRoot = EpochAccumulator.merkleRoot(headerRecords)
87146
```
88147

89-
### Read Era file
148+
### Era
149+
150+
Era files are used to store beacon chain data, including beacon states and blocks.
151+
152+
#### Read Era file
90153

91154
```ts
92-
import { readBeaconState } from "@ethereumjs/e2store"
155+
import { readBeaconState, readBlocksFromEra } from "@ethereumjs/e2store"
93156

94157
const eraFile = readBinaryFile(PATH_TO_ERA_FILE)
95158

96159
// Extract BeaconState
97160
const state = await readBeaconState(eraFile)
98-
console.log(state.slot)
161+
console.log(`Current slot: ${state.slot}`)
99162

100163
// Read Beacon Blocks from era file
101164
let count = 0
102165
for await (const block of readBlocksFromEra(eraFile)) {
103-
console.log(block.message.slot)
166+
console.log(`Block slot: ${block.message.slot}`)
104167
count++
105168
if (count > 10) break
106169
}
107170
```
108171

172+
## Common Use Cases
173+
174+
1. **Historical Data Access**: Use E2HS and Era1 formats to efficiently access historical blockchain data
175+
2. **Beacon Chain Analysis**: Read and analyze beacon chain states and blocks using Era files
176+
3. **Data Export**: Export historical data in standardized formats for analysis or archival
177+
4. **Portal History Network**: Bootstrap a Portal History Network DB using E2HS
178+
5. **Execution Client Sync**: Sync an execution client without devp2p using Era1 or E2HS files
179+
109180
## EthereumJS
110181

111182
See our organizational [documentation](https://ethereumjs.readthedocs.io) for an introduction to `EthereumJS` as well as information on current standards and best practices. If you want to join for work or carry out improvements on the libraries, please review our [contribution guidelines](https://ethereumjs.readthedocs.io/en/latest/contributing.html) first.

0 commit comments

Comments
 (0)