Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Check identical account flag #637

Merged
merged 4 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions plugins/account/client/cli/account_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/spf13/viper"

clientFlags "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"

"github.com/binance-chain/node/common/client"
"github.com/binance-chain/node/common/types"
Expand Down Expand Up @@ -44,6 +42,21 @@ func setAccountFlagsCmd(cdc *wire.Codec) *cobra.Command {
if err != nil {
return err
}
if !viper.GetBool(clientFlags.FlagOffline) {
acc, err := cliCtx.GetAccount(from)
abelliumnt marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
appAccount, ok := acc.(types.NamedAccount)
if !ok {
return fmt.Errorf("unexpected account type")
}
flags := appAccount.GetFlags()

if flags == accountFlags {
return fmt.Errorf("the specified account flags is identical to its current value")
}
}
// build message
msg := account.NewSetAccountFlagsMsg(from, accountFlags)
err = msg.ValidateBasic()
Expand Down Expand Up @@ -85,13 +98,6 @@ func enableMemoCheckFlagCmd(cdc *wire.Codec) *cobra.Command {
return err
}
} else {
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(authcmd.GetAccountDecoder(cdc))

if err := cliCtx.EnsureAccountExistsFromAddr(from); err != nil {
abelliumnt marked this conversation as resolved.
Show resolved Hide resolved
return err
}
acc, err := cliCtx.GetAccount(from)
if err != nil {
return err
Expand All @@ -102,7 +108,10 @@ func enableMemoCheckFlagCmd(cdc *wire.Codec) *cobra.Command {
}
flags = appAccount.GetFlags()
}
flags = flags | scripts.TransferMemoCheckerFlag
flags, err = enableFlag(flags, scripts.TransferMemoCheckerFlag)
if err != nil {
return err
}
// build message
msg := account.NewSetAccountFlagsMsg(from, flags)
err = msg.ValidateBasic()
Expand Down Expand Up @@ -144,13 +153,6 @@ func disableMemoCheckFlagCmd(cdc *wire.Codec) *cobra.Command {
return err
}
} else {
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(authcmd.GetAccountDecoder(cdc))

if err := cliCtx.EnsureAccountExistsFromAddr(from); err != nil {
return err
}
acc, err := cliCtx.GetAccount(from)
if err != nil {
return err
Expand All @@ -161,8 +163,10 @@ func disableMemoCheckFlagCmd(cdc *wire.Codec) *cobra.Command {
}
flags = appAccount.GetFlags()
}
invMemoCheck := ^scripts.TransferMemoCheckerFlag
flags = flags & invMemoCheck
flags, err = disableFlag(flags, scripts.TransferMemoCheckerFlag)
if err != nil {
return err
}
// build message
msg := account.NewSetAccountFlagsMsg(from, flags)
err = msg.ValidateBasic()
Expand Down
21 changes: 21 additions & 0 deletions plugins/account/client/cli/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cli

import "fmt"


func enableFlag(flags uint64, targetFlag uint64) (uint64, error) {
abelliumnt marked this conversation as resolved.
Show resolved Hide resolved
enabledFlags := flags | targetFlag
if flags == enabledFlags {
return 0, fmt.Errorf("flag %x has already been enabled", targetFlag)
}
return enabledFlags, nil
}

func disableFlag(flags uint64, targetFlag uint64) (uint64, error) {
inv := ^targetFlag
disabledFlags := flags & inv
if flags == disabledFlags {
return 0, fmt.Errorf("flag %x has already been disabled", targetFlag)
}
return disabledFlags, nil
}