Skip to content

Commit

Permalink
Fix all other revive linters
Browse files Browse the repository at this point in the history
  • Loading branch information
KaloyanTanev committed Jul 18, 2024
1 parent 4adfb52 commit edf1cfc
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ linters-settings:
disabled: true
- name: comment-spacings
disabled: true # Doesn't support latest go spec comments
- name: range-val-address
disabled: true # It is not an issue for go versions >=1.22
# Some configured revive rules
- name: unhandled-error
arguments:
Expand Down
9 changes: 6 additions & 3 deletions app/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ func WithLogger(ctx context.Context, logger zapLogger) context.Context {
}

func fieldsFromCtx(ctx context.Context) []z.Field {
resp, _ := ctx.Value(ctxKey{}).([]z.Field)
resp, ok := ctx.Value(ctxKey{}).([]z.Field)
if !ok {
return []z.Field{}
}
return resp
}

func metricsTopicFromCtx(ctx context.Context) string {
resp, _ := ctx.Value(topicKey{}).(string)
if resp == "" {
resp, ok := ctx.Value(topicKey{}).(string)
if resp == "" || !ok {
return "unknown"
}

Expand Down
1 change: 1 addition & 0 deletions app/monitoringapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func startReadyChecker(ctx context.Context, tcpNode host.Host, eth2Cl eth2wrap.C
}

syncing, syncDistance, err := beaconNodeSyncing(ctx, eth2Cl)
//nolint:revive // skip max-control-nesting for monitoring
if err != nil {
err = errReadyBeaconNodeDown
readyzGauge.Set(readyzBeaconNodeDown)
Expand Down
4 changes: 2 additions & 2 deletions app/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ func Compare(a, b SemVer) int {
return 0
} else if a.patch < b.patch {
return -1
} else {
return 1
}

return 1
}

var semverRegex = regexp.MustCompile(`^v(\d+)\.(\d+)(?:\.(\d+))?(?:-(.+))?$`)
Expand Down
4 changes: 2 additions & 2 deletions cluster/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,15 @@ func (d Definition) SetDefinitionHashes() (Definition, error) {
return Definition{}, errors.Wrap(err, "config hash")
}

d.ConfigHash = configHash[:]
d.ConfigHash = configHash[:] //nolint: revive // okay to assign to by-value receiver as we return the struct

// Marshal definition hashDefinition
defHash, err := hashDefinition(d, false)
if err != nil {
return Definition{}, errors.Wrap(err, "definition hashDefinition")
}

d.DefinitionHash = defHash[:]
d.DefinitionHash = defHash[:] //nolint: revive // okay to assign to by-value receiver as we return the struct

return d, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cluster/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (l Lock) SetLockHash() (Lock, error) {
return Lock{}, err
}

l.LockHash = lockHash[:]
l.LockHash = lockHash[:] //nolint: revive // okay to assign to by-value receiver as we return the struct

return l, nil
}
Expand Down
16 changes: 8 additions & 8 deletions cluster/ssz.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func getDefinitionHashFunc(version string) (func(Definition, ssz.HashWalker, boo
return hashDefinitionV1x5to7, nil
} else if isAnyVersion(version, v1_8) {
return hashDefinitionV1x8orLater, nil
} else {
return nil, errors.New("unknown version", z.Str("version", version))
}

return nil, errors.New("unknown version", z.Str("version", version))
}

// hashDefinition returns a config or definition hash. The config hash excludes operator ENRs and signatures
Expand Down Expand Up @@ -607,9 +607,9 @@ func getValidatorHashFunc(version string) (func(DistValidator, ssz.HashWalker, s
return hashValidatorV1x5to7, nil
} else if isAnyVersion(version, v1_8) {
return hashValidatorV1x8OrLater, nil
} else {
return nil, errors.New("unknown version", z.Str("version", version))
}

return nil, errors.New("unknown version", z.Str("version", version))
}

func hashValidatorPubsharesField(v DistValidator, hh ssz.HashWalker) error {
Expand Down Expand Up @@ -799,9 +799,9 @@ func getDepositDataHashFunc(version string) (func(DepositData, ssz.HashWalker) e
return hashDepositDataV1x6, nil
} else if isAnyVersion(version, v1_7, v1_8) {
return hashDepositDataV1x7OrLater, nil
} else {
return nil, errors.New("unknown version", z.Str("version", version))
}

return nil, errors.New("unknown version", z.Str("version", version))
}

// getRegistrationHashFunc returns the function to hash a BuilderRegistration based on the provided version.
Expand All @@ -811,9 +811,9 @@ func getRegistrationHashFunc(version string) (func(BuilderRegistration, ssz.Hash
return func(BuilderRegistration, ssz.HashWalker) error { return nil }, nil
} else if isAnyVersion(version, v1_7, v1_8) {
return hashBuilderRegistration, nil
} else {
return nil, errors.New("unknown version", z.Str("version", version))
}

return nil, errors.New("unknown version", z.Str("version", version))
}

// hashDepositDataV1x6 hashes the deposit data for version v1.6.0.
Expand Down
10 changes: 8 additions & 2 deletions core/consensus/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ func verifyMsgSig(msg *pbv1.QBFTMsg, pubkey *k1.PublicKey) (bool, error) {
return false, errors.New("empty signature")
}

clone := proto.Clone(msg).(*pbv1.QBFTMsg)
clone, ok := proto.Clone(msg).(*pbv1.QBFTMsg)
if !ok {
return false, errors.New("type assert qbft msg")
}
clone.Signature = nil
hash, err := hashProto(clone)
if err != nil {
Expand All @@ -169,7 +172,10 @@ func verifyMsgSig(msg *pbv1.QBFTMsg, pubkey *k1.PublicKey) (bool, error) {

// signMsg returns a copy of the proto message with a populated signature signed by the provided private key.
func signMsg(msg *pbv1.QBFTMsg, privkey *k1.PrivateKey) (*pbv1.QBFTMsg, error) {
clone := proto.Clone(msg).(*pbv1.QBFTMsg)
clone, ok := proto.Clone(msg).(*pbv1.QBFTMsg)
if !ok {
return nil, errors.New("type assert qbft msg")
}
clone.Signature = nil

hash, err := hashProto(clone)
Expand Down
10 changes: 8 additions & 2 deletions core/priority/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ func (c *Component) Prioritise(ctx context.Context, duty core.Duty, proposals ..

// signMsg returns a copy of the proto message with a populated signature signed by the provided private key.
func signMsg(msg *pbv1.PriorityMsg, privkey *k1.PrivateKey) (*pbv1.PriorityMsg, error) {
clone := proto.Clone(msg).(*pbv1.PriorityMsg)
clone, ok := proto.Clone(msg).(*pbv1.PriorityMsg)
if !ok {
return nil, errors.New("type assert priority msg")
}
clone.Signature = nil

hash, err := hashProto(clone)
Expand All @@ -173,7 +176,10 @@ func verifyMsgSig(msg *pbv1.PriorityMsg, pubkey *k1.PublicKey) (bool, error) {
return false, errors.New("empty signature")
}

clone := proto.Clone(msg).(*pbv1.PriorityMsg)
clone, ok := proto.Clone(msg).(*pbv1.PriorityMsg)
if !ok {
return false, errors.New("type assert priority msg")
}
clone.Signature = nil
hash, err := hashProto(clone)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions core/validatorapi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
eth2bellatrix "github.com/attestantio/go-eth2-client/api/v1/bellatrix"
eth2capella "github.com/attestantio/go-eth2-client/api/v1/capella"
deneb "github.com/attestantio/go-eth2-client/api/v1/deneb"
eth2deneb "github.com/attestantio/go-eth2-client/api/v1/deneb"
eth2spec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
Expand Down Expand Up @@ -791,7 +791,7 @@ func createProposeBlockResponse(proposal *eth2api.VersionedProposal) (*proposeBl

func submitProposal(p eth2client.ProposalSubmitter) handlerFunc {
return func(ctx context.Context, _ map[string]string, _ url.Values, typ contentType, body []byte) (any, http.Header, error) {
denebBlock := new(deneb.SignedBlockContents)
denebBlock := new(eth2deneb.SignedBlockContents)
err := unmarshal(typ, body, denebBlock)
if err == nil {
block := &eth2api.VersionedSignedProposal{
Expand Down Expand Up @@ -863,7 +863,7 @@ func submitProposal(p eth2client.ProposalSubmitter) handlerFunc {
func submitBlindedBlock(p eth2client.BlindedProposalSubmitter) handlerFunc {
return func(ctx context.Context, _ map[string]string, _ url.Values, typ contentType, body []byte) (any, http.Header, error) {
// The blinded block maybe either bellatrix, capella or deneb.
denebBlock := new(deneb.SignedBlindedBeaconBlock)
denebBlock := new(eth2deneb.SignedBlindedBeaconBlock)
err := unmarshal(typ, body, denebBlock)
if err == nil {
block := &eth2api.VersionedSignedBlindedProposal{
Expand Down
8 changes: 4 additions & 4 deletions dkg/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ func Run(ctx context.Context, conf Config) (err error) {
return errors.Wrap(err, "private key not matching definition file")
}

peerIds, err := def.PeerIDs()
peerIDs, err := def.PeerIDs()
if err != nil {
return errors.Wrap(err, "get peer IDs")
}

ex := newExchanger(tcpNode, nodeIdx.PeerIdx, peerIds, def.NumValidators, []sigType{
ex := newExchanger(tcpNode, nodeIdx.PeerIdx, peerIDs, def.NumValidators, []sigType{
sigLock,
sigDepositData,
sigValidatorRegistration,
Expand All @@ -203,7 +203,7 @@ func Run(ctx context.Context, conf Config) (err error) {
peerMap[p.ID] = nodeIdx
}

caster := bcast.New(tcpNode, peerIds, key)
caster := bcast.New(tcpNode, peerIDs, key)

// register bcast callbacks for frostp2p
tp, err := newFrostP2P(tcpNode, peerMap, caster, def.Threshold, def.NumValidators)
Expand All @@ -219,7 +219,7 @@ func Run(ctx context.Context, conf Config) (err error) {
// Improve UX of "context cancelled" errors when sync fails.
ctx = errors.WithCtxErr(ctx, "p2p connection failed, please retry DKG")

nextStepSync, stopSync, err := startSyncProtocol(ctx, tcpNode, key, def.DefinitionHash, peerIds, cancel, conf.TestConfig)
nextStepSync, stopSync, err := startSyncProtocol(ctx, tcpNode, key, def.DefinitionHash, peerIDs, cancel, conf.TestConfig)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion p2p/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ type Sender struct {
func (s *Sender) addResult(ctx context.Context, peerID peer.ID, err error) {
state := &peerState{}
if val, ok := s.states.Load(peerID); ok {
state = val.(*peerState)
state, ok = val.(*peerState)
if !ok {
log.Warn(ctx, "Type assertion peer state failing", err, z.Str("peer", PeerName(peerID)))
return
}
}

state.buffer.add(err)
Expand Down
3 changes: 2 additions & 1 deletion p2p/sender_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func TestSenderAddResult(t *testing.T) {
t.Helper()
state := &peerState{}
if val, ok := sender.states.Load(peerID); ok {
state = val.(*peerState)
state, ok = val.(*peerState)
require.True(t, ok)
}
require.Equal(t, expect, state.failing.Load())
}
Expand Down
7 changes: 6 additions & 1 deletion testutil/beaconmock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,12 @@ func newHTTPMock(optionalHandlers map[string]http.HandlerFunc, overrides ...stat
return nil, nil, errors.Wrap(err, "new http client")
}

return cl.(HTTPMock), srv, nil
httpMock, ok := cl.(HTTPMock)
if !ok {
return nil, nil, errors.New("type assert http mock")
}

return httpMock, srv, nil
}

// overrideResponse overrides the key in the raw response. If key is empty, it overrides the whole response.
Expand Down

0 comments on commit edf1cfc

Please sign in to comment.