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

allow current proposer or challenger can do update operation #11

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions proto/opinit/ophost/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 0 additions & 11 deletions x/ophost/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -50,8 +49,6 @@ var ModuleBasics = module.NewBasicManager(
)

var (
valPubKeys = testutilsims.CreateTestPubKeys(5)

pubKeys = []crypto.PubKey{
secp256k1.GenPrivKey().PubKey(),
secp256k1.GenPrivKey().PubKey(),
Expand All @@ -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",
Expand Down
16 changes: 10 additions & 6 deletions x/ophost/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,18 @@ 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)
if err != nil {
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
Expand All @@ -333,16 +335,18 @@ 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)
if err != nil {
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
Expand Down
20 changes: 20 additions & 0 deletions x/ophost/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions x/ophost/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading