mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-05-02 03:02:54 -04:00
* Ran gopls modernize to fix everything go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Override rules_go provided dependency for golang.org/x/tools to v0.38.0. To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches. * Fix buildtag violations and ignore buildtag violations in external * Introduce modernize analyzer package. * Add modernize "any" analyzer. * Fix violations of any analyzer * Add modernize "appendclipped" analyzer. * Fix violations of appendclipped * Add modernize "bloop" analyzer. * Add modernize "fmtappendf" analyzer. * Add modernize "forvar" analyzer. * Add modernize "mapsloop" analyzer. * Add modernize "minmax" analyzer. * Fix violations of minmax analyzer * Add modernize "omitzero" analyzer. * Add modernize "rangeint" analyzer. * Fix violations of rangeint. * Add modernize "reflecttypefor" analyzer. * Fix violations of reflecttypefor analyzer. * Add modernize "slicescontains" analyzer. * Add modernize "slicessort" analyzer. * Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686. * Add modernize "stringscutprefix" analyzer. * Add modernize "stringsbuilder" analyzer. * Fix violations of stringsbuilder analyzer. * Add modernize "stringsseq" analyzer. * Add modernize "testingcontext" analyzer. * Add modernize "waitgroup" analyzer. * Changelog fragment * gofmt * gazelle * Add modernize "newexpr" analyzer. * Disable newexpr until go1.26 * Add more details in WORKSPACE on how to update the override * @nalepae feedback on min() * gofmt * Fix violations of forvar
145 lines
5.2 KiB
Go
145 lines
5.2 KiB
Go
package filesystem
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
fieldparams "github.com/OffchainLabs/prysm/v7/config/fieldparams"
|
|
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
|
|
"github.com/OffchainLabs/prysm/v7/testing/assert"
|
|
"github.com/OffchainLabs/prysm/v7/testing/require"
|
|
"github.com/OffchainLabs/prysm/v7/validator/db/common"
|
|
"github.com/OffchainLabs/prysm/v7/validator/slashing-protection-history/format"
|
|
valtest "github.com/OffchainLabs/prysm/v7/validator/testing"
|
|
)
|
|
|
|
func TestStore_ImportInterchangeData_BadJSON(t *testing.T) {
|
|
// Create a database path.
|
|
databaseParentPath := t.TempDir()
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databaseParentPath, nil)
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
buf := bytes.NewBuffer([]byte("helloworld"))
|
|
err = s.ImportStandardProtectionJSON(t.Context(), buf)
|
|
require.ErrorContains(t, "could not unmarshal slashing protection JSON file", err)
|
|
}
|
|
|
|
func TestStore_ImportInterchangeData_NilData_FailsSilently(t *testing.T) {
|
|
// Create a database path.
|
|
databaseParentPath := t.TempDir()
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databaseParentPath, nil)
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
interchangeJSON := &format.EIPSlashingProtectionFormat{}
|
|
encoded, err := json.Marshal(interchangeJSON)
|
|
require.NoError(t, err)
|
|
|
|
buf := bytes.NewBuffer(encoded)
|
|
err = s.ImportStandardProtectionJSON(t.Context(), buf)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestStore_ImportInterchangeData_BadFormat_PreventsDBWrites(t *testing.T) {
|
|
ctx := t.Context()
|
|
numValidators := 10
|
|
publicKeys, err := valtest.CreateRandomPubKeys(numValidators)
|
|
require.NoError(t, err)
|
|
|
|
// Create a database path.
|
|
databaseParentPath := t.TempDir()
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databaseParentPath, &Config{PubKeys: publicKeys})
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// First we setup some mock attesting and proposal histories and create a mock
|
|
// standard slashing protection format JSON struct.
|
|
attestingHistory, proposalHistory := valtest.MockAttestingAndProposalHistories(publicKeys)
|
|
standardProtectionFormat, err := valtest.MockSlashingProtectionJSON(publicKeys, attestingHistory, proposalHistory)
|
|
require.NoError(t, err)
|
|
|
|
// We replace a slot of one of the blocks with junk data.
|
|
standardProtectionFormat.Data[0].SignedBlocks[0].Slot = "BadSlot"
|
|
|
|
// We encode the standard slashing protection struct into a JSON format.
|
|
blob, err := json.Marshal(standardProtectionFormat)
|
|
require.NoError(t, err)
|
|
buf := bytes.NewBuffer(blob)
|
|
|
|
// Next, we attempt to import it into our validator database and check that
|
|
// we obtain an error during the import process.
|
|
err = s.ImportStandardProtectionJSON(ctx, buf)
|
|
assert.NotNil(t, err)
|
|
|
|
// Next, we attempt to retrieve the attesting and proposals histories from our database and
|
|
// verify nothing was saved to the DB. If there is an error in the import process, we need to make
|
|
// sure writing is an atomic operation: either the import succeeds and saves the slashing protection
|
|
// data to our DB, or it does not.
|
|
for i := range publicKeys {
|
|
receivedHistory, err := s.ProposalHistoryForPubKey(ctx, publicKeys[i])
|
|
require.NoError(t, err)
|
|
require.DeepEqual(
|
|
t,
|
|
make([]*common.Proposal, 0),
|
|
receivedHistory,
|
|
"Imported proposal signing root is different than the empty default",
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestStore_ImportInterchangeData_OK(t *testing.T) {
|
|
ctx := t.Context()
|
|
numValidators := 10
|
|
publicKeys, err := valtest.CreateRandomPubKeys(numValidators)
|
|
require.NoError(t, err)
|
|
|
|
// Create a database path.
|
|
databaseParentPath := t.TempDir()
|
|
|
|
// Create a new store.
|
|
s, err := NewStore(databaseParentPath, &Config{PubKeys: publicKeys})
|
|
require.NoError(t, err, "NewStore should not return an error")
|
|
|
|
// First we setup some mock attesting and proposal histories and create a mock
|
|
// standard slashing protection format JSON struct.
|
|
attestingHistory, proposalHistory := valtest.MockAttestingAndProposalHistories(publicKeys)
|
|
standardProtectionFormat, err := valtest.MockSlashingProtectionJSON(publicKeys, attestingHistory, proposalHistory)
|
|
require.NoError(t, err)
|
|
|
|
// We encode the standard slashing protection struct into a JSON format.
|
|
blob, err := json.Marshal(standardProtectionFormat)
|
|
require.NoError(t, err)
|
|
buf := bytes.NewBuffer(blob)
|
|
|
|
// Next, we attempt to import it into our validator database.
|
|
err = s.ImportStandardProtectionJSON(ctx, buf)
|
|
require.NoError(t, err)
|
|
|
|
// Next, we attempt to retrieve the attesting and proposals histories from our database and
|
|
// verify those indeed match the originally generated mock histories.
|
|
for i := range publicKeys {
|
|
for _, att := range attestingHistory[i] {
|
|
indexedAtt := ðpb.IndexedAttestation{
|
|
Data: ðpb.AttestationData{
|
|
Source: ðpb.Checkpoint{
|
|
Epoch: att.Source,
|
|
},
|
|
Target: ðpb.Checkpoint{
|
|
Epoch: att.Target,
|
|
},
|
|
},
|
|
}
|
|
// We expect we have an attesting history for the attestation and when
|
|
// attempting to verify the same att is slashable with a different signing root,
|
|
// we expect to receive a double vote slashing kind.
|
|
err := s.SaveAttestationForPubKey(ctx, publicKeys[i], [fieldparams.RootLength]byte{}, indexedAtt)
|
|
require.ErrorContains(t, "could not sign attestation", err)
|
|
}
|
|
}
|
|
}
|