diff --git a/x/distribution/handler.go b/x/distribution/handler.go index f10f0f2..22e2b2c 100644 --- a/x/distribution/handler.go +++ b/x/distribution/handler.go @@ -27,22 +27,19 @@ func NewHandler(k keeper.Keeper) msg.Handler { } } -// These functions assume everything has been authenticated (ValidateBasic passed, and signatures checked) -func handleMsgModifyWithdrawAccountId(ctx chainTypes.Context, msg types.MsgSetWithdrawAccountId, k keeper.Keeper) (*sdk.Result, error) { - checkAcc := func(acc AccountID) bool { - _, ok := acc.ToName() - if ok { - return k.AccKeeper.IsAccountExist(ctx.Context(), acc) - } - return false +func CheckIsFindAccount(ctx chainTypes.Context, k keeper.Keeper, acc AccountID) bool { + _, ok := acc.ToName() + if ok { + return k.AccKeeper.IsAccountExist(ctx.Context(), acc) } + return false +} - types.FindAcc = checkAcc - +// These functions assume everything has been authenticated (ValidateBasic passed, and signatures checked) +func handleMsgModifyWithdrawAccountId(ctx chainTypes.Context, msg types.MsgSetWithdrawAccountId, k keeper.Keeper) (*sdk.Result, error) { dataMsg, _ := msg.GetData() ctx.RequireAuth(dataMsg.DelegatorAccountid) - ExistOk := checkAcc(dataMsg.WithdrawAccountid) - if ExistOk { + if existOk := CheckIsFindAccount(ctx, k, dataMsg.WithdrawAccountid); existOk { err := k.SetWithdrawAddr(ctx.Context(), dataMsg.DelegatorAccountid, dataMsg.WithdrawAccountid) if err != nil { return nil, err @@ -58,7 +55,7 @@ func handleMsgModifyWithdrawAccountId(ctx chainTypes.Context, msg types.MsgSetWi return &sdk.Result{Events: ctx.EventManager().Events()}, nil } else { ctx.Logger().Error("handleMsgModifyWithdrawAccountId, not found", - "WithdrawAccountid", dataMsg.WithdrawAccountid, "ExistOk", ExistOk) + "WithdrawAccountid", dataMsg.WithdrawAccountid, "ExistOk", existOk) } return nil, types.ErrSetWithdrawAddrDisabled diff --git a/x/distribution/keeper/msg_test.go b/x/distribution/keeper/msg_test.go index e2935a2..c4e6414 100644 --- a/x/distribution/keeper/msg_test.go +++ b/x/distribution/keeper/msg_test.go @@ -1,9 +1,10 @@ package keeper import ( + "testing" + chainTypes "github.com/KuChainNetwork/kuchain/chain/types" "github.com/KuChainNetwork/kuchain/x/distribution/types" - "testing" "github.com/stretchr/testify/require" ) @@ -27,14 +28,6 @@ func TestMsgSetWithdrawAddress(t *testing.T) { {emptyAcc, emptyAcc, false}, } - types.FindAcc = func(acc chainTypes.AccountID) bool { - _, ok := acc.ToName() - if ok { - return ak.IsAccountExist(ctx, acc) - } - return false - } - for i, tc := range tests { Name, _ := tc.delegatorAddr.ToName() Auth, _ := ak.GetAuth(ctx, Name) @@ -65,13 +58,6 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { {Acc2, emptyAcc, false}, {emptyAcc, emptyAcc, false}, } - types.FindAcc = func(acc chainTypes.AccountID) bool { - _, ok := acc.ToName() - if ok { - return ak.IsAccountExist(ctx, acc) - } - return false - } for i, tc := range tests { Name, _ := tc.delegatorAddr.ToName() @@ -101,14 +87,6 @@ func TestMsgWithdrawValidatorCommission(t *testing.T) { {emptyAcc, false}, } - types.FindAcc = func(acc chainTypes.AccountID) bool { - _, ok := acc.ToName() - if ok { - return ak.IsAccountExist(ctx, acc) - } - return false - } - for i, tc := range tests { Name, _ := tc.validatorAddr.ToName() Auth, _ := ak.GetAuth(ctx, Name) diff --git a/x/distribution/types/msg.go b/x/distribution/types/msg.go index 863e4b7..53f83b6 100644 --- a/x/distribution/types/msg.go +++ b/x/distribution/types/msg.go @@ -8,10 +8,6 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) -type findAccount func(acc chainType.AccountID) bool - -var FindAcc findAccount - // Verify interface at compile time var _, _, _ sdk.Msg = &MsgSetWithdrawAccountId{}, &MsgWithdrawDelegatorReward{}, &MsgWithdrawValidatorCommission{} @@ -54,13 +50,9 @@ func (m MsgSetWithdrawAccountId) ValidateBasic() error { if data.DelegatorAccountid.Equal(&data.WithdrawAccountid) { return chainType.ErrKuMsgDataSameAccount } - - if FindAcc != nil { - if !FindAcc(data.WithdrawAccountid) { - return chainType.ErrKuMsgDataNotFindAccount - } - } } + } else { + return err } return m.KuMsg.ValidateTransfer() @@ -103,24 +95,9 @@ type MsgWithdrawDelegatorReward struct { } func (m MsgWithdrawDelegatorReward) ValidateBasic() error { - data, err := m.GetData() - if err == nil { - _, ok := data.DelegatorAccountId.ToName() - if ok { - if FindAcc != nil { - if !FindAcc(data.DelegatorAccountId) { - return chainType.ErrKuMsgDataNotFindAccount - } - } - } - _, ok = data.ValidatorAccountId.ToName() - if ok { - if FindAcc != nil { - if !FindAcc(data.ValidatorAccountId) { - return chainType.ErrKuMsgDataNotFindAccount - } - } - } + _, err := m.GetData() + if err != nil { + return err } return m.KuMsg.ValidateTransfer() @@ -178,16 +155,9 @@ func (m MsgWithdrawValidatorCommission) GetData() (MsgWithdrawValidatorCommissio } func (m MsgWithdrawValidatorCommission) ValidateBasic() error { - data, err := m.GetData() - if err == nil { - _, ok := data.ValidatorAccountId.ToName() - if ok { - if FindAcc != nil { - if !FindAcc(data.ValidatorAccountId) { - return chainType.ErrKuMsgDataNotFindAccount - } - } - } + _, err := m.GetData() + if err != nil { + return err } return m.KuMsg.ValidateTransfer()