prysm-pulse/validator/keymanager/local/delete.go

77 lines
2.5 KiB
Go
Raw Normal View History

package local
import (
"bytes"
"context"
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v5/validator/keymanager"
"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.
Keymanager fixes for bad file writes (#12284) * WIP changes for keymanager * WIP fix * WIP needs unit tests * fixing order * adding unit test * fixing linter * updating unit tests and creating more reusable functions * making accountStore copy method part of struct * Update validator/keymanager/local/delete_test.go * Update validator/keymanager/local/delete.go * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing suggestion of not reinitializing from reading the file but instead update the information based on memory on hand * Update validator/accounts/wallet_create.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding changes based on suggestions * making logs more consistent * fixing linting --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
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
func (km *Keymanager) DeleteKeystores(
ctx context.Context, publicKeys [][]byte,
) ([]*keymanager.KeyStatus, error) {
// Check for duplicate keys and filter them out.
trackedPublicKeys := make(map[[fieldparams.BLSPubkeyLength]byte]bool)
statuses := make([]*keymanager.KeyStatus, 0, len(publicKeys))
deletedKeys := make([][]byte, 0, len(publicKeys))
Keymanager fixes for bad file writes (#12284) * WIP changes for keymanager * WIP fix * WIP needs unit tests * fixing order * adding unit test * fixing linter * updating unit tests and creating more reusable functions * making accountStore copy method part of struct * Update validator/keymanager/local/delete_test.go * Update validator/keymanager/local/delete.go * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing suggestion of not reinitializing from reading the file but instead update the information based on memory on hand * Update validator/accounts/wallet_create.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding changes based on suggestions * making logs more consistent * fixing linting --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2023-04-17 17:08:27 +00:00
// 1) Copy the in memory keystore
storeCopy := km.accountsStore.Copy()
for _, publicKey := range publicKeys {
Keymanager fixes for bad file writes (#12284) * WIP changes for keymanager * WIP fix * WIP needs unit tests * fixing order * adding unit test * fixing linter * updating unit tests and creating more reusable functions * making accountStore copy method part of struct * Update validator/keymanager/local/delete_test.go * Update validator/keymanager/local/delete.go * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing suggestion of not reinitializing from reading the file but instead update the information based on memory on hand * Update validator/accounts/wallet_create.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding changes based on suggestions * making logs more consistent * fixing linting --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2023-04-17 17:08:27 +00:00
// Check if the key in the request is a duplicate or not found
if _, ok := trackedPublicKeys[bytesutil.ToBytes48(publicKey)]; ok {
statuses = append(statuses, &keymanager.KeyStatus{
Status: keymanager.StatusNotActive,
})
continue
}
var index int
var found bool
Keymanager fixes for bad file writes (#12284) * WIP changes for keymanager * WIP fix * WIP needs unit tests * fixing order * adding unit test * fixing linter * updating unit tests and creating more reusable functions * making accountStore copy method part of struct * Update validator/keymanager/local/delete_test.go * Update validator/keymanager/local/delete.go * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing suggestion of not reinitializing from reading the file but instead update the information based on memory on hand * Update validator/accounts/wallet_create.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding changes based on suggestions * making logs more consistent * fixing linting --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2023-04-17 17:08:27 +00:00
for j, pubKey := range storeCopy.PublicKeys {
if bytes.Equal(pubKey, publicKey) {
index = j
found = true
break
}
}
if !found {
statuses = append(statuses, &keymanager.KeyStatus{
Status: keymanager.StatusNotFound,
})
continue
}
Keymanager fixes for bad file writes (#12284) * WIP changes for keymanager * WIP fix * WIP needs unit tests * fixing order * adding unit test * fixing linter * updating unit tests and creating more reusable functions * making accountStore copy method part of struct * Update validator/keymanager/local/delete_test.go * Update validator/keymanager/local/delete.go * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing suggestion of not reinitializing from reading the file but instead update the information based on memory on hand * Update validator/accounts/wallet_create.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding changes based on suggestions * making logs more consistent * fixing linting --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2023-04-17 17:08:27 +00:00
// 2) Delete the keys from copied in memory keystore
deletedPublicKey := storeCopy.PublicKeys[index]
deletedKeys = append(deletedKeys, deletedPublicKey)
Keymanager fixes for bad file writes (#12284) * WIP changes for keymanager * WIP fix * WIP needs unit tests * fixing order * adding unit test * fixing linter * updating unit tests and creating more reusable functions * making accountStore copy method part of struct * Update validator/keymanager/local/delete_test.go * Update validator/keymanager/local/delete.go * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing suggestion of not reinitializing from reading the file but instead update the information based on memory on hand * Update validator/accounts/wallet_create.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding changes based on suggestions * making logs more consistent * fixing linting --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
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:]...)
statuses = append(statuses, &keymanager.KeyStatus{
Status: keymanager.StatusDeleted,
})
trackedPublicKeys[bytesutil.ToBytes48(publicKey)] = true
}
if len(deletedKeys) == 0 {
return statuses, nil
}
Keymanager fixes for bad file writes (#12284) * WIP changes for keymanager * WIP fix * WIP needs unit tests * fixing order * adding unit test * fixing linter * updating unit tests and creating more reusable functions * making accountStore copy method part of struct * Update validator/keymanager/local/delete_test.go * Update validator/keymanager/local/delete.go * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing suggestion of not reinitializing from reading the file but instead update the information based on memory on hand * Update validator/accounts/wallet_create.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding changes based on suggestions * making logs more consistent * fixing linting --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
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 {
return nil, err
}
Keymanager fixes for bad file writes (#12284) * WIP changes for keymanager * WIP fix * WIP needs unit tests * fixing order * adding unit test * fixing linter * updating unit tests and creating more reusable functions * making accountStore copy method part of struct * Update validator/keymanager/local/delete_test.go * Update validator/keymanager/local/delete.go * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing suggestion of not reinitializing from reading the file but instead update the information based on memory on hand * Update validator/accounts/wallet_create.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding changes based on suggestions * making logs more consistent * fixing linting --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2023-04-17 17:08:27 +00:00
log.WithFields(logrus.Fields{
Keymanager fixes for bad file writes (#12284) * WIP changes for keymanager * WIP fix * WIP needs unit tests * fixing order * adding unit test * fixing linter * updating unit tests and creating more reusable functions * making accountStore copy method part of struct * Update validator/keymanager/local/delete_test.go * Update validator/keymanager/local/delete.go * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing suggestion of not reinitializing from reading the file but instead update the information based on memory on hand * Update validator/accounts/wallet_create.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding changes based on suggestions * making logs more consistent * fixing linting --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2023-04-17 17:08:27 +00:00
"publicKeys": CreatePrintoutOfKeys(deletedKeys),
}).Info("Successfully deleted validator key(s)")
Keymanager fixes for bad file writes (#12284) * WIP changes for keymanager * WIP fix * WIP needs unit tests * fixing order * adding unit test * fixing linter * updating unit tests and creating more reusable functions * making accountStore copy method part of struct * Update validator/keymanager/local/delete_test.go * Update validator/keymanager/local/delete.go * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/import.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update validator/keymanager/local/delete.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing suggestion of not reinitializing from reading the file but instead update the information based on memory on hand * Update validator/accounts/wallet_create.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding changes based on suggestions * making logs more consistent * fixing linting --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2023-04-17 17:08:27 +00:00
// 5) Return API response
return statuses, nil
}