2022-02-01 19:54:19 +00:00
|
|
|
package local
|
2021-11-19 04:11:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/validator/keymanager"
|
2021-11-19 04:11:54 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteKeystores takes in public keys and removes the accounts from the wallet.
|
|
|
|
// This includes their disk keystore and cached keystore, but maintains the slashing
|
|
|
|
// protection history in the database.
|
2023-04-17 17:08:27 +00:00
|
|
|
// 1) Copy the in memory keystore
|
|
|
|
// 2) Delete the keys from copied in memory keystore
|
|
|
|
// 3) Save the copy to disk
|
|
|
|
// 4) Reinitialize account store and updating the keymanager
|
|
|
|
// 5) Return API response
|
2021-11-19 04:11:54 +00:00
|
|
|
func (km *Keymanager) DeleteKeystores(
|
|
|
|
ctx context.Context, publicKeys [][]byte,
|
2023-10-31 16:33:54 +00:00
|
|
|
) ([]*keymanager.KeyStatus, error) {
|
2021-11-19 04:11:54 +00:00
|
|
|
// Check for duplicate keys and filter them out.
|
2022-01-06 17:33:08 +00:00
|
|
|
trackedPublicKeys := make(map[[fieldparams.BLSPubkeyLength]byte]bool)
|
2023-10-31 16:33:54 +00:00
|
|
|
statuses := make([]*keymanager.KeyStatus, 0, len(publicKeys))
|
2021-11-19 04:11:54 +00:00
|
|
|
deletedKeys := make([][]byte, 0, len(publicKeys))
|
2023-04-17 17:08:27 +00:00
|
|
|
// 1) Copy the in memory keystore
|
|
|
|
storeCopy := km.accountsStore.Copy()
|
2021-11-19 04:11:54 +00:00
|
|
|
for _, publicKey := range publicKeys {
|
2023-04-17 17:08:27 +00:00
|
|
|
// Check if the key in the request is a duplicate or not found
|
2021-11-19 04:11:54 +00:00
|
|
|
if _, ok := trackedPublicKeys[bytesutil.ToBytes48(publicKey)]; ok {
|
2023-10-31 16:33:54 +00:00
|
|
|
statuses = append(statuses, &keymanager.KeyStatus{
|
|
|
|
Status: keymanager.StatusNotActive,
|
2021-11-19 04:11:54 +00:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var index int
|
|
|
|
var found bool
|
2023-04-17 17:08:27 +00:00
|
|
|
for j, pubKey := range storeCopy.PublicKeys {
|
2021-11-19 04:11:54 +00:00
|
|
|
if bytes.Equal(pubKey, publicKey) {
|
|
|
|
index = j
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2023-10-31 16:33:54 +00:00
|
|
|
statuses = append(statuses, &keymanager.KeyStatus{
|
|
|
|
Status: keymanager.StatusNotFound,
|
2021-11-19 04:11:54 +00:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2023-04-17 17:08:27 +00:00
|
|
|
// 2) Delete the keys from copied in memory keystore
|
|
|
|
deletedPublicKey := storeCopy.PublicKeys[index]
|
2021-11-19 04:11:54 +00:00
|
|
|
deletedKeys = append(deletedKeys, deletedPublicKey)
|
2023-04-17 17:08:27 +00:00
|
|
|
storeCopy.PrivateKeys = append(storeCopy.PrivateKeys[:index], storeCopy.PrivateKeys[index+1:]...)
|
|
|
|
storeCopy.PublicKeys = append(storeCopy.PublicKeys[:index], storeCopy.PublicKeys[index+1:]...)
|
2023-10-31 16:33:54 +00:00
|
|
|
statuses = append(statuses, &keymanager.KeyStatus{
|
|
|
|
Status: keymanager.StatusDeleted,
|
2021-11-19 04:11:54 +00:00
|
|
|
})
|
|
|
|
trackedPublicKeys[bytesutil.ToBytes48(publicKey)] = true
|
|
|
|
}
|
|
|
|
if len(deletedKeys) == 0 {
|
|
|
|
return statuses, nil
|
|
|
|
}
|
2023-04-17 17:08:27 +00:00
|
|
|
// 3 & 4) save to disk and re-initializes keystore
|
|
|
|
if err := km.SaveStoreAndReInitialize(ctx, storeCopy); err != nil {
|
2021-11-19 04:11:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-17 17:08:27 +00:00
|
|
|
|
2021-11-24 15:40:49 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
2023-04-17 17:08:27 +00:00
|
|
|
"publicKeys": CreatePrintoutOfKeys(deletedKeys),
|
2021-11-24 15:40:49 +00:00
|
|
|
}).Info("Successfully deleted validator key(s)")
|
2023-04-17 17:08:27 +00:00
|
|
|
// 5) Return API response
|
2021-11-19 04:11:54 +00:00
|
|
|
return statuses, nil
|
|
|
|
}
|