From f891ac87122dd80ba2f15a759c59a8b9e90419d8 Mon Sep 17 00:00:00 2001 From: Bryan White Date: Wed, 14 Feb 2024 13:26:42 +0100 Subject: [PATCH] reconcile: unstake supplier CLI commands --- x/supplier/module/tx_unstake_supplier.go | 42 ++++++++ x/supplier/module/tx_unstake_supplier_test.go | 97 +++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 x/supplier/module/tx_unstake_supplier.go create mode 100644 x/supplier/module/tx_unstake_supplier_test.go diff --git a/x/supplier/module/tx_unstake_supplier.go b/x/supplier/module/tx_unstake_supplier.go new file mode 100644 index 000000000..9c5fa8633 --- /dev/null +++ b/x/supplier/module/tx_unstake_supplier.go @@ -0,0 +1,42 @@ +package supplier + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/spf13/cobra" + + "github.com/pokt-network/poktroll/x/supplier/types" +) + +func CmdUnstakeSupplier() *cobra.Command { + // fromAddress & signature is retrieved via `flags.FlagFrom` in the `clientCtx` + cmd := &cobra.Command{ + Use: "unstake-supplier", + Short: "Unstake a supplier", + Long: `Unstake an supplier with the provided parameters. This is a broadcast operation that will unstake the supplier specified by the 'from' address. + +Example: +$ poktrolld --home=$(POKTROLLD_HOME) tx supplier unstake-supplier --keyring-backend test --from $(SUPPLIER) --node $(POCKET_NODE)`, + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) (err error) { + + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + msg := types.NewMsgUnstakeSupplier( + clientCtx.GetFromAddress().String(), + ) + if err := msg.ValidateBasic(); err != nil { + return err + } + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} diff --git a/x/supplier/module/tx_unstake_supplier_test.go b/x/supplier/module/tx_unstake_supplier_test.go new file mode 100644 index 000000000..75644f58f --- /dev/null +++ b/x/supplier/module/tx_unstake_supplier_test.go @@ -0,0 +1,97 @@ +package supplier_test + +import ( + "fmt" + "testing" + + sdkerrors "cosmossdk.io/errors" + sdkmath "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/testutil" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/status" + + "github.com/pokt-network/poktroll/testutil/network" + supplier "github.com/pokt-network/poktroll/x/supplier/module" + "github.com/pokt-network/poktroll/x/supplier/types" +) + +func TestCLI_UnstakeSupplier(t *testing.T) { + net, _ := networkWithSupplierObjects(t, 2) + val := net.Validators[0] + ctx := val.ClientCtx + + // Create a keyring and add an account for the supplier to be unstaked + kr := ctx.Keyring + accounts := testutil.CreateKeyringAccounts(t, kr, 1) + supplierAccount := accounts[0] + + // Update the context with the new keyring + ctx = ctx.WithKeyring(kr) + + // Common args used for all requests + commonArgs := []string{ + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), + } + + tests := []struct { + desc string + address string + err *sdkerrors.Error + }{ + { + desc: "unstake supplier: valid", + address: supplierAccount.Address.String(), + }, + { + desc: "unstake supplier: missing address", + // address: supplierAccount.Address.String(), + err: types.ErrSupplierInvalidAddress, + }, + { + desc: "unstake supplier: invalid address", + address: "invalid", + err: types.ErrSupplierInvalidAddress, + }, + } + + // Initialize the Supplier Account by sending it some funds from the validator account that is part of genesis + network.InitAccount(t, net, supplierAccount.Address) + + // Run the tests + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + // Wait for a new block to be committed + require.NoError(t, net.WaitForNextBlock()) + + // Prepare the arguments for the CLI command + args := []string{ + fmt.Sprintf("--%s=%s", flags.FlagFrom, tt.address), + } + args = append(args, commonArgs...) + + // Execute the command + outUnstake, err := clitestutil.ExecTestCLICmd(ctx, supplier.CmdUnstakeSupplier(), args) + + // Validate the error if one is expected + if tt.err != nil { + stat, ok := status.FromError(tt.err) + require.True(t, ok) + require.Contains(t, stat.Message(), tt.err.Error()) + return + } + require.NoError(t, err) + + // Check the response + var resp sdk.TxResponse + require.NoError(t, net.Config.Codec.UnmarshalJSON(outUnstake.Bytes(), &resp)) + require.NotNil(t, resp) + require.NotNil(t, resp.TxHash) + require.Equal(t, uint32(0), resp.Code) + }) + } +}