Skip to content

Commit 259b127

Browse files
authored
Query param prop directly (#1264)
* 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
1 parent 2aca133 commit 259b127

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

interchaintest/tendermint_v0.37_boundary_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func TestScenarioTendermint37Boundary(t *testing.T) {
5454
rf := relayerinterchaintest.NewRelayerFactory(relayerinterchaintest.RelayerConfig{
5555
InitialBlockHistory: 50,
5656
})
57+
5758
r := rf.Build(t, client, network)
5859

5960
t.Parallel()

relayer/chains/cosmos/provider.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,11 @@ func (cc *CosmosProvider) AccountFromKeyOrAddress(keyOrAddress string) (out sdk.
226226
}
227227

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

231-
var unbondingTime time.Duration
230+
unbondingTime, err := cc.QueryUnbondingPeriod(ctx)
232231
if err != nil {
233-
// Attempt ICS query
234-
consumerUnbondingPeriod, consumerErr := cc.queryConsumerUnbondingPeriod(ctx)
235-
if consumerErr != nil {
236-
return 0,
237-
fmt.Errorf("failed to query unbonding period as both standard and consumer chain: %s: %w", err.Error(), consumerErr)
238-
}
239-
unbondingTime = consumerUnbondingPeriod
240-
} else {
241-
unbondingTime = res.UnbondingTime
232+
return 0, err
242233
}
243-
244234
// We want the trusting period to be 85% of the unbonding time.
245235
// Go mentions that the time.Duration type can track approximately 290 years.
246236
// We don't want to lose precision if the duration is a very long duration

relayer/chains/cosmos/query.go

+29-22
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
sdk "github.com/cosmos/cosmos-sdk/types"
1919
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
2020
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
21-
"github.com/cosmos/cosmos-sdk/types/query"
2221
querytypes "github.com/cosmos/cosmos-sdk/types/query"
2322
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
2423
"github.com/cosmos/cosmos-sdk/x/feegrant"
@@ -189,7 +188,7 @@ func parseEventsFromResponseDeliverTx(resp abci.ResponseDeliverTx) []provider.Re
189188

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

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

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

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

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

317-
params := proposal.QueryParamsRequest{Subspace: "ccvconsumer", Key: "UnbondingPeriod"}
316+
params := proposal.QueryParamsRequest{Subspace: subspace, Key: key}
318317

319-
resICS, err := queryClient.Params(ctx, &params)
318+
res, err := queryClient.Params(ctx, &params)
320319

321320
if err != nil {
322-
return 0, fmt.Errorf("failed to make ccvconsumer params request: %w", err)
321+
return 0, fmt.Errorf("failed to make %s params request: %w", subspace, err)
323322
}
324323

325-
if resICS.Param.Value == "" {
326-
return 0, fmt.Errorf("ccvconsumer unbonding period is empty")
324+
if res.Param.Value == "" {
325+
return 0, fmt.Errorf("%s %s is empty", subspace, key)
327326
}
328327

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

334-
return time.Duration(unbondingPeriod), nil
333+
return time.Duration(unbondingValue), nil
335334
}
336335

337336
// QueryUnbondingPeriod returns the unbonding period of the chain
338337
func (cc *CosmosProvider) QueryUnbondingPeriod(ctx context.Context) (time.Duration, error) {
338+
339+
// Attempt ICS query
340+
consumerUnbondingPeriod, consumerErr := cc.queryParamsSubspaceTime(ctx, "ccvconsumer", "UnbondingPeriod")
341+
if consumerErr == nil {
342+
return consumerUnbondingPeriod, nil
343+
}
344+
345+
//Attempt Staking query.
346+
unbondingPeriod, stakingParamsErr := cc.queryParamsSubspaceTime(ctx, "staking", "UnbondingTime")
347+
if stakingParamsErr == nil {
348+
return unbondingPeriod, nil
349+
}
350+
351+
// Fallback
339352
req := stakingtypes.QueryParamsRequest{}
340353
queryClient := stakingtypes.NewQueryClient(cc)
341-
342354
res, err := queryClient.Params(ctx, &req)
343-
if err != nil {
344-
// Attempt ICS query
345-
consumerUnbondingPeriod, consumerErr := cc.queryConsumerUnbondingPeriod(ctx)
346-
if consumerErr != nil {
347-
return 0,
348-
fmt.Errorf("failed to query unbonding period as both standard and consumer chain: %s: %w", err.Error(), consumerErr)
349-
}
355+
if err == nil {
356+
return res.Params.UnbondingTime, nil
350357

351-
return consumerUnbondingPeriod, nil
352358
}
353359

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

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

0 commit comments

Comments
 (0)