Skip to content

Commit acb543e

Browse files
committed
FIX: After review pt 2
1 parent 9fc0379 commit acb543e

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

cmd/proxy/config/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# before it should be updated
1919
EconomicsMetricsCacheValidityDurationSec = 600 # 10 minutes
2020

21-
# BlockCacheDurationSec defines how long block/hyperblock results(queried by hash or nonce) are kept in cache, in seconds.
21+
# BlockCacheDurationSec defines how long block/hyperblock results (queried by hash or nonce) are kept in cache, in seconds.
2222
BlockCacheDurationSec = 30
2323

2424
# BalancedObservers - if this flag is set to true, then the requests will be distributed equally between observers.

process/blockProcessorCache.go

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@ type cacheableBlock interface {
1414
// These caching errors should never happen, and if they do, they should not be blocking
1515

1616
func (bp *BlockProcessor) cacheObject(obj cacheableBlock, scope string, opts interface{}) {
17-
objKey := makeObjKey(scope, obj.Hash(), opts)
18-
hashLookupKey := makeHashCacheKey(scope, obj.Hash(), opts)
19-
nonceLookupKey := makeNonceCacheKey(scope, obj.Nonce(), opts)
17+
objKey, err := makeObjKey(scope, obj.Hash(), opts)
18+
if err != nil {
19+
return
20+
}
21+
22+
hashLookupKey, err := makeHashCacheKey(scope, obj.Hash(), opts)
23+
if err != nil {
24+
return
25+
}
26+
27+
nonceLookupKey, err := makeNonceCacheKey(scope, obj.Nonce(), opts)
28+
if err != nil {
29+
return
30+
}
2031

2132
// Store object
2233
_ = bp.cache.Put(objKey, obj, 0)
@@ -26,27 +37,51 @@ func (bp *BlockProcessor) cacheObject(obj cacheableBlock, scope string, opts int
2637
_ = bp.cache.Put(nonceLookupKey, objKey, 0)
2738
}
2839

29-
func makeObjKey(scope string, hash string, opts interface{}) []byte {
30-
optBytes, _ := json.Marshal(opts)
31-
return []byte(fmt.Sprintf("%s:%s|opts:%s", scope, hash, string(optBytes)))
40+
func makeObjKey(scope string, hash string, opts interface{}) ([]byte, error) {
41+
optBytes, err := json.Marshal(opts)
42+
if err != nil {
43+
log.Error("makeObjKey", "error", err)
44+
return nil, err
45+
}
46+
return []byte(fmt.Sprintf("%s:%s|opts:%s", scope, hash, string(optBytes))), nil
3247
}
3348

34-
func makeHashCacheKey(scope string, hash string, opts interface{}) []byte {
35-
optBytes, _ := json.Marshal(opts)
36-
return []byte(fmt.Sprintf("%s:hash:%s|opts:%s", scope, hash, string(optBytes)))
49+
func makeHashCacheKey(scope string, hash string, opts interface{}) ([]byte, error) {
50+
optBytes, err := json.Marshal(opts)
51+
if err != nil {
52+
log.Error("makeHashCacheKey", "error", err)
53+
return nil, err
54+
}
55+
return []byte(fmt.Sprintf("%s:hash:%s|opts:%s", scope, hash, string(optBytes))), nil
3756
}
3857

39-
func makeNonceCacheKey(scope string, nonce uint64, opts interface{}) []byte {
40-
optBytes, _ := json.Marshal(opts)
41-
return []byte(fmt.Sprintf("%s:nonce:%d|opts:%s", scope, nonce, string(optBytes)))
58+
func makeNonceCacheKey(scope string, nonce uint64, opts interface{}) ([]byte, error) {
59+
optBytes, err := json.Marshal(opts)
60+
if err != nil {
61+
log.Error("makeNonceCacheKey", "error", err)
62+
return nil, err
63+
}
64+
return []byte(fmt.Sprintf("%s:nonce:%d|opts:%s", scope, nonce, string(optBytes))), nil
4265
}
4366

4467
func getObjectFromCacheWithHash[T cacheableBlock](c TimedCache, scope string, hash string, opts interface{}) T {
45-
return getObjFromCache[T](c, makeHashCacheKey(scope, hash, opts))
68+
var nilRet T
69+
hashKey, err := makeHashCacheKey(scope, hash, opts)
70+
if err != nil {
71+
return nilRet
72+
}
73+
74+
return getObjFromCache[T](c, hashKey)
4675
}
4776

4877
func getObjectFromCacheWithNonce[T cacheableBlock](c TimedCache, scope string, nonce uint64, opts interface{}) T {
49-
return getObjFromCache[T](c, makeNonceCacheKey(scope, nonce, opts))
78+
var nilRet T
79+
nonceKey, err := makeNonceCacheKey(scope, nonce, opts)
80+
if err != nil {
81+
return nilRet
82+
}
83+
84+
return getObjFromCache[T](c, nonceKey)
5085
}
5186

5287
func getObjFromCache[T cacheableBlock](c TimedCache, lookUpKey []byte) T {

process/blockProcessorCache_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import (
1515

1616
func getObjectFromCache[T cacheableBlock](c TimedCache, scope string, hash string, nonce *uint64, opts interface{}) T {
1717
if hash != "" {
18-
return getObjFromCache[T](c, makeHashCacheKey(scope, hash, opts))
18+
hashKey, _ := makeHashCacheKey(scope, hash, opts)
19+
return getObjFromCache[T](c, hashKey)
1920
}
2021

21-
return getObjFromCache[T](c, makeNonceCacheKey(scope, *nonce, opts))
22+
nonceKey, _ := makeNonceCacheKey(scope, *nonce, opts)
23+
return getObjFromCache[T](c, nonceKey)
2224
}
2325

2426
func TestBlockProcessorCache(t *testing.T) {

0 commit comments

Comments
 (0)