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

feat: chain manager cron allow managing schedules #ntrn-387 #697

Closed
wants to merge 9 commits into from
Binary file modified contracts/neutron_chain_manager.wasm
Binary file not shown.
4 changes: 0 additions & 4 deletions wasmbinding/bindings/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ type NeutronMsg struct {
/// It allows the overwriting of the denom metadata in the bank module.
SetDenomMetadata *SetDenomMetadata `json:"set_denom_metadata,omitempty"`

// Cron types
AddSchedule *AddSchedule `json:"add_schedule,omitempty"`
RemoveSchedule *RemoveSchedule `json:"remove_schedule,omitempty"`

// Contractmanager types
/// A contract that has failed acknowledgement can resubmit it
ResubmitFailure *ResubmitFailure `json:"resubmit_failure,omitempty"`
Expand Down
89 changes: 0 additions & 89 deletions wasmbinding/message_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,6 @@ func (m *CustomMessenger) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAddre
return m.setDenomMetadata(ctx, contractAddr, contractMsg.SetDenomMetadata)
}

if contractMsg.AddSchedule != nil {
return m.addSchedule(ctx, contractAddr, contractMsg.AddSchedule)
}
if contractMsg.RemoveSchedule != nil {
return m.removeSchedule(ctx, contractAddr, contractMsg.RemoveSchedule)
}
if contractMsg.ResubmitFailure != nil {
return m.resubmitFailure(ctx, contractAddr, contractMsg.ResubmitFailure)
}
Expand Down Expand Up @@ -986,79 +980,6 @@ func (m *CustomMessenger) isLegacyProposal(proposal *bindings.AdminProposal) boo
}
}

func (m *CustomMessenger) addSchedule(ctx sdk.Context, contractAddr sdk.AccAddress, addSchedule *bindings.AddSchedule) ([]sdk.Event, [][]byte, [][]*types.Any, error) {
if !m.isAdmin(ctx, contractAddr) {
return nil, nil, nil, errors.Wrap(sdkerrors.ErrUnauthorized, "only admin can add schedule")
}

authority := authtypes.NewModuleAddress(admintypes.ModuleName)

msgs := make([]crontypes.MsgExecuteContract, 0, len(addSchedule.Msgs))
for _, msg := range addSchedule.Msgs {
msgs = append(msgs, crontypes.MsgExecuteContract{
Contract: msg.Contract,
Msg: msg.Msg,
})
}

_, err := m.CronMsgServer.AddSchedule(ctx, &crontypes.MsgAddSchedule{
Authority: authority.String(),
Name: addSchedule.Name,
Period: addSchedule.Period,
Msgs: msgs,
ExecutionStage: crontypes.ExecutionStage(crontypes.ExecutionStage_value[addSchedule.ExecutionStage]),
})
if err != nil {
ctx.Logger().Error("failed to addSchedule",
"from_address", contractAddr.String(),
"name", addSchedule.Name,
"error", err,
)
return nil, nil, nil, errors.Wrapf(err, "failed to add %s schedule", addSchedule.Name)
}

ctx.Logger().Debug("schedule added",
"from_address", contractAddr.String(),
"name", addSchedule.Name,
"period", addSchedule.Period,
)

return nil, nil, nil, nil
}

func (m *CustomMessenger) removeSchedule(ctx sdk.Context, contractAddr sdk.AccAddress, removeSchedule *bindings.RemoveSchedule) ([]sdk.Event, [][]byte, [][]*types.Any, error) {
params, err := m.CronQueryServer.Params(ctx, &crontypes.QueryParamsRequest{})
if err != nil {
ctx.Logger().Error("failed to removeSchedule", "error", err)
return nil, nil, nil, errors.Wrap(err, "failed to removeSchedule")
}

if !m.isAdmin(ctx, contractAddr) && contractAddr.String() != params.Params.SecurityAddress {
return nil, nil, nil, errors.Wrap(sdkerrors.ErrUnauthorized, "only admin or security dao can remove schedule")
}

authority := authtypes.NewModuleAddress(admintypes.ModuleName)

_, err = m.CronMsgServer.RemoveSchedule(ctx, &crontypes.MsgRemoveSchedule{
Authority: authority.String(),
Name: removeSchedule.Name,
})
if err != nil {
ctx.Logger().Error("failed to removeSchedule",
"from_address", contractAddr.String(),
"name", removeSchedule.Name,
"error", err,
)
return nil, nil, nil, errors.Wrapf(err, "failed to remove %s schedule", removeSchedule.Name)
}

ctx.Logger().Debug("schedule removed",
"from_address", contractAddr.String(),
"name", removeSchedule.Name,
)
return nil, nil, nil, nil
}

func (m *CustomMessenger) resubmitFailure(ctx sdk.Context, contractAddr sdk.AccAddress, resubmitFailure *bindings.ResubmitFailure) ([]sdk.Event, [][]byte, [][]*types.Any, error) {
failure, err := m.ContractmanagerKeeper.GetFailure(ctx, contractAddr, resubmitFailure.FailureId)
if err != nil {
Expand Down Expand Up @@ -1092,16 +1013,6 @@ func (m *CustomMessenger) resubmitFailure(ctx sdk.Context, contractAddr sdk.AccA
return nil, [][]byte{data}, msgResponses, nil
}

func (m *CustomMessenger) isAdmin(ctx sdk.Context, contractAddr sdk.AccAddress) bool {
for _, admin := range m.AdminKeeper.GetAdmins(ctx) {
if admin == contractAddr.String() {
return true
}
}

return false
}

func getRegisterFee(fee sdk.Coins) sdk.Coins {
if fee == nil {
return make(sdk.Coins, 0)
Expand Down
43 changes: 2 additions & 41 deletions wasmbinding/test/custom_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"fmt"
"testing"

contractmanagertypes "github.com/neutron-org/neutron/v4/x/contractmanager/types"
types2 "github.com/neutron-org/neutron/v4/x/cron/types"

"cosmossdk.io/math"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
admintypes "github.com/cosmos/admin-module/v2/x/adminmodule/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

contractmanagertypes "github.com/neutron-org/neutron/v4/x/contractmanager/types"

keeper2 "github.com/neutron-org/neutron/v4/x/contractmanager/keeper"
feeburnertypes "github.com/neutron-org/neutron/v4/x/feeburner/types"

Expand Down Expand Up @@ -693,44 +692,6 @@ func (suite *CustomMessengerTestSuite) TestNoProposals() {
suite.ErrorContains(err, "no admin proposal type is present in message")
}

func (suite *CustomMessengerTestSuite) TestAddRemoveSchedule() {
// Set admin so that we can execute this proposal without permission error
suite.neutron.AdminmoduleKeeper.SetAdmin(suite.ctx, suite.contractAddress.String())

// Craft AddSchedule message
msg := bindings.NeutronMsg{
AddSchedule: &bindings.AddSchedule{
Name: "schedule1",
Period: 5,
Msgs: []bindings.MsgExecuteContract{
{
Contract: suite.contractAddress.String(),
Msg: "{\"send\": { \"to\": \"asdf\", \"amount\": 1000 }}",
},
},
},
}

// Dispatch AddSchedule message
_, err := suite.executeNeutronMsg(suite.contractAddress, msg)
suite.NoError(err)

// Craft RemoveSchedule message
msg = bindings.NeutronMsg{
RemoveSchedule: &bindings.RemoveSchedule{
Name: "schedule1",
},
}

schedule, ok := suite.neutron.CronKeeper.GetSchedule(suite.ctx, "schedule1")
suite.True(ok)
suite.Equal(types2.ExecutionStage_EXECUTION_STAGE_END_BLOCKER, schedule.ExecutionStage)

// Dispatch AddSchedule message
_, err = suite.executeNeutronMsg(suite.contractAddress, msg)
suite.NoError(err)
}

func (suite *CustomMessengerTestSuite) TestResubmitFailureAck() {
// Add failure
packet := ibcchanneltypes.Packet{}
Expand Down
Loading