import { assertValidProposerSlashing, getProposerSlashingSignatureSets } from "@lodestar/state-transition";
import { GossipAction, ProposerSlashingError, ProposerSlashingErrorCode } from "../errors/index.js";
export async function validateApiProposerSlashing(chain, proposerSlashing) {
    const prioritizeBls = true;
    return validateProposerSlashing(chain, proposerSlashing, prioritizeBls);
}
export async function validateGossipProposerSlashing(chain, proposerSlashing) {
    return validateProposerSlashing(chain, proposerSlashing);
}
async function validateProposerSlashing(chain, proposerSlashing, prioritizeBls = false) {
    // [IGNORE] The proposer slashing is the first valid proposer slashing received for the proposer with index
    // proposer_slashing.signed_header_1.message.proposer_index.
    if (chain.opPool.hasSeenProposerSlashing(proposerSlashing.signedHeader1.message.proposerIndex)) {
        throw new ProposerSlashingError(GossipAction.IGNORE, {
            code: ProposerSlashingErrorCode.ALREADY_EXISTS,
        });
    }
    const state = chain.getHeadState();
    // [REJECT] All of the conditions within process_proposer_slashing pass validation.
    try {
        const proposer = state.validators.getReadonly(proposerSlashing.signedHeader1.message.proposerIndex);
        // verifySignature = false, verified in batch below
        assertValidProposerSlashing(chain.config, chain.pubkeyCache, state.slot, proposerSlashing, proposer, false);
    }
    catch (e) {
        throw new ProposerSlashingError(GossipAction.REJECT, {
            code: ProposerSlashingErrorCode.INVALID,
            error: e,
        });
    }
    const signatureSets = getProposerSlashingSignatureSets(chain.config, state.slot, proposerSlashing);
    if (!(await chain.bls.verifySignatureSets(signatureSets, { batchable: true, priority: prioritizeBls }))) {
        throw new ProposerSlashingError(GossipAction.REJECT, {
            code: ProposerSlashingErrorCode.INVALID,
            error: Error("Invalid signature"),
        });
    }
}
//# sourceMappingURL=proposerSlashing.js.map