From efa4ec131cfd508c9a508270bae6088f2470d14d Mon Sep 17 00:00:00 2001 From: BabyHalimao Date: Mon, 27 May 2024 15:38:02 +0800 Subject: [PATCH] refactor: reuse calculated sum --- x/accounts/defaults/multisig/account.go | 5 +++-- x/accounts/defaults/multisig/update_config.go | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/x/accounts/defaults/multisig/account.go b/x/accounts/defaults/multisig/account.go index d6fadbfabff9..62dbea15c3ba 100644 --- a/x/accounts/defaults/multisig/account.go +++ b/x/accounts/defaults/multisig/account.go @@ -395,10 +395,11 @@ func (a *Account) RegisterQueryHandlers(builder *accountstd.QueryBuilder) { func safeAdd(nums ...uint64) (uint64, error) { var sum uint64 for _, num := range nums { - if sum+num < sum { + if newSum := sum + num; newSum < sum { return 0, errors.New("overflow") + } else { + sum = newSum } - sum += num } return sum, nil } diff --git a/x/accounts/defaults/multisig/update_config.go b/x/accounts/defaults/multisig/update_config.go index d352d49926fa..b65b8b215213 100644 --- a/x/accounts/defaults/multisig/update_config.go +++ b/x/accounts/defaults/multisig/update_config.go @@ -43,11 +43,11 @@ func (a Account) UpdateConfig(ctx context.Context, msg *v1.MsgUpdateConfig) (*v1 // verify that the new set of members and config are valid // get the weight from the stored members totalWeight := uint64(0) + var addErr error err := a.Members.Walk(ctx, nil, func(_ []byte, value uint64) (stop bool, err error) { - var adderr error - totalWeight, adderr = safeAdd(totalWeight, value) - if adderr != nil { - return true, adderr + totalWeight, addErr = safeAdd(totalWeight, value) + if addErr != nil { + return true, addErr } return false, nil })