Skip to content

Commit 3e1c272

Browse files
Clean up beacon state cache after each beacon action (#2936)
* Clean up beacon state cache after each beacon action * Clean up old cached beacon chain state files if more than 1 day old --------- Co-authored-by: Nicholas Addison <nick@addisonbrown.com.au>
1 parent c52692d commit 3e1c272

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

contracts/tasks/actions/verifyBalances.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { types } from "hardhat/config";
44
import { action } from "../lib/action";
55

66
const { verifyBalances } = require("../beacon");
7+
const { cleanStateCache } = require("../../utils/beacon");
78

89
action({
910
name: "verifyBalances",
@@ -61,5 +62,6 @@ action({
6162
},
6263
run: async ({ signer, args }) => {
6364
await verifyBalances({ ...args, signer });
65+
cleanStateCache();
6466
},
6567
});

contracts/tasks/actions/verifyDeposits.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { types } from "hardhat/config";
44
import { action } from "../lib/action";
55

66
const { verifyDeposits } = require("../beacon");
7+
const { cleanStateCache } = require("../../utils/beacon");
78

89
action({
910
name: "verifyDeposits",
@@ -25,5 +26,6 @@ action({
2526
},
2627
run: async ({ signer, args }) => {
2728
await verifyDeposits({ ...args, signer });
29+
cleanStateCache();
2830
},
2931
});

contracts/utils/beacon.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,43 @@ const {
1111
const log = require("./logger")("utils:beacon");
1212

1313
const SLOTS_PER_EPOCH = 32;
14+
const BEACON_STATE_CACHE_DIR = "./cache";
15+
const BEACON_STATE_CACHE_MAX_AGE_MS = 24 * 60 * 60 * 1000;
16+
17+
// Beacon state SSZ files are large and each run fetches a unique slot, so a
18+
// long-lived runner accumulates them in ./cache until the disk fills (ENOSPC).
19+
// After successful beacon actions, prune old beacon state files while leaving
20+
// recent files available for retries and avoiding unrelated Hardhat cache data.
21+
const cleanStateCache = (now = Date.now()) => {
22+
if (!fs.existsSync(BEACON_STATE_CACHE_DIR)) {
23+
return;
24+
}
25+
26+
for (const entry of fs.readdirSync(BEACON_STATE_CACHE_DIR, {
27+
withFileTypes: true,
28+
})) {
29+
if (
30+
!entry.isFile() ||
31+
!entry.name.startsWith("state_") ||
32+
!entry.name.endsWith(".ssz")
33+
) {
34+
continue;
35+
}
36+
37+
const file = `${BEACON_STATE_CACHE_DIR}/${entry.name}`;
38+
try {
39+
const stats = fs.statSync(file);
40+
if (now - stats.mtimeMs <= BEACON_STATE_CACHE_MAX_AGE_MS) {
41+
continue;
42+
}
43+
44+
fs.rmSync(file, { force: true });
45+
log(`Removed cached beacon state older than 1 day ${file}`);
46+
} catch {
47+
// Best-effort cleanup; a missing file is fine.
48+
}
49+
}
50+
};
1451

1552
const normalizeValidatorResponse = ({ index, balance, status, validator }) => ({
1653
index: Number(index),
@@ -456,6 +493,7 @@ const verifyDepositSignatureAndMessageRoot = async ({
456493
};
457494

458495
module.exports = {
496+
cleanStateCache,
459497
concatProof,
460498
getBeaconBlock,
461499
getSlot,

0 commit comments

Comments
 (0)