Skip to content

Commit

Permalink
reconcile: unstake supplier CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Feb 14, 2024
1 parent 2168765 commit f891ac8
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
42 changes: 42 additions & 0 deletions x/supplier/module/tx_unstake_supplier.go
Original file line number Diff line number Diff line change
@@ -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
}
97 changes: 97 additions & 0 deletions x/supplier/module/tx_unstake_supplier_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit f891ac8

Please sign in to comment.