Skip to content

3.6 Code Lodestar Staking

To start staking, one needs to make a deposit of at least 32 ETH to become a validator.

Validator Registration and Exit#

Validator deposit is done through a smart contract at: https://etherscan.io/address/0x00000000219ab540356cBB839Cbe05303d7705Fa. Its source code is maintained in spec

Validator withdraw or exit is initialized by broadcasting message voluntary_exit through the gossipsub.

Both deposit and withdraw is processed by processOperations in https://github.com/ChainSafe/lodestar/blob/v1.5.1/packages/state-transition/src/block/processOperations.ts during 3.5 Code - Lodestar - Block Building#importBlock

Rewards and Penalties#

Rewards and penalties are processed per epoch change by consensus clients. While other accounts balances are stored in the database of the execution client.
For validators, the 'effective balance' is managed by the consensus client. (See Balances from eth2book).

Note that 'penalties' refer to light punishment for being slow and the amount is small. 'slashings' refer to heavy punishment for being dishonest or even malicious and it could mean the removal of the validator. For full explanation see here. Here you can see the list of slashed validators: https://beaconscan.com/slots-slashed.

In https://github.com/ChainSafe/lodestar/blob/v1.5.1/packages/state-transition/src/epoch/index.ts function processEpoch:

processRewardsAndPenalties(state, epochProcess);
processSlashings(state, epochProcess);

The rewards and penalties are calculated in https://github.com/ChainSafe/lodestar/blob/v1.5.1/packages/state-transition/src/epoch/getAttestationDeltas.ts.

Slashing#

Slashing as a much heavier penalty is done very differently. lodestar didn't have slashing detection built-in. Rather, it is expected that the node operator run separate tools to interact through the REST API to submit slashing(See https://ethereum.stackexchange.com/questions/83644/how-to-run-slasher-slashing-detectors-in-ethereum-2-0). Then it will broadcast to gossip network on topic proposer_slashing or attester_slashing. If the slashing is correct, it will be included in the next block proposal.

Slashing - My Take

The potential slashing period is very long: up to 18 days(half of EPOCHS_PER_SLASHINGS_VECTOR = 8192) after the block. Maybe that's because slashing detection is rare and difficult to detect so Ethereum is giving validators extra time to 'whistle blow'.

In https://github.com/ChainSafe/lodestar/blob/v1.5.1/packages/beacon-node/src/api/impl/beacon/pool/index.ts, submitPoolProposerSlashings and submitPoolAttesterSlashings. These functions are called by the REST API, e.g. POST /eth/v1/beacon/pool/attester_slashings:

async submitPoolAttesterSlashings(attesterSlashing) {
  /* check if slashing is according to the rules */
  await validateGossipAttesterSlashing(chain, attesterSlashing);
  /* 
  add to local pool and
  it will gets included in the next block
  if one of the validators will be the proposer
  */
  chain.opPool.insertAttesterSlashing(attesterSlashing);
  /* broadcast to gossip network */
  await network.gossip.publishAttesterSlashing(attesterSlashing);
},

During 3.5 Code - Lodestar - Block Building#importBlock, the slashing is processed in https://github.com/ChainSafe/lodestar/blob/v1.5.1/packages/state-transition/src/epoch/processSlashings.ts

for (const index of process.indicesToSlash) {
    const effectiveBalanceIncrement = effectiveBalanceIncrements[index];
    const penaltyNumerator = BigInt(effectiveBalanceIncrement) * adjustedTotalSlashingBalance;
    const penalty = Number(penaltyNumerator / totalBalance) * increment;
    decreaseBalance(state, index, penalty);
}