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

feat: add params BridgeCallRefundTimeout to crosschain module #267

Merged
merged 1 commit into from
Mar 12, 2024
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
597 changes: 594 additions & 3 deletions app/genesis_test.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions proto/fx/crosschain/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ message Params {
repeated string oracles = 9 [(gogoproto.nullable) = true];
cosmos.base.v1beta1.Coin delegate_threshold = 10 [(gogoproto.nullable) = false];
int64 delegate_multiple = 11;
uint64 bridge_call_refund_timeout = 12;
}

// Deprecated: after block 5713000
Expand Down
9 changes: 7 additions & 2 deletions x/arbitrum/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {
}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(_ module.Configurator) {}
func (am AppModule) RegisterServices(cfg module.Configurator) {
migrator := crosschainkeeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(am.Name(), 1, migrator.Migrate); err != nil {
panic(err)
}
}

// InitGenesis initializes the genesis state for this module and implements app module.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
Expand All @@ -127,7 +132,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw

// ConsensusVersion implements AppModule/ConsensusVersion.
func (am AppModule) ConsensusVersion() uint64 {
return 1
return 2
}

// EndBlock implements app module
Expand Down
1 change: 1 addition & 0 deletions x/arbitrum/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestDefaultGenesisState(t *testing.T) {
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: crosschaintypes.DefaultBridgeCallRefundTimeout,
},
},
},
Expand Down
9 changes: 7 additions & 2 deletions x/avalanche/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {
}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {}
func (am AppModule) RegisterServices(cfg module.Configurator) {
migrator := crosschainkeeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(am.Name(), 2, migrator.Migrate); err != nil {
panic(err)
}
}

// InitGenesis initializes the genesis state for this module and implements app module.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
Expand All @@ -130,7 +135,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw

// ConsensusVersion implements AppModule/ConsensusVersion.
func (am AppModule) ConsensusVersion() uint64 {
return 2
return 3
}

// EndBlock implements app module
Expand Down
1 change: 1 addition & 0 deletions x/avalanche/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestDefaultGenesisState(t *testing.T) {
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: crosschaintypes.DefaultBridgeCallRefundTimeout,
},
},
},
Expand Down
9 changes: 7 additions & 2 deletions x/bsc/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {
}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {}
func (am AppModule) RegisterServices(cfg module.Configurator) {
migrator := crosschainkeeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(am.Name(), 4, migrator.Migrate); err != nil {
panic(err)
}
}

// InitGenesis initializes the genesis state for this module and implements app module.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
Expand All @@ -130,7 +135,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw

// ConsensusVersion implements AppModule/ConsensusVersion.
func (am AppModule) ConsensusVersion() uint64 {
return 4
return 5
}

// EndBlock implements app module
Expand Down
1 change: 1 addition & 0 deletions x/bsc/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestDefaultGenesisState(t *testing.T) {
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: crosschaintypes.DefaultBridgeCallRefundTimeout,
},
},
},
Expand Down
4 changes: 1 addition & 3 deletions x/crosschain/keeper/bridge_call_refund.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper

import (
"time"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -59,7 +57,7 @@ func (k Keeper) AddRefundRecord(ctx sdk.Context, receiver string, eventNonce uin
k.SetRefundRecord(ctx, &types.RefundRecord{
EventNonce: eventNonce,
Receiver: receiver,
Timeout: uint64(ctx.BlockTime().Add(time.Hour * 24 * 7).Second()), // TODO need to be configurable
Timeout: k.GetBridgeCallRefundTimeout(ctx),
OracleSetNonce: oracleSet.Nonce,
Tokens: tokens,
})
Expand Down
23 changes: 23 additions & 0 deletions x/crosschain/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/functionx/fx-core/v7/x/crosschain/types"
)

type Migrator struct {
keeper Keeper
}

func NewMigrator(k Keeper) Migrator {
return Migrator{
keeper: k,
}
}

func (m Migrator) Migrate(ctx sdk.Context) error {
params := m.keeper.GetParams(ctx)
params.BridgeCallRefundTimeout = types.DefaultBridgeCallRefundTimeout
return m.keeper.SetParams(ctx, &params)
}
4 changes: 4 additions & 0 deletions x/crosschain/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ func (k Keeper) GetIbcTransferTimeoutHeight(ctx sdk.Context) uint64 {
func (k Keeper) GetOracleSetUpdatePowerChangePercent(ctx sdk.Context) sdk.Dec {
return k.GetParams(ctx).OracleSetUpdatePowerChangePercent
}

func (k Keeper) GetBridgeCallRefundTimeout(ctx sdk.Context) uint64 {
return k.GetParams(ctx).BridgeCallRefundTimeout
}
38 changes: 32 additions & 6 deletions x/crosschain/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (suite *KeeperTestSuite) TestParams() {
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: types.NewDelegateAmount(sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: types.DefaultBridgeCallRefundTimeout,
Oracles: nil,
},
expectErr: true,
Expand All @@ -45,6 +46,7 @@ func (suite *KeeperTestSuite) TestParams() {
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: types.NewDelegateAmount(sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: types.DefaultBridgeCallRefundTimeout,
Oracles: nil,
},
expectErr: true,
Expand All @@ -63,6 +65,7 @@ func (suite *KeeperTestSuite) TestParams() {
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: types.NewDelegateAmount(sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: types.DefaultBridgeCallRefundTimeout,
Oracles: nil,
},
expectErr: true,
Expand All @@ -81,6 +84,7 @@ func (suite *KeeperTestSuite) TestParams() {
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: types.NewDelegateAmount(sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: types.DefaultBridgeCallRefundTimeout,
Oracles: nil,
},
expectErr: true,
Expand All @@ -99,6 +103,7 @@ func (suite *KeeperTestSuite) TestParams() {
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: sdk.NewCoin("PX", sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: types.DefaultBridgeCallRefundTimeout,
Oracles: nil,
},
expectErr: true,
Expand All @@ -117,6 +122,7 @@ func (suite *KeeperTestSuite) TestParams() {
IbcTransferTimeoutHeight: 1,
DelegateThreshold: types.NewDelegateAmount(sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: types.DefaultBridgeCallRefundTimeout,
Oracles: nil,
},
expectErr: true,
Expand All @@ -131,10 +137,11 @@ func (suite *KeeperTestSuite) TestParams() {
ExternalBatchTimeout: 12 * 3600 * 1000,
SignedWindow: 30_000,
SlashFraction: sdk.NewDecWithPrec(8, 1), // 80%
OracleSetUpdatePowerChangePercent: sdk.NewDec(2), // 10%
OracleSetUpdatePowerChangePercent: sdk.NewDec(2), // 200%
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: types.NewDelegateAmount(sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: types.DefaultBridgeCallRefundTimeout,
Oracles: nil,
},
expectErr: true,
Expand All @@ -148,30 +155,49 @@ func (suite *KeeperTestSuite) TestParams() {
AverageExternalBlockTime: 5_000,
ExternalBatchTimeout: 12 * 3600 * 1000,
SignedWindow: 30_000,
SlashFraction: sdk.NewDec(2), // 80%
SlashFraction: sdk.NewDec(2), // 200%
OracleSetUpdatePowerChangePercent: sdk.NewDecWithPrec(1, 1), // 10%
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: types.NewDelegateAmount(sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: types.DefaultBridgeCallRefundTimeout,
Oracles: nil,
},
expectErr: true,
expErrMsg: "slash factor too large",
},
{
name: "invalid bridge call refund timeout",
input: &types.Params{
GravityId: "fx-gravity-id",
AverageBlockTime: 7_000,
AverageExternalBlockTime: 5_000,
ExternalBatchTimeout: 12 * 3600 * 1000,
SignedWindow: 30_000,
SlashFraction: sdk.NewDecWithPrec(8, 1), // 80%
OracleSetUpdatePowerChangePercent: sdk.NewDecWithPrec(1, 1), // 10%
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: types.NewDelegateAmount(sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: 10,
BridgeCallRefundTimeout: 1,
Oracles: nil,
},
expectErr: true,
expErrMsg: "invalid bridge call refund timeout",
},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
expected := suite.app.CrossChainKeepers.BscKeeper.GetParams(suite.ctx)
err := suite.app.CrossChainKeepers.BscKeeper.SetParams(suite.ctx, tc.input)
expected := suite.Keeper().GetParams(suite.ctx)
err := suite.Keeper().SetParams(suite.ctx, tc.input)
if tc.expectErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.expErrMsg)
} else {
expected = *tc.input
suite.Require().NoError(err)
}
params := suite.app.CrossChainKeepers.BscKeeper.GetParams(suite.ctx)
params := suite.Keeper().GetParams(suite.ctx)
suite.Require().Equal(expected, params)
})
}
Expand Down
6 changes: 6 additions & 0 deletions x/crosschain/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"errors"
"fmt"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -17,6 +18,7 @@
MaxResults = 100
MaxOracleSetRequestsResults = 5
MaxKeepEventSize = 100
DefaultBridgeCallRefundTimeout = uint64(7 * 24 * time.Hour)
Dismissed Show dismissed Hide dismissed
)

var (
Expand All @@ -43,6 +45,7 @@
IbcTransferTimeoutHeight: 20_000,
DelegateThreshold: NewDelegateAmount(sdkmath.NewInt(10_000).MulRaw(1e18)),
DelegateMultiple: DefaultOracleDelegateThreshold,
BridgeCallRefundTimeout: DefaultBridgeCallRefundTimeout,
Oracles: nil,
}
}
Expand Down Expand Up @@ -95,5 +98,8 @@
if len(m.Oracles) > 0 {
return errors.New("deprecated oracles")
}
if m.BridgeCallRefundTimeout <= uint64(1*time.Hour) {
Dismissed Show dismissed Hide dismissed
return fmt.Errorf("invalid bridge call refund timeout")
}
return nil
}
Loading
Loading