From 7b765f8ad7266b47a3ad3572f30aa2318c75d586 Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Tue, 19 Dec 2023 12:01:27 +0900 Subject: [PATCH] allow current proposer or challenger can do update operations --- proto/opinit/ophost/v1/tx.proto | 8 ++++---- x/ophost/keeper/common_test.go | 11 ----------- x/ophost/keeper/msg_server.go | 16 ++++++++++------ x/ophost/keeper/msg_server_test.go | 20 ++++++++++++++++++++ x/ophost/types/tx.pb.go | 8 ++++---- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/proto/opinit/ophost/v1/tx.proto b/proto/opinit/ophost/v1/tx.proto index 91e4190b..46ded850 100644 --- a/proto/opinit/ophost/v1/tx.proto +++ b/proto/opinit/ophost/v1/tx.proto @@ -198,8 +198,8 @@ message MsgUpdateProposer { option (cosmos.msg.v1.signer) = "authority"; option (amino.name) = "ophost/MsgChallenge"; - // authority is the address that controls the module - // (defaults to x/gov unless overwritten). + // authority is the address that controls the module (defaults to x/gov unless overwritten) + // or the current proposer address. string authority = 1 [(gogoproto.moretags) = "yaml:\"authority\"", (cosmos_proto.scalar) = "cosmos.AddressString"]; uint64 bridge_id = 2 [(gogoproto.moretags) = "yaml:\"bridge_id\""]; string new_proposer = 3 @@ -214,8 +214,8 @@ message MsgUpdateChallenger { option (cosmos.msg.v1.signer) = "authority"; option (amino.name) = "ophost/MsgChallenge"; - // authority is the address that controls the module - // (defaults to x/gov unless overwritten). + // authority is the address that controls the module (defaults to x/gov unless overwritten) + // or the current challenger address. string authority = 1 [(gogoproto.moretags) = "yaml:\"authority\"", (cosmos_proto.scalar) = "cosmos.AddressString"]; uint64 bridge_id = 2 [(gogoproto.moretags) = "yaml:\"bridge_id\""]; string new_challenger = 3 diff --git a/x/ophost/keeper/common_test.go b/x/ophost/keeper/common_test.go index 5bda556d..32af27a6 100644 --- a/x/ophost/keeper/common_test.go +++ b/x/ophost/keeper/common_test.go @@ -21,7 +21,6 @@ import ( "github.com/cosmos/cosmos-sdk/std" "github.com/cosmos/cosmos-sdk/store" storetypes "github.com/cosmos/cosmos-sdk/store/types" - testutilsims "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/auth" @@ -50,8 +49,6 @@ var ModuleBasics = module.NewBasicManager( ) var ( - valPubKeys = testutilsims.CreateTestPubKeys(5) - pubKeys = []crypto.PubKey{ secp256k1.GenPrivKey().PubKey(), secp256k1.GenPrivKey().PubKey(), @@ -68,14 +65,6 @@ var ( sdk.AccAddress(pubKeys[4].Address()), } - valAddrs = []sdk.ValAddress{ - sdk.ValAddress(pubKeys[0].Address()), - sdk.ValAddress(pubKeys[1].Address()), - sdk.ValAddress(pubKeys[2].Address()), - sdk.ValAddress(pubKeys[3].Address()), - sdk.ValAddress(pubKeys[4].Address()), - } - testDenoms = []string{ "test1", "test2", diff --git a/x/ophost/keeper/msg_server.go b/x/ophost/keeper/msg_server.go index 01955388..bf0169a2 100644 --- a/x/ophost/keeper/msg_server.go +++ b/x/ophost/keeper/msg_server.go @@ -309,9 +309,6 @@ func (ms MsgServer) FinalizeTokenWithdrawal(context context.Context, req *types. func (ms MsgServer) UpdateProposer(context context.Context, req *types.MsgUpdateProposer) (*types.MsgUpdateProposerResponse, error) { ctx := sdk.UnwrapSDKContext(context) - if ms.authority != req.Authority { - return nil, govtypes.ErrInvalidSigner.Wrapf("invalid authority; expected %s, got %s", ms.authority, req.Authority) - } bridgeId := req.BridgeId config, err := ms.GetBridgeConfig(ctx, bridgeId) @@ -319,6 +316,11 @@ func (ms MsgServer) UpdateProposer(context context.Context, req *types.MsgUpdate return nil, err } + // gov or current proposer can update proposer. + if ms.authority != req.Authority && config.Proposer != req.Authority { + return nil, govtypes.ErrInvalidSigner.Wrapf("invalid authority; expected %s or %s, got %s", ms.authority, config.Proposer, req.Authority) + } + config.Proposer = req.NewProposer if err := ms.Keeper.bridgeHook.BridgeProposerUpdated(ctx, bridgeId, config); err != nil { return nil, err @@ -333,9 +335,6 @@ func (ms MsgServer) UpdateProposer(context context.Context, req *types.MsgUpdate func (ms MsgServer) UpdateChallenger(context context.Context, req *types.MsgUpdateChallenger) (*types.MsgUpdateChallengerResponse, error) { ctx := sdk.UnwrapSDKContext(context) - if ms.authority != req.Authority { - return nil, govtypes.ErrInvalidSigner.Wrapf("invalid authority; expected %s, got %s", ms.authority, req.Authority) - } bridgeId := req.BridgeId config, err := ms.GetBridgeConfig(ctx, bridgeId) @@ -343,6 +342,11 @@ func (ms MsgServer) UpdateChallenger(context context.Context, req *types.MsgUpda return nil, err } + // gov or current challenger can update challenger. + if ms.authority != req.Authority && config.Challenger != req.Authority { + return nil, govtypes.ErrInvalidSigner.Wrapf("invalid authority; expected %s or %s, got %s", ms.authority, config.Challenger, req.Authority) + } + config.Challenger = req.NewChallenger if err := ms.Keeper.bridgeHook.BridgeChallengerUpdated(ctx, bridgeId, config); err != nil { return nil, err diff --git a/x/ophost/keeper/msg_server_test.go b/x/ophost/keeper/msg_server_test.go index 01383986..16ac9ed0 100644 --- a/x/ophost/keeper/msg_server_test.go +++ b/x/ophost/keeper/msg_server_test.go @@ -213,6 +213,7 @@ func Test_UpdateProposal(t *testing.T) { _, err := ms.CreateBridge(sdk.WrapSDKContext(ctx), types.NewMsgCreateBridge(addrs[0], config)) require.NoError(t, err) + // gov signer msg := types.NewMsgUpdateProposer(authtypes.NewModuleAddress("gov"), 1, addrs[1]) _, err = ms.UpdateProposer(sdk.WrapSDKContext(ctx), msg) require.NoError(t, err) @@ -221,6 +222,15 @@ func Test_UpdateProposal(t *testing.T) { require.Equal(t, addrs[1].String(), _config.Proposer) require.Equal(t, addrs[1].String(), input.BridgeHook.proposer) + // current proposer signer + msg = types.NewMsgUpdateProposer(addrs[1], 1, addrs[2]) + _, err = ms.UpdateProposer(sdk.WrapSDKContext(ctx), msg) + require.NoError(t, err) + _config, err = ms.GetBridgeConfig(ctx, 1) + require.NoError(t, err) + require.Equal(t, addrs[2].String(), _config.Proposer) + require.Equal(t, addrs[2].String(), input.BridgeHook.proposer) + // invalid signer msg = types.NewMsgUpdateProposer(authtypes.NewModuleAddress(types.ModuleName), 1, addrs[1]) require.NoError(t, err) @@ -248,6 +258,7 @@ func Test_UpdateChallenger(t *testing.T) { _, err := ms.CreateBridge(sdk.WrapSDKContext(ctx), types.NewMsgCreateBridge(addrs[0], config)) require.NoError(t, err) + // gov signer msg := types.NewMsgUpdateChallenger(authtypes.NewModuleAddress("gov"), 1, addrs[2]) _, err = ms.UpdateChallenger(sdk.WrapSDKContext(ctx), msg) require.NoError(t, err) @@ -256,6 +267,15 @@ func Test_UpdateChallenger(t *testing.T) { require.Equal(t, addrs[2].String(), _config.Challenger) require.Equal(t, addrs[2].String(), input.BridgeHook.challenger) + // current challenger + msg = types.NewMsgUpdateChallenger(addrs[2], 1, addrs[3]) + _, err = ms.UpdateChallenger(sdk.WrapSDKContext(ctx), msg) + require.NoError(t, err) + _config, err = ms.GetBridgeConfig(ctx, 1) + require.NoError(t, err) + require.Equal(t, addrs[3].String(), _config.Challenger) + require.Equal(t, addrs[3].String(), input.BridgeHook.challenger) + // invalid signer msg = types.NewMsgUpdateChallenger(authtypes.NewModuleAddress(types.ModuleName), 1, addrs[1]) require.NoError(t, err) diff --git a/x/ophost/types/tx.pb.go b/x/ophost/types/tx.pb.go index e8f1b86a..6b8bf122 100644 --- a/x/ophost/types/tx.pb.go +++ b/x/ophost/types/tx.pb.go @@ -514,8 +514,8 @@ var xxx_messageInfo_MsgFinalizeTokenWithdrawalResponse proto.InternalMessageInfo // MsgUpdateProposer is a message to change a proposer type MsgUpdateProposer struct { - // authority is the address that controls the module - // (defaults to x/gov unless overwritten). + // authority is the address that controls the module (defaults to x/gov unless overwritten) + // or the current proposer address. Authority string `protobuf:"bytes,1,opt,name=authority,proto3" json:"authority,omitempty" yaml:"authority"` BridgeId uint64 `protobuf:"varint,2,opt,name=bridge_id,json=bridgeId,proto3" json:"bridge_id,omitempty" yaml:"bridge_id"` NewProposer string `protobuf:"bytes,3,opt,name=new_proposer,json=newProposer,proto3" json:"new_proposer,omitempty" yaml:"new_proposer"` @@ -593,8 +593,8 @@ var xxx_messageInfo_MsgUpdateProposerResponse proto.InternalMessageInfo // MsgUpdateChallenger is a message to change a challenger type MsgUpdateChallenger struct { - // authority is the address that controls the module - // (defaults to x/gov unless overwritten). + // authority is the address that controls the module (defaults to x/gov unless overwritten) + // or the current challenger address. Authority string `protobuf:"bytes,1,opt,name=authority,proto3" json:"authority,omitempty" yaml:"authority"` BridgeId uint64 `protobuf:"varint,2,opt,name=bridge_id,json=bridgeId,proto3" json:"bridge_id,omitempty" yaml:"bridge_id"` NewChallenger string `protobuf:"bytes,3,opt,name=new_challenger,json=newChallenger,proto3" json:"new_challenger,omitempty" yaml:"new_challenger"`