Skip to content

Commit

Permalink
fix(other): add varnamelen linter to improve name convations (#1568)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad authored Oct 27, 2024
1 parent e7d7175 commit 9711f31
Show file tree
Hide file tree
Showing 215 changed files with 4,320 additions and 4,271 deletions.
35 changes: 31 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ linters:
- whitespace
# - wrapcheck
- zerologlint
- varnamelen

linters-settings:
gosimple:
Expand Down Expand Up @@ -128,6 +129,9 @@ linters-settings:
- name: "function-result-limit"
disabled: true

- name: "import-shadowing"
disabled: true

- name: unhandled-error
arguments:
- "fmt.Printf"
Expand Down Expand Up @@ -177,7 +181,7 @@ linters-settings:
disabled-checks:
- ifElseChain
- unnamedResult
- builtinShadow
- importShadow
enabled-tags:
- diagnostic
- style
Expand All @@ -188,6 +192,27 @@ linters-settings:
# Default: 5
min-complexity: 6

varnamelen:
ignore-names:
- ok
- ip
- no
- tt # table tests
- i # for simple loops
- j # for simple loops
- l
- h
- il
- r # reader
- w # writer

ignore-decls:
- wg sync.WaitGroup
- ts *testsuite.TestSuite
- td *testData
- ma multiaddr.Multiaddr
- db *leveldb.DB

issues:
exclude-use-default: false
exclude-rules:
Expand All @@ -198,6 +223,8 @@ issues:
- gocognit
- forbidigo

- linters:
- govet
text: "shadow: declaration of \"err\" shadows"
exclude:
- "shadow: declaration of \"err\" shadows"
- "builtinShadow: shadowing of predeclared identifier: min"
- "builtinShadow: shadowing of predeclared identifier: max"
- "builtinShadow: shadowing of predeclared identifier: len"
14 changes: 7 additions & 7 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ func FatalErrorCheck(err error) {
}
}

func PrintErrorMsgf(format string, a ...any) {
func PrintErrorMsgf(format string, args ...any) {
format = "[ERROR] " + format
if terminalSupported {
// Print error msg with red color
format = fmt.Sprintf("\033[31m%s\033[0m", format)
}
fmt.Printf(format+"\n", a...)
fmt.Printf(format+"\n", args...)
}

func PrintSuccessMsgf(format string, a ...any) {
Expand Down Expand Up @@ -408,21 +408,21 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string,
return nil, nil, err
}

nd, err := node.NewNode(gen, conf, valKeys, rewardAddrs)
node, err := node.NewNode(gen, conf, valKeys, rewardAddrs)
if err != nil {
return nil, nil, err
}

err = nd.Start()
err = node.Start()
if err != nil {
return nil, nil, err
}

return nd, walletInstance, nil
return node, walletInstance, nil
}

// makeLocalGenesis makes genesis file for the local network.
func makeLocalGenesis(w wallet.Wallet) *genesis.Genesis {
func makeLocalGenesis(wlt wallet.Wallet) *genesis.Genesis {
// Treasury account
acc := account.NewAccount(0)
acc.AddToBalance(21 * 1e14)
Expand All @@ -433,7 +433,7 @@ func makeLocalGenesis(w wallet.Wallet) *genesis.Genesis {
genValNum := 4
vals := make([]*validator.Validator, genValNum)
for i := 0; i < genValNum; i++ {
info := w.AddressInfo(w.AddressInfos()[i].Address)
info := wlt.AddressInfo(wlt.AddressInfos()[i].Address)
pub, _ := bls.PublicKeyFromString(info.PublicKey)
vals[i] = validator.NewValidator(pub, int32(i))
}
Expand Down
58 changes: 29 additions & 29 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,25 @@ func TestMakeConfig(t *testing.T) {
}

// captureOutput is a helper function to capture the printed output of a function.
func captureOutput(f func()) string {
func captureOutput(fun func()) string {
// Redirect stdout to a buffer
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
reader, writer, _ := os.Pipe()
os.Stdout = writer

// Capture the printed output
outC := make(chan string)
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
_, _ = io.Copy(&buf, reader)
outC <- buf.String()
}()

// Execute the function
f()
fun()

// Reset stdout
_ = w.Close()
_ = writer.Close()
os.Stdout = oldStdout
out := <-outC

Expand Down Expand Up @@ -167,16 +167,16 @@ func TestPathsUnix(t *testing.T) {
},
}

for _, test := range tests {
walletDir := PactusWalletDir(test.home)
defaultWalletPath := PactusDefaultWalletPath(test.home)
genesisPath := PactusGenesisPath(test.home)
configPath := PactusConfigPath(test.home)
for _, tt := range tests {
walletDir := PactusWalletDir(tt.home)
defaultWalletPath := PactusDefaultWalletPath(tt.home)
genesisPath := PactusGenesisPath(tt.home)
configPath := PactusConfigPath(tt.home)

assert.Equal(t, test.expectedWalletDir, walletDir)
assert.Equal(t, test.expectedDefaultWalletPath, defaultWalletPath)
assert.Equal(t, test.expectedGenesisPath, genesisPath)
assert.Equal(t, test.expectedConfigPath, configPath)
assert.Equal(t, tt.expectedWalletDir, walletDir)
assert.Equal(t, tt.expectedDefaultWalletPath, defaultWalletPath)
assert.Equal(t, tt.expectedGenesisPath, genesisPath)
assert.Equal(t, tt.expectedConfigPath, configPath)
}
}

Expand Down Expand Up @@ -207,16 +207,16 @@ func TestPathsWindows(t *testing.T) {
},
}

for _, test := range tests {
walletDir := PactusWalletDir(test.home)
defaultWalletPath := PactusDefaultWalletPath(test.home)
genesisPath := PactusGenesisPath(test.home)
configPath := PactusConfigPath(test.home)
for _, tt := range tests {
walletDir := PactusWalletDir(tt.home)
defaultWalletPath := PactusDefaultWalletPath(tt.home)
genesisPath := PactusGenesisPath(tt.home)
configPath := PactusConfigPath(tt.home)

assert.Equal(t, test.expectedWalletDir, walletDir)
assert.Equal(t, test.expectedDefaultWalletPath, defaultWalletPath)
assert.Equal(t, test.expectedGenesisPath, genesisPath)
assert.Equal(t, test.expectedConfigPath, configPath)
assert.Equal(t, tt.expectedWalletDir, walletDir)
assert.Equal(t, tt.expectedDefaultWalletPath, defaultWalletPath)
assert.Equal(t, tt.expectedGenesisPath, genesisPath)
assert.Equal(t, tt.expectedConfigPath, configPath)
}
}

Expand Down Expand Up @@ -364,16 +364,16 @@ func TestCreateNode(t *testing.T) {
},
}

for _, test := range tests {
for _, tt := range tests {
validatorAddrs, rewardAddrs, err := CreateNode(
test.numValidators, test.chain, test.workingDir, test.mnemonic, "")
tt.numValidators, tt.chain, tt.workingDir, tt.mnemonic, "")

if test.withErr {
if tt.withErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, test.validatorAddrs, validatorAddrs)
assert.Equal(t, test.rewardAddrs, rewardAddrs)
assert.Equal(t, tt.validatorAddrs, validatorAddrs)
assert.Equal(t, tt.rewardAddrs, rewardAddrs)
}
}
}
12 changes: 6 additions & 6 deletions cmd/daemon/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func buildImportCmd(parentCmd *cobra.Command) {
serverAddrOpt := importCmd.Flags().String("server-addr", cmd.DefaultSnapshotURL,
"import server address")

importCmd.Run = func(c *cobra.Command, _ []string) {
importCmd.Run = func(cobra *cobra.Command, _ []string) {
workingDir, err := filepath.Abs(*workingDirOpt)
cmd.FatalErrorCheck(err)

Expand Down Expand Up @@ -54,15 +54,15 @@ func buildImportCmd(parentCmd *cobra.Command) {
)
cmd.FatalErrorCheck(err)

metadata, err := importer.GetMetadata(c.Context())
metadata, err := importer.GetMetadata(cobra.Context())
cmd.FatalErrorCheck(err)

snapshots := make([]string, 0, len(metadata))

for _, m := range metadata {
for _, md := range metadata {
item := fmt.Sprintf("snapshot %s (%s)",
m.CreatedAtTime().Format("2006-01-02"),
util.FormatBytesToHumanReadable(m.Data.Size),
md.CreatedAtTime().Format("2006-01-02"),
util.FormatBytesToHumanReadable(md.Data.Size),
)

snapshots = append(snapshots, item)
Expand All @@ -81,7 +81,7 @@ func buildImportCmd(parentCmd *cobra.Command) {

cmd.PrintLine()

err = importer.Download(c.Context(), &selected, downloadProgressBar)
err = importer.Download(cobra.Context(), &selected, downloadProgressBar)
cmd.FatalErrorCheck(err)

cmd.PrintLine()
Expand Down
6 changes: 3 additions & 3 deletions cmd/daemon/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func buildPruneCmd(parentCmd *cobra.Command) {
}
cmd.PrintLine()

str, err := store.NewStore(conf.Store)
store, err := store.NewStore(conf.Store)
cmd.FatalErrorCheck(err)

prunedCount := uint32(0)
Expand All @@ -73,7 +73,7 @@ func buildPruneCmd(parentCmd *cobra.Command) {
<-closed
})

err = str.Prune(func(pruned bool, pruningHeight uint32) bool {
err = store.Prune(func(pruned bool, pruningHeight uint32) bool {
if pruned {
prunedCount++
} else {
Expand Down Expand Up @@ -108,7 +108,7 @@ func buildPruneCmd(parentCmd *cobra.Command) {
cmd.PrintInfoMsgf("./pactus-daemon start -w %v", workingDir)
}

str.Close()
store.Close()
_ = fileLock.Unlock()

closed <- true
Expand Down
14 changes: 7 additions & 7 deletions cmd/gtk/dialog_wallet_create_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
//go:embed assets/ui/dialog_wallet_create_address.ui
var uiWalletCreateAddressDialog []byte

func createAddress(ww *widgetWallet) {
func createAddress(wdgWallet *widgetWallet) {
builder, err := gtk.BuilderNewFromString(string(uiWalletCreateAddressDialog))
fatalErrorCheck(err)

Expand All @@ -38,24 +38,24 @@ func createAddress(ww *widgetWallet) {

switch walletAddressType {
case wallet.AddressTypeEd25519Account:
password, ok := getWalletPassword(ww.model.wallet)
password, ok := getWalletPassword(wdgWallet.model.wallet)
if !ok {
return
}

_, err = ww.model.wallet.NewEd25519AccountAddress(walletAddressLabel, password)
_, err = wdgWallet.model.wallet.NewEd25519AccountAddress(walletAddressLabel, password)
case wallet.AddressTypeBLSAccount:
_, err = ww.model.wallet.NewBLSAccountAddress(walletAddressLabel)
_, err = wdgWallet.model.wallet.NewBLSAccountAddress(walletAddressLabel)
case wallet.AddressTypeValidator:
_, err = ww.model.wallet.NewValidatorAddress(walletAddressLabel)
_, err = wdgWallet.model.wallet.NewValidatorAddress(walletAddressLabel)
}

errorCheck(err)

err = ww.model.wallet.Save()
err = wdgWallet.model.wallet.Save()
errorCheck(err)

ww.model.rebuildModel()
wdgWallet.model.rebuildModel()

dlg.Close()
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/gtk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func main() {
log.Println("application startup")
})

nd, wlt, err := newNode(workingDir)
node, wlt, err := newNode(workingDir)
fatalErrorCheck(err)

// Connect function to application activate event
Expand All @@ -114,7 +114,7 @@ func main() {

// Running the run-up logic in a separate goroutine
glib.TimeoutAdd(uint(100), func() bool {
run(nd, wlt, app)
run(node, wlt, app)
splashDlg.Destroy()

// Ensures the function is not called again
Expand All @@ -125,14 +125,14 @@ func main() {
// Connect function to application shutdown event, this is not required.
app.Connect("shutdown", func() {
log.Println("Application shutdown")
nd.Stop()
node.Stop()
_ = fileLock.Unlock()
})

cmd.TrapSignal(func() {
cmd.PrintInfoMsgf("Exiting...")

nd.Stop()
node.Stop()
_ = fileLock.Unlock()
})

Expand Down
Loading

0 comments on commit 9711f31

Please sign in to comment.