Skip to content

Commit

Permalink
rebase fixes, fixes, improvements and cleanup
Browse files Browse the repository at this point in the history
lint
  • Loading branch information
g11tech committed May 6, 2024
1 parent 5aba7f4 commit 271b7c5
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 83 deletions.
12 changes: 2 additions & 10 deletions packages/beacon-node/src/chain/produceBlock/produceBlockBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import {
capella,
deneb,
Wei,
electra,
} from "@lodestar/types";
import {
CachedBeaconStateAllForks,
CachedBeaconStateCapella,
CachedBeaconStateBellatrix,
CachedBeaconStateElectra,
CachedBeaconStateExecutions,
computeEpochAtSlot,
computeTimeAtSlot,
Expand Down Expand Up @@ -557,7 +555,8 @@ function preparePayloadAttributes(
suggestedFeeRecipient: feeRecipient,
};

if (ForkSeq[fork] === ForkSeq.capella || ForkSeq[fork] === ForkSeq.electra) {
if (ForkSeq[fork] >= ForkSeq.capella) {
// withdrawals logic is now fork aware as it changes on electra fork post capella
(payloadAttributes as capella.SSEPayloadAttributes["payloadAttributes"]).withdrawals = getExpectedWithdrawals(
ForkSeq[fork],
prepareState as CachedBeaconStateCapella
Expand All @@ -568,13 +567,6 @@ function preparePayloadAttributes(
(payloadAttributes as deneb.SSEPayloadAttributes["payloadAttributes"]).parentBeaconBlockRoot = parentBlockRoot;
}

if (ForkSeq[fork] >= ForkSeq.electra) {
(payloadAttributes as electra.SSEPayloadAttributes["payloadAttributes"]).withdrawals = getExpectedWithdrawals(
ForkSeq[fork],
prepareState as CachedBeaconStateElectra
).withdrawals;
}

return payloadAttributes;
}

Expand Down
10 changes: 5 additions & 5 deletions packages/beacon-node/src/metrics/metrics/lodestar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1211,16 +1211,16 @@ export function createLodestarMetrics(
help: "Histogram of cloned count per state every time state.clone() is called",
buckets: [1, 2, 5, 10, 50, 250],
}),
numStatesUpdated: register.histogram({
name: "lodestar_cp_state_cache_state_updated_count",
help: "Histogram of number of state cache items updated every time removing and adding pubkeys to pubkey cache",
buckets: [1, 2, 5, 10, 50, 250],
}),
stateSerializeDuration: register.histogram({
name: "lodestar_cp_state_cache_state_serialize_seconds",
help: "Histogram of time to serialize state to db",
buckets: [0.1, 0.5, 1, 2, 3, 4],
}),
numStatesUpdated: register.histogram({
name: "lodestar_cp_state_cache_state_updated_count",
help: "Histogram of number of state cache items updated every time removing and adding pubkeys to pubkey cache",
buckets: [1, 2, 5, 10, 50, 250],
}),
statePruneFromMemoryCount: register.gauge({
name: "lodestar_cp_state_cache_state_prune_from_memory_count",
help: "Total number of states pruned from memory",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ describe("AggregatedAttestationPool", function () {
epochParticipation[committee[i]] = 0b000;
}
}
(originalState as unknown as CachedBeaconStateAltair).previousEpochParticipation =
(originalState as CachedBeaconStateAltair).previousEpochParticipation =
ssz.altair.EpochParticipation.toViewDU(epochParticipation);
(originalState as unknown as CachedBeaconStateAltair).currentEpochParticipation =
(originalState as CachedBeaconStateAltair).currentEpochParticipation =
ssz.altair.EpochParticipation.toViewDU(epochParticipation);
originalState.commit();
let altairState: CachedBeaconStateAllForks;
Expand Down
16 changes: 2 additions & 14 deletions packages/state-transition/src/block/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {ForkSeq} from "@lodestar/params";
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} from "../types.js";
import {processExecutionPayload} from "./processExecutionPayload.js";
import {processSyncAggregate} from "./processSyncCommittee.js";
import {processBlockHeader} from "./processBlockHeader.js";
Expand Down Expand Up @@ -50,20 +45,13 @@ export function processBlock(
const fullOrBlindedPayload = getFullOrBlindedPayload(block);
// TODO Deneb: Allow to disable withdrawals for interop testing
// https://github.com/ethereum/consensus-specs/blob/b62c9e877990242d63aa17a2a59a49bc649a2f2e/specs/eip4844/beacon-chain.md#disabling-withdrawals
if (fork === ForkSeq.capella || fork === ForkSeq.deneb) {
if (fork >= ForkSeq.capella) {
processWithdrawals(
fork,
state as CachedBeaconStateCapella,
fullOrBlindedPayload as capella.FullOrBlindedExecutionPayload
);
}
if (fork >= ForkSeq.electra) {
processWithdrawals(
fork,
state as CachedBeaconStateElectra,
fullOrBlindedPayload as capella.FullOrBlindedExecutionPayload
);
}

processExecutionPayload(fork, state as CachedBeaconStateBellatrix, block.body, externalData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,24 @@ export function processExecutionLayerWithdrawalRequest(
): void {
const amount = Number(executionLayerWithdrawalRequest.amount);
const {pendingPartialWithdrawals, validators, epochCtx} = state;
const {pubkey2index, config} = epochCtx; // TODO Electra: Use finalized+unfinalized pubkey cache from 6110
// no need to use unfinalized pubkey cache from 6110 as validator won't be active anyway
const {pubkey2index, config} = epochCtx;
const isFullExitRequest = amount === FULL_EXIT_REQUEST_AMOUNT;

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

// bail out if validator is not in beacon state
// note that we don't need to check for 6110 unfinalized vals as they won't be eligible for withdraw/exit anyway
const validatorIndex = pubkey2index.get(executionLayerWithdrawalRequest.validatorPubkey);

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

const validator = validators.getReadonly(validatorIndex);

if (!isValidValidator(validator, executionLayerWithdrawalRequest.sourceAddress, state)) {
if (!isValidatorEligibleForWithdrawOrExit(validator, executionLayerWithdrawalRequest.sourceAddress, state)) {
return;
}

Expand All @@ -53,13 +50,11 @@ export function processExecutionLayerWithdrawalRequest(
// only exit validator if it has no pending withdrawals in the queue
if (pendingBalanceToWithdraw === 0) {
initiateValidatorExit(fork, state, validator);
} else {
// Full request can't be fulfilled because still has pending withdrawals.
// TODO Electra: add log here
}
return;
}

// partial withdrawal request
const hasSufficientEffectiveBalance = validator.effectiveBalance >= MIN_ACTIVATION_BALANCE;
const hasExcessBalance = validatorBalance > MIN_ACTIVATION_BALANCE + pendingBalanceToWithdraw;

Expand All @@ -84,7 +79,7 @@ export function processExecutionLayerWithdrawalRequest(
}
}

function isValidValidator(
function isValidatorEligibleForWithdrawOrExit(
validator: phase0.Validator,
sourceAddress: Uint8Array,
state: CachedBeaconStateElectra
Expand Down
12 changes: 6 additions & 6 deletions packages/state-transition/src/block/processOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import {processProposerSlashing} from "./processProposerSlashing.js";
import {processAttesterSlashing} from "./processAttesterSlashing.js";
import {processDeposit} from "./processDeposit.js";
import {processVoluntaryExit} from "./processVoluntaryExit.js";
import {processExecutionLayerWithdrawalRequest} from "./processExecutionLayerWithdrawalRequest.js";
import {processBlsToExecutionChange} from "./processBlsToExecutionChange.js";
import {processExecutionLayerWithdrawalRequest} from "./processExecutionLayerWithdrawalRequest.js";
import {processDepositReceipt} from "./processDepositReceipt.js";
import {ProcessBlockOpts} from "./types.js";
import {processExecutionLayerWithdrawalRequest} from "./processExecutionLayerWithdrawalRequest.js";
import {processConsolidation} from "./processConsolidation.js";

export {
Expand Down Expand Up @@ -66,12 +65,13 @@ export function processOperations(
if (fork >= ForkSeq.electra) {
const stateElectra = state as CachedBeaconStateElectra;
const bodyElectra = body as electra.BeaconBlockBody;
for (const depositReceipt of bodyElectra.executionPayload.depositReceipts) {
processDepositReceipt(fork, stateElectra, depositReceipt);
}

for (const elWithdrawalRequest of bodyElectra.executionPayload.withdrawalRequests) {
processExecutionLayerWithdrawalRequest(fork ,state as CachedBeaconStateElectra, elWithdrawalRequest);
processExecutionLayerWithdrawalRequest(fork, state as CachedBeaconStateElectra, elWithdrawalRequest);
}

for (const depositReceipt of bodyElectra.executionPayload.depositReceipts) {
processDepositReceipt(fork, stateElectra, depositReceipt);
}

for (const consolidation of bodyElectra.consolidations) {
Expand Down
2 changes: 1 addition & 1 deletion packages/state-transition/src/block/processWithdrawals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function processWithdrawals(
}

if (fork >= ForkSeq.electra) {
const stateElectra = state as CachedBeaconStateElectra;
const _stateElectra = state as CachedBeaconStateElectra;
// TODO Electra: stateElectra.pendingPartialWithdrawals need to implement slicing mechanism for ListCompositeTreeViewDU
}

Expand Down
7 changes: 1 addition & 6 deletions packages/state-transition/src/cache/epochTransitionCache.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {Epoch, ValidatorIndex} from "@lodestar/types";
import {intDiv} from "@lodestar/utils";
import {
EPOCHS_PER_SLASHINGS_VECTOR,
FAR_FUTURE_EPOCH,
ForkSeq,
MIN_ACTIVATION_BALANCE,
} from "@lodestar/params";
import {EPOCHS_PER_SLASHINGS_VECTOR, FAR_FUTURE_EPOCH, ForkSeq, MIN_ACTIVATION_BALANCE} from "@lodestar/params";

import {
AttesterStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {getActivationExitChurnLimit} from "../util/validator.js";
export function processPendingBalanceDeposits(state: CachedBeaconStateElectra): void {
const availableForProcessing = state.depositBalanceToConsume + BigInt(getActivationExitChurnLimit(state));
let processedAmount = 0n;
let nextDepositIndex = 0;
let _nextDepositIndex = 0;

for (const deposit of state.pendingBalanceDeposits.getAllReadonly()) {
const {amount} = deposit;
Expand All @@ -21,7 +21,7 @@ export function processPendingBalanceDeposits(state: CachedBeaconStateElectra):
}
increaseBalance(state, deposit.index, Number(amount));
processedAmount = processedAmount + amount;
nextDepositIndex++;
_nextDepositIndex++;
}

// TODO Electra: Impl slicing for ssz
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import {switchToCompoundingValidator} from "../util/electra.js";
*
*/
export function processPendingConsolidations(state: CachedBeaconStateElectra): void {
let nextPendingConsolidation = 0;
let _nextPendingConsolidation = 0;

for (const pendingConsolidation of state.pendingConsolidations.getAllReadonly()) {
const {sourceIndex, targetIndex} = pendingConsolidation;
const sourceValidator = state.validators.getReadonly(sourceIndex);

if (sourceValidator.slashed) {
nextPendingConsolidation++;
_nextPendingConsolidation++;
continue;
}

Expand All @@ -38,7 +38,7 @@ export function processPendingConsolidations(state: CachedBeaconStateElectra): v
decreaseBalance(state, sourceIndex, activeBalance);
increaseBalance(state, targetIndex, activeBalance);

nextPendingConsolidation++;
_nextPendingConsolidation++;
}

// TODO Electra: impl slicing for ssz
Expand Down
2 changes: 1 addition & 1 deletion packages/state-transition/src/util/capella.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ETH1_ADDRESS_WITHDRAWAL_PREFIX} from "@lodestar/params/";
import {ETH1_ADDRESS_WITHDRAWAL_PREFIX} from "@lodestar/params";

/**
* https://github.com/ethereum/consensus-specs/blob/3d235740e5f1e641d3b160c8688f26e7dc5a1894/specs/capella/beacon-chain.md#has_eth1_withdrawal_credential
Expand Down
14 changes: 6 additions & 8 deletions packages/state-transition/src/util/electra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ export function isFullyWithdrawableValidator(
epoch: number
): boolean {
const {withdrawableEpoch, withdrawalCredentials: withdrawalCredential} = validatorCredential;
if (fork === ForkSeq.capella || fork === ForkSeq.deneb) {
return hasEth1WithdrawalCredential(withdrawalCredential) && withdrawableEpoch <= epoch && balance > 0;
}

if (fork === ForkSeq.electra) {
if (fork >= ForkSeq.electra) {
return hasExecutionWithdrawalCredential(withdrawalCredential) && withdrawableEpoch <= epoch && balance > 0;
} else if (fork >= ForkSeq.capella) {
return hasEth1WithdrawalCredential(withdrawalCredential) && withdrawableEpoch <= epoch && balance > 0;
} else {
return false;
}

return false;
}

export function isPartiallyWithdrawableValidator(
Expand All @@ -48,7 +46,7 @@ export function isPartiallyWithdrawableValidator(
const {effectiveBalance, withdrawalCredentials: withdrawalCredential} = validatorCredential;

if (fork < ForkSeq.capella) {
throw new Error("Unsupported fork");
throw new Error(`isPartiallyWithdrawableValidator not supported at forkSeq=${fork} < ForkSeq.capella`);
}

const validatorMaxEffectiveBalance =
Expand Down
10 changes: 0 additions & 10 deletions packages/types/src/electra/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
PENDING_BALANCE_DEPOSITS_LIMIT,
PENDING_CONSOLIDATIONS_LIMIT,
PENDING_PARTIAL_WITHDRAWALS_LIMIT,
MAX_WITHDRAWALS_PER_PAYLOAD,
} from "@lodestar/params";
import {ssz as primitiveSsz} from "../primitive/index.js";
import {ssz as phase0Ssz} from "../phase0/index.js";
Expand Down Expand Up @@ -137,15 +136,6 @@ export const ExecutionLayerWithdrawalRequests = new ListCompositeType(
MAX_WITHDRAWAL_REQUESTS_PER_PAYLOAD
);

export const ExecutionLayerWithdrawalRequest = new ContainerType(
{
sourceAddress: ExecutionAddress,
validatorPubkey: BLSPubkey,
amount: Gwei,
},
{typeName: "ExecutionLayerWithdrawalRequest", jsonCase: "eth2"}
);

export const ExecutionPayload = new ContainerType(
{
...denebSsz.ExecutionPayload.fields,
Expand Down
2 changes: 0 additions & 2 deletions packages/types/src/electra/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,3 @@ export type SignedConsolidation = ValueOf<typeof ssz.SignedConsolidation>;
export type PendingBalanceDeposit = ValueOf<typeof ssz.PendingBalanceDeposit>;
export type PartialWithdrawal = ValueOf<typeof ssz.PartialWithdrawal>;
export type PendingConsolidation = ValueOf<typeof ssz.PendingConsolidation>;

export type ExecutionLayerWithdrawalRequest = ValueOf<typeof ssz.ExecutionLayerWithdrawalRequest>;

0 comments on commit 271b7c5

Please sign in to comment.