Skip to content

Commit

Permalink
Add query for poa chains
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Feb 13, 2023
1 parent 6b72407 commit 36e2b07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
14 changes: 2 additions & 12 deletions relayer/chains/cosmos/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,9 @@ func (cc *CosmosProvider) Address() (string, error) {
}

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.
Expand Down
38 changes: 21 additions & 17 deletions relayer/chains/cosmos/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,47 +178,51 @@ 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) querySubspaceUnbondingPeriod(subspace string, ctx context.Context) (time.Duration, error) {
queryClient := proposal.NewQueryClient(cc)

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

resICS, 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")
return 0, fmt.Errorf("%s unbonding period is empty", subspace)
}

unbondingPeriod, err := strconv.ParseUint(strings.ReplaceAll(resICS.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 unbonding period from %s param: %w", subspace, err)
}

return time.Duration(unbondingPeriod), nil
}

// QueryUnbondingPeriod returns the unbonding period of the chain
func (cc *CosmosProvider) QueryUnbondingPeriod(ctx context.Context) (time.Duration, error) {
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)
}
res, err := cc.QueryStakingParams(ctx)
if err == nil {
return res.UnbondingTime, nil
}

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

return res.Params.UnbondingTime, nil
poaUnbondingPeriod, poaErr := cc.querySubspaceUnbondingPeriod("poa", ctx)
if poaErr == nil {
return poaUnbondingPeriod, nil
}

return 0, fmt.Errorf(
"failed to query unbonding period as both standard, consumer, and poa chain: %s, %s, %s",
err.Error(), consumerErr.Error(), poaErr.Error(),
)
}

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

0 comments on commit 36e2b07

Please sign in to comment.