@@ -14,9 +14,20 @@ type cacheableBlock interface {
1414// These caching errors should never happen, and if they do, they should not be blocking
1515
1616func (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
4467func 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
4877func 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
5287func getObjFromCache [T cacheableBlock ](c TimedCache , lookUpKey []byte ) T {
0 commit comments