Skip to content

Commit

Permalink
feat(network): param-change (#3142)
Browse files Browse the repository at this point in the history
* utd

* add command

* typo

* add test

* changelog

* format

* Update ignite/cmd/network_request_param_change.go

Co-authored-by: Lucas Bertrand <[email protected]>

* Update changelog.md

Co-authored-by: Lucas Bertrand <[email protected]>

Co-authored-by: Lucas Bertrand <[email protected]>
  • Loading branch information
Alex Johnson and lumtis authored Nov 21, 2022
1 parent a418768 commit 1295dec
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- [#3142](https://github.com/ignite/cli/pull/3142) Add `ignite network request param-change` command.
- [#3038](https://github.com/ignite/cli/pull/3038) Addition of Plugin Hooks in Plugin System
- [#3056](https://github.com/ignite/cli/pull/3056) Add `--genesis-config` flag option to `ignite network chain publish`
- [#2892](https://github.com/ignite/cli/pull/2982/) Add `ignite scaffold react` command.
Expand Down
1 change: 1 addition & 0 deletions ignite/cmd/network_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Each request has a status:
NewNetworkRequestAddAccount(),
NewNetworkRequestRemoveAccount(),
NewNetworkRequestRemoveValidator(),
NewNetworkRequestParamChange(),
)

return c
Expand Down
58 changes: 58 additions & 0 deletions ignite/cmd/network_request_param_change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ignitecmd

import (
"github.com/spf13/cobra"

"github.com/ignite/cli/ignite/pkg/cliui"
"github.com/ignite/cli/ignite/services/network"
)

// NewNetworkRequestParamChange creates a new command to send param change request
func NewNetworkRequestParamChange() *cobra.Command {
c := &cobra.Command{
Use: "param-change [launch-id] [module-name] [param-name] [value (json, string, number)]",
Short: "Send request to change param",
RunE: networkRequestParamChangeHandler,
Args: cobra.ExactArgs(4),
}

flagSetClearCache(c)
c.Flags().AddFlagSet(flagNetworkFrom())
c.Flags().AddFlagSet(flagSetHome())
c.Flags().AddFlagSet(flagSetKeyringBackend())
c.Flags().AddFlagSet(flagSetKeyringDir())
return c
}

func networkRequestParamChangeHandler(cmd *cobra.Command, args []string) error {
session := cliui.New(cliui.StartSpinner())
defer session.End()

nb, err := newNetworkBuilder(cmd, CollectEvents(session.EventBus()))
if err != nil {
return err
}

// parse launch ID
launchID, err := network.ParseID(args[0])
if err != nil {
return err
}

module := args[1]
param := args[2]
value := []byte(args[3])

n, err := nb.Network()
if err != nil {
return err
}

return n.SendParamChangeRequest(
cmd.Context(),
launchID,
module,
param,
value,
)
}
49 changes: 49 additions & 0 deletions ignite/services/network/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,52 @@ func (n Network) SendAccountRemoveRequest(
}
return nil
}

// SendParamChangeRequest creates the RequestParamChange message to SPN
func (n Network) SendParamChangeRequest(
ctx context.Context,
launchID uint64,
module,
param string,
value []byte,
) error {
addr, err := n.account.Address(networktypes.SPN)
if err != nil {
return err
}

msg := launchtypes.NewMsgSendRequest(
addr,
launchID,
launchtypes.NewParamChange(
launchID,
module,
param,
value,
),
)

n.ev.Send("Broadcasting transaction", events.ProgressStart())

res, err := n.cosmos.BroadcastTx(ctx, n.account, msg)
if err != nil {
return err
}

var requestRes launchtypes.MsgSendRequestResponse
if err := res.Decode(&requestRes); err != nil {
return err
}

if requestRes.AutoApproved {
n.ev.Send("Param changed on network by the coordinator!", events.ProgressFinish())
} else {
n.ev.Send(
fmt.Sprintf(
"Request %d to change param on the network has been submitted!", requestRes.RequestID,
),
events.ProgressFinish(),
)
}
return nil
}
53 changes: 53 additions & 0 deletions ignite/services/network/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package network

import (
"context"
"testing"

"github.com/stretchr/testify/require"
launchtypes "github.com/tendermint/spn/x/launch/types"

"github.com/ignite/cli/ignite/services/network/networktypes"
"github.com/ignite/cli/ignite/services/network/testutil"
)

func TestRequestParamChange(t *testing.T) {
t.Run("successfully send request", func(t *testing.T) {
var (
account = testutil.NewTestAccount(t, testutil.TestAccountName)
suite, network = newSuite(account)
module = "module"
param = "param"
value = []byte("value")
)

addr, err := account.Address(networktypes.SPN)
require.NoError(t, err)

suite.CosmosClientMock.
On(
"BroadcastTx",
context.Background(),
account,
launchtypes.NewMsgSendRequest(
addr,
testutil.LaunchID,
launchtypes.NewParamChange(
testutil.LaunchID,
module,
param,
value,
),
),
).
Return(testutil.NewResponse(&launchtypes.MsgSendRequestResponse{
RequestID: 0,
AutoApproved: false,
}), nil).
Once()

sendRequestError := network.SendParamChangeRequest(context.Background(), testutil.LaunchID, module, param, value)
require.NoError(t, sendRequestError)
suite.AssertAllMocks(t)
})
}
30 changes: 15 additions & 15 deletions ignite/services/network/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
func TestSetReward(t *testing.T) {
t.Run("successfully set reward", func(t *testing.T) {
var (
account = testutil.NewTestAccount(t, testutil.TestAccountName)
suite, network = newSuite(account)
coins = sdk.NewCoins(sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)))
lastRewarHeight = int64(10)
account = testutil.NewTestAccount(t, testutil.TestAccountName)
suite, network = newSuite(account)
coins = sdk.NewCoins(sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)))
lastRewardHeight = int64(10)
)

addr, err := account.Address(networktypes.SPN)
Expand All @@ -35,28 +35,28 @@ func TestSetReward(t *testing.T) {
Provider: addr,
LaunchID: testutil.LaunchID,
Coins: coins,
LastRewardHeight: lastRewarHeight,
LastRewardHeight: lastRewardHeight,
},
).
Return(testutil.NewResponse(&rewardtypes.MsgSetRewardsResponse{
PreviousCoins: nil,
PreviousLastRewardHeight: lastRewarHeight - 1,
PreviousLastRewardHeight: lastRewardHeight - 1,
NewCoins: coins,
NewLastRewardHeight: lastRewarHeight,
NewLastRewardHeight: lastRewardHeight,
}), nil).
Once()

setRewardError := network.SetReward(context.Background(), testutil.LaunchID, lastRewarHeight, coins)
setRewardError := network.SetReward(context.Background(), testutil.LaunchID, lastRewardHeight, coins)
require.NoError(t, setRewardError)
suite.AssertAllMocks(t)
})
t.Run("failed to set reward, failed to broadcast set reward tx", func(t *testing.T) {
var (
account = testutil.NewTestAccount(t, testutil.TestAccountName)
suite, network = newSuite(account)
coins = sdk.NewCoins(sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)))
lastRewarHeight = int64(10)
expectedErr = errors.New("failed to set reward")
account = testutil.NewTestAccount(t, testutil.TestAccountName)
suite, network = newSuite(account)
coins = sdk.NewCoins(sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)))
lastRewardHeight = int64(10)
expectedErr = errors.New("failed to set reward")
)

addr, err := account.Address(networktypes.SPN)
Expand All @@ -71,12 +71,12 @@ func TestSetReward(t *testing.T) {
Provider: addr,
LaunchID: testutil.LaunchID,
Coins: coins,
LastRewardHeight: lastRewarHeight,
LastRewardHeight: lastRewardHeight,
},
).
Return(testutil.NewResponse(&rewardtypes.MsgSetRewardsResponse{}), expectedErr).
Once()
setRewardError := network.SetReward(context.Background(), testutil.LaunchID, lastRewarHeight, coins)
setRewardError := network.SetReward(context.Background(), testutil.LaunchID, lastRewardHeight, coins)
require.Error(t, setRewardError)
require.Equal(t, expectedErr, setRewardError)
suite.AssertAllMocks(t)
Expand Down

0 comments on commit 1295dec

Please sign in to comment.