Skip to content

Commit

Permalink
fix sync error
Browse files Browse the repository at this point in the history
  • Loading branch information
aquarius-kuchain committed Nov 25, 2020
1 parent e020ab2 commit c923db1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 75 deletions.
23 changes: 10 additions & 13 deletions x/distribution/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 2 additions & 24 deletions x/distribution/keeper/msg_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
46 changes: 8 additions & 38 deletions x/distribution/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c923db1

Please sign in to comment.