Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean code #2945

Merged
merged 18 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/neofs-adm/internal/modules/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ func TestGenerateConfigExample(t *testing.T) {
require.Equal(t, 1000, v.GetInt("network.fee.container"))
require.Equal(t, 100000000, v.GetInt("network.fee.withdraw"))

var i int
for i = 0; i < n; i++ {
for i := range n {
key := "credentials." + glagolitsa.LetterByIndex(i)
require.Equal(t, "password", v.GetString(key))
}

key := "credentials." + glagolitsa.LetterByIndex(i)
key := "credentials." + glagolitsa.LetterByIndex(n)
require.Equal(t, "", v.GetString(key))
}
4 changes: 2 additions & 2 deletions cmd/neofs-adm/internal/modules/morph/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func TestGenerateAlphabet(t *testing.T) {
buf.Reset()
v.Set(alphabetWalletsFlag, walletDir)
require.NoError(t, generateAlphabetCmd.Flags().Set(alphabetSizeFlag, strconv.FormatUint(size, 10)))
for i := uint64(0); i < size; i++ {
for i := range uint64(size) {
buf.WriteString(strconv.FormatUint(i, 10) + "\r")
}

require.NoError(t, generateAlphabetCreds(generateAlphabetCmd, nil))

for i := uint64(0); i < size; i++ {
for i := range uint64(size) {
p := filepath.Join(walletDir, glagolitsa.LetterByIndex(int(i))+".json")
w, err := wallet.NewWalletFromFile(p)
require.NoError(t, err, "wallet doesn't exist")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@
func (c *initializeContext) transferNEOFinished() (bool, error) {
neoR := neo.NewReader(invoker.New(c.Client, nil))
bal, err := neoR.BalanceOf(c.CommitteeAcc.Contract.ScriptHash())
return bal.Int64() < native.NEOTotalSupply, err
if err != nil {
return false, err

Check warning on line 134 in cmd/neofs-adm/internal/modules/morph/initialize_register.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/initialize_register.go#L133-L134

Added lines #L133 - L134 were not covered by tests
}
return bal.Int64() < native.NEOTotalSupply, nil

Check warning on line 136 in cmd/neofs-adm/internal/modules/morph/initialize_register.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/initialize_register.go#L136

Added line #L136 was not covered by tests
}

var errGetPriceInvalid = errors.New("`getRegisterPrice`: invalid response")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@

gasR := gas.NewReader(invoker.New(c.Client, nil))
res, err := gasR.BalanceOf(acc.Contract.ScriptHash())
return res.Int64() > initialAlphabetGASAmount/2, err
if err != nil {
return false, err

Check warning on line 77 in cmd/neofs-adm/internal/modules/morph/initialize_transfer.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/initialize_transfer.go#L76-L77

Added lines #L76 - L77 were not covered by tests
}
return res.Int64() > initialAlphabetGASAmount/2, nil

Check warning on line 79 in cmd/neofs-adm/internal/modules/morph/initialize_transfer.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/initialize_transfer.go#L79

Added line #L79 was not covered by tests
}

func (c *initializeContext) multiSignAndSend(tx *transaction.Transaction, accType string) error {
Expand Down
9 changes: 4 additions & 5 deletions cmd/neofs-adm/internal/modules/morph/local_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,12 @@
continue
}

fee, sizeDelta := fee.Calculate(ef, verificationScript)
netFee += fee
verificationFee, sizeDelta := fee.Calculate(ef, verificationScript)
netFee += verificationFee

Check warning on line 216 in cmd/neofs-adm/internal/modules/morph/local_client.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/local_client.go#L215-L216

Added lines #L215 - L216 were not covered by tests
size += sizeDelta
}

fee := l.bc.FeePerByte()
netFee += int64(size) * fee
netFee += int64(size) * l.bc.FeePerByte()

Check warning on line 220 in cmd/neofs-adm/internal/modules/morph/local_client.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/local_client.go#L220

Added line #L220 was not covered by tests

return netFee, nil
}
Expand Down Expand Up @@ -319,7 +318,7 @@

script, err := b.Script()
if err != nil {
return nil, fmt.Errorf("BUG: invalid parameters for '%s': %v", method, err)
return nil, fmt.Errorf("BUG: invalid parameters for '%s': %w", method, err)

Check warning on line 321 in cmd/neofs-adm/internal/modules/morph/local_client.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/local_client.go#L321

Added line #L321 was not covered by tests
}

return c.InvokeScript(script, signers)
Expand Down
8 changes: 4 additions & 4 deletions cmd/neofs-adm/internal/modules/morph/notary.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

w, err := wallet.NewWalletFromFile(p)
if err != nil {
return fmt.Errorf("can't open wallet: %v", err)
return fmt.Errorf("can't open wallet: %w", err)

Check warning on line 34 in cmd/neofs-adm/internal/modules/morph/notary.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/notary.go#L34

Added line #L34 was not covered by tests
}

accHash := w.GetChangeAddress()
Expand All @@ -50,12 +50,12 @@
prompt := fmt.Sprintf("Enter password for %s >", address.Uint160ToString(accHash))
pass, err := input.ReadPassword(prompt)
if err != nil {
return fmt.Errorf("can't get password: %v", err)
return fmt.Errorf("can't get password: %w", err)

Check warning on line 53 in cmd/neofs-adm/internal/modules/morph/notary.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/notary.go#L53

Added line #L53 was not covered by tests
}

err = acc.Decrypt(pass, keys.NEP2ScryptParams())
if err != nil {
return fmt.Errorf("can't unlock account: %v", err)
return fmt.Errorf("can't unlock account: %w", err)

Check warning on line 58 in cmd/neofs-adm/internal/modules/morph/notary.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/notary.go#L58

Added line #L58 was not covered by tests
}

gasStr, err := cmd.Flags().GetString(refillGasAmountFlag)
Expand Down Expand Up @@ -86,7 +86,7 @@

height, err := c.GetBlockCount()
if err != nil {
return fmt.Errorf("can't get current height: %v", err)
return fmt.Errorf("can't get current height: %w", err)

Check warning on line 89 in cmd/neofs-adm/internal/modules/morph/notary.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/notary.go#L89

Added line #L89 was not covered by tests
}

act, err := actor.New(c, []actor.SignerAccount{{
Expand Down
6 changes: 3 additions & 3 deletions cmd/neofs-adm/internal/modules/morph/verified_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@

w, err := wallet.NewWalletFromFile(viper.GetString(walletFlag))
if err != nil {
return fmt.Errorf("decode Neo wallet from file: %v", err)
return fmt.Errorf("decode Neo wallet from file: %w", err)

Check warning on line 123 in cmd/neofs-adm/internal/modules/morph/verified_domains.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/verified_domains.go#L123

Added line #L123 was not covered by tests
}

var accAddr util.Uint160
Expand All @@ -141,12 +141,12 @@
prompt := fmt.Sprintf("Enter password for %s >", address.Uint160ToString(accAddr))
pass, err := input.ReadPassword(prompt)
if err != nil {
return fmt.Errorf("failed to read account password: %v", err)
return fmt.Errorf("failed to read account password: %w", err)

Check warning on line 144 in cmd/neofs-adm/internal/modules/morph/verified_domains.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/verified_domains.go#L144

Added line #L144 was not covered by tests
}

err = acc.Decrypt(pass, keys.NEP2ScryptParams())
if err != nil {
return fmt.Errorf("failed to unlock the account with password: %v", err)
return fmt.Errorf("failed to unlock the account with password: %w", err)

Check warning on line 149 in cmd/neofs-adm/internal/modules/morph/verified_domains.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-adm/internal/modules/morph/verified_domains.go#L149

Added line #L149 was not covered by tests
}

n3Client, err := getN3Client(vpr)
Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-cli/modules/acl/extended/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestParseTable(t *testing.T) {
tests := [...]struct {
name string // test name
rule string // input extended ACL rule
jsonRecord string // produced record after successfull parsing
jsonRecord string // produced record after successful parsing
}{
{
name: "valid rule with multiple filters",
Expand Down
4 changes: 2 additions & 2 deletions cmd/neofs-cli/modules/util/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
for _, ruleStr := range rules {
err := ParseEACLRule(table, ruleStr)
if err != nil {
return fmt.Errorf("can't create extended acl record from rule '%s': %v", ruleStr, err)
return fmt.Errorf("can't create extended acl record from rule '%s': %w", ruleStr, err)

Check warning on line 186 in cmd/neofs-cli/modules/util/acl.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/util/acl.go#L186

Added line #L186 was not covered by tests
}
}
return nil
Expand All @@ -199,7 +199,7 @@
func ParseEACLRule(table *eacl.Table, rule string) error {
r, err := shlex.Split(rule)
if err != nil {
return fmt.Errorf("can't parse rule '%s': %v", rule, err)
return fmt.Errorf("can't parse rule '%s': %w", rule, err)

Check warning on line 202 in cmd/neofs-cli/modules/util/acl.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/util/acl.go#L202

Added line #L202 was not covered by tests
}
return parseEACLTable(table, r)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-node/config/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (x *Config) Sub(name string) *Config {

// Value returns the configuration value by name.
//
// Result can be casted to a particular type
// Result can be cast to a particular type
// via corresponding function (e.g. StringSlice).
// Note: casting via Go `.()` operator is not
// recommended.
Expand Down
32 changes: 16 additions & 16 deletions cmd/neofs-node/config/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func panicOnErr(err error) {
// StringSlice reads a configuration value
// from c by name and casts it to a []string.
//
// Panics if the value can not be casted.
// Panics if the value can not be cast.
func StringSlice(c *Config, name string) []string {
x, err := cast.ToStringSliceE(c.Value(name))
panicOnErr(err)
Expand All @@ -29,15 +29,15 @@ func StringSlice(c *Config, name string) []string {
// StringSliceSafe reads a configuration value
// from c by name and casts it to a []string.
//
// Returns nil if the value can not be casted.
// Returns nil if the value can not be cast.
func StringSliceSafe(c *Config, name string) []string {
return cast.ToStringSlice(c.Value(name))
}

// String reads a configuration value
// from c by name and casts it to a string.
//
// Panics if the value can not be casted.
// Panics if the value can not be cast.
func String(c *Config, name string) string {
x, err := cast.ToStringE(c.Value(name))
panicOnErr(err)
Expand All @@ -48,15 +48,15 @@ func String(c *Config, name string) string {
// StringSafe reads a configuration value
// from c by name and casts it to a string.
//
// Returns "" if the value can not be casted.
// Returns "" if the value can not be cast.
func StringSafe(c *Config, name string) string {
return cast.ToString(c.Value(name))
}

// Duration reads a configuration value
// from c by name and casts it to time.Duration.
//
// Panics if the value can not be casted.
// Panics if the value can not be cast.
func Duration(c *Config, name string) time.Duration {
x, err := cast.ToDurationE(c.Value(name))
panicOnErr(err)
Expand All @@ -67,15 +67,15 @@ func Duration(c *Config, name string) time.Duration {
// DurationSafe reads a configuration value
// from c by name and casts it to time.Duration.
//
// Returns 0 if the value can not be casted.
// Returns 0 if the value can not be cast.
func DurationSafe(c *Config, name string) time.Duration {
return cast.ToDuration(c.Value(name))
}

// Bool reads a configuration value
// from c by name and casts it to bool.
//
// Panics if the value can not be casted.
// Panics if the value can not be cast.
func Bool(c *Config, name string) bool {
x, err := cast.ToBoolE(c.Value(name))
panicOnErr(err)
Expand All @@ -86,15 +86,15 @@ func Bool(c *Config, name string) bool {
// BoolSafe reads a configuration value
// from c by name and casts it to bool.
//
// Returns false if the value can not be casted.
// Returns false if the value can not be cast.
func BoolSafe(c *Config, name string) bool {
return cast.ToBool(c.Value(name))
}

// Uint32 reads a configuration value
// from c by name and casts it to uint32.
//
// Panics if the value can not be casted.
// Panics if the value can not be cast.
func Uint32(c *Config, name string) uint32 {
x, err := cast.ToUint32E(c.Value(name))
panicOnErr(err)
Expand All @@ -105,15 +105,15 @@ func Uint32(c *Config, name string) uint32 {
// Uint32Safe reads a configuration value
// from c by name and casts it to uint32.
//
// Returns 0 if the value can not be casted.
// Returns 0 if the value can not be cast.
func Uint32Safe(c *Config, name string) uint32 {
return cast.ToUint32(c.Value(name))
}

// Uint reads a configuration value
// from c by name and casts it to uint64.
//
// Panics if the value can not be casted.
// Panics if the value can not be cast.
func Uint(c *Config, name string) uint64 {
x, err := cast.ToUint64E(c.Value(name))
panicOnErr(err)
Expand All @@ -124,15 +124,15 @@ func Uint(c *Config, name string) uint64 {
// UintSafe reads a configuration value
// from c by name and casts it to uint64.
//
// Returns 0 if the value can not be casted.
// Returns 0 if the value can not be cast.
func UintSafe(c *Config, name string) uint64 {
return cast.ToUint64(c.Value(name))
}

// Int reads a configuration value
// from c by name and casts it to int64.
//
// Panics if the value can not be casted.
// Panics if the value can not be cast.
func Int(c *Config, name string) int64 {
x, err := cast.ToInt64E(c.Value(name))
panicOnErr(err)
Expand All @@ -143,7 +143,7 @@ func Int(c *Config, name string) int64 {
// IntSafe reads a configuration value
// from c by name and casts it to int64.
//
// Returns 0 if the value can not be casted.
// Returns 0 if the value can not be cast.
func IntSafe(c *Config, name string) int64 {
return cast.ToInt64(c.Value(name))
}
Expand All @@ -155,14 +155,14 @@ func IntSafe(c *Config, name string) int64 {
// an additional b at the end. Spaces between the number and the suffix
// are allowed. All multipliers are power of 2 (i.e. k is for kibi-byte).
//
// Returns 0 if a value can't be casted.
// Returns 0 if a value can't be cast.
func SizeInBytesSafe(c *Config, name string) uint64 {
s := StringSafe(c, name)
return parseSizeInBytes(s)
}

// The following code is taken from https://github.com/spf13/viper/blob/master/util.go
// with minor corrections (allow to use both `k` and `kb` forms.
// with minor corrections (allow to use both `k` and `kb` forms).
// Seems like viper allows to convert sizes but corresponding parser in `cast` package
// is missing.

Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-node/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (i *delNetInfo) TombstoneLifetime() (uint64, error) {
return i.tsLifetime, nil
}

// returns node owner ID calculated from configured private key.
// LocalNodeID returns node owner ID calculated from configured private key.
//
// Implements method needed for Object.Delete service.
func (i *delNetInfo) LocalNodeID() user.ID {
Expand Down
2 changes: 1 addition & 1 deletion docs/cli-adm.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ info. These commands **do not migrate actual objects**.

## Private network deployment

Read step-by-step guide of private storage deployment [in docs](./docs/deploy.md).
Read step-by-step guide of private storage deployment [in docs](./deploy.md).
2 changes: 1 addition & 1 deletion pkg/core/netmap/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package netmap

// AnnouncedKeys is an interface of utility for working with the announced public keys of the storage nodes.
type AnnouncedKeys interface {
// Checks if the key was announced by a local node.
// IsLocalKey checks if the key was announced by a local node.
IsLocalKey(key []byte) bool
}
2 changes: 1 addition & 1 deletion pkg/core/object/replicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
// all the errors in the stackitem relate only cases when it is
// impossible to use serialized values (too many values, unsupported
// types, etc.), unexpected errors at all
panic(fmt.Errorf("unexpected stackitem map serialization failure: %v", err))
panic(fmt.Errorf("unexpected stackitem map serialization failure: %w", err))

Check warning on line 57 in pkg/core/object/replicate.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/object/replicate.go#L57

Added line #L57 was not covered by tests
}

return result
Expand Down
4 changes: 2 additions & 2 deletions pkg/innerring/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ func TestIsAutoDeploymentMode(t *testing.T) {
err = os.Setenv(envKey, "not a boolean")
require.NoError(t, err)

b, err = isAutoDeploymentMode(v)
_, err = isAutoDeploymentMode(v)
require.Error(t, err)

err = os.Setenv(envKey, "false")
Expand Down Expand Up @@ -571,7 +571,7 @@ fschain_autodeploy: true

v.Set("fschain_autodeploy", "not a boolean")

b, err = isAutoDeploymentMode(v)
_, err = isAutoDeploymentMode(v)
require.Error(t, err)

v.Set("fschain_autodeploy", "false")
Expand Down
4 changes: 2 additions & 2 deletions pkg/innerring/innerring.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@
return nil, fmt.Errorf("can't read balance contract precision: %w", err)
}

repClient, err := repClient.NewFromMorph(server.morphClient, server.contracts.reputation, 0, repClient.AsAlphabet())
reputationClient, err := repClient.NewFromMorph(server.morphClient, server.contracts.reputation, 0, repClient.AsAlphabet())

Check warning on line 685 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L685

Added line #L685 was not covered by tests
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -991,7 +991,7 @@
PoolSize: cfg.GetInt("workers.reputation"),
EpochState: server,
AlphabetState: server,
ReputationWrapper: repClient,
ReputationWrapper: reputationClient,

Check warning on line 994 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L994

Added line #L994 was not covered by tests
ManagerBuilder: reputationcommon.NewManagerBuilder(
reputationcommon.ManagersPrm{
NetMapSource: server.netmapClient,
Expand Down
Loading
Loading