Skip to content

Commit

Permalink
rebase fixes, fixes, improvements and cleanup
Browse files Browse the repository at this point in the history
cleanup
  • Loading branch information
g11tech committed May 6, 2024
1 parent 5aba7f4 commit 57c8aaa
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 55 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
9 changes: 1 addition & 8 deletions packages/state-transition/src/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,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
10 changes: 5 additions & 5 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,14 +65,15 @@ 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);
}

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

for (const consolidation of bodyElectra.consolidations) {
processConsolidation(stateElectra, consolidation);
}
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
9 changes: 0 additions & 9 deletions packages/types/src/electra/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,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 57c8aaa

Please sign in to comment.