Skip to content

Commit

Permalink
Query param prop directly (#1264)
Browse files Browse the repository at this point in the history
* Query param prop directly

* Flip order of queries for QueryUnbondingPeriod

* Add fallback for chains using cosmos-sdk 47+

* Trusting period logic remains same

* Add Fallback

* Consolidate functions into a single queryParamsSubspaceTime
  • Loading branch information
vimystic authored Sep 13, 2023
1 parent 2aca133 commit 259b127
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
1 change: 1 addition & 0 deletions interchaintest/tendermint_v0.37_boundary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestScenarioTendermint37Boundary(t *testing.T) {
rf := relayerinterchaintest.NewRelayerFactory(relayerinterchaintest.RelayerConfig{
InitialBlockHistory: 50,
})

r := rf.Build(t, client, network)

t.Parallel()
Expand Down
14 changes: 2 additions & 12 deletions relayer/chains/cosmos/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,11 @@ func (cc *CosmosProvider) AccountFromKeyOrAddress(keyOrAddress string) (out sdk.
}

func (cc *CosmosProvider) TrustingPeriod(ctx context.Context) (time.Duration, error) {
res, err := cc.QueryStakingParams(ctx)

var unbondingTime time.Duration
unbondingTime, err := cc.QueryUnbondingPeriod(ctx)
if err != nil {
// Attempt ICS query
consumerUnbondingPeriod, consumerErr := cc.queryConsumerUnbondingPeriod(ctx)
if consumerErr != nil {
return 0,
fmt.Errorf("failed to query unbonding period as both standard and consumer chain: %s: %w", err.Error(), consumerErr)
}
unbondingTime = consumerUnbondingPeriod
} else {
unbondingTime = res.UnbondingTime
return 0, err
}

// We want the trusting period to be 85% of the unbonding time.
// Go mentions that the time.Duration type can track approximately 290 years.
// We don't want to lose precision if the duration is a very long duration
Expand Down
51 changes: 29 additions & 22 deletions relayer/chains/cosmos/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/cosmos/cosmos-sdk/types/query"
querytypes "github.com/cosmos/cosmos-sdk/types/query"
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
Expand Down Expand Up @@ -189,7 +188,7 @@ func parseEventsFromResponseDeliverTx(resp abci.ResponseDeliverTx) []provider.Re

// QueryFeegrantsByGrantee returns all requested grants for the given grantee.
// Default behavior will return all grants.
func (cc *CosmosProvider) QueryFeegrantsByGrantee(address string, paginator *query.PageRequest) ([]*feegrant.Grant, error) {
func (cc *CosmosProvider) QueryFeegrantsByGrantee(address string, paginator *querytypes.PageRequest) ([]*feegrant.Grant, error) {
grants := []*feegrant.Grant{}
allPages := paginator == nil

Expand Down Expand Up @@ -228,7 +227,7 @@ func (cc *CosmosProvider) QueryFeegrantsByGrantee(address string, paginator *que

// Feegrant_GrantsByGranterRPC returns all requested grants for the given Granter.
// Default behavior will return all grants.
func (cc *CosmosProvider) QueryFeegrantsByGranter(address string, paginator *query.PageRequest) ([]*feegrant.Grant, error) {
func (cc *CosmosProvider) QueryFeegrantsByGranter(address string, paginator *querytypes.PageRequest) ([]*feegrant.Grant, error) {
grants := []*feegrant.Grant{}
allPages := paginator == nil

Expand Down Expand Up @@ -311,47 +310,55 @@ func (cc *CosmosProvider) QueryBalanceWithAddress(ctx context.Context, address s
return coins, nil
}

func (cc *CosmosProvider) queryConsumerUnbondingPeriod(ctx context.Context) (time.Duration, error) {
func (cc *CosmosProvider) queryParamsSubspaceTime(ctx context.Context, subspace string, key string) (time.Duration, error) {
queryClient := proposal.NewQueryClient(cc)

params := proposal.QueryParamsRequest{Subspace: "ccvconsumer", Key: "UnbondingPeriod"}
params := proposal.QueryParamsRequest{Subspace: subspace, Key: key}

resICS, err := queryClient.Params(ctx, &params)
res, err := queryClient.Params(ctx, &params)

if err != nil {
return 0, fmt.Errorf("failed to make ccvconsumer params request: %w", err)
return 0, fmt.Errorf("failed to make %s params request: %w", subspace, err)
}

if resICS.Param.Value == "" {
return 0, fmt.Errorf("ccvconsumer unbonding period is empty")
if res.Param.Value == "" {
return 0, fmt.Errorf("%s %s is empty", subspace, key)
}

unbondingPeriod, err := strconv.ParseUint(strings.ReplaceAll(resICS.Param.Value, `"`, ""), 10, 64)
unbondingValue, err := strconv.ParseUint(strings.ReplaceAll(res.Param.Value, `"`, ""), 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse unbonding period from ccvconsumer param: %w", err)
return 0, fmt.Errorf("failed to parse %s from %s param: %w", key, subspace, err)
}

return time.Duration(unbondingPeriod), nil
return time.Duration(unbondingValue), nil
}

// QueryUnbondingPeriod returns the unbonding period of the chain
func (cc *CosmosProvider) QueryUnbondingPeriod(ctx context.Context) (time.Duration, error) {

// Attempt ICS query
consumerUnbondingPeriod, consumerErr := cc.queryParamsSubspaceTime(ctx, "ccvconsumer", "UnbondingPeriod")
if consumerErr == nil {
return consumerUnbondingPeriod, nil
}

//Attempt Staking query.
unbondingPeriod, stakingParamsErr := cc.queryParamsSubspaceTime(ctx, "staking", "UnbondingTime")
if stakingParamsErr == nil {
return unbondingPeriod, nil
}

// Fallback
req := stakingtypes.QueryParamsRequest{}
queryClient := stakingtypes.NewQueryClient(cc)

res, err := queryClient.Params(ctx, &req)
if err != nil {
// Attempt ICS query
consumerUnbondingPeriod, consumerErr := cc.queryConsumerUnbondingPeriod(ctx)
if consumerErr != nil {
return 0,
fmt.Errorf("failed to query unbonding period as both standard and consumer chain: %s: %w", err.Error(), consumerErr)
}
if err == nil {
return res.Params.UnbondingTime, nil

return consumerUnbondingPeriod, nil
}

return res.Params.UnbondingTime, nil
return 0,
fmt.Errorf("failed to query unbonding period from ccvconsumer, staking & fallback : %w: %s : %s", consumerErr, stakingParamsErr.Error(), err.Error())
}

// QueryTendermintProof performs an ABCI query with the given key and returns
Expand Down

0 comments on commit 259b127

Please sign in to comment.