ChainSafe / lodestar

🌟 TypeScript Implementation of Ethereum Consensus
https://lodestar.chainsafe.io
Apache License 2.0
1.18k stars 289 forks source link

Support better voluntary exit errors #6330

Open wemeetagain opened 9 months ago

wemeetagain commented 9 months ago

If we wanted to support better logging, esp in the case an exit is already processed by the chain, we could create a getVoluntaryExitValidity to return an enum value

export enum VoluntaryExitValidity {
  valid = "valid",
  inactive = "inactive",
  alreadyExited = "already_exited",
  earlyEpoch = "early_epoch",
  shortTimeActive = "short_time_active"
  invalidSignature = "invalid_signature"
}

export function isValidVoluntaryExit(
  state: CachedBeaconStateAllForks,
  signedVoluntaryExit: phase0.SignedVoluntaryExit,
  verifySignature = true
): boolean {
  return getVoluntaryExitValidity(state,signedVoluntaryExit, verifySignature) === VoluntaryExitValidity.valid;
}

// this function is almost exactly what isValidVoluntaryExit was, except that each error case is handled separately,
// returning an enum value that describes the error case
export function getVoluntaryExitValidity(
  state: CachedBeaconStateAllForks,
  signedVoluntaryExit: phase0.SignedVoluntaryExit,
  verifySignature = true
): VoluntaryExitValidity {
  const {config, epochCtx} = state;
  const voluntaryExit = signedVoluntaryExit.message;
  const validator = state.validators.get(voluntaryExit.validatorIndex);
  const currentEpoch = epochCtx.epoch;

  // verify the validator is active
  if(isActiveValidator(validator, currentEpoch) return VoluntaryExitValidity.inactive;
  ...

  return VoluntaryExitValidity.valid;
}

That enum value can get passed up into an error message / code and checked against for logging.

_Originally posted by @wemeetagain in https://github.com/ChainSafe/lodestar/pull/6327#discussion_r1459367323_

mabsattar commented 9 months ago

Hi, I'd like to tackle this..

nflaig commented 9 months ago

I assigned the issue to you @mabsattar, feel free to join us on discord in the #lodestar-developer channel

wemeetagain commented 9 months ago

@mabsattar yeah best course of action is to join our discord for questions. This feature is fairly well scoped. Start by implementing the function described above. Then this new function needs to be used in the validator exit flow, instead of isValidVoluntaryExit to provide better logging. For this step, you can review the code that was added in #6327 to get a start of where you need to tweak things.