Skip to content

Commit

Permalink
fix: todo: on-chain historical data
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 13, 2024
1 parent 62f15ae commit fd61a91
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
36 changes: 30 additions & 6 deletions x/proof/types/shared_query_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ func NewSharedKeeperQueryClient(
// for the session which includes queryHeight elapses.
// The grace period is the number of blocks after the session ends during which relays
// SHOULD be included in the session which most recently ended.
//
// TODO_MAINNET(@bryanchriswhite, #931): We don't really want to use the current value of the params.
// Instead, we should be using the value that the params had for the session given by queryHeight.
func (sqc *sharedKeeperQueryClient) GetSessionGracePeriodEndHeight(
ctx context.Context,
queryHeight int64,
) (int64, error) {
sharedParams, err := sqc.GetParamsAtHeight(ctx, queryHeight)
// TODO_MAINNET(#931): sqc.GetParamsAtHeight(ctx, queryHeight)
sharedParams, err := sqc.GetParams(ctx)
if err != nil {
return 0, err
}
Expand All @@ -53,11 +57,15 @@ func (sqc *sharedKeeperQueryClient) GetSessionGracePeriodEndHeight(

// GetClaimWindowOpenHeight returns the block height at which the claim window of
// the session that includes queryHeight opens.
//
// TODO_MAINNET(@bryanchriswhite, #931): We don't really want to use the current value of the params.
// Instead, we should be using the value that the params had for the session given by queryHeight.
func (sqc *sharedKeeperQueryClient) GetClaimWindowOpenHeight(
ctx context.Context,
queryHeight int64,
) (int64, error) {
sharedParams, err := sqc.GetParamsAtHeight(ctx, queryHeight)
// TODO_MAINNET(#931): sqc.GetParamsAtHeight(ctx, queryHeight)
sharedParams, err := sqc.GetParams(ctx)
if err != nil {
return 0, err
}
Expand All @@ -67,11 +75,15 @@ func (sqc *sharedKeeperQueryClient) GetClaimWindowOpenHeight(

// GetProofWindowOpenHeight returns the block height at which the proof window of
// the session that includes queryHeight opens.
//
// TODO_MAINNET(@bryanchriswhite, #931): We don't really want to use the current value of the params.
// Instead, we should be using the value that the params had for the session given by queryHeight.
func (sqc *sharedKeeperQueryClient) GetProofWindowOpenHeight(
ctx context.Context,
queryHeight int64,
) (int64, error) {
sharedParams, err := sqc.GetParamsAtHeight(ctx, queryHeight)
// TODO_MAINNET(#931): sqc.GetParamsAtHeight(ctx, queryHeight)
sharedParams, err := sqc.GetParams(ctx)
if err != nil {
return 0, err
}
Expand All @@ -81,12 +93,16 @@ func (sqc *sharedKeeperQueryClient) GetProofWindowOpenHeight(

// GetEarliestSupplierClaimCommitHeight returns the earliest block height at which a claim
// for the session that includes queryHeight can be committed for a given supplier.
//
// TODO_MAINNET(@bryanchriswhite, #931): We don't really want to use the current value of the params.
// Instead, we should be using the value that the params had for the session given by queryHeight.
func (sqc *sharedKeeperQueryClient) GetEarliestSupplierClaimCommitHeight(
ctx context.Context,
queryHeight int64,
supplierOperatorAddr string,
) (int64, error) {
sharedParams, err := sqc.GetParamsAtHeight(ctx, queryHeight)
// TODO_MAINNET(#931): sqc.GetParamsAtHeight(ctx, queryHeight)
sharedParams, err := sqc.GetParams(ctx)
if err != nil {
return 0, err
}
Expand All @@ -109,12 +125,16 @@ func (sqc *sharedKeeperQueryClient) GetEarliestSupplierClaimCommitHeight(

// GetEarliestSupplierProofCommitHeight returns the earliest block height at which a proof
// for the session that includes queryHeight can be committed for a given supplier.
//
// TODO_MAINNET(@bryanchriswhite, #931): We don't really want to use the current value of the params.
// Instead, we should be using the value that the params had for the session given by queryHeight.
func (sqc *sharedKeeperQueryClient) GetEarliestSupplierProofCommitHeight(
ctx context.Context,
queryHeight int64,
supplierOperatorAddr string,
) (int64, error) {
sharedParams, err := sqc.GetParamsAtHeight(ctx, queryHeight)
// TODO_MAINNET(#931): sqc.GetParamsAtHeight(ctx, queryHeight)
sharedParams, err := sqc.GetParams(ctx)
if err != nil {
return 0, err
}
Expand All @@ -138,8 +158,12 @@ func (sqc *sharedKeeperQueryClient) GetEarliestSupplierProofCommitHeight(
// GetComputeUnitsToTokensMultiplier returns the multiplier used to convert compute
// units to tokens. The caller likely SHOULD pass the session start height for queryHeight
// as to avoid miscalculations in scenarios where the params were changed mid-session.
//
// TODO_MAINNET(@bryanchriswhite, #931): We don't really want to use the current value of the params.
// Instead, we should be using the value that the params had for the session given by queryHeight.
func (sqc *sharedKeeperQueryClient) GetComputeUnitsToTokensMultiplier(ctx context.Context, queryHeight int64) (uint64, error) {
sharedParams, err := sqc.GetParamsAtHeight(ctx, queryHeight)
// TODO_MAINNET(#931): sqc.GetParamsAtHeight(ctx, queryHeight)
sharedParams, err := sqc.GetParams(ctx)
if err != nil {
return 0, err
}
Expand Down
23 changes: 9 additions & 14 deletions x/shared/keeper/params_query_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewKeeperParamsQuerier[P any, K paramsKeeperIface[P]](
) *keeperParamsQuerier[P, K] {
// Use sensible defaults for keeper-based params cache
defaultOpts := []cache.QueryCacheOptionFn{
cache.WithHistoricalMode(100), // Keep history of last 100 blocks
//cache.WithHistoricalMode(100), // Keep history of last 100 blocks
cache.WithEvictionPolicy(cache.FirstInFirstOut),
}
opts = append(defaultOpts, opts...)
Expand Down Expand Up @@ -67,17 +67,12 @@ func (kpq *keeperParamsQuerier[P, K]) GetParams(ctx context.Context) (*P, error)
}

// GetParamsAtHeight retrieves parameters as they were at a specific height
func (kpq *keeperParamsQuerier[P, K]) GetParamsAtHeight(ctx context.Context, height int64) (*P, error) {
// Try cache first
cached, err := kpq.cache.GetAtHeight("params", height)
if err == nil {
return &cached, nil
}
if err != nil && !errors.Is(err, cache.ErrCacheMiss) {
return &cached, err
}

// For now, return current params as historical params are not yet implemented
// TODO_MAINNET: Implement historical parameter querying from state
return kpq.GetParams(ctx)
//
// TODO_MAINNET(@bryanchriswhite, #931): Integrate with indexer module/mixin once available.
// Currently, this method is (and MUST) NEVER called on-chain and only exists to satisfy the
// client.ParamsQuerier interface. However, it will be needed as part of #931 to support
// querying for params at historical heights, so it's short-circuited for now to always
// return an error.
func (kpq *keeperParamsQuerier[P, K]) GetParamsAtHeight(_ context.Context, _ int64) (*P, error) {
return nil, fmt.Errorf("TODO(#931): Support on-chain historical queries")
}

0 comments on commit fd61a91

Please sign in to comment.