-
-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add --chain.pruneHistory flag (#7427)
**Motivation** - #6869 **Description** - add `MIN_EPOCHS_FOR_BLOCK_REQUESTS` config (PS we're missing a lot of the network config entries from the consensus specs) - add `--chain.pruneHistory` flag, default to false - when chain.pruneHistory is true - prune all historical blocks/states on startup and then on every subsequent finalization --------- Co-authored-by: Nico Flaig <[email protected]>
- Loading branch information
1 parent
a7755ad
commit 90bd72f
Showing
15 changed files
with
375 additions
and
43 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {ChainConfig} from "@lodestar/config"; | ||
import {computeStartSlotAtEpoch} from "@lodestar/state-transition"; | ||
import {Epoch} from "@lodestar/types"; | ||
import {Logger} from "@lodestar/utils"; | ||
import {IBeaconDb} from "../../db/interface.js"; | ||
import {Metrics} from "../../metrics/index.js"; | ||
|
||
export async function pruneHistory( | ||
config: ChainConfig, | ||
db: IBeaconDb, | ||
logger: Logger, | ||
metrics: Metrics | null | undefined, | ||
finalizedEpoch: Epoch, | ||
currentEpoch: Epoch | ||
): Promise<void> { | ||
const blockCutoffEpoch = Math.min( | ||
// set by config, with underflow protection | ||
Math.max(currentEpoch - config.MIN_EPOCHS_FOR_BLOCK_REQUESTS, 0), | ||
// ensure that during (extremely lol) long periods of non-finality we don't delete unfinalized epoch data | ||
finalizedEpoch | ||
); | ||
const blockCutoffSlot = computeStartSlotAtEpoch(blockCutoffEpoch); | ||
|
||
logger.debug("Preparing to prune history", { | ||
currentEpoch, | ||
finalizedEpoch, | ||
blockCutoffEpoch, | ||
}); | ||
|
||
const step0 = metrics?.pruneHistory.fetchKeys.startTimer(); | ||
const [blocks, states] = await Promise.all([ | ||
db.blockArchive.keys({gte: 0, lt: blockCutoffSlot}), | ||
db.stateArchive.keys({gte: 0, lt: finalizedEpoch}), | ||
]); | ||
step0?.(); | ||
|
||
logger.debug("Pruning history", { | ||
currentEpoch, | ||
blocksToPrune: blocks.length, | ||
statesToPrune: states.length, | ||
}); | ||
|
||
const step1 = metrics?.pruneHistory.pruneKeys.startTimer(); | ||
await Promise.all([ | ||
// -> | ||
db.blockArchive.batchDelete(blocks), | ||
db.stateArchive.batchDelete(states), | ||
]); | ||
step1?.(); | ||
|
||
logger.debug("Pruned history", { | ||
currentEpoch, | ||
}); | ||
|
||
metrics?.pruneHistory.pruneCount.inc(); | ||
} |
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
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