Files
prysm/validator/testing/protection_history.go
Preston Van Loon 2fd6bd8150 Add golang.org/x/tools modernize static analyzer and fix violations (#15946)
* 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
2025-11-14 01:27:22 +00:00

118 lines
4.3 KiB
Go

package testing
import (
"fmt"
fieldparams "github.com/OffchainLabs/prysm/v7/config/fieldparams"
"github.com/OffchainLabs/prysm/v7/config/params"
"github.com/OffchainLabs/prysm/v7/consensus-types/primitives"
"github.com/OffchainLabs/prysm/v7/crypto/bls"
"github.com/OffchainLabs/prysm/v7/crypto/rand"
"github.com/OffchainLabs/prysm/v7/encoding/bytesutil"
"github.com/OffchainLabs/prysm/v7/validator/db/common"
"github.com/OffchainLabs/prysm/v7/validator/slashing-protection-history/format"
)
// MockSlashingProtectionJSON creates a mock, full slashing protection JSON struct
// using attesting and proposing histories provided.
func MockSlashingProtectionJSON(
publicKeys [][fieldparams.BLSPubkeyLength]byte,
attestingHistories [][]*common.AttestationRecord,
proposalHistories []common.ProposalHistoryForPubkey,
) (*format.EIPSlashingProtectionFormat, error) {
standardProtectionFormat := &format.EIPSlashingProtectionFormat{}
standardProtectionFormat.Metadata.GenesisValidatorsRoot = fmt.Sprintf("%#x", bytesutil.PadTo([]byte{32}, 32))
standardProtectionFormat.Metadata.InterchangeFormatVersion = format.InterchangeFormatVersion
for i := range publicKeys {
data := &format.ProtectionData{
Pubkey: fmt.Sprintf("%#x", publicKeys[i]),
}
if len(attestingHistories) > 0 {
for _, att := range attestingHistories[i] {
data.SignedAttestations = append(data.SignedAttestations, &format.SignedAttestation{
TargetEpoch: fmt.Sprintf("%d", att.Target),
SourceEpoch: fmt.Sprintf("%d", att.Source),
SigningRoot: fmt.Sprintf("%#x", att.SigningRoot),
})
}
}
if len(proposalHistories) > 0 {
for _, proposal := range proposalHistories[i].Proposals {
block := &format.SignedBlock{
Slot: fmt.Sprintf("%d", proposal.Slot),
SigningRoot: fmt.Sprintf("%#x", proposal.SigningRoot),
}
data.SignedBlocks = append(data.SignedBlocks, block)
}
}
standardProtectionFormat.Data = append(standardProtectionFormat.Data, data)
}
return standardProtectionFormat, nil
}
// MockAttestingAndProposalHistories given a number of validators, creates mock attesting
// and proposing histories within WEAK_SUBJECTIVITY_PERIOD bounds.
func MockAttestingAndProposalHistories(pubkeys [][fieldparams.BLSPubkeyLength]byte) ([][]*common.AttestationRecord, []common.ProposalHistoryForPubkey) {
// deduplicate and transform them into our internal format.
numValidators := len(pubkeys)
attData := make([][]*common.AttestationRecord, numValidators)
proposalData := make([]common.ProposalHistoryForPubkey, numValidators)
gen := rand.NewGenerator()
for v := range numValidators {
latestTarget := primitives.Epoch(gen.Intn(int(params.BeaconConfig().WeakSubjectivityPeriod) / 1000))
// If 0, we change the value to 1 as we compute source by doing (target-1)
// to prevent any underflows in this setup helper.
if latestTarget == 0 {
latestTarget = 1
}
historicalAtts := make([]*common.AttestationRecord, 0)
proposals := make([]common.Proposal, 0)
for i := primitives.Epoch(1); i < latestTarget; i++ {
var signingRoot [32]byte
signingRootStr := fmt.Sprintf("%d", i)
copy(signingRoot[:], signingRootStr)
historicalAtts = append(historicalAtts, &common.AttestationRecord{
Source: i - 1,
Target: i,
SigningRoot: signingRoot[:],
PubKey: pubkeys[v],
})
}
for i := primitives.Epoch(1); i <= latestTarget; i++ {
var signingRoot [32]byte
signingRootStr := fmt.Sprintf("%d", i)
copy(signingRoot[:], signingRootStr)
proposals = append(proposals, common.Proposal{
Slot: primitives.Slot(i),
SigningRoot: signingRoot[:],
})
}
proposalData[v] = common.ProposalHistoryForPubkey{Proposals: proposals}
attData[v] = historicalAtts
}
return attData, proposalData
}
// CreateRandomPubKeys --
func CreateRandomPubKeys(numValidators int) ([][fieldparams.BLSPubkeyLength]byte, error) {
pubKeys := make([][fieldparams.BLSPubkeyLength]byte, numValidators)
for i := range numValidators {
randKey, err := bls.RandKey()
if err != nil {
return nil, err
}
copy(pubKeys[i][:], randKey.PublicKey().Marshal())
}
return pubKeys, nil
}
// CreateMockRoots --
func CreateMockRoots(numRoots int) [][32]byte {
roots := make([][32]byte, numRoots)
for i := range numRoots {
var rt [32]byte
copy(rt[:], fmt.Sprintf("%d", i))
}
return roots
}