Skip to content

Commit

Permalink
feat: process deposit requests at epoch processing (#7131)
Browse files Browse the repository at this point in the history
* initial commit

* process deposits on epoch processing

* lint

* Fix spec test

* Remove commented out code

* Update packages/state-transition/src/epoch/processPendingDeposits.ts

Co-authored-by: twoeths <tuyen@chainsafe.io>

* Address comments

* lint

---------

Co-authored-by: twoeths <tuyen@chainsafe.io>
  • Loading branch information
ensi321 and twoeths authored Oct 11, 2024
1 parent f2b96ff commit 2a0a535
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const epochTransitionFns: Record<string, EpochTransitionFn> = {
epochFns.processSyncCommitteeUpdates(fork, state as CachedBeaconStateAltair);
},
historical_summaries_update: epochFns.processHistoricalSummariesUpdate as EpochTransitionFn,
pending_balance_deposits: epochFns.processPendingBalanceDeposits as EpochTransitionFn,
pending_deposits: epochFns.processPendingDeposits as EpochTransitionFn,
pending_consolidations: epochFns.processPendingConsolidations as EpochTransitionFn,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/spec/specTestVersioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {DownloadTestsOptions} from "@lodestar/spec-test-util/downloadTests";
const __dirname = path.dirname(fileURLToPath(import.meta.url));

export const ethereumConsensusSpecsTests: DownloadTestsOptions = {
specVersion: "v1.5.0-alpha.6",
specVersion: "v1.5.0-alpha.7",
// Target directory is the host package root: 'packages/*/spec-tests'
outputDir: path.join(__dirname, "../../spec-tests"),
specTestsRepoUrl: "https://github.com/ethereum/consensus-spec-tests",
Expand Down
3 changes: 2 additions & 1 deletion packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const {

MAX_EFFECTIVE_BALANCE_ELECTRA,
MIN_ACTIVATION_BALANCE,
PENDING_BALANCE_DEPOSITS_LIMIT,
PENDING_DEPOSITS_LIMIT,
PENDING_PARTIAL_WITHDRAWALS_LIMIT,
PENDING_CONSOLIDATIONS_LIMIT,
MIN_SLASHING_PENALTY_QUOTIENT_ELECTRA,
Expand All @@ -107,6 +107,7 @@ export const {
MAX_ATTESTER_SLASHINGS_ELECTRA,
MAX_ATTESTATIONS_ELECTRA,
MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP,
MAX_PENDING_DEPOSITS_PER_EPOCH,
WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA,
} = activePreset;

Expand Down
3 changes: 2 additions & 1 deletion packages/params/src/presets/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ export const mainnetPreset: BeaconPreset = {
MAX_ATTESTER_SLASHINGS_ELECTRA: 1,
MAX_ATTESTATIONS_ELECTRA: 8,
MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP: 8,
MAX_PENDING_DEPOSITS_PER_EPOCH: 16,
// 2**11 * 10**9 (= 2,048,000,000,000) Gwei
MAX_EFFECTIVE_BALANCE_ELECTRA: 2048000000000,
MIN_SLASHING_PENALTY_QUOTIENT_ELECTRA: 4096,
MIN_ACTIVATION_BALANCE: 32000000000,
PENDING_BALANCE_DEPOSITS_LIMIT: 134217728,
PENDING_DEPOSITS_LIMIT: 134217728,
PENDING_PARTIAL_WITHDRAWALS_LIMIT: 134217728,
PENDING_CONSOLIDATIONS_LIMIT: 262144,
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD: 1,
Expand Down
3 changes: 2 additions & 1 deletion packages/params/src/presets/minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ export const minimalPreset: BeaconPreset = {
MAX_ATTESTER_SLASHINGS_ELECTRA: 1,
MAX_ATTESTATIONS_ELECTRA: 8,
MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP: 2,
MAX_PENDING_DEPOSITS_PER_EPOCH: 16,
// 2**11 * 10**9 (= 2,048,000,000,000) Gwei
MAX_EFFECTIVE_BALANCE_ELECTRA: 2048000000000,
MIN_SLASHING_PENALTY_QUOTIENT_ELECTRA: 4096,
MIN_ACTIVATION_BALANCE: 32000000000,
PENDING_BALANCE_DEPOSITS_LIMIT: 134217728,
PENDING_DEPOSITS_LIMIT: 134217728,
PENDING_PARTIAL_WITHDRAWALS_LIMIT: 64,
PENDING_CONSOLIDATIONS_LIMIT: 64,
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD: 1,
Expand Down
6 changes: 4 additions & 2 deletions packages/params/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ export type BeaconPreset = {
MAX_ATTESTER_SLASHINGS_ELECTRA: number;
MAX_ATTESTATIONS_ELECTRA: number;
MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP: number;
MAX_PENDING_DEPOSITS_PER_EPOCH: number;
MAX_EFFECTIVE_BALANCE_ELECTRA: number;
MIN_SLASHING_PENALTY_QUOTIENT_ELECTRA: number;
MIN_ACTIVATION_BALANCE: number;
PENDING_BALANCE_DEPOSITS_LIMIT: number;
PENDING_DEPOSITS_LIMIT: number;
PENDING_PARTIAL_WITHDRAWALS_LIMIT: number;
PENDING_CONSOLIDATIONS_LIMIT: number;
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD: number;
Expand Down Expand Up @@ -189,10 +190,11 @@ export const beaconPresetTypes: BeaconPresetTypes = {
MAX_ATTESTER_SLASHINGS_ELECTRA: "number",
MAX_ATTESTATIONS_ELECTRA: "number",
MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP: "number",
MAX_PENDING_DEPOSITS_PER_EPOCH: "number",
MAX_EFFECTIVE_BALANCE_ELECTRA: "number",
MIN_SLASHING_PENALTY_QUOTIENT_ELECTRA: "number",
MIN_ACTIVATION_BALANCE: "number",
PENDING_BALANCE_DEPOSITS_LIMIT: "number",
PENDING_DEPOSITS_LIMIT: "number",
PENDING_PARTIAL_WITHDRAWALS_LIMIT: "number",
PENDING_CONSOLIDATIONS_LIMIT: "number",
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD: "number",
Expand Down
81 changes: 36 additions & 45 deletions packages/state-transition/src/block/processDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@ import {
EFFECTIVE_BALANCE_INCREMENT,
FAR_FUTURE_EPOCH,
ForkSeq,
GENESIS_SLOT,
MAX_EFFECTIVE_BALANCE,
} from "@lodestar/params";

import {DepositData} from "@lodestar/types/lib/phase0/types.js";
import {DepositRequest} from "@lodestar/types/lib/electra/types.js";
import {BeaconConfig} from "@lodestar/config";
import {ZERO_HASH} from "../constants/index.js";
import {
computeDomain,
computeSigningRoot,
hasCompoundingWithdrawalCredential,
hasEth1WithdrawalCredential,
increaseBalance,
switchToCompoundingValidator,
} from "../util/index.js";
import {computeDomain, computeSigningRoot, getMaxEffectiveBalance, increaseBalance} from "../util/index.js";
import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStateElectra} from "../types.js";

/**
Expand Down Expand Up @@ -61,38 +55,43 @@ export function applyDeposit(
state: CachedBeaconStateAllForks,
deposit: DepositData | DepositRequest
): void {
const {config, validators, epochCtx} = state;
const {pubkey, withdrawalCredentials, amount} = deposit;
const {config, epochCtx} = state;
const {pubkey, withdrawalCredentials, amount, signature} = deposit;

const cachedIndex = epochCtx.getValidatorIndex(pubkey);
if (cachedIndex === null || !Number.isSafeInteger(cachedIndex) || cachedIndex >= validators.length) {
if (isValidDepositSignature(config, pubkey, withdrawalCredentials, amount, deposit.signature)) {
addValidatorToRegistry(fork, state, pubkey, withdrawalCredentials, amount);
}
} else {
if (fork < ForkSeq.electra) {
const isNewValidator = cachedIndex === null || !Number.isSafeInteger(cachedIndex);

if (fork < ForkSeq.electra) {
if (isNewValidator) {
if (isValidDepositSignature(config, pubkey, withdrawalCredentials, amount, signature)) {
addValidatorToRegistry(fork, state, pubkey, withdrawalCredentials, amount);
}
} else {
// increase balance by deposit amount right away pre-electra
increaseBalance(state, cachedIndex, amount);
} else if (fork >= ForkSeq.electra) {
const stateElectra = state as CachedBeaconStateElectra;
const pendingBalanceDeposit = ssz.electra.PendingBalanceDeposit.toViewDU({
index: cachedIndex,
amount: BigInt(amount),
});
stateElectra.pendingBalanceDeposits.push(pendingBalanceDeposit);

if (
hasCompoundingWithdrawalCredential(withdrawalCredentials) &&
hasEth1WithdrawalCredential(validators.getReadonly(cachedIndex).withdrawalCredentials) &&
isValidDepositSignature(config, pubkey, withdrawalCredentials, amount, deposit.signature)
) {
switchToCompoundingValidator(stateElectra, cachedIndex);
}
} else {
const stateElectra = state as CachedBeaconStateElectra;
const pendingDeposit = ssz.electra.PendingDeposit.toViewDU({
pubkey,
withdrawalCredentials,
amount,
signature,
slot: GENESIS_SLOT, // Use GENESIS_SLOT to distinguish from a pending deposit request
});

if (isNewValidator) {
if (isValidDepositSignature(config, pubkey, withdrawalCredentials, amount, deposit.signature)) {
addValidatorToRegistry(fork, state, pubkey, withdrawalCredentials, 0);
stateElectra.pendingDeposits.push(pendingDeposit);
}
} else {
stateElectra.pendingDeposits.push(pendingDeposit);
}
}
}

function addValidatorToRegistry(
export function addValidatorToRegistry(
fork: ForkSeq,
state: CachedBeaconStateAllForks,
pubkey: BLSPubkey,
Expand All @@ -101,8 +100,10 @@ function addValidatorToRegistry(
): void {
const {validators, epochCtx} = state;
// add validator and balance entries
const effectiveBalance =
fork < ForkSeq.electra ? Math.min(amount - (amount % EFFECTIVE_BALANCE_INCREMENT), MAX_EFFECTIVE_BALANCE) : 0;
const effectiveBalance = Math.min(
amount - (amount % EFFECTIVE_BALANCE_INCREMENT),
fork < ForkSeq.electra ? MAX_EFFECTIVE_BALANCE : getMaxEffectiveBalance(withdrawalCredentials)
);
validators.push(
ssz.phase0.Validator.toViewDU({
pubkey,
Expand Down Expand Up @@ -138,20 +139,10 @@ function addValidatorToRegistry(
stateAltair.currentEpochParticipation.push(0);
}

if (fork < ForkSeq.electra) {
state.balances.push(amount);
} else if (fork >= ForkSeq.electra) {
state.balances.push(0);
const stateElectra = state as CachedBeaconStateElectra;
const pendingBalanceDeposit = ssz.electra.PendingBalanceDeposit.toViewDU({
index: validatorIndex,
amount: BigInt(amount),
});
stateElectra.pendingBalanceDeposits.push(pendingBalanceDeposit);
}
state.balances.push(amount);
}

function isValidDepositSignature(
export function isValidDepositSignature(
config: BeaconConfig,
pubkey: Uint8Array,
withdrawalCredentials: Uint8Array,
Expand Down
13 changes: 10 additions & 3 deletions packages/state-transition/src/block/processDepositRequest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {electra} from "@lodestar/types";
import {electra, ssz} from "@lodestar/types";
import {ForkSeq, UNSET_DEPOSIT_REQUESTS_START_INDEX} from "@lodestar/params";

import {CachedBeaconStateElectra} from "../types.js";
import {applyDeposit} from "./processDeposit.js";

export function processDepositRequest(
fork: ForkSeq,
Expand All @@ -13,5 +12,13 @@ export function processDepositRequest(
state.depositRequestsStartIndex = BigInt(depositRequest.index);
}

applyDeposit(fork, state, depositRequest);
// Create pending deposit
const pendingDeposit = ssz.electra.PendingDeposit.toViewDU({
pubkey: depositRequest.pubkey,
withdrawalCredentials: depositRequest.withdrawalCredentials,
amount: depositRequest.amount,
signature: depositRequest.signature,
slot: state.slot,
});
state.pendingDeposits.push(pendingDeposit);
}
10 changes: 5 additions & 5 deletions packages/state-transition/src/epoch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ 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 {processPendingDeposits} from "./processPendingDeposits.js";
import {processPendingConsolidations} from "./processPendingConsolidations.js";

// For spec tests
Expand All @@ -48,7 +48,7 @@ export {
processParticipationFlagUpdates,
processSyncCommitteeUpdates,
processHistoricalSummariesUpdate,
processPendingBalanceDeposits,
processPendingDeposits,
processPendingConsolidations,
};

Expand All @@ -70,7 +70,7 @@ export enum EpochTransitionStep {
processEffectiveBalanceUpdates = "processEffectiveBalanceUpdates",
processParticipationFlagUpdates = "processParticipationFlagUpdates",
processSyncCommitteeUpdates = "processSyncCommitteeUpdates",
processPendingBalanceDeposits = "processPendingBalanceDeposits",
processPendingDeposits = "processPendingDeposits",
processPendingConsolidations = "processPendingConsolidations",
}

Expand Down Expand Up @@ -131,9 +131,9 @@ export function processEpoch(
const stateElectra = state as CachedBeaconStateElectra;
{
const timer = metrics?.epochTransitionStepTime.startTimer({
step: EpochTransitionStep.processPendingBalanceDeposits,
step: EpochTransitionStep.processPendingDeposits,
});
processPendingBalanceDeposits(stateElectra, cache);
processPendingDeposits(stateElectra, cache);
timer?.();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function processEffectiveBalanceUpdates(
// update effective balances with hysteresis

// epochTransitionCache.balances is initialized in processRewardsAndPenalties()
// and updated in processPendingBalanceDeposits() and processPendingConsolidations()
// and updated in processPendingDeposits() and processPendingConsolidations()
// so it's recycled here for performance.
const balances = cache.balances ?? state.balances.getAll();
const currentEpochValidators = cache.validators;
Expand Down

This file was deleted.

Loading

0 comments on commit 2a0a535

Please sign in to comment.