Skip to content

Commit

Permalink
Clean code (#2945)
Browse files Browse the repository at this point in the history
Refs #2943.
  • Loading branch information
roman-khimov authored Sep 24, 2024
2 parents ca16d59 + d5f1eb7 commit a385b78
Show file tree
Hide file tree
Showing 100 changed files with 330 additions and 342 deletions.
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
5 changes: 4 additions & 1 deletion cmd/neofs-adm/internal/modules/morph/initialize_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ func (c *initializeContext) transferNEOToAlphabetContracts() error {
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
}
return bal.Int64() < native.NEOTotalSupply, nil
}

var errGetPriceInvalid = errors.New("`getRegisterPrice`: invalid response")
Expand Down
5 changes: 4 additions & 1 deletion cmd/neofs-adm/internal/modules/morph/initialize_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func (c *initializeContext) transferFundsFinished() (bool, error) {

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
}
return res.Int64() > initialAlphabetGASAmount/2, nil
}

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 @@ func (l *localClient) CalculateNetworkFee(tx *transaction.Transaction) (int64, e
continue
}

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

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

return netFee, nil
}
Expand Down Expand Up @@ -319,7 +318,7 @@ func invokeFunction(c Client, h util.Uint160, method string, parameters []any, s

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)
}

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 @@ func depositNotary(cmd *cobra.Command, _ []string) error {

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)
}

accHash := w.GetChangeAddress()
Expand All @@ -50,12 +50,12 @@ func depositNotary(cmd *cobra.Command, _ []string) error {
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)
}

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)
}

gasStr, err := cmd.Flags().GetString(refillGasAmountFlag)
Expand Down Expand Up @@ -86,7 +86,7 @@ func depositNotary(cmd *cobra.Command, _ []string) error {

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)
}

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 @@ func verifiedNodesDomainSetAccessList(cmd *cobra.Command, _ []string) error {

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)
}

var accAddr util.Uint160
Expand All @@ -141,12 +141,12 @@ func verifiedNodesDomainSetAccessList(cmd *cobra.Command, _ []string) error {
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)
}

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)
}

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 @@ func ParseEACLRules(table *eacl.Table, rules []string) error {
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)
}
}
return nil
Expand All @@ -199,7 +199,7 @@ func ParseEACLRules(table *eacl.Table, rules []string) error {
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)
}
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:/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 @@ func EncodeReplicationMetaInfo(cID cid.ID, oID oid.ID, pSize uint64,
// 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))
}

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 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper, errChan chan<-
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())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -991,7 +991,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper, errChan chan<-
PoolSize: cfg.GetInt("workers.reputation"),
EpochState: server,
AlphabetState: server,
ReputationWrapper: repClient,
ReputationWrapper: reputationClient,
ManagerBuilder: reputationcommon.NewManagerBuilder(
reputationcommon.ManagersPrm{
NetMapSource: server.netmapClient,
Expand Down
Loading

0 comments on commit a385b78

Please sign in to comment.