Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ensi321 committed Apr 1, 2024
1 parent 5914c4e commit 02cb2dc
Show file tree
Hide file tree
Showing 24 changed files with 198 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,6 @@ function preparePayloadAttributes(
).withdrawals;
}


return payloadAttributes;
}

Expand Down
1 change: 0 additions & 1 deletion packages/config/src/chainConfig/configs/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export const chainConfig: ChainConfig = {
// `2**12` (= 4096 epochs, ~18 days)
MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS: 4096,


// Electra
// 2**8 * 10**9 (= 256,000,000,000)
MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 256000000000,
Expand Down
1 change: 0 additions & 1 deletion packages/config/src/chainConfig/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export type ChainConfig = {

// Networking
MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS: number;

};

export const chainConfigTypes: SpecTypes<ChainConfig> = {
Expand Down
9 changes: 7 additions & 2 deletions packages/state-transition/src/block/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {ForkSeq} from "@lodestar/params";
import {allForks, altair, capella, electra} from "@lodestar/types";
import {allForks, altair, capella} from "@lodestar/types";
import {getFullOrBlindedPayload, isExecutionEnabled} from "../util/execution.js";
import {CachedBeaconStateAllForks, CachedBeaconStateCapella, CachedBeaconStateBellatrix, CachedBeaconStateElectra} from "../types.js";
import {
CachedBeaconStateAllForks,
CachedBeaconStateCapella,
CachedBeaconStateBellatrix,
CachedBeaconStateElectra,
} from "../types.js";
import {processExecutionPayload} from "./processExecutionPayload.js";
import {processSyncAggregate} from "./processSyncCommittee.js";
import {processBlockHeader} from "./processBlockHeader.js";
Expand Down
45 changes: 25 additions & 20 deletions packages/state-transition/src/block/processConsolidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import {FAR_FUTURE_EPOCH, MIN_ACTIVATION_BALANCE, PENDING_CONSOLIDATIONS_LIMIT}
import {verifyConsolidationSignature} from "../signatureSets/index.js";

import {CachedBeaconStateElectra} from "../types.js";
import { getConsolidationChurnLimit, isActiveValidator } from "../util/validator.js";
import { hasExecutionWithdrawalCredential } from "../util/capella.js";
import {getConsolidationChurnLimit, isActiveValidator} from "../util/validator.js";
import {hasExecutionWithdrawalCredential} from "../util/capella.js";

export function processConsolidation(
state: CachedBeaconStateElectra,
signedConsolidation: electra.SignedConsolidation,
signedConsolidation: electra.SignedConsolidation
): void {


assertValidConsolidation(state, signedConsolidation);

// Initiate source validator exit and append pending consolidation
Expand All @@ -21,21 +19,20 @@ export function processConsolidation(
const sourceValidator = state.validators.get(sourceIndex);

const exitEpoch = 0; // TODO Electra: compute_consolidation_epoch_and_update_churn
sourceValidator.exitEpoch = exitEpoch;
sourceValidator.exitEpoch = exitEpoch;
sourceValidator.withdrawableEpoch = exitEpoch + state.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY;

const pendingConsolidation = ssz.electra.PendingConsolidation.toViewDU({
sourceIndex,
targetIndex
targetIndex,
});
state.pendingConsolidations.push(pendingConsolidation);

}

function assertValidConsolidation(state: CachedBeaconStateElectra, signedConsolidation: electra.SignedConsolidation) {
function assertValidConsolidation(state: CachedBeaconStateElectra, signedConsolidation: electra.SignedConsolidation): void {
// If the pending consolidations queue is full, no consolidations are allowed in the block
if (state.pendingConsolidations.length >= PENDING_CONSOLIDATIONS_LIMIT) {
throw new Error(`Pending consolidation queue is full`);
throw new Error("Pending consolidation queue is full");
}

// If there is too little available consolidation churn limit, no consolidations are allowed in the block
Expand All @@ -44,16 +41,16 @@ function assertValidConsolidation(state: CachedBeaconStateElectra, signedConsoli
throw new Error(`Consolidation churn limit too low. consolidationChurnLimit=${getConsolidationChurnLimit(state)}`);
}


const consolidation = signedConsolidation.message;
const {sourceIndex, targetIndex} = consolidation;

// Verify that source != target, so a consolidation cannot be used as an exit.
if (sourceIndex === targetIndex) {
throw new Error(`Consolidation source and target index cannot be the same: sourceIndex=${sourceIndex} targetIndex=${targetIndex}`);
throw new Error(
`Consolidation source and target index cannot be the same: sourceIndex=${sourceIndex} targetIndex=${targetIndex}`
);
}


const sourceValidator = state.validators.getReadonly(sourceIndex);
const targetValidator = state.validators.getReadonly(targetIndex);
const currentEpoch = state.epochCtx.epoch;
Expand All @@ -68,36 +65,44 @@ function assertValidConsolidation(state: CachedBeaconStateElectra, signedConsoli
}

// Verify exits for source and target have not been initiated
if (sourceValidator.exitEpoch !== FAR_FUTURE_EPOCH){
if (sourceValidator.exitEpoch !== FAR_FUTURE_EPOCH) {
throw new Error(`Consolidation source validator has initialized exit: sourceIndex=${sourceIndex}`);
}
if (targetValidator.exitEpoch !== FAR_FUTURE_EPOCH){
if (targetValidator.exitEpoch !== FAR_FUTURE_EPOCH) {
throw new Error(`Consolidation target validator has initialized exit: targetIndex=${targetIndex}`);
}

// Consolidations must specify an epoch when they become valid; they are not valid before then
if (currentEpoch < consolidation.epoch) {
throw new Error(`Consolidation epoch is after the current epoch: consolidationEpoch=${consolidation.epoch} currentEpoch=${currentEpoch}`);
throw new Error(
`Consolidation epoch is after the current epoch: consolidationEpoch=${consolidation.epoch} currentEpoch=${currentEpoch}`
);
}

// Verify the source and the target have Execution layer withdrawal credentials
if (!hasExecutionWithdrawalCredential(sourceValidator.withdrawalCredentials)) {
throw new Error(`Consolidation source validator does not have execution withdrawal credentials: sourceIndex=${sourceIndex}`);
throw new Error(
`Consolidation source validator does not have execution withdrawal credentials: sourceIndex=${sourceIndex}`
);
}
if (!hasExecutionWithdrawalCredential(targetValidator.withdrawalCredentials)) {
throw new Error(`Consolidation target validator does not have execution withdrawal credentials: targetIndex=${targetIndex}`);
throw new Error(
`Consolidation target validator does not have execution withdrawal credentials: targetIndex=${targetIndex}`
);
}

// Verify the same withdrawal address
const sourceWithdrawalAddress = toHexString(sourceValidator.withdrawalCredentials.slice(1));
const targetWithdrawalAddress = toHexString(targetValidator.withdrawalCredentials.slice(1));

if (sourceWithdrawalAddress !== targetWithdrawalAddress) {
throw new Error(`Consolidation source and target withdrawal address are different: source: ${sourceWithdrawalAddress} target: ${targetWithdrawalAddress}`);
throw new Error(
`Consolidation source and target withdrawal address are different: source: ${sourceWithdrawalAddress} target: ${targetWithdrawalAddress}`
);
}

// Verify consolidation is signed by the source and the target
if (!verifyConsolidationSignature(state, signedConsolidation)) {
throw new Error("Consolidation not valid");
}
}
}
1 change: 0 additions & 1 deletion packages/state-transition/src/block/processDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ function addValidatorToRegistry(
stateElectra.pendingBalanceDeposits.push(pendingBalanceDeposit);
}
} else {

if (fork < ForkSeq.electra) {
// increase balance by deposit amount right away pre-electra
increaseBalance(state, cachedIndex, amount);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
import {toHexString, byteArrayEquals} from "@chainsafe/ssz";
import {digest} from "@chainsafe/as-sha256";
import {Epoch, capella, electra, phase0, ssz} from "@lodestar/types";
import {BLS_WITHDRAWAL_PREFIX, ETH1_ADDRESS_WITHDRAWAL_PREFIX, FAR_FUTURE_EPOCH, MIN_ACTIVATION_BALANCE, PENDING_PARTIAL_WITHDRAWALS_LIMIT} from "@lodestar/params";
import {verifyBlsToExecutionChangeSignature} from "../signatureSets/index.js";
import {toHexString} from "@chainsafe/ssz";
import { electra, phase0, ssz} from "@lodestar/types";
import {
FAR_FUTURE_EPOCH,
MIN_ACTIVATION_BALANCE,
PENDING_PARTIAL_WITHDRAWALS_LIMIT,
} from "@lodestar/params";

import {CachedBeaconStateElectra} from "../types.js";
import { hasCompoundingWithdrawalCredential, hasEth1WithdrawalCredential, hasExecutionWithdrawalCredential } from "../util/capella.js";
import { isActiveValidator } from "../util/validator.js";
import { initiateValidatorExit } from "./initiateValidatorExit.js";
import { computeExitEpochAndUpdateChurn } from "../util/epoch.js";
import {
hasCompoundingWithdrawalCredential,
hasExecutionWithdrawalCredential,
} from "../util/capella.js";
import {isActiveValidator} from "../util/validator.js";
import {computeExitEpochAndUpdateChurn} from "../util/epoch.js";
import {initiateValidatorExit} from "./initiateValidatorExit.js";

export function processExecutionLayerWithdrawRequest(
state: CachedBeaconStateElectra,
executionLayerWithdrawRequest: electra.ExecutionLayerWithdrawRequest,
executionLayerWithdrawRequest: electra.ExecutionLayerWithdrawRequest
): void {

const amount = Number(executionLayerWithdrawRequest.amount);
const {pendingPartialWithdrawals, validators, epochCtx} = state;
const {pubkey2index, config} = epochCtx; // TODO Electra: Use finalized+unfinalized pubkey cache from 6110
const isFullExitRequest = amount === 0;

// If partial withdrawal queue is full, only full exits are processed
// If partial withdrawal queue is full, only full exits are processed
if (pendingPartialWithdrawals.length >= PENDING_PARTIAL_WITHDRAWALS_LIMIT && !isFullExitRequest) {
return;
}

const validatorIndex = pubkey2index.get(executionLayerWithdrawRequest.validatorPubkey);

if (validatorIndex === undefined) {
throw new Error(`Can't find validator index from ExecutionLayerWithdrawRequest : pubkey=${toHexString(executionLayerWithdrawRequest.validatorPubkey)}`);
throw new Error(
`Can't find validator index from ExecutionLayerWithdrawRequest : pubkey=${toHexString(
executionLayerWithdrawRequest.validatorPubkey
)}`
);
}

const validator = state.validators.getReadonly(validatorIndex);
Expand All @@ -42,7 +50,10 @@ export function processExecutionLayerWithdrawRequest(
}

// TODO Electra: Consider caching pendingPartialWithdrawals
const pendingBalanceToWithdraw = state.pendingPartialWithdrawals.getAllReadonly().filter(item => item.index === validatorIndex).reduce((total, item) => total + Number(item.amount), 0);
const pendingBalanceToWithdraw = state.pendingPartialWithdrawals
.getAllReadonly()
.filter((item) => item.index === validatorIndex)
.reduce((total, item) => total + Number(item.amount), 0);
const validatorBalance = state.balances.get(validatorIndex);

// only exit validator if it has no pending withdrawals in the queue
Expand All @@ -61,22 +72,23 @@ export function processExecutionLayerWithdrawRequest(

state.pendingPartialWithdrawals.push(pendingPartialWithdrawal);
}

}

function isValidValidator(validator: phase0.Validator, sourceAddress: Uint8Array, state: CachedBeaconStateElectra): boolean {
function isValidValidator(
validator: phase0.Validator,
sourceAddress: Uint8Array,
state: CachedBeaconStateElectra
): boolean {
const {withdrawalCredentials} = validator;
const addressStr = toHexString(withdrawalCredentials.slice(12));
const sourceAddressStr = toHexString(sourceAddress);
const {epoch: currentEpoch, config} = state.epochCtx;

return hasExecutionWithdrawalCredential(withdrawalCredentials)
&&
addressStr === sourceAddressStr
&&
isActiveValidator(validator, currentEpoch)
&&
validator.exitEpoch === FAR_FUTURE_EPOCH
&&
currentEpoch >= validator.activationEpoch + config.SHARD_COMMITTEE_PERIOD;
}
return (
hasExecutionWithdrawalCredential(withdrawalCredentials) &&
addressStr === sourceAddressStr &&
isActiveValidator(validator, currentEpoch) &&
validator.exitEpoch === FAR_FUTURE_EPOCH &&
currentEpoch >= validator.activationEpoch + config.SHARD_COMMITTEE_PERIOD
);
}
4 changes: 2 additions & 2 deletions packages/state-transition/src/block/processOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {processVoluntaryExit} from "./processVoluntaryExit.js";
import {processBlsToExecutionChange} from "./processBlsToExecutionChange.js";
import {processDepositReceipt} from "./processDepositReceipt.js";
import {ProcessBlockOpts} from "./types.js";
import { processExecutionLayerWithdrawRequest } from "./processExecutionLayerWithdrawRequest.js";
import { processConsolidation } from "./processConsolidation.js";
import {processExecutionLayerWithdrawRequest} from "./processExecutionLayerWithdrawRequest.js";
import {processConsolidation} from "./processConsolidation.js";

export {
processProposerSlashing,
Expand Down
28 changes: 20 additions & 8 deletions packages/state-transition/src/block/processWithdrawals.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {byteArrayEquals, toHexString} from "@chainsafe/ssz";
import {ssz, capella} from "@lodestar/types";
import {
MAX_EFFECTIVE_BALANCE,
MAX_WITHDRAWALS_PER_PAYLOAD,
MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP,
ForkSeq,
Expand All @@ -11,7 +10,15 @@ import {
} from "@lodestar/params";

import {CachedBeaconStateCapella, CachedBeaconStateElectra} from "../types.js";
import {decreaseBalance, getValidatorExcessBalance, hasCompoundingWithdrawalCredential, hasEth1WithdrawalCredential, hasExecutionWithdrawalCredential, isCapellaPayloadHeader, isFullyWithdrawableValidator, isPartiallyWithdrawableValidator} from "../util/index.js";
import {
decreaseBalance,
getValidatorExcessBalance,
hasEth1WithdrawalCredential,
hasExecutionWithdrawalCredential,
isCapellaPayloadHeader,
isFullyWithdrawableValidator,
isPartiallyWithdrawableValidator,
} from "../util/index.js";

export function processWithdrawals(
fork: ForkSeq,
Expand Down Expand Up @@ -73,7 +80,10 @@ export function processWithdrawals(
}
}

export function getExpectedWithdrawals(fork: ForkSeq, state: CachedBeaconStateCapella | CachedBeaconStateElectra): {
export function getExpectedWithdrawals(
fork: ForkSeq,
state: CachedBeaconStateCapella | CachedBeaconStateElectra
): {
withdrawals: capella.Withdrawal[];
sampledValidators: number;
partialWithdrawalsCount: number;
Expand All @@ -83,7 +93,6 @@ export function getExpectedWithdrawals(fork: ForkSeq, state: CachedBeaconStateCa
const {validators, balances, nextWithdrawalValidatorIndex} = state;
const bound = Math.min(validators.length, MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP);


const withdrawals: capella.Withdrawal[] = [];

if (fork >= ForkSeq.electra) {
Expand All @@ -98,17 +107,17 @@ export function getExpectedWithdrawals(fork: ForkSeq, state: CachedBeaconStateCa

if (validator.exitEpoch === FAR_FUTURE_EPOCH && balances.get(withdrawalIndex) > MIN_ACTIVATION_BALANCE) {
const balanceOverMinActivationBalance = BigInt(balances.get(withdrawalIndex) - MIN_ACTIVATION_BALANCE);
const withdrawableBalance = balanceOverMinActivationBalance < withdrawal.amount ? balanceOverMinActivationBalance : withdrawal.amount;
const withdrawableBalance =
balanceOverMinActivationBalance < withdrawal.amount ? balanceOverMinActivationBalance : withdrawal.amount;
withdrawals.push({
index: withdrawalIndex,
validatorIndex: withdrawal.index,
address: validator.withdrawalCredentials.slice(12),
amount: withdrawableBalance,
})
});
withdrawalIndex++;
}
}

}

const partialWithdrawalsCount = withdrawals.length;
Expand All @@ -121,7 +130,10 @@ export function getExpectedWithdrawals(fork: ForkSeq, state: CachedBeaconStateCa

// It's most likely for validators to not have set eth1 credentials, than having 0 balance
const validator = validators.getReadonly(validatorIndex);
if ((fork === ForkSeq.capella || fork === ForkSeq.deneb) && !hasEth1WithdrawalCredential(validator.withdrawalCredentials)) {
if (
(fork === ForkSeq.capella || fork === ForkSeq.deneb) &&
!hasEth1WithdrawalCredential(validator.withdrawalCredentials)
) {
continue;
}
if (fork === ForkSeq.electra && !hasExecutionWithdrawalCredential(validator.withdrawalCredentials)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/state-transition/src/block/slashValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function slashValidator(
decreaseBalance(state, slashedIndex, Math.floor(effectiveBalance / minSlashingPenaltyQuotient));

// apply proposer and whistleblower rewards
const whistleblowerReward =
const whistleblowerReward =
fork <= ForkSeq.deneb
? Math.floor(effectiveBalance / WHISTLEBLOWER_REWARD_QUOTIENT)
: Math.floor(effectiveBalance / WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA);
Expand Down
2 changes: 1 addition & 1 deletion packages/state-transition/src/cache/epochCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ export class EpochCache {
);

// Maybe advance exitQueueEpoch at the end of the epoch if there haven't been any exists for a while
const exitQueueEpoch = computeActivationExitEpoch(currEpoch); // TODO Electra: New exit epoch calculation
const exitQueueEpoch = computeActivationExitEpoch(currEpoch); // TODO Electra: New exit epoch calculation
if (exitQueueEpoch > this.exitQueueEpoch) {
this.exitQueueEpoch = exitQueueEpoch;
this.exitQueueChurn = 0;
Expand Down
6 changes: 3 additions & 3 deletions packages/state-transition/src/epoch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import {processRewardsAndPenalties} from "./processRewardsAndPenalties.js";
import {processSlashings} from "./processSlashings.js";
import {processSlashingsReset} from "./processSlashingsReset.js";
import {processSyncCommitteeUpdates} from "./processSyncCommitteeUpdates.js";
import { processPendingBalanceDeposits } from "./processPendingBalanceDeposits.js";
import { processPendingConsolidations } from "./processPendingConsolidations.js";
import {processPendingBalanceDeposits} from "./processPendingBalanceDeposits.js";
import {processPendingConsolidations} from "./processPendingConsolidations.js";

// For spec tests
export {getRewardsAndPenalties} from "./processRewardsAndPenalties.js";
Expand Down Expand Up @@ -126,7 +126,7 @@ export function processEpoch(
processEth1DataReset(state, cache);

if (fork >= ForkSeq.electra) {
const stateElectra = state as CachedBeaconStateElectra
const stateElectra = state as CachedBeaconStateElectra;
// TODO Electra: Add timer
processPendingBalanceDeposits(stateElectra);
processPendingConsolidations(stateElectra);
Expand Down

0 comments on commit 02cb2dc

Please sign in to comment.