mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-08 02:31:19 +00:00
ef21d3adf8
* `EpochFromString`: Use already defined `Uint64FromString` function. * `Test_uint64FromString` => `Test_FromString` This test function tests more functions than `Uint64FromString`. * Slashing protection history: Remove unreachable code. The function `NewKVStore` creates, via `kv.UpdatePublicKeysBuckets`, a new item in the `proposal-history-bucket-interchange`. IMO there is no real reason to prefer `proposal` than `attestation` as a prefix for this bucket, but this is the way it is done right now and renaming the bucket will probably be backward incompatible. An `attestedPublicKey` cannot exist without the corresponding `proposedPublicKey`. Thus, the `else` portion of code removed in this commit is not reachable. We raise an error if we get there. This is also probably the reason why the removed `else` portion was not tested. * `NewKVStore`: Switch items in `createBuckets`. So the order corresponds to `schema.go` * `slashableAttestationCheck`: Fix comments and logs. * `ValidatorClient.db`: Use `iface.ValidatorDB`. * BoltDB database: Implement `GraffitiFileHash`. * Filesystem database: Creates `db.go`. This file defines the following structs: - `Store` - `Graffiti` - `Configuration` - `ValidatorSlashingProtection` This files implements the following public functions: - `NewStore` - `Close` - `Backup` - `DatabasePath` - `ClearDB` - `UpdatePublicKeysBuckets` This files implements the following private functions: - `slashingProtectionDirPath` - `configurationFilePath` - `configuration` - `saveConfiguration` - `validatorSlashingProtection` - `saveValidatorSlashingProtection` - `publicKeys` * Filesystem database: Creates `genesis.go`. This file defines the following public functions: - `GenesisValidatorsRoot` - `SaveGenesisValidatorsRoot` * Filesystem database: Creates `graffiti.go`. This file defines the following public functions: - `SaveGraffitiOrderedIndex` - `GraffitiOrderedIndex` * Filesystem database: Creates `migration.go`. This file defines the following public functions: - `RunUpMigrations` - `RunDownMigrations` * Filesystem database: Creates proposer_settings.go. This file defines the following public functions: - `ProposerSettings` - `ProposerSettingsExists` - `SaveProposerSettings` * Filesystem database: Creates `attester_protection.go`. This file defines the following public functions: - `EIPImportBlacklistedPublicKeys` - `SaveEIPImportBlacklistedPublicKeys` - `SigningRootAtTargetEpoch` - `LowestSignedTargetEpoch` - `LowestSignedSourceEpoch` - `AttestedPublicKeys` - `CheckSlashableAttestation` - `SaveAttestationForPubKey` - `SaveAttestationsForPubKey` - `AttestationHistoryForPubKey` * Filesystem database: Creates `proposer_protection.go`. This file defines the following public functions: - `HighestSignedProposal` - `LowestSignedProposal` - `ProposalHistoryForPubKey` - `ProposalHistoryForSlot` - `ProposedPublicKeys` * Ensure that the filesystem store implements the `ValidatorDB` interface. * `slashableAttestationCheck`: Check the database type. * `slashableProposalCheck`: Check the database type. * `slashableAttestationCheck`: Allow usage of minimal slashing protection. * `slashableProposalCheck`: Allow usage of minimal slashing protection. * `ImportStandardProtectionJSON`: Check the database type. * `ImportStandardProtectionJSON`: Allow usage of min slashing protection. * Implement `RecursiveDirFind`. * Implement minimal<->complete DB conversion. 3 public functions are implemented: - `IsCompleteDatabaseExisting` - `IsMinimalDatabaseExisting` - `ConvertDatabase` * `setupDB`: Add `isSlashingProtectionMinimal` argument. The feature addition is located in `validator/node/node_test.go`. The rest of this commit consists in minimal slashing protection testing. * `setupWithKey`: Add `isSlashingProtectionMinimal` argument. The feature addition is located in `validator/client/propose_test.go`. The rest of this commit consists in tests wrapping. * `setup`: Add `isSlashingProtectionMinimal` argument. The added feature is located in the `validator/client/propose_test.go` file. The rest of this commit consists in tests wrapping. * `initializeFromCLI` and `initializeForWeb`: Factorize db init. * Add `convert-complete-to-minimal` command. * Creates `--enable-minimal-slashing-protection` flag. * `importSlashingProtectionJSON`: Check database type. * `exportSlashingProtectionJSON`: Check database type. * `TestClearDB`: Test with minimal slashing protection. * KeyManager: Test with minimal slashing protection. * RPC: KeyManager: Test with minimal slashing protection. * `convert-complete-to-minimal`: Change option names. Options were: - `--source` (for source data directory), and - `--target` (for target data directory) However, since this command deals with slashing protection, which has source (epochs) and target (epochs), the initial option names may confuse the user. In this commit: `--source` ==> `--source-data-dir` `--target` ==> `--target-data-dir` * Set `SlashableAttestationCheck` as an iface method. And delete `CheckSlashableAttestation` from iface. * Move helpers functions in a more general directory. No functional change. * Extract common structs out of `kv`. ==> `filesystem` does not depend anymore on `kv`. ==> `iface` does not depend anymore on `kv`. ==> `slashing-protection` does not depend anymore on `kv`. * Move `ValidateMetadata` in `validator/helpers`. * `ValidateMetadata`: Test with mock. This way, we can: - Avoid any circular import for tests. - Implement once for all `iface.ValidatorDB` implementations the `ValidateMetadata`function. - Have tests (and coverage) of `ValidateMetadata`in its own package. The ideal solution would have been to implement `ValidateMetadata` as a method with the `iface.ValidatorDB`receiver. Unfortunately, golang does not allow that. * `iface.ValidatorDB`: Implement ImportStandardProtectionJSON. The whole purpose of this commit is to avoid the `switch validatorDB.(type)` in `ImportStandardProtectionJSON`. * `iface.ValidatorDB`: Implement `SlashableProposalCheck`. * Remove now useless `slashableProposalCheck`. * Delete useless `ImportStandardProtectionJSON`. * `file.Exists`: Detect directories and return an error. Before, `Exists` was only able to detect if a file exists. Now, this function takes an extra `File` or `Directory` argument. It detects either if a file or a directory exists. Before, if an error was returned by `os.Stat`, the the file was considered as non existing. Now, it is treated as a real error. * Replace `os.Stat` by `file.Exists`. * Remove `Is{Complete,Minimal}DatabaseExisting`. * `publicKeys`: Add log if unexpected file found. * Move `{Source,Target}DataDirFlag`in `db.go`. * `failedAttLocalProtectionErr`: `var`==> `const` * `signingRoot`: `32`==> `fieldparams.RootLength`. * `validatorClientData`==> `validator-client-data`. To be consistent with `slashing-protection`. * Add progress bars for `import` and `convert`. * `parseBlocksForUniquePublicKeys`: Move in `db/kv`. * helpers: Remove unused `initializeProgressBar` function.
311 lines
10 KiB
Go
311 lines
10 KiB
Go
package filesystem
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
|
|
"github.com/prysmaticlabs/prysm/v5/config/proposer"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/crypto/bls"
|
|
"github.com/prysmaticlabs/prysm/v5/io/file"
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
|
)
|
|
|
|
func getPubKeys(t *testing.T, count int) [][fieldparams.BLSPubkeyLength]byte {
|
|
pubKeys := make([][fieldparams.BLSPubkeyLength]byte, count)
|
|
|
|
for i := range pubKeys {
|
|
validatorKey, err := bls.RandKey()
|
|
require.NoError(t, err, "RandKey should not return an error")
|
|
|
|
copy(pubKeys[i][:], validatorKey.PublicKey().Marshal())
|
|
}
|
|
|
|
return pubKeys
|
|
}
|
|
|
|
func TestStore_NewStore(t *testing.T) {
|
|
// Create some pubkeys.
|
|
pubkeys := getPubKeys(t, 5)
|
|
|
|
// Just check `NewStore` does not return an error.
|
|
_, err := NewStore(t.TempDir(), &Config{PubKeys: pubkeys})
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
}
|
|
|
|
func TestStore_Close(t *testing.T) {
|
|
// Create a new store.
|
|
s, err := NewStore(t.TempDir(), nil)
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// Close the DB.
|
|
require.NoError(t, s.Close(), "Close should not return an error")
|
|
}
|
|
|
|
func TestStore_DatabasePath(t *testing.T) {
|
|
// Get a database parent path.
|
|
databaseParentPath := t.TempDir()
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databaseParentPath, nil)
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
expected := databaseParentPath
|
|
actual := s.DatabasePath()
|
|
|
|
require.Equal(t, expected, actual)
|
|
}
|
|
|
|
func TestStore_ClearDB(t *testing.T) {
|
|
// Get a database parent path.
|
|
databaseParentPath := t.TempDir()
|
|
|
|
// Compute slashing protection directory and configuration file paths.
|
|
databasePath := path.Join(databaseParentPath, DatabaseDirName)
|
|
|
|
// Create some pubkeys.
|
|
pubkeys := getPubKeys(t, 5)
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databaseParentPath, &Config{PubKeys: pubkeys})
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// Check the presence of the database directory.
|
|
exists, err := file.Exists(databasePath, file.Directory)
|
|
require.NoError(t, err, "file.Exists should not return an error")
|
|
require.Equal(t, true, exists, "file.Exists should return true")
|
|
|
|
// Clear the DB.
|
|
err = s.ClearDB()
|
|
require.NoError(t, err, "ClearDB should not return an error")
|
|
|
|
// Check the absence of the database directory.
|
|
exists, err = file.Exists(databasePath, file.Directory)
|
|
require.NoError(t, err, "file.Exists should not return an error")
|
|
require.Equal(t, false, exists, "file.Exists should return false")
|
|
}
|
|
|
|
func TestStore_Backup(t *testing.T) {
|
|
// Get a database parent path.
|
|
databaseParentPath := t.TempDir()
|
|
originalDatabaseDirPath := path.Join(databaseParentPath, DatabaseDirName)
|
|
|
|
// Get a backups directory path.
|
|
backupsPath := t.TempDir()
|
|
|
|
// Create some pubkeys.
|
|
pubkeys := getPubKeys(t, 5)
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databaseParentPath, &Config{PubKeys: pubkeys})
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// Update the proposer settings.
|
|
err = s.SaveProposerSettings(context.Background(), &proposer.Settings{
|
|
DefaultConfig: &proposer.Option{
|
|
FeeRecipientConfig: &proposer.FeeRecipientConfig{
|
|
FeeRecipient: common.Address{},
|
|
},
|
|
},
|
|
})
|
|
require.NoError(t, err, "SaveProposerSettings should not return an error")
|
|
|
|
// Backup the DB.
|
|
require.NoError(t, s.Backup(context.Background(), backupsPath, true), "Backup should not return an error")
|
|
|
|
// Get the directory path of the backup.
|
|
files, err := os.ReadDir(path.Join(backupsPath, backupsDirectoryName))
|
|
require.NoError(t, err, "os.ReadDir should not return an error")
|
|
require.Equal(t, 1, len(files), "os.ReadDir should return one file")
|
|
backupDirEntry := files[0]
|
|
require.Equal(t, true, backupDirEntry.IsDir(), "os.ReadDir should return a directory")
|
|
backupDirPath := path.Join(backupsPath, backupsDirectoryName, backupDirEntry.Name())
|
|
|
|
// Get the path database directory.
|
|
backupDatabaseDirPath := path.Join(backupDirPath, DatabaseDirName)
|
|
|
|
// Compare the content of the slashing protection directory.
|
|
require.Equal(t, true, file.DirsEqual(originalDatabaseDirPath, backupDatabaseDirPath))
|
|
}
|
|
|
|
func TestStore_UpdatePublickKeysBuckets(t *testing.T) {
|
|
// Get a database path.
|
|
databasePath := t.TempDir()
|
|
|
|
// Create some pubkeys.
|
|
pubkeys := getPubKeys(t, 5)
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databasePath, &Config{PubKeys: pubkeys})
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// Update the public keys.
|
|
err = s.UpdatePublicKeysBuckets(pubkeys)
|
|
require.NoError(t, err, "UpdatePublicKeysBuckets should not return an error")
|
|
|
|
// Check if the public keys files have been created.
|
|
for i := range pubkeys {
|
|
pubkeyHex := hexutil.Encode(pubkeys[i][:])
|
|
pubkeyFile := path.Join(databasePath, DatabaseDirName, slashingProtectionDirName, fmt.Sprintf("%s.yaml", pubkeyHex))
|
|
|
|
exists, err := file.Exists(pubkeyFile, file.Regular)
|
|
require.NoError(t, err, "file.Exists should not return an error")
|
|
require.Equal(t, true, exists, "file.Exists should return true")
|
|
}
|
|
}
|
|
|
|
func TestStore_slashingProtectionDirPath(t *testing.T) {
|
|
// Get a database path.
|
|
databasePath := t.TempDir()
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databasePath, nil)
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// Check the slashing protection directory path.
|
|
expected := path.Join(databasePath, DatabaseDirName, slashingProtectionDirName)
|
|
actual := s.slashingProtectionDirPath()
|
|
require.Equal(t, expected, actual)
|
|
}
|
|
|
|
func TestStore_pubkeySlashingProtectionFilePath(t *testing.T) {
|
|
// Get a database path.
|
|
databasePath := t.TempDir()
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databasePath, nil)
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// Create a pubkey.
|
|
pubkey := getPubKeys(t, 1)[0]
|
|
|
|
// Check the pubkey slashing protection file path.
|
|
expected := path.Join(databasePath, DatabaseDirName, slashingProtectionDirName, hexutil.Encode(pubkey[:])+".yaml")
|
|
actual := s.pubkeySlashingProtectionFilePath(pubkey)
|
|
require.Equal(t, path.Join(databasePath, DatabaseDirName, slashingProtectionDirName, hexutil.Encode(pubkey[:])+".yaml"), s.pubkeySlashingProtectionFilePath(pubkey))
|
|
require.Equal(t, expected, actual)
|
|
}
|
|
|
|
func TestStore_configurationFilePath(t *testing.T) {
|
|
// Get a database path.
|
|
databasePath := t.TempDir()
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databasePath, nil)
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// Check the configuration file path.
|
|
expected := path.Join(databasePath, DatabaseDirName, configurationFileName)
|
|
actual := s.configurationFilePath()
|
|
require.Equal(t, expected, actual)
|
|
}
|
|
|
|
func TestStore_configuration_saveConfiguration(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
expectedConfiguration *Configuration
|
|
}{
|
|
{
|
|
name: "nil configuration",
|
|
expectedConfiguration: nil,
|
|
},
|
|
{
|
|
name: "some configuration",
|
|
expectedConfiguration: &Configuration{},
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Create a database path.
|
|
databasePath := t.TempDir()
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databasePath, nil)
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// Save the configuration.
|
|
err = s.saveConfiguration(tt.expectedConfiguration)
|
|
require.NoError(t, err, "saveConfiguration should not return an error")
|
|
|
|
// Retrieve the configuration.
|
|
actualConfiguration, err := s.configuration()
|
|
require.NoError(t, err, "configuration should not return an error")
|
|
|
|
// Compare the configurations.
|
|
require.DeepEqual(t, tt.expectedConfiguration, actualConfiguration)
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
func TestStore_validatorSlashingProtection_saveValidatorSlashingProtection(t *testing.T) {
|
|
// We get a database path
|
|
databasePath := t.TempDir()
|
|
|
|
// We create a new store
|
|
s, err := NewStore(databasePath, nil)
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// We create a pubkey
|
|
pubkey := getPubKeys(t, 1)[0]
|
|
|
|
// We save an empty validator slashing protection for the pubkey
|
|
err = s.saveValidatorSlashingProtection(pubkey, nil)
|
|
require.NoError(t, err, "saveValidatorSlashingProtection should not return an error")
|
|
|
|
// We check the validator slashing protection for the pubkey
|
|
var expected *ValidatorSlashingProtection
|
|
actual, err := s.validatorSlashingProtection(pubkey)
|
|
require.NoError(t, err, "validatorSlashingProtection should not return an error")
|
|
require.Equal(t, expected, actual)
|
|
|
|
// We update the validator slashing protection for the pubkey
|
|
epoch := uint64(1)
|
|
validatorSlashingProtection := &ValidatorSlashingProtection{LatestSignedBlockSlot: &epoch}
|
|
err = s.saveValidatorSlashingProtection(pubkey, validatorSlashingProtection)
|
|
require.NoError(t, err, "saveValidatorSlashingProtection should not return an error")
|
|
|
|
// We check the validator slashing protection for the pubkey
|
|
expected = &ValidatorSlashingProtection{LatestSignedBlockSlot: &epoch}
|
|
actual, err = s.validatorSlashingProtection(pubkey)
|
|
require.NoError(t, err, "validatorSlashingProtection should not return an error")
|
|
require.DeepEqual(t, expected, actual)
|
|
}
|
|
|
|
func TestStore_publicKeys(t *testing.T) {
|
|
// We get a database path
|
|
databasePath := t.TempDir()
|
|
|
|
// We create some pubkeys
|
|
pubkeys := getPubKeys(t, 5)
|
|
|
|
// We create a new store
|
|
s, err := NewStore(databasePath, &Config{PubKeys: pubkeys})
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// We check the public keys
|
|
expected := pubkeys
|
|
actual, err := s.publicKeys()
|
|
require.NoError(t, err, "publicKeys should not return an error")
|
|
|
|
// We cannot compare the slices directly because the order is not guaranteed,
|
|
// so we compare sets instead.
|
|
|
|
expectedSet := make(map[[fieldparams.BLSPubkeyLength]byte]bool)
|
|
for _, pubkey := range expected {
|
|
expectedSet[pubkey] = true
|
|
}
|
|
|
|
actualSet := make(map[[fieldparams.BLSPubkeyLength]byte]bool)
|
|
for _, pubkey := range actual {
|
|
actualSet[pubkey] = true
|
|
}
|
|
|
|
require.DeepEqual(t, expectedSet, actualSet)
|
|
}
|