Replace context.Background with testing.TB.Context where possible (#15416)

* Replace context.Background with testing.TB.Context where possible

* Fix failing tests
This commit is contained in:
Preston Van Loon
2025-06-16 17:09:18 -05:00
committed by GitHub
parent 6a13ba9125
commit 62fec4d1f3
409 changed files with 3175 additions and 3456 deletions

View File

@@ -1,7 +1,6 @@
package db
import (
"context"
"fmt"
"path/filepath"
"testing"
@@ -36,7 +35,7 @@ func getFeeRecipientFromString(t *testing.T, feeRecipientString string) [fieldpa
}
func TestDB_ConvertDatabase(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
pubKeyString1 := "0x80000060606fa05c7339dd7bcd0d3e4d8b573fa30dea2fdb4997031a703e3300326e3c054be682f92d9c367cd647bbea"
pubKeyString2 := "0x81000060606fa05c7339dd7bcd0d3e4d8b573fa30dea2fdb4997031a703e3300326e3c054be682f92d9c367cd647bbea"

View File

@@ -1,7 +1,6 @@
package filesystem
import (
"context"
"sync"
"testing"
@@ -19,7 +18,7 @@ func TestStore_EIPImportBlacklistedPublicKeys(t *testing.T) {
require.NoError(t, err, "could not create store")
var expected = [][fieldparams.BLSPubkeyLength]byte{}
actual, err := store.EIPImportBlacklistedPublicKeys(context.Background())
actual, err := store.EIPImportBlacklistedPublicKeys(t.Context())
require.NoError(t, err, "could not get blacklisted public keys")
require.DeepSSZEqual(t, expected, actual, "blacklisted public keys do not match")
}
@@ -30,7 +29,7 @@ func TestStore_SaveEIPImportBlacklistedPublicKeys(t *testing.T) {
require.NoError(t, err, "could not create store")
// Save blacklisted public keys.
err = store.SaveEIPImportBlacklistedPublicKeys(context.Background(), [][fieldparams.BLSPubkeyLength]byte{})
err = store.SaveEIPImportBlacklistedPublicKeys(t.Context(), [][fieldparams.BLSPubkeyLength]byte{})
require.NoError(t, err, "could not save blacklisted public keys")
}
@@ -46,7 +45,7 @@ func TestStore_LowestSignedTargetEpoch(t *testing.T) {
require.NoError(t, err, "could not create store")
// Get the lowest signed target epoch.
_, exists, err := store.LowestSignedTargetEpoch(context.Background(), [fieldparams.BLSPubkeyLength]byte{})
_, exists, err := store.LowestSignedTargetEpoch(t.Context(), [fieldparams.BLSPubkeyLength]byte{})
require.NoError(t, err, "could not get lowest signed target epoch")
require.Equal(t, false, exists, "lowest signed target epoch should not exist")
@@ -59,12 +58,12 @@ func TestStore_LowestSignedTargetEpoch(t *testing.T) {
}
// Save the attestation.
err = store.SaveAttestationForPubKey(context.Background(), pubkey, [32]byte{}, attestation)
err = store.SaveAttestationForPubKey(t.Context(), pubkey, [32]byte{}, attestation)
require.NoError(t, err, "SaveAttestationForPubKey should not return an error")
// Get the lowest signed target epoch.
expected := primitives.Epoch(savedTargetEpoch)
actual, exists, err := store.LowestSignedTargetEpoch(context.Background(), pubkey)
actual, exists, err := store.LowestSignedTargetEpoch(t.Context(), pubkey)
require.NoError(t, err, "could not get lowest signed target epoch")
require.Equal(t, true, exists, "lowest signed target epoch should not exist")
require.Equal(t, expected, actual, "lowest signed target epoch should match")
@@ -79,7 +78,7 @@ func TestStore_LowestSignedSourceEpoch(t *testing.T) {
require.NoError(t, err, "could not create store")
// Get the lowest signed target epoch.
_, exists, err := store.LowestSignedSourceEpoch(context.Background(), [fieldparams.BLSPubkeyLength]byte{})
_, exists, err := store.LowestSignedSourceEpoch(t.Context(), [fieldparams.BLSPubkeyLength]byte{})
require.NoError(t, err, "could not get lowest signed source epoch")
require.Equal(t, false, exists, "lowest signed source epoch should not exist")
@@ -93,12 +92,12 @@ func TestStore_LowestSignedSourceEpoch(t *testing.T) {
}
// Save the attestation.
err = store.SaveAttestationForPubKey(context.Background(), pubkey, [32]byte{}, attestation)
err = store.SaveAttestationForPubKey(t.Context(), pubkey, [32]byte{}, attestation)
require.NoError(t, err, "SaveAttestationForPubKey should not return an error")
// Get the lowest signed target epoch.
expected := primitives.Epoch(savedSourceEpoch)
actual, exists, err := store.LowestSignedSourceEpoch(context.Background(), pubkey)
actual, exists, err := store.LowestSignedSourceEpoch(t.Context(), pubkey)
require.NoError(t, err, "could not get lowest signed target epoch")
require.Equal(t, true, exists, "lowest signed target epoch should exist")
require.Equal(t, expected, actual, "lowest signed target epoch should match")
@@ -118,7 +117,7 @@ func TestStore_AttestedPublicKeys(t *testing.T) {
// Attest for some pubkeys.
attestedPubkeys := pubkeys[1:3]
for _, pubkey := range attestedPubkeys {
err = s.SaveAttestationForPubKey(context.Background(), pubkey, [32]byte{}, &ethpb.IndexedAttestation{
err = s.SaveAttestationForPubKey(t.Context(), pubkey, [32]byte{}, &ethpb.IndexedAttestation{
Data: &ethpb.AttestationData{
Source: &ethpb.Checkpoint{Epoch: 42},
Target: &ethpb.Checkpoint{Epoch: 43},
@@ -128,7 +127,7 @@ func TestStore_AttestedPublicKeys(t *testing.T) {
}
// Check the public keys.
actual, err := s.AttestedPublicKeys(context.Background())
actual, err := s.AttestedPublicKeys(t.Context())
require.NoError(t, err, "publicKeys should not return an error")
// We cannot compare the slices directly because the order is not guaranteed,
@@ -276,13 +275,13 @@ func TestStore_SaveAttestationForPubKey(t *testing.T) {
if tt.existingAttInDB != nil {
// Simulate an already existing slashing protection.
err = store.SaveAttestationForPubKey(context.Background(), pubkey, [32]byte{}, tt.existingAttInDB)
err = store.SaveAttestationForPubKey(t.Context(), pubkey, [32]byte{}, tt.existingAttInDB)
require.NoError(t, err, "failed to save attestation when simulating an already existing slashing protection")
}
if tt.incomingAtt != nil {
// Attempt to save a new attestation.
err = store.SaveAttestationForPubKey(context.Background(), pubkey, [32]byte{}, tt.incomingAtt)
err = store.SaveAttestationForPubKey(t.Context(), pubkey, [32]byte{}, tt.incomingAtt)
if len(tt.expectedErr) > 0 {
require.ErrorContains(t, tt.expectedErr, err)
} else {
@@ -299,7 +298,7 @@ func pointerFromInt(i uint64) *uint64 {
func TestStore_SaveAttestationsForPubKey2(t *testing.T) {
// Get the context.
ctx := context.Background()
ctx := t.Context()
// Create a public key.
pubkey := getPubKeys(t, 1)[0]
@@ -430,7 +429,7 @@ func TestStore_AttestationHistoryForPubKey(t *testing.T) {
require.NoError(t, err, "NewStore should not return an error")
// Get the attestation history.
actual, err := store.AttestationHistoryForPubKey(context.Background(), pubkey)
actual, err := store.AttestationHistoryForPubKey(t.Context(), pubkey)
require.NoError(t, err, "AttestationHistoryForPubKey should not return an error")
require.DeepEqual(t, []*common.AttestationRecord{}, actual)
@@ -444,7 +443,7 @@ func TestStore_AttestationHistoryForPubKey(t *testing.T) {
}
// Save the attestation.
err = store.SaveAttestationForPubKey(context.Background(), pubkey, [32]byte{}, attestation)
err = store.SaveAttestationForPubKey(t.Context(), pubkey, [32]byte{}, attestation)
require.NoError(t, err, "SaveAttestationForPubKey should not return an error")
// Get the attestation history.
@@ -456,14 +455,14 @@ func TestStore_AttestationHistoryForPubKey(t *testing.T) {
},
}
actual, err = store.AttestationHistoryForPubKey(context.Background(), pubkey)
actual, err = store.AttestationHistoryForPubKey(t.Context(), pubkey)
require.NoError(t, err, "AttestationHistoryForPubKey should not return an error")
require.DeepEqual(t, expected, actual)
}
func BenchmarkStore_SaveAttestationForPubKey(b *testing.B) {
var wg sync.WaitGroup
ctx := context.Background()
ctx := b.Context()
// Create pubkeys
pubkeys := make([][fieldparams.BLSPubkeyLength]byte, 2000)

View File

@@ -1,7 +1,6 @@
package filesystem
import (
"context"
"fmt"
"os"
"path"
@@ -106,7 +105,7 @@ func TestStore_Backup(t *testing.T) {
require.NoError(t, err, "NewStore should not return an error")
// Update the proposer settings.
err = s.SaveProposerSettings(context.Background(), &proposer.Settings{
err = s.SaveProposerSettings(t.Context(), &proposer.Settings{
DefaultConfig: &proposer.Option{
FeeRecipientConfig: &proposer.FeeRecipientConfig{
FeeRecipient: common.Address{},
@@ -116,7 +115,7 @@ func TestStore_Backup(t *testing.T) {
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")
require.NoError(t, s.Backup(t.Context(), backupsPath, true), "Backup should not return an error")
// Get the directory path of the backup.
files, err := os.ReadDir(path.Join(backupsPath, backupsDirectoryName))

View File

@@ -1,14 +1,13 @@
package filesystem
import (
"context"
"testing"
"github.com/OffchainLabs/prysm/v6/testing/require"
)
func TestStore_GenesisValidatorsRoot(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
genesisValidatorRootString := "0x0100"
genesisValidatorRootBytes := []byte{1, 0}
@@ -52,7 +51,7 @@ func TestStore_GenesisValidatorsRoot(t *testing.T) {
}
func TestStore_SaveGenesisValidatorsRoot(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
genesisValidatorRootString := "0x0100"
for _, tt := range []struct {

View File

@@ -1,7 +1,6 @@
package filesystem
import (
"context"
"testing"
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
@@ -29,7 +28,7 @@ func TestStore_SaveGraffitiOrderedIndex(t *testing.T) {
require.NoError(t, err)
// Save graffiti ordered index.
err = store.SaveGraffitiOrderedIndex(context.Background(), graffitiOrderedIndex)
err = store.SaveGraffitiOrderedIndex(t.Context(), graffitiOrderedIndex)
require.NoError(t, err)
})
}
@@ -87,7 +86,7 @@ func TestStore_GraffitiOrderedIndex(t *testing.T) {
require.NoError(t, err)
// Get graffiti ordered index.
actualGraffitiOrderedIndex, err := store.GraffitiOrderedIndex(context.Background(), tt.fileHash)
actualGraffitiOrderedIndex, err := store.GraffitiOrderedIndex(t.Context(), tt.fileHash)
require.NoError(t, err)
require.Equal(t, tt.expectedGraffitiOrderedIndex, actualGraffitiOrderedIndex)
})

View File

@@ -2,7 +2,6 @@ package filesystem
import (
"bytes"
"context"
"encoding/json"
"testing"
@@ -24,7 +23,7 @@ func TestStore_ImportInterchangeData_BadJSON(t *testing.T) {
require.NoError(t, err, "NewStore should not return an error")
buf := bytes.NewBuffer([]byte("helloworld"))
err = s.ImportStandardProtectionJSON(context.Background(), buf)
err = s.ImportStandardProtectionJSON(t.Context(), buf)
require.ErrorContains(t, "could not unmarshal slashing protection JSON file", err)
}
@@ -41,12 +40,12 @@ func TestStore_ImportInterchangeData_NilData_FailsSilently(t *testing.T) {
require.NoError(t, err)
buf := bytes.NewBuffer(encoded)
err = s.ImportStandardProtectionJSON(context.Background(), buf)
err = s.ImportStandardProtectionJSON(t.Context(), buf)
require.NoError(t, err)
}
func TestStore_ImportInterchangeData_BadFormat_PreventsDBWrites(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
numValidators := 10
publicKeys, err := valtest.CreateRandomPubKeys(numValidators)
require.NoError(t, err)
@@ -94,7 +93,7 @@ func TestStore_ImportInterchangeData_BadFormat_PreventsDBWrites(t *testing.T) {
}
func TestStore_ImportInterchangeData_OK(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
numValidators := 10
publicKeys, err := valtest.CreateRandomPubKeys(numValidators)
require.NoError(t, err)

View File

@@ -1,7 +1,6 @@
package filesystem
import (
"context"
"testing"
"github.com/OffchainLabs/prysm/v6/testing/require"
@@ -13,7 +12,7 @@ func TestStore_RunUpMigrations(t *testing.T) {
require.NoError(t, err, "NewStore should not return an error")
// Just check `RunUpMigrations` does not return an error.
err = store.RunUpMigrations(context.Background())
err = store.RunUpMigrations(t.Context())
require.NoError(t, err, "RunUpMigrations should not return an error")
}
@@ -23,6 +22,6 @@ func TestStore_RunDownMigrations(t *testing.T) {
require.NoError(t, err, "NewStore should not return an error")
// Just check `RunDownMigrations` does not return an error.
err = store.RunDownMigrations(context.Background())
err = store.RunDownMigrations(t.Context())
require.NoError(t, err, "RunUpMigrations should not return an error")
}

View File

@@ -1,7 +1,6 @@
package filesystem
import (
"context"
"testing"
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
@@ -16,7 +15,7 @@ import (
func TestStore_ProposalHistoryForPubKey(t *testing.T) {
var slot uint64 = 42
ctx := context.Background()
ctx := t.Context()
for _, tt := range []struct {
name string
@@ -73,7 +72,7 @@ func TestStore_SaveProposalHistoryForSlot(t *testing.T) {
slot43 uint64 = 43
)
ctx := context.Background()
ctx := t.Context()
for _, tt := range []struct {
name string
@@ -164,7 +163,7 @@ func TestStore_ProposedPublicKeys(t *testing.T) {
// We check the public keys
expected := pubkeys
actual, err := s.ProposedPublicKeys(context.Background())
actual, err := s.ProposedPublicKeys(t.Context())
require.NoError(t, err, "publicKeys should not return an error")
// We cannot compare the slices directly because the order is not guaranteed,
@@ -183,7 +182,7 @@ func TestStore_ProposedPublicKeys(t *testing.T) {
}
func Test_slashableProposalCheck_PreventsLowerThanMinProposal(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
// We get a database path
databasePath := t.TempDir()
@@ -214,7 +213,7 @@ func Test_slashableProposalCheck_PreventsLowerThanMinProposal(t *testing.T) {
}
wsb, err := blocks.NewSignedBeaconBlock(blk)
require.NoError(t, err)
err = s.SlashableProposalCheck(context.Background(), pubkey, wsb, [32]byte{4}, false, nil)
err = s.SlashableProposalCheck(t.Context(), pubkey, wsb, [32]byte{4}, false, nil)
require.ErrorContains(t, common.FailedBlockSignLocalErr, err)
// We expect the same block with a slot equal to the lowest
@@ -229,14 +228,14 @@ func Test_slashableProposalCheck_PreventsLowerThanMinProposal(t *testing.T) {
}
wsb, err = blocks.NewSignedBeaconBlock(blk)
require.NoError(t, err)
err = s.SlashableProposalCheck(context.Background(), pubkey, wsb, [32]byte{1}, false, nil)
err = s.SlashableProposalCheck(t.Context(), pubkey, wsb, [32]byte{1}, false, nil)
require.ErrorContains(t, common.FailedBlockSignLocalErr, err)
// We expect the same block with a slot equal to the lowest
// signed slot to fail validation if signing roots are different.
wsb, err = blocks.NewSignedBeaconBlock(blk)
require.NoError(t, err)
err = s.SlashableProposalCheck(context.Background(), pubkey, wsb, [32]byte{4}, false, nil)
err = s.SlashableProposalCheck(t.Context(), pubkey, wsb, [32]byte{4}, false, nil)
require.ErrorContains(t, common.FailedBlockSignLocalErr, err)
// We expect the same block with a slot > than the lowest
@@ -252,12 +251,12 @@ func Test_slashableProposalCheck_PreventsLowerThanMinProposal(t *testing.T) {
wsb, err = blocks.NewSignedBeaconBlock(blk)
require.NoError(t, err)
err = s.SlashableProposalCheck(context.Background(), pubkey, wsb, [32]byte{3}, false, nil)
err = s.SlashableProposalCheck(t.Context(), pubkey, wsb, [32]byte{3}, false, nil)
require.NoError(t, err)
}
func Test_slashableProposalCheck(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
// We get a database path
databasePath := t.TempDir()
@@ -291,11 +290,11 @@ func Test_slashableProposalCheck(t *testing.T) {
require.NoError(t, err)
// We expect the same block sent out should be slasahble.
err = s.SlashableProposalCheck(context.Background(), pubkey, sBlock, dummySigningRoot, false, nil)
err = s.SlashableProposalCheck(t.Context(), pubkey, sBlock, dummySigningRoot, false, nil)
require.ErrorContains(t, common.FailedBlockSignLocalErr, err)
// We expect the same block sent out with a different signing root should be slashable.
err = s.SlashableProposalCheck(context.Background(), pubkey, sBlock, [32]byte{2}, false, nil)
err = s.SlashableProposalCheck(t.Context(), pubkey, sBlock, [32]byte{2}, false, nil)
require.ErrorContains(t, common.FailedBlockSignLocalErr, err)
// We save a proposal at slot 11 with a nil signing root.
@@ -307,7 +306,7 @@ func Test_slashableProposalCheck(t *testing.T) {
// We expect the same block sent out should return slashable error even
// if we had a nil signing root stored in the database.
err = s.SlashableProposalCheck(context.Background(), pubkey, sBlock, [32]byte{2}, false, nil)
err = s.SlashableProposalCheck(t.Context(), pubkey, sBlock, [32]byte{2}, false, nil)
require.ErrorContains(t, common.FailedBlockSignLocalErr, err)
// A block with a different slot for which we do not have a proposing history
@@ -315,7 +314,7 @@ func Test_slashableProposalCheck(t *testing.T) {
blk.Block.Slot = 9
sBlock, err = blocks.NewSignedBeaconBlock(blk)
require.NoError(t, err)
err = s.SlashableProposalCheck(context.Background(), pubkey, sBlock, [32]byte{3}, false, nil)
err = s.SlashableProposalCheck(t.Context(), pubkey, sBlock, [32]byte{3}, false, nil)
require.ErrorContains(t, common.FailedBlockSignLocalErr, err)
}
@@ -336,6 +335,6 @@ func Test_slashableProposalCheck_RemoteProtection(t *testing.T) {
sBlock, err := blocks.NewSignedBeaconBlock(blk)
require.NoError(t, err)
err = s.SlashableProposalCheck(context.Background(), pubkey, sBlock, [32]byte{2}, false, nil)
err = s.SlashableProposalCheck(t.Context(), pubkey, sBlock, [32]byte{2}, false, nil)
require.NoError(t, err, "Expected allowed block not to throw error")
}

View File

@@ -1,7 +1,6 @@
package filesystem
import (
"context"
"testing"
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
@@ -28,7 +27,7 @@ func getFeeRecipientFromString(t *testing.T, feeRecipientString string) [fieldpa
}
func TestStore_ProposerSettings(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
pubkeyString := "0xb3533c600c6c22aa5177f295667deacffde243980d3c04da4057ab0941dcca1dff83ae8e6534bedd2d23d83446e604d6"
customFeeRecipientString := "0xd4E96eF8eee8678dBFf4d535E033Ed1a4F7605b7"
@@ -110,7 +109,7 @@ func TestStore_ProposerSettings(t *testing.T) {
}
func TestStore_ProposerSettingsExists(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
for _, tt := range []struct {
name string
@@ -151,7 +150,7 @@ func TestStore_ProposerSettingsExists(t *testing.T) {
}
func TestStore_SaveProposerSettings(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
preExistingFeeRecipientString := "0xD871172AE08B5FC37B3AC3D445225928DE883876"
incomingFeeRecipientString := "0xC771172AE08B5FC37B3AC3D445225928DE883876"

View File

@@ -45,7 +45,7 @@ func TestPendingAttestationRecords_Len(t *testing.T) {
}
func TestStore_CheckSlashableAttestation_DoubleVote(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
numValidators := 1
pubKeys := make([][fieldparams.BLSPubkeyLength]byte, numValidators)
validatorDB := setupDB(t, pubKeys)
@@ -116,7 +116,7 @@ func TestStore_CheckSlashableAttestation_DoubleVote(t *testing.T) {
}
func TestStore_CheckSlashableAttestation_SurroundVote_MultipleTargetsPerSource(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
numValidators := 1
pubKeys := make([][fieldparams.BLSPubkeyLength]byte, numValidators)
validatorDB := setupDB(t, pubKeys)
@@ -141,7 +141,7 @@ func TestStore_CheckSlashableAttestation_SurroundVote_MultipleTargetsPerSource(t
}
func TestStore_CheckSlashableAttestation_SurroundVote_54kEpochs(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
numValidators := 1
numEpochs := primitives.Epoch(54000)
pubKeys := make([][fieldparams.BLSPubkeyLength]byte, numValidators)
@@ -214,7 +214,7 @@ func TestStore_CheckSlashableAttestation_SurroundVote_54kEpochs(t *testing.T) {
}
func TestLowestSignedSourceEpoch_SaveRetrieve(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
validatorDB, err := NewKVStore(ctx, t.TempDir(), &Config{})
require.NoError(t, err, "Failed to instantiate DB")
t.Cleanup(func() {
@@ -273,7 +273,7 @@ func TestLowestSignedSourceEpoch_SaveRetrieve(t *testing.T) {
}
func TestLowestSignedTargetEpoch_SaveRetrieveReplace(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
validatorDB, err := NewKVStore(ctx, t.TempDir(), &Config{})
require.NoError(t, err, "Failed to instantiate DB")
t.Cleanup(func() {
@@ -332,7 +332,7 @@ func TestLowestSignedTargetEpoch_SaveRetrieveReplace(t *testing.T) {
}
func TestStore_SaveAttestationsForPubKey(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
numValidators := 1
pubKeys := make([][fieldparams.BLSPubkeyLength]byte, numValidators)
validatorDB := setupDB(t, pubKeys)
@@ -373,7 +373,7 @@ func TestStore_SaveAttestationsForPubKey(t *testing.T) {
func TestSaveAttestationForPubKey_BatchWrites_FullCapacity(t *testing.T) {
hook := logTest.NewGlobal()
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
numValidators := attestationBatchCapacity
pubKeys := make([][fieldparams.BLSPubkeyLength]byte, numValidators)
@@ -426,7 +426,7 @@ func TestSaveAttestationForPubKey_BatchWrites_FullCapacity(t *testing.T) {
func TestSaveAttestationForPubKey_BatchWrites_LowCapacity_TimerReached(t *testing.T) {
hook := logTest.NewGlobal()
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
// Number of validators equal to half the total capacity
// of batch attestation processing. This will allow us to
@@ -501,7 +501,7 @@ func benchCheckSurroundVote(
numEpochs primitives.Epoch,
shouldSurround bool,
) {
ctx := context.Background()
ctx := b.Context()
validatorDB, err := NewKVStore(ctx, filepath.Join(b.TempDir(), "benchsurroundvote"), &Config{
PubKeys: pubKeys,
})
@@ -562,13 +562,13 @@ func TestStore_flushAttestationRecords_InProgress(t *testing.T) {
s.batchedAttestationsFlushInProgress.Set()
hook := logTest.NewGlobal()
s.flushAttestationRecords(context.Background(), nil)
s.flushAttestationRecords(t.Context(), nil)
assert.LogsContain(t, hook, "Attempted to flush attestation records when already in progress")
}
func BenchmarkStore_SaveAttestationForPubKey(b *testing.B) {
var wg sync.WaitGroup
ctx := context.Background()
ctx := b.Context()
// Create pubkeys
pubkeys := make([][fieldparams.BLSPubkeyLength]byte, 10)

View File

@@ -1,7 +1,6 @@
package kv
import (
"context"
"os"
"path/filepath"
"testing"
@@ -14,7 +13,7 @@ import (
func TestStore_Backup(t *testing.T) {
db := setupDB(t, nil)
ctx := context.Background()
ctx := t.Context()
root := [32]byte{1}
require.NoError(t, db.SaveGenesisValidatorsRoot(ctx, root[:]))
require.NoError(t, db.Backup(ctx, "", true))
@@ -44,7 +43,7 @@ func TestStore_Backup(t *testing.T) {
func TestStore_NestedBackup(t *testing.T) {
keys := [][fieldparams.BLSPubkeyLength]byte{{'A'}, {'B'}}
db := setupDB(t, keys)
ctx := context.Background()
ctx := t.Context()
root := [32]byte{1}
idxAtt := &ethpb.IndexedAttestation{
AttestingIndices: nil,
@@ -64,8 +63,8 @@ func TestStore_NestedBackup(t *testing.T) {
Signature: make([]byte, 96),
}
require.NoError(t, db.SaveGenesisValidatorsRoot(ctx, root[:]))
require.NoError(t, db.SaveAttestationForPubKey(context.Background(), keys[0], [32]byte{'C'}, idxAtt))
require.NoError(t, db.SaveAttestationForPubKey(context.Background(), keys[1], [32]byte{'C'}, idxAtt))
require.NoError(t, db.SaveAttestationForPubKey(t.Context(), keys[0], [32]byte{'C'}, idxAtt))
require.NoError(t, db.SaveAttestationForPubKey(t.Context(), keys[1], [32]byte{'C'}, idxAtt))
require.NoError(t, db.Backup(ctx, "", true))
backupsPath := filepath.Join(db.databasePath, backupsDirectoryName)
@@ -91,7 +90,7 @@ func TestStore_NestedBackup(t *testing.T) {
signingRoot32 := [32]byte{'C'}
hist, err := backedDB.AttestationHistoryForPubKey(context.Background(), keys[0])
hist, err := backedDB.AttestationHistoryForPubKey(t.Context(), keys[0])
require.NoError(t, err)
require.DeepEqual(t, &common.AttestationRecord{
PubKey: keys[0],
@@ -100,7 +99,7 @@ func TestStore_NestedBackup(t *testing.T) {
SigningRoot: signingRoot32[:],
}, hist[0])
hist, err = backedDB.AttestationHistoryForPubKey(context.Background(), keys[1])
hist, err = backedDB.AttestationHistoryForPubKey(t.Context(), keys[1])
require.NoError(t, err)
require.DeepEqual(t, &common.AttestationRecord{
PubKey: keys[1],
@@ -109,12 +108,12 @@ func TestStore_NestedBackup(t *testing.T) {
SigningRoot: signingRoot32[:],
}, hist[0])
ep, exists, err := backedDB.LowestSignedSourceEpoch(context.Background(), keys[0])
ep, exists, err := backedDB.LowestSignedSourceEpoch(t.Context(), keys[0])
require.NoError(t, err)
require.Equal(t, true, exists)
require.Equal(t, 10, int(ep))
ep, exists, err = backedDB.LowestSignedSourceEpoch(context.Background(), keys[1])
ep, exists, err = backedDB.LowestSignedSourceEpoch(t.Context(), keys[1])
require.NoError(t, err)
require.Equal(t, true, exists)
require.Equal(t, 10, int(ep))

View File

@@ -1,7 +1,6 @@
package kv
import (
"context"
"fmt"
"testing"
@@ -11,7 +10,7 @@ import (
)
func TestStore_EIPBlacklistedPublicKeys(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
numValidators := 100
publicKeys := make([][fieldparams.BLSPubkeyLength]byte, numValidators)
for i := 0; i < numValidators; i++ {

View File

@@ -1,7 +1,6 @@
package kv
import (
"context"
"testing"
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
@@ -10,7 +9,7 @@ import (
)
func TestStore_GenesisValidatorsRoot_ReadAndWrite(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
tests := []struct {
name string

View File

@@ -1,7 +1,6 @@
package kv
import (
"context"
"testing"
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
@@ -10,7 +9,7 @@ import (
)
func TestStore_GraffitiOrderedIndex_ReadAndWrite(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
db := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{})
tests := []struct {
name string
@@ -60,7 +59,7 @@ func TestStore_GraffitiOrderedIndex_ReadAndWrite(t *testing.T) {
}
func TestStore_GraffitiFileHash(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
// Creates database
db := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{})

View File

@@ -2,7 +2,6 @@ package kv
import (
"bytes"
"context"
"encoding/json"
"fmt"
"reflect"
@@ -20,7 +19,7 @@ import (
)
func TestStore_ImportInterchangeData_BadJSON(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
validatorDB := setupDB(t, nil)
buf := bytes.NewBuffer([]byte("helloworld"))
@@ -30,7 +29,7 @@ func TestStore_ImportInterchangeData_BadJSON(t *testing.T) {
func TestStore_ImportInterchangeData_NilData_FailsSilently(t *testing.T) {
hook := logTest.NewGlobal()
ctx := context.Background()
ctx := t.Context()
validatorDB := setupDB(t, nil)
interchangeJSON := &format.EIPSlashingProtectionFormat{}
@@ -44,7 +43,7 @@ func TestStore_ImportInterchangeData_NilData_FailsSilently(t *testing.T) {
}
func TestStore_ImportInterchangeData_BadFormat_PreventsDBWrites(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
numValidators := 10
publicKeys, err := valtest.CreateRandomPubKeys(numValidators)
require.NoError(t, err)
@@ -104,7 +103,7 @@ func TestStore_ImportInterchangeData_BadFormat_PreventsDBWrites(t *testing.T) {
}
func TestStore_ImportInterchangeData_OK(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
numValidators := 10
publicKeys, err := valtest.CreateRandomPubKeys(numValidators)
require.NoError(t, err)
@@ -775,14 +774,14 @@ func Test_filterSlashablePubKeysFromBlocks(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
historyByPubKey := make(map[[fieldparams.BLSPubkeyLength]byte]common.ProposalHistoryForPubkey)
for pubKey, signedBlocks := range tt.given {
proposalHistory, err := transformSignedBlocks(ctx, signedBlocks)
require.NoError(t, err)
historyByPubKey[pubKey] = *proposalHistory
}
slashablePubKeys := filterSlashablePubKeysFromBlocks(context.Background(), historyByPubKey)
slashablePubKeys := filterSlashablePubKeysFromBlocks(t.Context(), historyByPubKey)
wantedPubKeys := make(map[[fieldparams.BLSPubkeyLength]byte]bool)
for _, pk := range tt.expected {
wantedPubKeys[pk] = true
@@ -798,7 +797,7 @@ func Test_filterSlashablePubKeysFromBlocks(t *testing.T) {
func Test_filterSlashablePubKeysFromAttestations(t *testing.T) {
// filterSlashablePubKeysFromAttestations is used only for complete slashing protection.
ctx := context.Background()
ctx := t.Context()
tests := []struct {
name string
previousAttsByPubKey map[[fieldparams.BLSPubkeyLength]byte][]*format.SignedAttestation

View File

@@ -1,7 +1,6 @@
package kv
import (
"context"
"io"
"os"
"testing"
@@ -20,7 +19,7 @@ func TestMain(m *testing.M) {
// setupDB instantiates and returns a DB instance for the validator client.
func setupDB(t testing.TB, pubkeys [][fieldparams.BLSPubkeyLength]byte) *Store {
db, err := NewKVStore(context.Background(), t.TempDir(), &Config{
db, err := NewKVStore(t.Context(), t.TempDir(), &Config{
PubKeys: pubkeys,
})
require.NoError(t, err, "Failed to instantiate DB")

View File

@@ -1,7 +1,6 @@
package kv
import (
"context"
"fmt"
"testing"
@@ -96,7 +95,7 @@ func Test_migrateOptimalAttesterProtectionUp(t *testing.T) {
{
name: "partial data saved for both types still completes the migration successfully",
setup: func(t *testing.T, validatorDB *Store) {
ctx := context.Background()
ctx := t.Context()
pubKey := [fieldparams.BLSPubkeyLength]byte{1}
history := newDeprecatedAttestingHistory(0)
// Attest all epochs from genesis to 50.
@@ -184,7 +183,7 @@ func Test_migrateOptimalAttesterProtectionUp(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
validatorDB := setupDB(t, nil)
tt.setup(t, validatorDB)
require.NoError(t, validatorDB.migrateOptimalAttesterProtectionUp(context.Background()))
require.NoError(t, validatorDB.migrateOptimalAttesterProtectionUp(t.Context()))
tt.eval(t, validatorDB)
})
}
@@ -293,7 +292,7 @@ func Test_migrateOptimalAttesterProtectionDown(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
validatorDB := setupDB(t, nil)
tt.setup(t, validatorDB)
require.NoError(t, validatorDB.migrateOptimalAttesterProtectionDown(context.Background()))
require.NoError(t, validatorDB.migrateOptimalAttesterProtectionDown(t.Context()))
tt.eval(t, validatorDB)
})
}

View File

@@ -1,7 +1,6 @@
package kv
import (
"context"
"errors"
"fmt"
"reflect"
@@ -109,7 +108,7 @@ func TestStore_migrateSourceTargetEpochsBucketUp(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
validatorDB := setupDB(t, pubKeys)
tt.setup(t, validatorDB)
require.NoError(t, validatorDB.migrateSourceTargetEpochsBucketUp(context.Background()))
require.NoError(t, validatorDB.migrateSourceTargetEpochsBucketUp(t.Context()))
tt.eval(t, validatorDB)
})
}
@@ -204,7 +203,7 @@ func TestStore_migrateSourceTargetEpochsBucketDown(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
validatorDB := setupDB(t, nil)
tt.setup(t, validatorDB)
require.NoError(t, validatorDB.migrateSourceTargetEpochsBucketDown(context.Background()))
require.NoError(t, validatorDB.migrateSourceTargetEpochsBucketDown(t.Context()))
tt.eval(t, validatorDB)
})
}

View File

@@ -1,7 +1,6 @@
package kv
import (
"context"
"testing"
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
@@ -21,7 +20,7 @@ func TestNewProposalHistoryForSlot_ReturnsNilIfNoHistory(t *testing.T) {
valPubkey := [fieldparams.BLSPubkeyLength]byte{1, 2, 3}
db := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{})
_, proposalExists, signingRootExists, err := db.ProposalHistoryForSlot(context.Background(), valPubkey, 0)
_, proposalExists, signingRootExists, err := db.ProposalHistoryForSlot(t.Context(), valPubkey, 0)
require.NoError(t, err)
assert.Equal(t, false, proposalExists)
assert.Equal(t, false, signingRootExists)
@@ -32,7 +31,7 @@ func TestProposalHistoryForSlot_InitializesNewPubKeys(t *testing.T) {
db := setupDB(t, pubkeys)
for _, pub := range pubkeys {
_, proposalExists, signingRootExists, err := db.ProposalHistoryForSlot(context.Background(), pub, 0)
_, proposalExists, signingRootExists, err := db.ProposalHistoryForSlot(t.Context(), pub, 0)
require.NoError(t, err)
assert.Equal(t, false, proposalExists)
assert.Equal(t, false, signingRootExists)
@@ -45,10 +44,10 @@ func TestNewProposalHistoryForSlot_SigningRootNil(t *testing.T) {
db := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{})
err := db.SaveProposalHistoryForSlot(context.Background(), pubkey, slot, nil)
err := db.SaveProposalHistoryForSlot(t.Context(), pubkey, slot, nil)
require.NoError(t, err, "Saving proposal history failed: %v")
_, proposalExists, signingRootExists, err := db.ProposalHistoryForSlot(context.Background(), pubkey, slot)
_, proposalExists, signingRootExists, err := db.ProposalHistoryForSlot(t.Context(), pubkey, slot)
require.NoError(t, err)
assert.Equal(t, true, proposalExists)
assert.Equal(t, false, signingRootExists)
@@ -60,9 +59,9 @@ func TestSaveProposalHistoryForSlot_OK(t *testing.T) {
slot := primitives.Slot(2)
err := db.SaveProposalHistoryForSlot(context.Background(), pubkey, slot, []byte{1})
err := db.SaveProposalHistoryForSlot(t.Context(), pubkey, slot, []byte{1})
require.NoError(t, err, "Saving proposal history failed: %v")
signingRoot, proposalExists, signingRootExists, err := db.ProposalHistoryForSlot(context.Background(), pubkey, slot)
signingRoot, proposalExists, signingRootExists, err := db.ProposalHistoryForSlot(t.Context(), pubkey, slot)
require.NoError(t, err, "Failed to get proposal history")
assert.Equal(t, true, proposalExists)
assert.Equal(t, true, signingRootExists)
@@ -75,7 +74,7 @@ func TestNewProposalHistoryForPubKey_ReturnsEmptyIfNoHistory(t *testing.T) {
valPubkey := [fieldparams.BLSPubkeyLength]byte{1, 2, 3}
db := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{})
proposalHistory, err := db.ProposalHistoryForPubKey(context.Background(), valPubkey)
proposalHistory, err := db.ProposalHistoryForPubKey(t.Context(), valPubkey)
require.NoError(t, err)
assert.DeepEqual(t, make([]*common.Proposal, 0), proposalHistory)
}
@@ -87,9 +86,9 @@ func TestSaveProposalHistoryForPubKey_OK(t *testing.T) {
slot := primitives.Slot(2)
root := [32]byte{1}
err := db.SaveProposalHistoryForSlot(context.Background(), pubkey, slot, root[:])
err := db.SaveProposalHistoryForSlot(t.Context(), pubkey, slot, root[:])
require.NoError(t, err, "Saving proposal history failed: %v")
proposalHistory, err := db.ProposalHistoryForPubKey(context.Background(), pubkey)
proposalHistory, err := db.ProposalHistoryForPubKey(t.Context(), pubkey)
require.NoError(t, err, "Failed to get proposal history")
require.NotNil(t, proposalHistory)
@@ -120,9 +119,9 @@ func TestSaveProposalHistoryForSlot_Overwrites(t *testing.T) {
for _, tt := range tests {
db := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubkey})
err := db.SaveProposalHistoryForSlot(context.Background(), pubkey, 0, tt.signingRoot)
err := db.SaveProposalHistoryForSlot(t.Context(), pubkey, 0, tt.signingRoot)
require.NoError(t, err, "Saving proposal history failed")
proposalHistory, err := db.ProposalHistoryForPubKey(context.Background(), pubkey)
proposalHistory, err := db.ProposalHistoryForPubKey(t.Context(), pubkey)
require.NoError(t, err, "Failed to get proposal history")
require.NotNil(t, proposalHistory)
@@ -176,12 +175,12 @@ func TestPruneProposalHistoryBySlot_OK(t *testing.T) {
for _, tt := range tests {
db := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubKey})
for _, slot := range tt.slots {
err := db.SaveProposalHistoryForSlot(context.Background(), pubKey, slot, signedRoot)
err := db.SaveProposalHistoryForSlot(t.Context(), pubKey, slot, signedRoot)
require.NoError(t, err, "Saving proposal history failed")
}
signingRootsBySlot := make(map[primitives.Slot][]byte)
proposalHistory, err := db.ProposalHistoryForPubKey(context.Background(), pubKey)
proposalHistory, err := db.ProposalHistoryForPubKey(t.Context(), pubKey)
require.NoError(t, err)
for _, hist := range proposalHistory {
@@ -202,7 +201,7 @@ func TestPruneProposalHistoryBySlot_OK(t *testing.T) {
}
func TestStore_ProposedPublicKeys(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
validatorDB, err := NewKVStore(ctx, t.TempDir(), &Config{})
require.NoError(t, err, "Failed to instantiate DB")
t.Cleanup(func() {
@@ -225,7 +224,7 @@ func TestStore_ProposedPublicKeys(t *testing.T) {
}
func TestStore_LowestSignedProposal(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
pubkey := [fieldparams.BLSPubkeyLength]byte{3}
var dummySigningRoot [32]byte
validatorDB := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubkey})
@@ -266,7 +265,7 @@ func TestStore_LowestSignedProposal(t *testing.T) {
}
func TestStore_HighestSignedProposal(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
pubkey := [fieldparams.BLSPubkeyLength]byte{3}
var dummySigningRoot [32]byte
validatorDB := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubkey})
@@ -307,7 +306,7 @@ func TestStore_HighestSignedProposal(t *testing.T) {
}
func Test_slashableProposalCheck_PreventsLowerThanMinProposal(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
lowestSignedSlot := primitives.Slot(10)
var pubkey [fieldparams.BLSPubkeyLength]byte
@@ -334,7 +333,7 @@ func Test_slashableProposalCheck_PreventsLowerThanMinProposal(t *testing.T) {
}
wsb, err := blocks.NewSignedBeaconBlock(blk)
require.NoError(t, err)
err = db.SlashableProposalCheck(context.Background(), pubkey, wsb, [32]byte{4}, false, nil)
err = db.SlashableProposalCheck(t.Context(), pubkey, wsb, [32]byte{4}, false, nil)
require.ErrorContains(t, "could not sign block with slot < lowest signed", err)
// We expect the same block with a slot equal to the lowest
@@ -377,7 +376,7 @@ func Test_slashableProposalCheck_PreventsLowerThanMinProposal(t *testing.T) {
}
func Test_slashableProposalCheck(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
var pubkey [fieldparams.BLSPubkeyLength]byte
pubkeyBytes, err := hexutil.Decode("0xa057816155ad77931185101128655c0191bd0214c201ca48ed887f6c4c6adf334070efcd75140eada5ac83a92506dd7a")
@@ -450,6 +449,6 @@ func Test_slashableProposalCheck_RemoteProtection(t *testing.T) {
sBlock, err := blocks.NewSignedBeaconBlock(blk)
require.NoError(t, err)
err = db.SlashableProposalCheck(context.Background(), pubkey, sBlock, [32]byte{2}, false, nil)
err = db.SlashableProposalCheck(t.Context(), pubkey, sBlock, [32]byte{2}, false, nil)
require.NoError(t, err, "Expected allowed block not to throw error")
}

View File

@@ -1,7 +1,6 @@
package kv
import (
"context"
"testing"
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
@@ -16,7 +15,7 @@ import (
func TestStore_ProposerSettings_ReadAndWrite(t *testing.T) {
t.Run("save to db in full", func(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
db := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{})
key1, err := hexutil.Decode("0xa057816155ad77931185101128655c0191bd0214c201ca48ed887f6c4c6adf334070efcd75140eada5ac83a92506dd7a")
require.NoError(t, err)
@@ -50,7 +49,7 @@ func TestStore_ProposerSettings_ReadAndWrite(t *testing.T) {
require.DeepEqual(t, settings, dbSettings)
})
t.Run("update default settings then update at specific key", func(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
db := setupDB(t, [][fieldparams.BLSPubkeyLength]byte{})
key1, err := hexutil.Decode("0xa057816155ad77931185101128655c0191bd0214c201ca48ed887f6c4c6adf334070efcd75140eada5ac83a92506dd7a")
require.NoError(t, err)

View File

@@ -1,7 +1,6 @@
package kv
import (
"context"
"fmt"
"testing"
@@ -24,7 +23,7 @@ func TestPruneAttestations_NoPruning(t *testing.T) {
require.NoError(t, err)
// Next, attempt to prune and realize that we still have all epochs intact
err = validatorDB.PruneAttestations(context.Background())
err = validatorDB.PruneAttestations(t.Context())
require.NoError(t, err)
startEpoch := primitives.Epoch(0)
@@ -54,7 +53,7 @@ func TestPruneAttestations_OK(t *testing.T) {
require.NoError(t, setupAttestationsForEveryEpoch(validatorDB, pk, numEpochs))
}
require.NoError(t, validatorDB.PruneAttestations(context.Background()))
require.NoError(t, validatorDB.PruneAttestations(t.Context()))
// Next, verify that we pruned every epoch
// from genesis to SLASHING_PROTECTION_PRUNING_EPOCHS - 1.
@@ -108,7 +107,7 @@ func BenchmarkPruneAttestations(b *testing.B) {
}
b.StartTimer()
require.NoError(b, validatorDB.PruneAttestations(context.Background()))
require.NoError(b, validatorDB.PruneAttestations(b.Context()))
}
}

View File

@@ -1,7 +1,6 @@
package db
import (
"context"
"flag"
"os"
"path"
@@ -18,7 +17,7 @@ import (
func TestRestore(t *testing.T) {
logHook := logTest.NewGlobal()
ctx := context.Background()
ctx := t.Context()
backupDb, err := kv.NewKVStore(ctx, t.TempDir(), &kv.Config{})
defer func() {

View File

@@ -1,7 +1,6 @@
package testing
import (
"context"
"fmt"
"path/filepath"
"testing"
@@ -27,7 +26,7 @@ func TestClearDB(t *testing.T) {
PubKeys: nil,
})
} else {
testDB, err = kv.NewKVStore(context.Background(), t.TempDir(), &kv.Config{
testDB, err = kv.NewKVStore(t.Context(), t.TempDir(), &kv.Config{
PubKeys: nil,
})
}