2020-10-15 22:31:52 +00:00
|
|
|
package accounts
|
2020-08-13 15:23:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
2020-10-16 18:45:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/imported"
|
2020-08-13 15:23:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeleteAccounts_Noninteractive(t *testing.T) {
|
|
|
|
walletDir, _, passwordFilePath := setupWalletAndPasswordsDir(t)
|
|
|
|
// Write a directory where we will import keys from.
|
2020-11-10 22:45:17 +00:00
|
|
|
keysDir := filepath.Join(t.TempDir(), "keysDir")
|
2020-08-13 15:23:08 +00:00
|
|
|
require.NoError(t, os.MkdirAll(keysDir, os.ModePerm))
|
|
|
|
|
2020-08-20 19:14:03 +00:00
|
|
|
// Create 3 keystore files in the keys directory we can then
|
2020-08-13 15:23:08 +00:00
|
|
|
// import from in our wallet.
|
|
|
|
k1, _ := createKeystore(t, keysDir)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
k2, _ := createKeystore(t, keysDir)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
k3, _ := createKeystore(t, keysDir)
|
|
|
|
generatedPubKeys := []string{k1.Pubkey, k2.Pubkey, k3.Pubkey}
|
|
|
|
// Only delete keys 0 and 1.
|
|
|
|
deletePublicKeys := strings.Join(generatedPubKeys[0:2], ",")
|
|
|
|
|
2020-10-16 18:45:14 +00:00
|
|
|
// We initialize a wallet with a imported keymanager.
|
2020-08-13 15:23:08 +00:00
|
|
|
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
|
|
|
// Wallet configuration flags.
|
|
|
|
walletDir: walletDir,
|
2020-10-16 18:45:14 +00:00
|
|
|
keymanagerKind: keymanager.Imported,
|
2020-08-13 15:23:08 +00:00
|
|
|
walletPasswordFile: passwordFilePath,
|
|
|
|
accountPasswordFile: passwordFilePath,
|
|
|
|
// Flags required for ImportAccounts to work.
|
|
|
|
keysDir: keysDir,
|
|
|
|
// Flags required for DeleteAccounts to work.
|
|
|
|
deletePublicKeys: deletePublicKeys,
|
|
|
|
})
|
2020-09-17 01:34:42 +00:00
|
|
|
w, err := CreateWalletWithKeymanager(cliCtx.Context, &CreateWalletConfig{
|
|
|
|
WalletCfg: &wallet.Config{
|
2020-08-31 19:46:45 +00:00
|
|
|
WalletDir: walletDir,
|
2020-10-16 18:45:14 +00:00
|
|
|
KeymanagerKind: keymanager.Imported,
|
2020-08-31 19:46:45 +00:00
|
|
|
WalletPassword: password,
|
|
|
|
},
|
|
|
|
})
|
2020-08-13 15:23:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// We attempt to import accounts.
|
2020-08-31 19:46:45 +00:00
|
|
|
require.NoError(t, ImportAccountsCli(cliCtx))
|
2020-08-13 15:23:08 +00:00
|
|
|
|
|
|
|
// We attempt to delete the accounts specified.
|
2020-08-31 19:46:45 +00:00
|
|
|
require.NoError(t, DeleteAccountCli(cliCtx))
|
2020-08-13 15:23:08 +00:00
|
|
|
|
2020-10-16 18:45:14 +00:00
|
|
|
keymanager, err := imported.NewKeymanager(
|
2020-08-31 19:46:45 +00:00
|
|
|
cliCtx.Context,
|
2020-10-16 18:45:14 +00:00
|
|
|
&imported.SetupConfig{
|
2021-02-24 18:05:46 +00:00
|
|
|
Wallet: w,
|
|
|
|
ListenForChanges: false,
|
2020-08-31 19:46:45 +00:00
|
|
|
},
|
2020-08-13 15:23:08 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2020-08-31 19:46:45 +00:00
|
|
|
remainingAccounts, err := keymanager.FetchValidatingPublicKeys(cliCtx.Context)
|
2020-08-13 15:23:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(remainingAccounts), 1)
|
|
|
|
remainingPublicKey, err := hex.DecodeString(k3.Pubkey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, remainingAccounts[0], bytesutil.ToBytes48(remainingPublicKey))
|
|
|
|
}
|