Skip to content

Commit

Permalink
Merge pull request #106 from keithsue/sufay/update-params
Browse files Browse the repository at this point in the history
Add update params msg
  • Loading branch information
liangping authored Jul 9, 2024
2 parents 4ab260c + 228ff36 commit 6bfa524
Show file tree
Hide file tree
Showing 10 changed files with 576 additions and 48 deletions.
2 changes: 2 additions & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appCodec,
appKeepers.keys[btcbridgetypes.StoreKey],
appKeepers.keys[btcbridgetypes.StoreKey],
govAuthor,
appKeepers.BankKeeper,
)

Expand Down Expand Up @@ -471,6 +472,7 @@ func (appKeepers *AppKeepers) initParamsKeeper(appCodec codec.BinaryCodec, legac
paramsKeeper.Subspace(icahosttypes.SubModuleName)
paramsKeeper.Subspace(gmmmoduletypes.ModuleName)
paramsKeeper.Subspace(yieldmoduletypes.ModuleName)
paramsKeeper.Subspace(btcbridgetypes.ModuleName)

return paramsKeeper
}
Expand Down
23 changes: 22 additions & 1 deletion proto/side/btcbridge/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ service Msg {
rpc SubmitWithdrawSignatures (MsgSubmitWithdrawSignaturesRequest) returns (MsgSubmitWithdrawSignaturesResponse);
// SubmitWithdrawStatus submits the status of the withdraw transaction.
rpc SubmitWithdrawStatus (MsgSubmitWithdrawStatusRequest) returns (MsgSubmitWithdrawStatusResponse);

// UpdateParams defines a governance operation for updating the x/btcbridge module
// parameters. The authority defaults to the x/gov module account.
//
// Since: cosmos-sdk 0.47
rpc UpdateParams(MsgUpdateParamsRequest) returns (MsgUpdateParamsResponse);
}

// MsgSubmitWithdrawStatusRequest defines the Msg/SubmitWithdrawStatus request type.
Expand Down Expand Up @@ -114,3 +118,20 @@ message MsgSubmitWithdrawSignaturesRequest {
message MsgSubmitWithdrawSignaturesResponse {
}

// MsgUpdateParamsRequest is the Msg/UpdateParams request type.
//
// Since: cosmos-sdk 0.47
message MsgUpdateParamsRequest {
// authority is the address that controls the module (defaults to x/gov unless overwritten).
string authority = 1;

// params defines the x/btcbridge parameters to be updated.
//
// NOTE: All parameters must be supplied.
Params params = 2 [(gogoproto.nullable) = false];
}

// MsgUpdateParamsResponse defines the Msg/UpdateParams response type.
//
// Since: cosmos-sdk 0.47
message MsgUpdateParamsResponse {}
5 changes: 5 additions & 0 deletions testutil/keeper/btc_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/sideprotocol/side/app"
"github.com/sideprotocol/side/x/btcbridge/keeper"
"github.com/sideprotocol/side/x/btcbridge/types"
Expand All @@ -32,10 +34,13 @@ func BtcBridgeKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

authority := authtypes.NewModuleAddress(govtypes.ModuleName).String()

k := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey,
authority,
app.BankKeeper,
)

Expand Down
5 changes: 4 additions & 1 deletion x/btcbridge/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type (
storeKey storetypes.StoreKey
memKey storetypes.StoreKey

authority string

bankKeeper types.BankKeeper
}
)
Expand All @@ -27,13 +29,14 @@ func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,

authority string,
bankKeeper types.BankKeeper,
) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
authority: authority,
bankKeeper: bankKeeper,
BaseUTXOKeeper: *NewBaseUTXOKeeper(cdc, storeKey),
}
Expand Down
20 changes: 20 additions & 0 deletions x/btcbridge/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
"strconv"

"github.com/btcsuite/btcd/btcutil/psbt"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/sideprotocol/side/x/btcbridge/types"
)

Expand Down Expand Up @@ -237,6 +241,22 @@ func (m msgServer) SubmitWithdrawStatus(goCtx context.Context, msg *types.MsgSub
return &types.MsgSubmitWithdrawStatusResponse{}, nil
}

// UpdateParams updates the module params.
func (m msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParamsRequest) (*types.MsgUpdateParamsResponse, error) {
if m.authority != msg.Authority {
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", m.authority, msg.Authority)
}

if err := msg.ValidateBasic(); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)
m.SetParams(ctx, msg.Params)

return &types.MsgUpdateParamsResponse{}, nil
}

// NewMsgServerImpl returns an implementation of the MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
Expand Down
2 changes: 2 additions & 0 deletions x/btcbridge/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgSubmitDepositTransactionRequest{}, "btcbridge/MsgSubmitDepositTransactionRequest", nil)
cdc.RegisterConcrete(&MsgSubmitWithdrawSignaturesRequest{}, "btcbridge/MsgSubmitWithdrawSignaturesRequest", nil)
cdc.RegisterConcrete(&MsgSubmitWithdrawTransactionRequest{}, "btcbridge/MsgSubmitWithdrawTransactionRequest", nil)
cdc.RegisterConcrete(&MsgUpdateParamsRequest{}, "btcbridge/MsgUpdateParamsRequest", nil)
// this line is used by starport scaffolding # 2
}

Expand All @@ -22,6 +23,7 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSubmitDepositTransactionRequest{})
registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSubmitWithdrawSignaturesRequest{})
registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSubmitWithdrawTransactionRequest{})
registry.RegisterImplementations((*sdk.Msg)(nil), &MsgUpdateParamsRequest{})
// this line is used by starport scaffolding # 3

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
4 changes: 3 additions & 1 deletion x/btcbridge/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
errorsmod "cosmossdk.io/errors"
)

// x/yield module sentinel errors
// x/btcbridge module sentinel errors
var (
ErrSenderAddressNotAuthorized = errorsmod.Register(ModuleName, 1000, "sender address not authorized")
ErrInvalidHeader = errorsmod.Register(ModuleName, 1100, "invalid block header")
Expand Down Expand Up @@ -42,4 +42,6 @@ var (

ErrInvalidRunes = errorsmod.Register(ModuleName, 7100, "invalid runes")
ErrInvalidRuneId = errorsmod.Register(ModuleName, 7101, "invalid rune id")

ErrInvalidParams = errorsmod.Register(ModuleName, 8100, "invalid module params")
)
42 changes: 42 additions & 0 deletions x/btcbridge/types/message_update_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const TypeMsgUpdateParams = "update_params"

// Route returns the route of MsgUpdateParams.
func (msg *MsgUpdateParamsRequest) Route() string {
return RouterKey
}

// Type returns the type of MsgUpdateParams.
func (msg *MsgUpdateParamsRequest) Type() string {
return TypeMsgUpdateParams
}

// GetSignBytes implements the LegacyMsg interface.
func (m MsgUpdateParamsRequest) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}

// GetSigners returns the expected signers for a MsgUpdateParams message.
func (m *MsgUpdateParamsRequest) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(m.Authority)
return []sdk.AccAddress{addr}
}

// ValidateBasic performs basic MsgUpdateParams message validation.
func (m *MsgUpdateParamsRequest) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return sdkerrors.Wrap(err, "invalid authority address")
}

if err := m.Params.Validate(); err != nil {
return err
}

return nil
}
31 changes: 31 additions & 0 deletions x/btcbridge/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"bytes"

secp256k1 "github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/txscript"

Expand Down Expand Up @@ -41,6 +42,36 @@ func (p Params) Validate() error {
return err
}
}

if err := sdk.ValidateDenom(p.BtcVoucherDenom); err != nil {
return err
}

vaults := make(map[string]bool)

for _, vault := range p.Vaults {
_, ok := vaults[vault.Address]
if ok {
return ErrInvalidParams
}

_, err := sdk.AccAddressFromBech32(vault.Address)
if err != nil {
return err
}

if len(vault.PubKey) != 0 {
_, err := secp256k1.ParsePubKey([]byte(vault.PubKey))
if err != nil {
return err
}
}

if vault.AssetType == AssetType_ASSET_TYPE_UNSPECIFIED {
return err
}
}

return nil
}

Expand Down
Loading

0 comments on commit 6bfa524

Please sign in to comment.