Skip to content

Commit

Permalink
Fix trusting period query for ics chains (#1072)
Browse files Browse the repository at this point in the history
* Fix trusting period query for ics chains

* avoid truncation if less than an hour
  • Loading branch information
agouin committed Jan 30, 2023
1 parent 921407a commit ee9f4f6
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions relayer/chains/cosmos/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,18 @@ 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
if err != nil {
return 0, err
// 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
}

// We want the trusting period to be 85% of the unbonding time.
Expand All @@ -206,10 +216,15 @@ func (cc *CosmosProvider) TrustingPeriod(ctx context.Context) (time.Duration, er
// by converting int64 to float64.
// Use integer math the whole time, first reducing by a factor of 100
// and then re-growing by 85x.
tp := res.UnbondingTime / 100 * 85
tp := unbondingTime / 100 * 85

// We only want the trusting period to be whole hours unless it's less than an hour (for testing).
truncated := tp.Truncate(time.Hour)
if truncated.Hours() == 0 {
return tp, nil
}

// And we only want the trusting period to be whole hours.
return tp.Truncate(time.Hour), nil
return truncated, nil
}

// Sprint returns the json representation of the specified proto message.
Expand Down

0 comments on commit ee9f4f6

Please sign in to comment.