-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: batch io operations when verifying & importing block (#5473)
* feat: verify and import block - batch all I/O operations * feat: import block to LightClientServer in the next event loop * review PR * fix: rename removeEagerlyPersistedBlockInputs() * feat: add eagerPersistBlock flag --------- Co-authored-by: dapplion <35266934+dapplion@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
143 additions
and
38 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
81 changes: 81 additions & 0 deletions
81
packages/beacon-node/src/chain/blocks/writeBlockInputToDb.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,81 @@ | ||
import {WithOptionalBytes, allForks, deneb} from "@lodestar/types"; | ||
import {toHex} from "@lodestar/utils"; | ||
import {BeaconChain} from "../chain.js"; | ||
import {BlockInput, BlockInputType} from "./types.js"; | ||
|
||
/** | ||
* Persists block input data to DB. This operation must be eventually completed if a block is imported to the fork-choice. | ||
* Else the node will be in an inconsistent state that can lead to being stuck. | ||
* | ||
* This operation may be performed before, during or after importing to the fork-choice. As long as errors | ||
* are handled properly for eventual consistency. | ||
*/ | ||
export async function writeBlockInputToDb( | ||
this: BeaconChain, | ||
blocksInput: WithOptionalBytes<BlockInput>[] | ||
): Promise<void> { | ||
const fnPromises: Promise<void>[] = []; | ||
|
||
for (const blockInput of blocksInput) { | ||
const {block, serializedData, type} = blockInput; | ||
const blockRoot = this.config.getForkTypes(block.message.slot).BeaconBlock.hashTreeRoot(block.message); | ||
const blockRootHex = toHex(blockRoot); | ||
if (serializedData) { | ||
// skip serializing data if we already have it | ||
this.metrics?.importBlock.persistBlockWithSerializedDataCount.inc(); | ||
fnPromises.push(this.db.block.putBinary(this.db.block.getId(block), serializedData)); | ||
} else { | ||
this.metrics?.importBlock.persistBlockNoSerializedDataCount.inc(); | ||
fnPromises.push(this.db.block.add(block)); | ||
} | ||
this.logger.debug("Persist block to hot DB", { | ||
slot: block.message.slot, | ||
root: blockRootHex, | ||
}); | ||
|
||
if (type === BlockInputType.postDeneb) { | ||
const {blobs} = blockInput; | ||
// NOTE: Old blobs are pruned on archive | ||
fnPromises.push(this.db.blobsSidecar.add(blobs)); | ||
this.logger.debug("Persist blobsSidecar to hot DB", { | ||
blobsLen: blobs.blobs.length, | ||
slot: blobs.beaconBlockSlot, | ||
root: toHex(blobs.beaconBlockRoot), | ||
}); | ||
} | ||
} | ||
|
||
await Promise.all(fnPromises); | ||
} | ||
|
||
/** | ||
* Prunes eagerly persisted block inputs only if not known to the fork-choice | ||
*/ | ||
export async function removeEagerlyPersistedBlockInputs( | ||
this: BeaconChain, | ||
blockInputs: WithOptionalBytes<BlockInput>[] | ||
): Promise<void> { | ||
const blockToRemove: allForks.SignedBeaconBlock[] = []; | ||
const blobsToRemove: deneb.BlobsSidecar[] = []; | ||
|
||
for (const blockInput of blockInputs) { | ||
const {block, type} = blockInput; | ||
const blockRoot = toHex(this.config.getForkTypes(block.message.slot).BeaconBlock.hashTreeRoot(block.message)); | ||
if (!this.forkChoice.hasBlockHex(blockRoot)) { | ||
blockToRemove.push(block); | ||
|
||
if (type === BlockInputType.postDeneb) { | ||
blobsToRemove.push(blockInput.blobs); | ||
this.db.blobsSidecar.remove(blockInput.blobs).catch((e) => { | ||
this.logger.verbose("Error removing eagerly imported blobsSidecar", {blockRoot}, e); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
await Promise.all([ | ||
// TODO: Batch DB operations not with Promise.all but with level db ops | ||
this.db.block.batchRemove(blockToRemove), | ||
this.db.blobsSidecar.batchRemove(blobsToRemove), | ||
]); | ||
} |
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