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

Block Delay Parameter #171

Merged
merged 18 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
- [ConnectionPaths](#ibc.core.connection.v1.ConnectionPaths)
- [Counterparty](#ibc.core.connection.v1.Counterparty)
- [IdentifiedConnection](#ibc.core.connection.v1.IdentifiedConnection)
- [Params](#ibc.core.connection.v1.Params)
- [Version](#ibc.core.connection.v1.Version)

- [State](#ibc.core.connection.v1.State)
Expand Down Expand Up @@ -2399,6 +2400,21 @@ identifier field.



<a name="ibc.core.connection.v1.Params"></a>

### Params
Params defines the set of Connection parameters.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `max_time_per_block` | [uint64](#uint64) | | maximum time expected per block, used to enforce block delay. |






<a name="ibc.core.connection.v1.Version"></a>

### Version
Expand Down Expand Up @@ -2458,6 +2474,7 @@ GenesisState defines the ibc connection submodule's genesis state.
| `connections` | [IdentifiedConnection](#ibc.core.connection.v1.IdentifiedConnection) | repeated | |
| `client_connection_paths` | [ConnectionPaths](#ibc.core.connection.v1.ConnectionPaths) | repeated | |
| `next_connection_sequence` | [uint64](#uint64) | | the sequence for the next generated connection identifier |
| `params` | [Params](#ibc.core.connection.v1.Params) | | |



Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ require (
github.com/tendermint/tm-db v0.6.4
google.golang.org/genproto v0.0.0-20210114201628-6edceaf6022f
google.golang.org/grpc v1.37.0
google.golang.org/protobuf v1.26.0
)
6 changes: 3 additions & 3 deletions modules/core/02-client/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
"github.com/cosmos/ibc-go/modules/core/02-client/types"
)

// GetAllowedClients retrieves the receive enabled boolean from the paramstore
// GetAllowedClients retrieves the allowed clients from the paramstore
func (k Keeper) GetAllowedClients(ctx sdk.Context) []string {
var res []string
k.paramSpace.Get(ctx, types.KeyAllowedClients, &res)
return res
}

// GetParams returns the total set of ibc-transfer parameters.
// GetParams returns the total set of ibc-client parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(k.GetAllowedClients(ctx)...)
}

// SetParams sets the total set of ibc-transfer parameters.
// SetParams sets the total set of ibc-client parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}
8 changes: 4 additions & 4 deletions modules/core/02-client/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"strings"

"github.com/cosmos/ibc-go/modules/core/exported"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/ibc-go/modules/core/exported"
)

var (
Expand All @@ -21,19 +21,19 @@ func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

// NewParams creates a new parameter configuration for the ibc transfer module
// NewParams creates a new parameter configuration for the ibc client module
func NewParams(allowedClients ...string) Params {
return Params{
AllowedClients: allowedClients,
}
}

// DefaultParams is the default parameter configuration for the ibc-transfer module
// DefaultParams is the default parameter configuration for the ibc-client module
func DefaultParams() Params {
return NewParams(DefaultAllowedClients...)
}

// Validate all ibc-transfer module parameters
// Validate all ibc-client module parameters
func (p Params) Validate() error {
return validateClients(p.AllowedClients)
}
Expand Down
10 changes: 9 additions & 1 deletion modules/core/03-connection/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
"github.com/cosmos/ibc-go/modules/core/03-connection/types"
commitmenttypes "github.com/cosmos/ibc-go/modules/core/23-commitment/types"
Expand All @@ -19,15 +20,22 @@ type Keeper struct {
types.QueryServer

storeKey sdk.StoreKey
paramSpace paramtypes.Subspace
cdc codec.BinaryCodec
clientKeeper types.ClientKeeper
}

// NewKeeper creates a new IBC connection Keeper instance
func NewKeeper(cdc codec.BinaryCodec, key sdk.StoreKey, ck types.ClientKeeper) Keeper {
func NewKeeper(cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace, ck types.ClientKeeper) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

return Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace,
clientKeeper: ck,
}
}
Expand Down
23 changes: 23 additions & 0 deletions modules/core/03-connection/keeper/params.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/cosmos/ibc-go/modules/core/03-connection/types"
)

// GetMaxTimePerBlock retrieves the maximum time per block from the paramstore
func (k Keeper) GetMaxTimePerBlock(ctx sdk.Context) uint64 {
var res uint64
k.paramSpace.Get(ctx, types.KeyMaxTimePerBlock, &res)
return res
}

// GetParams returns the total set of ibc-connection parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(k.GetMaxTimePerBlock(ctx))
}

// SetParams sets the total set of ibc-connection parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}
17 changes: 17 additions & 0 deletions modules/core/03-connection/keeper/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper_test

import (
"github.com/cosmos/ibc-go/modules/core/03-connection/types"
)

func (suite *KeeperTestSuite) TestParams() {
expParams := types.DefaultParams()

params := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetParams(suite.chainA.GetContext())
suite.Require().Equal(expParams, params)

expParams.MaxTimePerBlock = 0
suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetParams(suite.chainA.GetContext(), expParams)
params = suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetParams(suite.chainA.GetContext())
suite.Require().Equal(0, expParams.MaxTimePerBlock)
}
44 changes: 36 additions & 8 deletions modules/core/03-connection/keeper/verify.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"math"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
Expand Down Expand Up @@ -159,9 +161,17 @@ func (k Keeper) VerifyPacketCommitment(
return sdkerrors.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

// calculate minimum block delay by dividing time delay period
// by the maximum expected time per block. Round up the block delay.
timeDelay := connection.GetDelayPeriod()
maxTimePerBlock := k.GetMaxTimePerBlock(ctx)
blockDelay := uint64(math.Ceil(float64(timeDelay) / float64(maxTimePerBlock)))
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved

// verify that block delay has passed
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved

if err := clientState.VerifyPacketCommitment(
clientStore, k.cdc, height,
uint64(ctx.BlockTime().UnixNano()), connection.GetDelayPeriod(),
clientStore, k.cdc, height, clienttypes.GetSelfHeight(ctx),
uint64(ctx.BlockTime().UnixNano()), timeDelay, blockDelay,
connection.GetCounterparty().GetPrefix(), proof, portID, channelID,
sequence, commitmentBytes,
); err != nil {
Expand Down Expand Up @@ -195,9 +205,15 @@ func (k Keeper) VerifyPacketAcknowledgement(
return sdkerrors.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

// calculate minimum block delay by dividing time delay period
// by the maximum expected time per block. Round up the block delay.
timeDelay := connection.GetDelayPeriod()
maxTimePerBlock := k.GetMaxTimePerBlock(ctx)
blockDelay := uint64(math.Ceil(float64(timeDelay) / float64(maxTimePerBlock)))
colin-axner marked this conversation as resolved.
Show resolved Hide resolved

if err := clientState.VerifyPacketAcknowledgement(
clientStore, k.cdc, height,
uint64(ctx.BlockTime().UnixNano()), connection.GetDelayPeriod(),
clientStore, k.cdc, height, clienttypes.GetSelfHeight(ctx),
uint64(ctx.BlockTime().UnixNano()), timeDelay, blockDelay,
connection.GetCounterparty().GetPrefix(), proof, portID, channelID,
sequence, acknowledgement,
); err != nil {
Expand Down Expand Up @@ -231,9 +247,15 @@ func (k Keeper) VerifyPacketReceiptAbsence(
return sdkerrors.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

// calculate minimum block delay by dividing time delay period
// by the maximum expected time per block. Round up the block delay.
timeDelay := connection.GetDelayPeriod()
maxTimePerBlock := k.GetMaxTimePerBlock(ctx)
blockDelay := uint64(math.Ceil(float64(timeDelay) / float64(maxTimePerBlock)))

if err := clientState.VerifyPacketReceiptAbsence(
clientStore, k.cdc, height,
uint64(ctx.BlockTime().UnixNano()), connection.GetDelayPeriod(),
clientStore, k.cdc, height, clienttypes.GetSelfHeight(ctx),
uint64(ctx.BlockTime().UnixNano()), timeDelay, blockDelay,
connection.GetCounterparty().GetPrefix(), proof, portID, channelID,
sequence,
); err != nil {
Expand Down Expand Up @@ -266,9 +288,15 @@ func (k Keeper) VerifyNextSequenceRecv(
return sdkerrors.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

// calculate minimum block delay by dividing time delay period
// by the maximum expected time per block. Round up the block delay.
timeDelay := connection.GetDelayPeriod()
maxTimePerBlock := k.GetMaxTimePerBlock(ctx)
blockDelay := uint64(math.Ceil(float64(timeDelay) / float64(maxTimePerBlock)))

if err := clientState.VerifyNextSequenceRecv(
clientStore, k.cdc, height,
uint64(ctx.BlockTime().UnixNano()), connection.GetDelayPeriod(),
clientStore, k.cdc, height, clienttypes.GetSelfHeight(ctx),
uint64(ctx.BlockTime().UnixNano()), timeDelay, blockDelay,
connection.GetCounterparty().GetPrefix(), proof, portID, channelID,
nextSequenceRecv,
); err != nil {
Expand Down
Loading