From 084673d36e47259b704e7b4f20a13cf243f3abfb Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Fri, 8 Sep 2023 15:51:39 +0900 Subject: [PATCH 1/5] Add update-censorship to tx cli --- x/foundation/client/cli/tx.go | 57 ++++++++++++++++++++++++++++++ x/foundation/client/testutil/tx.go | 55 ++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/x/foundation/client/cli/tx.go b/x/foundation/client/cli/tx.go index bc4eddad12..4d5f5bbb54 100644 --- a/x/foundation/client/cli/tx.go +++ b/x/foundation/client/cli/tx.go @@ -101,6 +101,15 @@ func voteOptionFromString(str string) (foundation.VoteOption, error) { return foundation.VoteOption(vo), nil } +// CensorshipAuthorityFromString returns a CensorshipAuthority from a string. It returns an error if the string is invalid. +func censorshipAuthorityFromString(str string) (foundation.CensorshipAuthority, error) { + ca, ok := foundation.CensorshipAuthority_value[str] + if !ok { + return foundation.CensorshipAuthorityUnspecified, fmt.Errorf("'%s' is not a valid censorship authority", str) + } + return foundation.CensorshipAuthority(ca), nil +} + func parseMsgs(cdc codec.Codec, msgsJSON string) ([]sdk.Msg, error) { var cliMsgs []json.RawMessage if err := json.Unmarshal([]byte(msgsJSON), &cliMsgs); err != nil { @@ -549,6 +558,54 @@ func NewTxCmdExec() *cobra.Command { return cmd } +func NewTxCmdUpdateCensorship() *cobra.Command { + cmd := &cobra.Command{ + Use: "update-censorship [authority] [msg-type-url] [new-authority]", + Args: cobra.ExactArgs(3), + Short: "Update censorship over a message", + Long: `Update censorship over a message + +Parameters: + authority: the current authority of the censorship + msg-type-url: the message type url of the censorship + new-authority: a new authority of the censorship + CENSORSHIP_AUTHORITY_UNSPECIFIED: no authority, which means removing the censorship + CENSORSHIP_AUTHORITY_GOVERNANCE: x/gov + CENSORSHIP_AUTHORITY_FOUNDATION: x/foundation +`, + RunE: func(cmd *cobra.Command, args []string) error { + if err := validateGenerateOnly(cmd); err != nil { + return err + } + + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + newAuthority, err := censorshipAuthorityFromString(args[2]) + if err != nil { + return err + } + + msg := foundation.MsgUpdateCensorship{ + Authority: args[0], + Censorship: foundation.Censorship{ + MsgTypeUrl: args[1], + Authority: newAuthority, + }, + } + if err := msg.ValidateBasic(); err != nil { + return err + } + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + return cmd +} + func NewTxCmdLeaveFoundation() *cobra.Command { cmd := &cobra.Command{ Use: "leave-foundation [address]", diff --git a/x/foundation/client/testutil/tx.go b/x/foundation/client/testutil/tx.go index 36f7169dc6..a8d62f630e 100644 --- a/x/foundation/client/testutil/tx.go +++ b/x/foundation/client/testutil/tx.go @@ -420,6 +420,61 @@ func (s *IntegrationTestSuite) TestNewTxCmdExec() { } } +func (s *IntegrationTestSuite) TestNewTxCmdUpdateCensorship() { + val := s.network.Validators[0] + commonArgs := []string{ + fmt.Sprintf("--%s", flags.FlagGenerateOnly), + } + + testCases := map[string]struct { + args []string + valid bool + }{ + "valid transaction": { + []string{ + s.authority.String(), + foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), + foundation.CensorshipAuthorityGovernance.String(), + }, + true, + }, + "wrong number of args": { + []string{ + s.authority.String(), + foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), + foundation.CensorshipAuthorityGovernance.String(), + "extra", + }, + false, + }, + "invalid new authority": { + []string{ + s.authority.String(), + foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), + "invalid-new-authority", + }, + false, + }, + } + + for name, tc := range testCases { + tc := tc + + s.Run(name, func() { + cmd := cli.NewTxCmdUpdateCensorship() + out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) + if !tc.valid { + s.Require().Error(err) + return + } + s.Require().NoError(err) + + var res txtypes.Tx + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + }) + } +} + func (s *IntegrationTestSuite) TestNewTxCmdLeaveFoundation() { val := s.network.Validators[0] commonArgs := []string{ From 02af9c0425b76a09e1a68ec0447d2a460a8c9b44 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Fri, 8 Sep 2023 16:20:59 +0900 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc328bf36d..b704aa412e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * (third_party/proto) [\#1037](https://github.com/Finschia/finschia-sdk/pull/1037) change the proof.proto path to third_party/proto/confio * (ostracon) [\#1057](https://github.com/Finschia/finschia-sdk/pull/1057) Bump up Ostracon from to v1.1.1 +* (feat) [\#1121](https://github.com/Finschia/finschia-sdk/pull/1121) Add update-censorship cmd to x/foundation cli ### Bug Fixes * (ledger) [\#1040](https://github.com/Finschia/finschia-sdk/pull/1040) fix a bug(unable to connect nano S plus ledger on ubuntu) @@ -59,4 +60,4 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Document Updates * (readme) [\#997](https://github.com/finschia/finschia-sdk/pull/997) fix swagger url -* (docs) [\#1059](https://github.com/Finschia/finschia-sdk/pull/1059) create ERRORS.md for x/module \ No newline at end of file +* (docs) [\#1059](https://github.com/Finschia/finschia-sdk/pull/1059) create ERRORS.md for x/module From 073fdbe9190ea39c4105e5efc13189962e879237 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Fri, 8 Sep 2023 16:51:30 +0900 Subject: [PATCH 3/5] Lint --- x/foundation/client/cli/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/foundation/client/cli/tx.go b/x/foundation/client/cli/tx.go index 4d5f5bbb54..6ae7c0cc42 100644 --- a/x/foundation/client/cli/tx.go +++ b/x/foundation/client/cli/tx.go @@ -589,7 +589,7 @@ Parameters: } msg := foundation.MsgUpdateCensorship{ - Authority: args[0], + Authority: args[0], Censorship: foundation.Censorship{ MsgTypeUrl: args[1], Authority: newAuthority, From 0e93f94abe91676790b3116e60a4c59ecc55fa9c Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Mon, 11 Sep 2023 08:44:38 +0000 Subject: [PATCH 4/5] Add normalizer --- x/foundation/client/cli/tx.go | 25 +++++++++++++++++++++---- x/foundation/client/testutil/tx.go | 8 ++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/x/foundation/client/cli/tx.go b/x/foundation/client/cli/tx.go index 6ae7c0cc42..3fee54bfbe 100644 --- a/x/foundation/client/cli/tx.go +++ b/x/foundation/client/cli/tx.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "strconv" + "strings" "github.com/spf13/cobra" @@ -91,6 +92,22 @@ func execFromString(execStr string) foundation.Exec { return exec } +func normalizeCensorshipAuthority(option string) string { + prefix := getEnumPrefix(foundation.CensorshipAuthority_name[0]) + candidate := strings.ToUpper(prefix + option) + if _, ok := foundation.CensorshipAuthority_value[candidate]; ok { + return candidate + } + + return option +} + +func getEnumPrefix(str string) string { + delimiter := "_" + splitted := strings.Split(str, delimiter) + return strings.Join(splitted[:len(splitted) - 1], delimiter) + delimiter +} + // VoteOptionFromString returns a VoteOption from a string. It returns an error // if the string is invalid. func voteOptionFromString(str string) (foundation.VoteOption, error) { @@ -569,9 +586,9 @@ Parameters: authority: the current authority of the censorship msg-type-url: the message type url of the censorship new-authority: a new authority of the censorship - CENSORSHIP_AUTHORITY_UNSPECIFIED: no authority, which means removing the censorship - CENSORSHIP_AUTHORITY_GOVERNANCE: x/gov - CENSORSHIP_AUTHORITY_FOUNDATION: x/foundation + unspecified: no authority, which means removing the censorship + governance: x/gov + foundation: x/foundation `, RunE: func(cmd *cobra.Command, args []string) error { if err := validateGenerateOnly(cmd); err != nil { @@ -583,7 +600,7 @@ Parameters: return err } - newAuthority, err := censorshipAuthorityFromString(args[2]) + newAuthority, err := censorshipAuthorityFromString(normalizeCensorshipAuthority(args[2])) if err != nil { return err } diff --git a/x/foundation/client/testutil/tx.go b/x/foundation/client/testutil/tx.go index a8d62f630e..ccc283cb8c 100644 --- a/x/foundation/client/testutil/tx.go +++ b/x/foundation/client/testutil/tx.go @@ -438,6 +438,14 @@ func (s *IntegrationTestSuite) TestNewTxCmdUpdateCensorship() { }, true, }, + "valid abbreviation": { + []string{ + s.authority.String(), + foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), + "governance", + }, + true, + }, "wrong number of args": { []string{ s.authority.String(), From aaf70fefcfaa18910bf96bcbdcc00ab3982e2130 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Mon, 11 Sep 2023 15:06:45 +0000 Subject: [PATCH 5/5] Lint --- x/foundation/client/cli/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/foundation/client/cli/tx.go b/x/foundation/client/cli/tx.go index 3fee54bfbe..e654e0f887 100644 --- a/x/foundation/client/cli/tx.go +++ b/x/foundation/client/cli/tx.go @@ -105,7 +105,7 @@ func normalizeCensorshipAuthority(option string) string { func getEnumPrefix(str string) string { delimiter := "_" splitted := strings.Split(str, delimiter) - return strings.Join(splitted[:len(splitted) - 1], delimiter) + delimiter + return strings.Join(splitted[:len(splitted)-1], delimiter) + delimiter } // VoteOptionFromString returns a VoteOption from a string. It returns an error