Skip to content

Commit

Permalink
feat(x/fbridge): set target denom as module parameters (#1366) (#1368)
Browse files Browse the repository at this point in the history
* set fbridge target denom through params

* add UpdateParams

* add event and unittest

* use sdk denom checker

(cherry picked from commit a9be0cb)

Co-authored-by: Jaeseung Lee <41176085+tkxkd0159@users.noreply.github.com>
  • Loading branch information
mergify[bot] and tkxkd0159 authored May 8, 2024
1 parent 74e5686 commit abe3a24
Show file tree
Hide file tree
Showing 22 changed files with 1,047 additions and 192 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements
* (types) [\#1314](https://github.com/Finschia/finschia-sdk/pull/1314) replace IsEqual with Equal
* (x/fswap) [\#1363](https://github.com/Finschia/finschia-sdk/pull/1363) introduce new event for MakeSwapProposal
* (x/fbridge) [\#1366](https://github.com/Finschia/finschia-sdk/pull/1366) Set target denom as module parameters

### Bug Fixes
* (x/auth) [#1281](https://github.com/Finschia/finschia-sdk/pull/1281) `ModuleAccount.Validate` now reports a nil `.BaseAccount` instead of panicking. (backport #1274)
Expand Down
48 changes: 48 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@
- [EventProvision](#lbm.fbridge.v1.EventProvision)
- [EventSuggestRole](#lbm.fbridge.v1.EventSuggestRole)
- [EventTransfer](#lbm.fbridge.v1.EventTransfer)
- [EventUpdateParams](#lbm.fbridge.v1.EventUpdateParams)

- [lbm/fbridge/v1/genesis.proto](#lbm/fbridge/v1/genesis.proto)
- [BlockSeqInfo](#lbm.fbridge.v1.BlockSeqInfo)
Expand Down Expand Up @@ -868,6 +869,8 @@
- [MsgSuggestRoleResponse](#lbm.fbridge.v1.MsgSuggestRoleResponse)
- [MsgTransfer](#lbm.fbridge.v1.MsgTransfer)
- [MsgTransferResponse](#lbm.fbridge.v1.MsgTransferResponse)
- [MsgUpdateParams](#lbm.fbridge.v1.MsgUpdateParams)
- [MsgUpdateParamsResponse](#lbm.fbridge.v1.MsgUpdateParamsResponse)

- [Msg](#lbm.fbridge.v1.Msg)

Expand Down Expand Up @@ -11812,6 +11815,7 @@ supports positive values.
| `judge_trust_level` | [Fraction](#lbm.fbridge.v1.Fraction) | | ratio of how many judges' confirmations are needed to be valid. |
| `timelock_period` | [uint64](#uint64) | | default timelock period for each provision (unix timestamp) |
| `proposal_period` | [uint64](#uint64) | | default period of the proposal to update the role |
| `target_denom` | [string](#string) | | target denom of the bridge module. This is the base denom of Finschia normally. |



Expand Down Expand Up @@ -12080,6 +12084,21 @@ VoteOption enumerates the valid vote options for a given role proposal.




<a name="lbm.fbridge.v1.EventUpdateParams"></a>

### EventUpdateParams



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `params` | [Params](#lbm.fbridge.v1.Params) | | |





<!-- end messages -->

<!-- end enums -->
Expand Down Expand Up @@ -13043,6 +13062,34 @@ MsgTransfer is input values required for bridge transfer




<a name="lbm.fbridge.v1.MsgUpdateParams"></a>

### MsgUpdateParams



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `authority` | [string](#string) | | the authority address |
| `params` | [Params](#lbm.fbridge.v1.Params) | | params defines the x/fbridge parameters to update.

NOTE: All parameters must be supplied. |






<a name="lbm.fbridge.v1.MsgUpdateParamsResponse"></a>

### MsgUpdateParamsResponse






<!-- end messages -->

<!-- end enums -->
Expand All @@ -13057,6 +13104,7 @@ MsgTransfer is input values required for bridge transfer

| Method Name | Request Type | Response Type | Description | HTTP Verb | Endpoint |
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `UpdateParams` | [MsgUpdateParams](#lbm.fbridge.v1.MsgUpdateParams) | [MsgUpdateParamsResponse](#lbm.fbridge.v1.MsgUpdateParamsResponse) | UpdateParams updates the x/fbridge parameters. | |
| `Transfer` | [MsgTransfer](#lbm.fbridge.v1.MsgTransfer) | [MsgTransferResponse](#lbm.fbridge.v1.MsgTransferResponse) | Submit a transfer request to the bridge module. | |
| `Provision` | [MsgProvision](#lbm.fbridge.v1.MsgProvision) | [MsgProvisionResponse](#lbm.fbridge.v1.MsgProvisionResponse) | Submit a provision to the bridge module. | |
| `HoldTransfer` | [MsgHoldTransfer](#lbm.fbridge.v1.MsgHoldTransfer) | [MsgHoldTransferResponse](#lbm.fbridge.v1.MsgHoldTransferResponse) | Set the time lock value from default value to uint64.max for specific confirmed provision. | |
Expand Down
4 changes: 4 additions & 0 deletions proto/lbm/fbridge/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fbridge/types";
import "gogoproto/gogo.proto";
import "lbm/fbridge/v1/fbridge.proto";

message EventUpdateParams {
Params params = 1 [(gogoproto.nullable) = false];
}

message EventTransfer {
// the sequence number of the bridge request
uint64 seq = 1;
Expand Down
2 changes: 2 additions & 0 deletions proto/lbm/fbridge/v1/fbridge.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ message Params {
uint64 timelock_period = 4;
// default period of the proposal to update the role
uint64 proposal_period = 5;
// target denom of the bridge module. This is the base denom of Finschia normally.
string target_denom = 6;
}

// Provision is a struct that represents a provision internally.
Expand Down
15 changes: 15 additions & 0 deletions proto/lbm/fbridge/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import "gogoproto/gogo.proto";
import "lbm/fbridge/v1/fbridge.proto";

service Msg {
// UpdateParams updates the x/fbridge parameters.
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);

// Submit a transfer request to the bridge module.
rpc Transfer(MsgTransfer) returns (MsgTransferResponse);

Expand Down Expand Up @@ -41,6 +44,18 @@ service Msg {
rpc SetBridgeStatus(MsgSetBridgeStatus) returns (MsgSetBridgeStatusResponse);
}

message MsgUpdateParams {
// the authority address
string authority = 1;

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

message MsgUpdateParamsResponse {}

// MsgTransfer is input values required for bridge transfer
message MsgTransfer {
// the sender address on the source chain
Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func NewSimApp(
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper

app.FbridgeKeeper = fbridgekeeper.NewKeeper(appCodec, keys[fbridgetypes.StoreKey], memKeys[fbridgetypes.MemStoreKey], app.AccountKeeper, app.BankKeeper, "stake", fbridgetypes.DefaultAuthority().String())
app.FbridgeKeeper = fbridgekeeper.NewKeeper(appCodec, keys[fbridgetypes.StoreKey], memKeys[fbridgetypes.MemStoreKey], app.AccountKeeper, app.BankKeeper, fbridgetypes.DefaultAuthority().String())

/**** Module Options ****/

Expand Down
4 changes: 2 additions & 2 deletions x/fbridge/keeper/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestAssignRole(t *testing.T) {
key, memKey, ctx, encCfg, authKeeper, bankKeeper, addrs := testutil.PrepareFbridgeTest(t, 3)
auth := types.DefaultAuthority()
k := NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, "stake", auth.String())
k := NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, auth.String())
err := k.InitGenesis(ctx, types.DefaultGenesisState())
require.NoError(t, err)

Expand Down Expand Up @@ -61,7 +61,7 @@ func TestAssignRole(t *testing.T) {
func TestBridgeHaltAndResume(t *testing.T) {
key, memKey, ctx, encCfg, authKeeper, bankKeeper, addrs := testutil.PrepareFbridgeTest(t, 3)
auth := types.DefaultAuthority()
k := NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, "stake", auth.String())
k := NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, auth.String())
err := k.InitGenesis(ctx, types.DefaultGenesisState())
require.NoError(t, err)
for _, addr := range addrs {
Expand Down
5 changes: 4 additions & 1 deletion x/fbridge/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
)

func (k Keeper) InitGenesis(ctx sdk.Context, gs *types.GenesisState) error {
k.SetParams(ctx, gs.Params)
if err := k.SetParams(ctx, gs.Params); err != nil {
return err
}

k.setNextSequence(ctx, gs.SendingState.NextSeq)
for _, info := range gs.SendingState.SeqToBlocknum {
k.setSeqToBlocknum(ctx, info.Seq, info.Blocknum)
Expand Down
18 changes: 7 additions & 11 deletions x/fbridge/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ type Keeper struct {
authKeeper types.AccountKeeper
bankKeeper types.BankKeeper

// the target denom for the bridge
targetDenom string

// authority can give a role to a specific address like guardian
authority string
}
Expand All @@ -31,7 +28,7 @@ func NewKeeper(
key, memKey sdk.StoreKey,
authKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
targetDenom, authority string,
authority string,
) Keeper {
if addr := authKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(errors.New("fbridge module account has not been set"))
Expand All @@ -50,13 +47,12 @@ func NewKeeper(
}

return Keeper{
storeKey: key,
memKey: memKey,
cdc: cdc,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
targetDenom: targetDenom,
authority: authority,
storeKey: key,
memKey: memKey,
cdc: cdc,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
authority: authority,
}
}

Expand Down
8 changes: 4 additions & 4 deletions x/fbridge/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ func TestNewKeeper(t *testing.T) {
"fbridge module account has not been set": {
malleate: func() {
authKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(nil).Times(1)
keeper.NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, "stake", types.DefaultAuthority().String())
keeper.NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, types.DefaultAuthority().String())
},
isPanic: true,
},
"fbridge authority must be the gov or foundation module account": {
malleate: func() {
authKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(authtypes.NewModuleAddress(types.ModuleName)).Times(1)
keeper.NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, "stake", authtypes.NewModuleAddress("invalid").String())
keeper.NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, authtypes.NewModuleAddress("invalid").String())
},
isPanic: true,
},
"success - gov authority": {
malleate: func() {
authKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(authtypes.NewModuleAddress(types.ModuleName)).Times(1)
keeper.NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, "stake", authtypes.NewModuleAddress(govtypes.ModuleName).String())
keeper.NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String())
},
isPanic: false,
},
"success - foundation authority": {
malleate: func() {
authKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(authtypes.NewModuleAddress(types.ModuleName)).Times(1)
keeper.NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, "stake", authtypes.NewModuleAddress(foundation.ModuleName).String())
keeper.NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, authtypes.NewModuleAddress(foundation.ModuleName).String())
},
isPanic: false,
},
Expand Down
23 changes: 23 additions & 0 deletions x/fbridge/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"fmt"

sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
Expand All @@ -18,6 +19,28 @@ func NewMsgServer(k Keeper) types.MsgServer {
return &msgServer{k}
}

func (m msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if msg.Authority != m.Keeper.GetAuthority() {
return nil, fmt.Errorf(
"invalid authority; expected %s, got %s",
m.Keeper.GetAuthority(), msg.Authority)
}

if err := m.Keeper.SetParams(ctx, msg.Params); err != nil {
return nil, err
}

if err := ctx.EventManager().EmitTypedEvent(&types.EventUpdateParams{
Params: msg.Params,
}); err != nil {
panic(err)
}

return &types.MsgUpdateParamsResponse{}, nil
}

func (m msgServer) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types.MsgTransferResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down
7 changes: 6 additions & 1 deletion x/fbridge/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import (
"github.com/Finschia/finschia-sdk/x/fbridge/types"
)

func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
if err := params.ValidateParams(); err != nil {
return err
}

store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&params)
store.Set(types.KeyParams, bz)
return nil
}

func (k Keeper) GetParams(ctx sdk.Context) types.Params {
Expand Down
Loading

0 comments on commit abe3a24

Please sign in to comment.