Skip to content

Commit f6c01f2

Browse files
denis-chernov-smartcontractro-texapp-token-issuer-data-feeds[bot]
authored
implemented adapter-specific cache key transform (#4764)
Co-authored-by: Ivaylo Novakov <inovakov@gmail.com> Co-authored-by: app-token-issuer-data-feeds[bot] <134377064+app-token-issuer-data-feeds[bot]@users.noreply.github.com>
1 parent d66b6a4 commit f6c01f2

4 files changed

Lines changed: 55 additions & 39 deletions

File tree

packages/streams-adapter/helpers/aliases.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,7 @@ func BuildCacheKeyParams(data map[string]interface{}) (types.RequestParams, erro
268268
}
269269
}
270270

271-
// Normalize all other parameters through the alias index if available.
272-
// Also, filter to only those canonical parameters that are required for this endpoint
273-
// when we have requirement metadata.
274-
requiredForEndpoint, haveReq := activeAliasIndex.requiredParams[canonicalEndpoint]
271+
overriddenKeys := make(map[string]bool)
275272

276273
for k, v := range data {
277274
// Ignore the `endpoint` and `overrides` keys.
@@ -287,35 +284,30 @@ func BuildCacheKeyParams(data map[string]interface{}) (types.RequestParams, erro
287284
continue
288285
}
289286

287+
canonicalKey := keyLower
288+
if pm, ok := activeAliasIndex.paramAlias[canonicalEndpoint]; ok {
289+
if pIdx, ok := pm[keyLower]; ok {
290+
canonicalKey = pIdx.CanonicalName
291+
}
292+
}
293+
290294
value := rawValue
291295

292296
// If there are per-adapter overrides, and the current value has an
293297
// override defined for the active adapter, use the override instead.
294298
if adapterOverrides != nil {
295299
if ov, ok := adapterOverrides[strings.ToUpper(rawValue)]; ok && ov != "" {
296300
value = ov
301+
overriddenKeys[canonicalKey] = true
297302
}
298303
}
299304

300-
valueUpper := strings.ToUpper(value)
301-
302-
canonicalKey := keyLower
303-
if pm, ok := activeAliasIndex.paramAlias[canonicalEndpoint]; ok {
304-
if pIdx, ok := pm[keyLower]; ok {
305-
canonicalKey = pIdx.CanonicalName
306-
}
307-
}
308-
309-
// If we don't have requirement metadata for this endpoint, keep all params.
310-
if !haveReq {
311-
out[canonicalKey] = valueUpper
312-
continue
313-
}
305+
out[canonicalKey] = strings.ToUpper(value)
306+
}
314307

315-
// Otherwise, only keep parameters that are marked as required.
316-
if required, ok := requiredForEndpoint[canonicalKey]; ok && required {
317-
out[canonicalKey] = valueUpper
318-
}
308+
// Apply adapter-specific cache key transform if registered.
309+
if transform, ok := adapterTransforms[activeAdapter]; ok {
310+
transform(out, overriddenKeys)
319311
}
320312

321313
return out, nil

packages/streams-adapter/helpers/aliases_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,12 @@ func TestBuildCacheKeyParams_BasicCanonical(t *testing.T) {
258258
"endpoint": "price",
259259
"base": "eth",
260260
"quote": "usd",
261-
"amount": "100",
262261
})
263262
require.NoError(t, err)
264263

265264
assertParam(t, result, "endpoint", "price")
266265
assertParam(t, result, "base", "ETH")
267266
assertParam(t, result, "quote", "USD")
268-
269-
// "amount" is not required, should be absent even if passed.
270-
if _, ok := result["amount"]; ok {
271-
t.Error("non-required param 'amount' should not be in output")
272-
}
273267
}
274268

275269
func TestBuildCacheKeyParams_EndpointAlias(t *testing.T) {

packages/streams-adapter/helpers/helpers_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,6 @@ func TestRequestParamsFromKey_ParamAliasesResolved(t *testing.T) {
7070
assertParam(t, result, "quote", "USD")
7171
}
7272

73-
func TestRequestParamsFromKey_NonRequiredParamsOmitted(t *testing.T) {
74-
initTestAdapter(t)
75-
76-
result, err := RequestParamsFromKey(`adapter-price-{"endpoint":"price","base":"eth","quote":"usd","amount":"100"}`)
77-
require.NoError(t, err)
78-
79-
if _, ok := result["amount"]; ok {
80-
t.Error("non-required param 'amount' should be filtered out")
81-
}
82-
}
83-
8473
func TestRequestParamsFromKey_AliasIndexNotInitialized(t *testing.T) {
8574
resetGlobals()
8675

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package helpers
2+
3+
import (
4+
types "streams-adapter/common"
5+
)
6+
7+
// CacheKeyTransformFunc transforms canonical request params for cache keying.
8+
// It receives the already-canonicalized params (with overrides applied) and a set
9+
// of canonical param names whose values were changed by adapter overrides.
10+
type CacheKeyTransformFunc func(params types.RequestParams, overriddenKeys map[string]bool)
11+
12+
// adapterTransforms maps adapter name to its cache key transform function.
13+
// To add support for a new adapter, define a transform function and register it here.
14+
var adapterTransforms = map[string]CacheKeyTransformFunc{
15+
"cfbenchmarks": cfbenchmarksTransform,
16+
}
17+
18+
// cfbenchmarksTransform computes a CFBenchmarks index from base/quote and removes
19+
// the individual base/quote params so the cache key uses only the index.
20+
// Applies to the "crypto" and "crypto-lwba" endpoints.
21+
func cfbenchmarksTransform(params types.RequestParams, overriddenKeys map[string]bool) {
22+
endpoint := params["endpoint"]
23+
if endpoint != "crypto" && endpoint != "crypto-lwba" {
24+
return
25+
}
26+
27+
base, hasBase := params["base"]
28+
if !hasBase {
29+
return
30+
}
31+
32+
if overriddenKeys["base"] {
33+
params["index"] = base
34+
} else if _, hasIndex := params["index"]; !hasIndex {
35+
quote := params["quote"]
36+
params["index"] = "U_" + base + quote + "_RTI"
37+
}
38+
params["adapterNameOverride"] = "cfbenchmarks2"
39+
delete(params, "base")
40+
delete(params, "quote")
41+
}

0 commit comments

Comments
 (0)