From 17b5370c2216f3a08acdc818843aefafa122a69a Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Mon, 9 Jul 2018 16:08:35 -0700 Subject: [PATCH] Continue fixing gocyclo errors --- types/errors.go | 1 + types/rational.go | 47 +++++++++++++++++++++---------------- x/stake/client/cli/tx.go | 2 ++ x/stake/client/rest/tx.go | 2 ++ x/stake/keeper/validator.go | 2 ++ 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/types/errors.go b/types/errors.go index 6969549d90..a106ee9bb7 100644 --- a/types/errors.go +++ b/types/errors.go @@ -66,6 +66,7 @@ const ( ) // NOTE: Don't stringer this, we'll put better messages in later. +// nolint: gocyclo func CodeToDefaultMsg(code CodeType) string { switch code { case CodeInternal: diff --git a/types/rational.go b/types/rational.go index 24072fc09e..f81ee08364 100644 --- a/types/rational.go +++ b/types/rational.go @@ -37,6 +37,30 @@ func NewRat(Numerator int64, Denominator ...int64) Rat { } } +func getNumeratorDenominator(str []string, prec int) (numerator string, denom int64, err Error) { + switch len(str) { + case 1: + if len(str[0]) == 0 { + return "", 0, ErrUnknownRequest("not a decimal string") + } + numerator = str[0] + return numerator, 1, nil + case 2: + if len(str[0]) == 0 || len(str[1]) == 0 { + return "", 0, ErrUnknownRequest("not a decimal string") + } + if len(str[1]) > prec { + return "", 0, ErrUnknownRequest("string has too many decimals") + } + numerator = str[0] + str[1] + len := int64(len(str[1])) + denom = new(big.Int).Exp(big.NewInt(10), big.NewInt(len), nil).Int64() + return numerator, denom, nil + default: + return "", 0, ErrUnknownRequest("not a decimal string") + } +} + // create a rational from decimal string or integer string // precision is the number of values after the decimal point which should be read func NewRatFromDecimal(decimalStr string, prec int) (f Rat, err Error) { @@ -53,26 +77,9 @@ func NewRatFromDecimal(decimalStr string, prec int) (f Rat, err Error) { str := strings.Split(decimalStr, ".") - var numStr string - var denom int64 = 1 - switch len(str) { - case 1: - if len(str[0]) == 0 { - return f, ErrUnknownRequest("not a decimal string") - } - numStr = str[0] - case 2: - if len(str[0]) == 0 || len(str[1]) == 0 { - return f, ErrUnknownRequest("not a decimal string") - } - if len(str[1]) > prec { - return f, ErrUnknownRequest("string has too many decimals") - } - numStr = str[0] + str[1] - len := int64(len(str[1])) - denom = new(big.Int).Exp(big.NewInt(10), big.NewInt(len), nil).Int64() - default: - return f, ErrUnknownRequest("not a decimal string") + numStr, denom, err := getNumeratorDenominator(str, prec) + if err != nil { + return f, err } num, errConv := strconv.Atoi(numStr) diff --git a/x/stake/client/cli/tx.go b/x/stake/client/cli/tx.go index 9b8acb8cc8..4701e4c201 100644 --- a/x/stake/client/cli/tx.go +++ b/x/stake/client/cli/tx.go @@ -208,6 +208,8 @@ func GetCmdBeginRedelegate(storeName string, cdc *wire.Codec) *cobra.Command { return cmd } +// nolint: gocyclo +// TODO: Make this pass gocyclo linting func getShares(storeName string, cdc *wire.Codec, sharesAmountStr, sharesPercentStr string, delegatorAddr, validatorAddr sdk.Address) (sharesAmount sdk.Rat, err error) { diff --git a/x/stake/client/rest/tx.go b/x/stake/client/rest/tx.go index 51a854528d..4475d8c480 100644 --- a/x/stake/client/rest/tx.go +++ b/x/stake/client/rest/tx.go @@ -65,6 +65,8 @@ type EditDelegationsBody struct { CompleteRedelegates []msgCompleteRedelegateInput `json:"complete_redelegates"` } +// nolint: gocyclo +// TODO: Split this up into several smaller functions, and remove the above nolint func editDelegationsRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var m EditDelegationsBody diff --git a/x/stake/keeper/validator.go b/x/stake/keeper/validator.go index e8713c04d5..69ca032f4e 100644 --- a/x/stake/keeper/validator.go +++ b/x/stake/keeper/validator.go @@ -194,6 +194,8 @@ func (k Keeper) ClearTendermintUpdates(ctx sdk.Context) { // perfom all the nessisary steps for when a validator changes its power // updates all validator stores as well as tendermint update store // may kick out validators if new validator is entering the bonded validator group +// nolint: gocyclo +// TODO: Remove above nolint, function needs to be simplified func (k Keeper) UpdateValidator(ctx sdk.Context, validator types.Validator) types.Validator { store := ctx.KVStore(k.storeKey) pool := k.GetPool(ctx)