From 259b1278264180a2aefc2085f1b55753849c4815 Mon Sep 17 00:00:00 2001 From: vimystic <122659254+vimystic@users.noreply.github.com> Date: Wed, 13 Sep 2023 13:38:36 -0600 Subject: [PATCH] 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 --- .../tendermint_v0.37_boundary_test.go | 1 + relayer/chains/cosmos/provider.go | 14 +---- relayer/chains/cosmos/query.go | 51 +++++++++++-------- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/interchaintest/tendermint_v0.37_boundary_test.go b/interchaintest/tendermint_v0.37_boundary_test.go index 04a7a8c47..e25790176 100644 --- a/interchaintest/tendermint_v0.37_boundary_test.go +++ b/interchaintest/tendermint_v0.37_boundary_test.go @@ -54,6 +54,7 @@ func TestScenarioTendermint37Boundary(t *testing.T) { rf := relayerinterchaintest.NewRelayerFactory(relayerinterchaintest.RelayerConfig{ InitialBlockHistory: 50, }) + r := rf.Build(t, client, network) t.Parallel() diff --git a/relayer/chains/cosmos/provider.go b/relayer/chains/cosmos/provider.go index c33743faf..3c7ff47fc 100644 --- a/relayer/chains/cosmos/provider.go +++ b/relayer/chains/cosmos/provider.go @@ -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 diff --git a/relayer/chains/cosmos/query.go b/relayer/chains/cosmos/query.go index dfc040214..d41f25bdc 100644 --- a/relayer/chains/cosmos/query.go +++ b/relayer/chains/cosmos/query.go @@ -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" @@ -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 @@ -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 @@ -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, ¶ms) + res, err := queryClient.Params(ctx, ¶ms) 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