Skip to content

Commit

Permalink
add admin params and fix ophost to charge registration fee
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Apr 5, 2024
1 parent 8608273 commit 37f2247
Show file tree
Hide file tree
Showing 19 changed files with 739 additions and 452 deletions.
395 changes: 235 additions & 160 deletions api/opinit/opchild/v1/tx.pulsar.go

Large diffs are not rendered by default.

166 changes: 122 additions & 44 deletions api/opinit/opchild/v1/types.pulsar.go

Large diffs are not rendered by default.

206 changes: 103 additions & 103 deletions api/opinit/ophost/v1/tx.pulsar.go

Large diffs are not rendered by default.

119 changes: 60 additions & 59 deletions api/opinit/ophost/v1/types.pulsar.go

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

7 changes: 7 additions & 0 deletions proto/opinit/opchild/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ message Params {
(amino.dont_omitempty) = true,
(gogoproto.moretags) = "yaml:\"bridge_executor\""
];
// the account address of admin who can execute permissioned cosmos upgrade
// messages.
string admin = 5 [
(cosmos_proto.scalar) = "cosmos.AddressString",
(amino.dont_omitempty) = true,
(gogoproto.moretags) = "yaml:\"admin\""
];
}

// Validator defines a validator, together with the total amount of the
Expand Down
3 changes: 3 additions & 0 deletions x/opchild/client/cli/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ $ %s add-genesis-validator my-key-name --home=/path/to/home/dir --keyring-backen

opchildState := opchildtypes.GetGenesisStateFromAppState(cdc, appState)
opchildState.Validators = append((*opchildState).Validators, validator)
if opchildState.Params.Admin == "" {
opchildState.Params.Admin = addr.String()
}
if opchildState.Params.BridgeExecutor == "" {
opchildState.Params.BridgeExecutor = addr.String()
}
Expand Down
10 changes: 4 additions & 6 deletions x/opchild/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"cosmossdk.io/core/address"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -80,8 +79,8 @@ func NewDepositCmd(ac address.Codec) *cobra.Command {
}

txf, msg, err := newBuildDepositMsg(
clientCtx, ac, txf, cmd.Flags(),
sequence, from, to, amount, baseDenom,
clientCtx, ac, txf, sequence,
from, to, amount, baseDenom,
[]byte(hookMsg),
)
if err != nil {
Expand Down Expand Up @@ -124,7 +123,7 @@ func NewWithdrawCmd(ac address.Codec) *cobra.Command {
return err
}

txf, msg, err := newBuildWithdrawMsg(clientCtx, ac, txf, cmd.Flags(), to, amount)
txf, msg, err := newBuildWithdrawMsg(clientCtx, ac, txf, to, amount)
if err != nil {
return err
}
Expand Down Expand Up @@ -203,7 +202,7 @@ Where proposal.json contains:
return cmd
}

func newBuildWithdrawMsg(clientCtx client.Context, ac address.Codec, txf tx.Factory, fs *flag.FlagSet, to sdk.AccAddress, amount sdk.Coin) (tx.Factory, *types.MsgInitiateTokenWithdrawal, error) {
func newBuildWithdrawMsg(clientCtx client.Context, ac address.Codec, txf tx.Factory, to sdk.AccAddress, amount sdk.Coin) (tx.Factory, *types.MsgInitiateTokenWithdrawal, error) {
sender := clientCtx.GetFromAddress()
senderAddr, err := ac.BytesToString(sender)
if err != nil {
Expand All @@ -227,7 +226,6 @@ func newBuildDepositMsg(
clientCtx client.Context,
ac address.Codec,
txf tx.Factory,
fs *flag.FlagSet,
sequence uint64,
from, to sdk.AccAddress,
amount sdk.Coin,
Expand Down
3 changes: 2 additions & 1 deletion x/opchild/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ func _createTestInput(
)

opchildParams := opchildtypes.DefaultParams()
opchildParams.Admin = addrs[0].String()
opchildParams.BridgeExecutor = addrs[0].String()
opchildKeeper.SetParams(ctx, opchildParams)
require.NoError(t, opchildKeeper.SetParams(ctx, opchildParams))

// register handlers to msg router
opchildtypes.RegisterMsgServer(msgRouter, opchildkeeper.NewMsgServerImpl(*opchildKeeper))
Expand Down
13 changes: 6 additions & 7 deletions x/opchild/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ func NewMsgServerImpl(k Keeper) MsgServer {
return MsgServer{k}
}

// checkValidatorPermission checks if the sender is the one of validator
func (ms MsgServer) checkValidatorPermission(ctx context.Context, sender string) error {
addr, err := ms.authKeeper.AddressCodec().StringToBytes(sender)
// checkAdminPermission checks if the sender is the admin
func (ms MsgServer) checkAdminPermission(ctx context.Context, sender string) error {
params, err := ms.GetParams(ctx)
if err != nil {
return err
}

valAddr := sdk.ValAddress(addr)
if _, found := ms.GetValidator(ctx, valAddr); !found {
return errors.Wrapf(sdkerrors.ErrUnauthorized, "the message is allowed to be executed by validator")
if params.Admin != sender {
return errors.Wrapf(sdkerrors.ErrUnauthorized, "the message is allowed to be executed by admin %s", params.Admin)
}

return nil
Expand Down Expand Up @@ -69,7 +68,7 @@ func (ms MsgServer) ExecuteMessages(ctx context.Context, req *types.MsgExecuteMe
}

// permission check
if err := ms.checkValidatorPermission(ctx, req.Sender); err != nil {
if err := ms.checkAdminPermission(ctx, req.Sender); err != nil {
return nil, err
}

Expand Down
19 changes: 17 additions & 2 deletions x/opchild/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func Test_MsgServer_ExecuteMessages(t *testing.T) {
ctx, input := createDefaultTestInput(t)

ms := keeper.NewMsgServerImpl(input.OPChildKeeper)

params, err := ms.GetParams(ctx)
require.NoError(t, err)

// admin to 0
params.Admin = addrsStr[0]
require.NoError(t, ms.SetParams(ctx, params))

valPubKeys := testutilsims.CreateTestPubKeys(2)

// register validator
Expand All @@ -45,7 +53,15 @@ func Test_MsgServer_ExecuteMessages(t *testing.T) {
removeMsg, err := types.NewMsgRemoveValidator(moduleAddr, valAddrsStr[0])
require.NoError(t, err)

msg, err := types.NewMsgExecuteMessages(addrsStr[0], []sdk.Msg{addMsg, removeMsg})
// should failed with unauthorized
msg, err := types.NewMsgExecuteMessages(addrsStr[1], []sdk.Msg{addMsg, removeMsg})
require.NoError(t, err)

_, err = ms.ExecuteMessages(ctx, msg)
require.Error(t, err)

// success
msg, err = types.NewMsgExecuteMessages(addrsStr[0], []sdk.Msg{addMsg, removeMsg})
require.NoError(t, err)

_, err = ms.ExecuteMessages(ctx, msg)
Expand All @@ -60,7 +76,6 @@ func Test_MsgServer_ExecuteMessages(t *testing.T) {
require.Equal(t, vals[0].Moniker, "val2")

// should failed with err (denom not sorted)
params := types.DefaultParams()
params.MinGasPrices = sdk.DecCoins{{
Denom: "22222",
Amount: math.LegacyNewDec(1),
Expand Down
4 changes: 4 additions & 0 deletions x/opchild/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func (k Keeper) BridgeExecutor(ctx context.Context) (sdk.AccAddress, error) {

// SetParams sets the x/opchild module parameters.
func (k Keeper) SetParams(ctx context.Context, params types.Params) error {
if err := params.Validate(k.authKeeper.AddressCodec()); err != nil {
return err
}

return k.Params.Set(ctx, params)
}

Expand Down
9 changes: 7 additions & 2 deletions x/opchild/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ var (
// DefaultParams returns default move parameters
func DefaultParams() Params {
return NewParams(
"",
"",
DefaultMaxValidators,
DefaultHistoricalEntries,
"",
DefaultMinGasPrices,
)
}

// NewParams creates a new Params instance
func NewParams(maxValidators, historicalEntries uint32, bridgeExecutor string, minGasPrice sdk.DecCoins) Params {
func NewParams(admin, bridgeExecutor string, maxValidators, historicalEntries uint32, minGasPrice sdk.DecCoins) Params {
return Params{
Admin: admin,
BridgeExecutor: bridgeExecutor,
MaxValidators: maxValidators,
HistoricalEntries: historicalEntries,
Expand All @@ -43,6 +45,9 @@ func (p Params) String() string {

// Validate performs basic validation on move parameters
func (p Params) Validate(ac address.Codec) error {
if _, err := ac.StringToBytes(p.Admin); err != nil {
return err
}
if _, err := ac.StringToBytes(p.BridgeExecutor); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 37f2247

Please sign in to comment.