Repository navigation
node, wallet: clean up derived validator keys on failed deposits (#379) - #1331
Sanchay117 wants to merge 2 commits into
Conversation
| } | ||
| if status.Exists { | ||
| return nil, fmt.Errorf("**** ALERT ****\n"+ | ||
| return nil, nil, fmt.Errorf("**** ALERT ****\n"+ |
There was a problem hiding this comment.
Every pubkey that goes through CreateValidatorKey gets appended to createdPubkeys and deleted whenever depositSucceeded stays false. One of those paths is the beacon status.Exists return: the key was just stored (file keystores overwrite an existing directory; Prysm StoreValidatorKey no-ops when the pubkey is already present), then the defer removes it from every consensus keystore. That pubkey is already a live validator, and this branch exists specifically to avoid depositing it again, so cleanup takes the VC's signing key offline.
The same list also deletes a key that was already on disk when next_account is stale (the #379 state this change is aimed at) and a later step fails, for example a status RPC error, before DepositMulti. Earlier new keys in the batch should still be removed; this pubkey should not.
I'd leave pre-existing pubkeys out of createdPubkeys, either before CreateValidatorKey or via a store result that reports "already present". On status.Exists, keep that pubkey's keystores (they're the operator's key for the active validator) and still persist next_account on submit == true so the index isn't reused.
c2b04f7 to
daca570
Compare
|
Good catch, thank you. Updated in
|
Fixes #379
Summary
When
rocketpool node deposit(nodeDeposits) fails mid-batch or duringDepositMultisubmission, Smartnode previously left newly derived validator keystore and secret files on disk across all consensus client keystores and discarded the in-memorynext_accountincrement (becausew.Save()was only called on the happy path at the end ofnodeDeposits).Under Saturn megapool batch deposits (
count >= 1), this created two compounding hazards:#379): Everyw.CreateValidatorKey()iteration immediately writes encrypted keystore + password files for Lighthouse, Lodestar, Nimbus, Prysm (all-accounts.keystore.json), and Teku. If deposit validation, status lookup, ornode.DepositMultifailed (e.g.FeeTooLowToCompeteas in#386, RPC timeout, or contract revert), up tocountunfunded validator keys remained on disk and would be loaded on the next VC restart.#379/#427): Becauseservices.GetWalletreloadshdWalletfrom disk on each API call, omittingw.Save()on failure causednext_accountto revert to its pre-batch value. If an earlier transaction broadcast was ambiguous (e.g. mempool propagation timeout that later landed), a retry re-derived the exact same validator pubkeys.Per
@jshufro's specification in #379, failed deposits now delete all newly derived validator keys from every keystore while preserving (persisting) the incrementednext_accountcounter on live (submit == true) deposit attempts.Changes
Per-key deletion across all 5 consensus client keystores (
shared/services/wallet/keystore/...):DeleteValidatorKey(pubkey types.ValidatorPubkey) errortokeystore.Keystore.validators/0x<pubkey>directory andsecrets/0x<pubkey>file (os.ErrNotExisttreated asnil).keys/0x<pubkey>.jsonandpasswords/0x<pubkey>.txt.prysm-non-hd): Ifall-accounts.keystore.jsondoes not exist yet, returnnilwithout creating directories orsecret. Otherwise load the account store, filter out matching pubkey/privkey entries while preserving the order of all remaining keys, and re-encryptall-accounts.keystore.jsonin place.Wallet-level key deletion & partial-write rollback (
shared/services/wallet/...):DeleteValidatorKey(pubkey rptypes.ValidatorPubkey) errortowallet.Wallet,hdWallet, andmasqueradeWallet(ErrIsMasquerading).hdWallet.DeleteValidatorKeyiterates deterministically across all registered keystores and joins errors viaerrors.Joinso an error in one keystore never prevents cleanup of the remaining keystores. It intentionally leavesw.ws.NextAccountuntouched.hdWallet.CreateValidatorKeynow rolls back any partially written keystore artifacts viaDeleteValidatorKeyifStoreValidatorKeyfails mid-way across client keystores, while keepingNextAccountincremented.Transactional batch cleanup in
nodeDeposits(rocketpool/api/node/deposit.go):executeDepositBatchwith adefercleanup guard trackingcreatedPubkeysanddepositSucceeded.!depositSucceeded), allcreatedPubkeysfrom the batch are deleted across all 5 keystores, and whensubmit == true,w.Save()persists the advancednext_accountto disk.submit == false(offline/unsigned tx generation), temporary keystore files are cleaned up on failure without mutatingnext_accounton disk.depositSucceeded = trueis set immediately uponsubmitDeposits(node.DepositMulti) returningerr == nil(prior tow.Save()), guaranteeing that live validator keys for an already-broadcast deposit transaction are never deleted even if saving the wallet file subsequently fails.Testing
Added unit tests covering both layers:
shared/services/wallet/validator_cleanup_test.go:lighthouse,lodestar,nimbus,prysm,teku), verifying disk artifact removal, neighbor key preservation, Prysm reload from disk, idempotency, andnext_accountnon-decrement.all-accounts.keystore.jsondown to 0 keys and reusing it for subsequent keys.CreateValidatorKeyencounters a failing keystore.masqueradeWallet.DeleteValidatorKeyreturningErrIsMasquerading.rocketpool/api/node/deposit_cleanup_test.go:count = 2) across all 5 real keystores + persistednext_account.DepositMultisubmission failure with pre-existing active validators: verifies pre-existing keys survive, all failed batch keys are wiped across all 5 keystores, reloaded wallet preservesnext_account(5), subsequent retry derives fresh index5, andRecoverValidatorKeyrecovers index5across the burned index gap (2..4).getBondRequirement,getDepositData,getValidatorStatusRPC error,status.Existsduplicate pubkey alert,validateDepositsignature failure).submit = falsefailure leaving persisted walletnext_accountuntouched while wiping temporary keystore files.w.Save()failure retaining all live validator keys on disk.panicrecovery guard wiping derived keys and savingnext_accountbefore re-panicking.go test -v ./shared/services/wallet/... ./rocketpool/api/node/... ./shared/services ./rocketpool/api/wallet/...