Skip to content

Commit

Permalink
fix: check pubkey or validator index known to a state (#7284)
Browse files Browse the repository at this point in the history
* fix: check pubkey or validator index known to a state

* chore: add more comments
  • Loading branch information
twoeths authored Dec 6, 2024
1 parent cd1211f commit e353f67
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {electra, ssz} from "@lodestar/types";

import {CachedBeaconStateElectra} from "../types.js";
import {hasEth1WithdrawalCredential} from "../util/capella.js";
import {hasExecutionWithdrawalCredential, switchToCompoundingValidator} from "../util/electra.js";
import {hasExecutionWithdrawalCredential, isPubkeyKnown, switchToCompoundingValidator} from "../util/electra.js";
import {computeConsolidationEpochAndUpdateChurn} from "../util/epoch.js";
import {getConsolidationChurnLimit, isActiveValidator} from "../util/validator.js";

Expand All @@ -13,6 +13,10 @@ export function processConsolidationRequest(
consolidationRequest: electra.ConsolidationRequest
): void {
const {sourcePubkey, targetPubkey, sourceAddress} = consolidationRequest;
if (!isPubkeyKnown(state, sourcePubkey) || !isPubkeyKnown(state, targetPubkey)) {
return;
}

const sourceIndex = state.epochCtx.getValidatorIndex(sourcePubkey);
const targetIndex = state.epochCtx.getValidatorIndex(targetPubkey);

Expand Down Expand Up @@ -97,6 +101,7 @@ function isValidSwitchToCompoundRequest(

// Verify pubkey exists
if (sourceIndex === null) {
// this check is mainly to make the compiler happy, pubkey is checked by the consumer already
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/state-transition/src/epoch/processPendingDeposits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {PendingDeposit} from "@lodestar/types/lib/electra/types.js";
import {addValidatorToRegistry, isValidDepositSignature} from "../block/processDeposit.js";
import {CachedBeaconStateElectra, EpochTransitionCache} from "../types.js";
import {increaseBalance} from "../util/balance.js";
import {hasCompoundingWithdrawalCredential} from "../util/electra.js";
import {hasCompoundingWithdrawalCredential, isValidatorKnown} from "../util/electra.js";
import {computeStartSlotAtEpoch} from "../util/epoch.js";
import {getActivationExitChurnLimit} from "../util/validator.js";

Expand Down Expand Up @@ -51,7 +51,7 @@ export function processPendingDeposits(state: CachedBeaconStateElectra, cache: E
let isValidatorWithdrawn = false;

const validatorIndex = state.epochCtx.getValidatorIndex(deposit.pubkey);
if (validatorIndex !== null) {
if (isValidatorKnown(state, validatorIndex)) {
const validator = state.validators.getReadonly(validatorIndex);
isValidatorExited = validator.exitEpoch < FAR_FUTURE_EPOCH;
isValidatorWithdrawn = validator.withdrawableEpoch < nextEpoch;
Expand Down Expand Up @@ -103,7 +103,7 @@ function applyPendingDeposit(
const {pubkey, withdrawalCredentials, amount, signature} = deposit;
const cachedBalances = cache.balances;

if (validatorIndex === null) {
if (!isValidatorKnown(state, validatorIndex)) {
// Verify the deposit signature (proof of possession) which is not checked by the deposit contract
if (isValidDepositSignature(state.config, pubkey, withdrawalCredentials, amount, signature)) {
addValidatorToRegistry(ForkSeq.electra, state, pubkey, withdrawalCredentials, amount);
Expand Down
17 changes: 17 additions & 0 deletions packages/state-transition/src/util/electra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,20 @@ export function queueExcessActiveBalance(state: CachedBeaconStateElectra, index:
state.pendingDeposits.push(pendingDeposit);
}
}

/**
* Since we share pubkey2index, pubkey maybe added by other epoch transition but we don't have that validator in this state
*/
export function isPubkeyKnown(state: CachedBeaconStateElectra, pubkey: Uint8Array): boolean {
return isValidatorKnown(state, state.epochCtx.getValidatorIndex(pubkey));
}

/**
* Since we share pubkey2index, validatorIndex maybe not null but we don't have that validator in this state
*/
export function isValidatorKnown(
state: CachedBeaconStateElectra,
index: ValidatorIndex | null
): index is ValidatorIndex {
return index !== null && index < state.validators.length;
}

1 comment on commit e353f67

@github-actions

This comment was marked as resolved.

Please sign in to comment.