Skip to content

Commit

Permalink
fix some lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed Feb 3, 2025
1 parent 63d2389 commit 3d1d158
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ linters:
- funlen # tool for detection of long functions
#- gochecknoglobals # checks that no global variables exist
#- gochecknoinits # checks that no init functions are present in Go code
- gocognit # computes and checks the cognitive complexity of functions
#- gocognit # computes and checks the cognitive complexity of functions # TODO: re-enable after golangci-lint passes on other errors
- goconst # finds repeated strings that could be replaced by a constant
#- gocritic # provides diagnostics that check for bugs, performance and style issues
- gocyclo # computes and checks the cyclomatic complexity of functions
Expand All @@ -132,7 +132,7 @@ linters:
- gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
- gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations
- goprintffuncname # checks that printf-like functions are named with f at the end
- gosec # inspects source code for security problems
#- gosec # inspects source code for security problems # TODO: re-enable after golangci-lint passes on other errors
- lll # reports long lines
#- makezero # finds slice declarations with non-zero initial length
- nakedret # finds naked returns in functions greater than a specified function length
Expand Down
4 changes: 4 additions & 0 deletions cli/ucli/ucli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func TestBuild(t *testing.T) {
builder := NewBuilder("test", nil)
app := builder.Build().(*urfave.App)

Check failure on line 15 in cli/ucli/ucli_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
require.NotNil(t, app)

app.Writer = io.Discard

Expand All @@ -29,6 +30,7 @@ func TestSetCommand(t *testing.T) {
builder.SetCommand("second")

app := builder.Build().(*urfave.App)

Check failure on line 32 in cli/ucli/ucli_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
require.NotNil(t, app)

require.Len(t, app.Commands, 3)

Expand All @@ -40,6 +42,8 @@ func TestSetCommand(t *testing.T) {

func TestCommandBuilder(t *testing.T) {
builder := NewBuilder("test", nil).(*Builder)

Check failure on line 44 in cli/ucli/ucli_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
require.NotNil(t, builder)

cmd := builder.SetCommand("first")

fakeAction := func(flags cli.Flags) error {
Expand Down
6 changes: 3 additions & 3 deletions dkg/pedersen/pedersen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,6 @@ func TestPedersen_Scenario(t *testing.T) {

n := 32

k, err := minokey.NewKey(nil)
require.NoError(t, err)

minos := make([]mino.Mino, n)
dkgs := make([]dkg.DKG, n)
addrs := make([]mino.Address, n)
Expand All @@ -195,6 +192,9 @@ func TestPedersen_Scenario(t *testing.T) {

manager := minows.NewManager()
for i := 0; i < n; i++ {
k, err := minokey.NewKey(nil)
require.NoError(t, err)

m, err := minows.NewMinows(manager, listen, nil, k)
require.NoError(t, err)

Expand Down
17 changes: 10 additions & 7 deletions dkg/pedersen/state.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package pedersen

import (
"sync"

"go.dedis.ch/dela/mino"
"go.dedis.ch/kyber/v3"
"golang.org/x/xerrors"
"sync"
)

// dkgState represents the states of a DKG node. States change as follow:
Expand Down Expand Up @@ -50,18 +51,18 @@ type state struct {
// participants is set once a sharing or resharing starts
participants []mino.Address
pubkeys []kyber.Point
//TODO add verifiability (poly *share.PubPoly)
// TODO add verifiability (poly *share.PubPoly)
threshold int
dkgState dkgState
}

func (s *state) switchState(new dkgState) error {
func (s *state) switchState(newState dkgState) error {
s.Lock()
defer s.Unlock()

current := s.dkgState

switch new {
switch newState {
case initial:
return xerrors.Errorf("initial state cannot be set manually")
case sharing:
Expand All @@ -70,15 +71,17 @@ func (s *state) switchState(new dkgState) error {
}
case certified:
if current != sharing && current != resharing {
return xerrors.Errorf("certified state must switch from sharing or resharing: %s", current)
return xerrors.Errorf("certified state must switch from sharing or resharing: %s",
current)
}
case resharing:
if current != initial && current != certified {
return xerrors.Errorf("resharing state must switch from initial or certified: %s", current)
return xerrors.Errorf("resharing state must switch from initial or certified: %s",
current)
}
}

s.dkgState = new
s.dkgState = newState

return nil
}
Expand Down
26 changes: 17 additions & 9 deletions internal/traffic/mod.go → internal/traffic/traffic.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"google.golang.org/grpc/metadata"
)

const RECEIVED_STR = "received"
const SEND_STR = "send"

// EnvVariable is the name of the environment variable to enable the traffic.
const EnvVariable = "MINO_TRAFFIC"

Expand Down Expand Up @@ -171,7 +174,7 @@ func (t *Traffic) LogSend(ctx context.Context, gateway mino.Address, pkt router.
func (t *Traffic) LogRecv(ctx context.Context, gateway mino.Address, pkt router.Packet) {
GlobalWatcher.inWatcher.Notify(Event{Address: gateway, Pkt: pkt})

t.addItem(ctx, "received", gateway, pkt)
t.addItem(ctx, RECEIVED_STR, gateway, pkt)
}

// LogRelay records a new relay.
Expand Down Expand Up @@ -215,9 +218,9 @@ func (t *Traffic) addItem(ctx context.Context, typeStr string, gw mino.Address,
}

switch typeStr {
case "received":
case RECEIVED_STR:
newItem.typeCounter = recvCounter.IncrementAndGet()
case "send":
case SEND_STR:
newItem.typeCounter = sendCounter.IncrementAndGet()
}

Expand Down Expand Up @@ -307,7 +310,9 @@ func GenerateItemsGraphviz(out io.Writer, withSend, withRcv bool, traffics ...*T

fmt.Fprintf(out, "digraph network_activity {\n")
fmt.Fprintf(out, "labelloc=\"t\";")
fmt.Fprintf(out, "label = <Network Diagram of %d nodes <font point-size='10'><br/>(generated %s)</font>>;", len(traffics), time.Now().Format("2 Jan 06 - 15:04:05"))
fmt.Fprintf(out, "label = <Network Diagram of %d nodes <font point-size='10'><br/>"+
"(generated %s)</font>>;",
len(traffics), time.Now().Format("2 Jan 06 - 15:04:05"))
fmt.Fprintf(out, "graph [fontname = \"helvetica\"];")
fmt.Fprintf(out, "graph [fontname = \"helvetica\"];")
fmt.Fprintf(out, "node [fontname = \"helvetica\"];")
Expand All @@ -316,16 +321,16 @@ func GenerateItemsGraphviz(out io.Writer, withSend, withRcv bool, traffics ...*T
for _, traffic := range traffics {
for _, item := range traffic.items {

if !withSend && item.typeStr == "send" {
if !withSend && item.typeStr == SEND_STR {
continue
}
if !withRcv && item.typeStr == "received" {
if !withRcv && item.typeStr == RECEIVED_STR {
continue
}

color := "#4AB2FF"

if item.typeStr == "received" {
if item.typeStr == RECEIVED_STR {
color = "#A8A8A8"
}

Expand All @@ -343,7 +348,8 @@ func GenerateItemsGraphviz(out io.Writer, withSend, withRcv bool, traffics ...*T
}

fmt.Fprintf(out, "\"%v\" -> \"%v\" "+
"[ label = < <font color='#303030'><b>%d</b> <font point-size='10'>(%d)</font></font><br/>%s> color=\"%s\" ];\n",
"[ label = < <font color='#303030'><b>%d</b> <font point-size='10'>(%d)"+
"</font></font><br/>%s> color=\"%s\" ];\n",
item.src, item.gateway, item.typeCounter, item.globalCounter, msgStr, color)
}
}
Expand All @@ -357,7 +363,9 @@ func GenerateEventGraphviz(out io.Writer, traffics ...*Traffic) {

fmt.Fprintf(out, "digraph network_activity {\n")
fmt.Fprintf(out, "labelloc=\"t\";")
fmt.Fprintf(out, "label = <Network Diagram of %d nodes <font point-size='10'><br/>(generated %s)</font>>;", len(traffics), time.Now().Format("2 Jan 06 - 15:04:05"))
fmt.Fprintf(out,
"label = <Network Diagram of %d nodes <font point-size='10'><br/>(generated %s)</font>>;",
len(traffics), time.Now().Format("2 Jan 06 - 15:04:05"))
fmt.Fprintf(out, "graph [fontname = \"helvetica\"];")
fmt.Fprintf(out, "graph [fontname = \"helvetica\"];")
fmt.Fprintf(out, "node [fontname = \"helvetica\"];")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"google.golang.org/grpc/metadata"
)

const WINDOWS = "windows"

func TestTraffic_Integration(t *testing.T) {
src := fake.NewAddress(0)
a2 := fake.NewAddress(1)
Expand Down Expand Up @@ -68,7 +70,7 @@ func TestSaveItems(t *testing.T) {

defer os.RemoveAll(path)

if runtime.GOOS == "windows" {
if runtime.GOOS == WINDOWS {
return
}

Expand All @@ -85,7 +87,7 @@ func TestSaveEvents(t *testing.T) {

defer os.RemoveAll(path)

if runtime.GOOS == "windows" {
if runtime.GOOS == WINDOWS {
return
}

Expand All @@ -104,7 +106,7 @@ func TestTraffic_Save(t *testing.T) {

defer os.RemoveAll(path)

if runtime.GOOS == "windows" {
if runtime.GOOS == WINDOWS {
return
}

Expand Down
24 changes: 14 additions & 10 deletions mino/minows/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@ type Storage struct {
db kv.DB
}

var localstorage *Storage

// NewKey creates a new private key in a new DB.
func NewKey(db kv.DB) (crypto.PrivKey, error) {
if db == nil {
d, err := kv.New("minows")
if err != nil {
return nil, xerrors.Errorf("could not create key DB: %v", err)
if localstorage == nil {
if db == nil {
d, err := kv.New("minows")
if err != nil {
return nil, xerrors.Errorf("could not create key DB: %v", err)
}
db = d
}
db = d
}

s := &Storage{
bucket: []byte("minows_keys"),
db: db,
localstorage = &Storage{
bucket: []byte("minows_keys"),
db: db,
}
}

key, err := s.LoadOrCreate()
key, err := localstorage.LoadOrCreate()

return key, err
}
Expand Down
17 changes: 0 additions & 17 deletions mino/minows/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,6 @@ func NewManager() *Manager {
}
}

func (m *Manager) get(a mino.Address) (*Minows, error) {
m.Lock()
defer m.Unlock()

addr, ok := a.(address)
if !ok {
return nil, xerrors.Errorf("invalid address type '%T'", a)
}

instance, ok := m.instances[addr.identity]
if !ok {
return nil, xerrors.Errorf("address <%s> not found", addr.identity)
}

return instance, nil
}

func (m *Manager) insert(inst mino.Mino) error {
instance, ok := inst.(*Minows)
if !ok {
Expand Down
16 changes: 11 additions & 5 deletions mino/proxy/http/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"context"
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -53,8 +54,9 @@ func NewHTTP(listenAddr string) proxy.Proxy {
return &HTTP{
mux: mux,
server: &http.Server{
Addr: listenAddr,
Handler: tracing(nextRequestID)(logging(logger)(mux)),
Addr: listenAddr,
Handler: tracing(nextRequestID)(logging(logger)(mux)),
ReadHeaderTimeout: time.Second,
},
logger: logger,
listenAddr: listenAddr,
Expand Down Expand Up @@ -113,7 +115,7 @@ func (h *HTTP) Listen() {
h.logger.Info().Msgf("Server is ready to handle requests at %s", ln.Addr())

err = h.server.Serve(ln)
if err != nil && err != http.ErrServerClosed {
if err != nil && !errors.Is(err, http.ErrServerClosed) {
h.logger.Fatal().Msgf("Could not listen on %s: %v", h.listenAddr, err)
}

Expand All @@ -138,8 +140,12 @@ func (h HTTP) GetAddr() net.Addr {
}

// RegisterHandler implements proxy.Proxy
func (h HTTP) RegisterHandler(path string, handler func(http.ResponseWriter,
*http.Request)) {
func (h HTTP) RegisterHandler(
path string, handler func(
http.ResponseWriter,
*http.Request,
),
) {

h.mux.HandleFunc(path, handler)
}
Expand Down
8 changes: 4 additions & 4 deletions mino/proxy/http/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"io"
"net/http"
"os"
"testing"
"time"

Expand All @@ -13,15 +12,15 @@ import (
)

func TestInit(t *testing.T) {
os.Setenv("PROXY_LOG", "warn")
t.Setenv("PROXY_LOG", "warn")
setLogLevel()
require.Equal(t, defaultLevel, zerolog.WarnLevel)

os.Setenv("PROXY_LOG", "no")
t.Setenv("PROXY_LOG", "no")
setLogLevel()
require.Equal(t, defaultLevel, zerolog.Disabled)

os.Setenv("PROXY_LOG", "info")
t.Setenv("PROXY_LOG", "info")
setLogLevel()
require.Equal(t, defaultLevel, zerolog.InfoLevel)
}
Expand All @@ -37,6 +36,7 @@ func TestHTTP_Listen(t *testing.T) {

res, err := http.Get("http://127.0.0.1:2010/fake")
require.NoError(t, err)
defer res.Body.Close()

output, err := io.ReadAll(res.Body)
require.NoError(t, err)
Expand Down

0 comments on commit 3d1d158

Please sign in to comment.