import { SLOTS_PER_EPOCH } from "@lodestar/params";
import { pruneSetToMax } from "@lodestar/utils";
// Idealy this only need to be set to the max head reorgs number
const MAX_PAYLOAD_IDS = SLOTS_PER_EPOCH;
export class PayloadIdCache {
    payloadIdByFcuAttributes = new Map();
    getFullKey({ headBlockHash, finalizedBlockHash, timestamp, prevRandao, suggestedFeeRecipient }) {
        return `${headBlockHash}-${finalizedBlockHash}-${timestamp}-${prevRandao}-${suggestedFeeRecipient}`;
    }
    getKey({ timestamp }) {
        return timestamp;
    }
    hasPayload(fcuAttributes) {
        const key = this.getKey(fcuAttributes);
        return this.payloadIdByFcuAttributes.get(key) !== undefined;
    }
    add(fcuAttributes, payloadId) {
        const key = this.getKey(fcuAttributes);
        const fullKey = this.getFullKey(fcuAttributes);
        this.payloadIdByFcuAttributes.set(key, { payloadId, fullKey });
    }
    prune() {
        // This is not so optimized function, but could maintain a 2d array may be?
        pruneSetToMax(this.payloadIdByFcuAttributes, MAX_PAYLOAD_IDS);
    }
    get(fcuAttributes) {
        const key = this.getKey(fcuAttributes);
        const fullKey = this.getFullKey(fcuAttributes);
        const payloadInfo = this.payloadIdByFcuAttributes.get(key);
        return payloadInfo?.fullKey === fullKey ? payloadInfo.payloadId : undefined;
    }
}
//# sourceMappingURL=payloadIdCache.js.map