Skip to content

Commit 77c182a

Browse files
committed
FEAT: Inject timed cache in block processor
1 parent c201a97 commit 77c182a

File tree

10 files changed

+128
-81
lines changed

10 files changed

+128
-81
lines changed

cmd/proxy/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,18 @@ func createVersionsRegistry(
512512
return nil, err
513513
}
514514

515-
closableComponents.Add(nodeGroupProc, valStatsProc, nodeStatusProc, bp)
515+
timedCache, err := cache.NewTimeCacher(time.Second * 5)
516+
if err != nil {
517+
return nil, err
518+
}
519+
520+
closableComponents.Add(nodeGroupProc, valStatsProc, nodeStatusProc, bp, timedCache)
516521

517522
nodeGroupProc.StartCacheUpdate()
518523
valStatsProc.StartCacheUpdate()
519524
nodeStatusProc.StartCacheUpdate()
520525

521-
blockProc, err := process.NewBlockProcessor(bp)
526+
blockProc, err := process.NewBlockProcessor(bp, timedCache)
522527
if err != nil {
523528
return nil, err
524529
}

data/interface.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package data
22

3-
func (h *HyperblockApiResponse) ID() string {
4-
return h.Data.Hyperblock.Hash
5-
}
6-
73
func (h *HyperblockApiResponse) Hash() string {
84
return h.Data.Hyperblock.Hash
95
}
@@ -12,10 +8,6 @@ func (h *HyperblockApiResponse) Nonce() uint64 {
128
return h.Data.Hyperblock.Nonce
139
}
1410

15-
func (h *BlockApiResponse) ID() string {
16-
return h.Data.Block.Hash
17-
}
18-
1911
func (h *BlockApiResponse) Hash() string {
2012
return h.Data.Block.Hash
2113
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ func (mock *TimedCacheMock) Get(key []byte) (value interface{}, ok bool) {
2121
val, found := mock.Cache[string(key)]
2222
return val, found
2323
}
24+
25+
// Close -
26+
func (mock *TimedCacheMock) Close() error {
27+
return nil
28+
}
29+
30+
// IsInterfaceNil -
31+
func (mock *TimedCacheMock) IsInterfaceNil() bool {
32+
return mock == nil
33+
}

facade/mock/timedCacheStub.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package mock
2+
3+
// TimedCacheStub -
4+
type TimedCacheStub struct {
5+
}
6+
7+
// Put -
8+
func (stub *TimedCacheStub) Put(_ []byte, _ interface{}) error {
9+
return nil
10+
}
11+
12+
// Get -
13+
func (stub *TimedCacheStub) Get(_ []byte) (value interface{}, ok bool) {
14+
return nil, false
15+
}
16+
17+
// Close -
18+
func (stub *TimedCacheStub) Close() error {
19+
return nil
20+
}
21+
22+
// IsInterfaceNil -
23+
func (stub *TimedCacheStub) IsInterfaceNil() bool {
24+
return stub == nil
25+
}

process/blockProcessor.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/multiversx/mx-chain-core-go/data/api"
1010
"github.com/multiversx/mx-chain-proxy-go/common"
1111
"github.com/multiversx/mx-chain-proxy-go/data"
12-
"github.com/multiversx/mx-chain-proxy-go/facade/mock"
1312
)
1413

1514
const (
@@ -48,13 +47,17 @@ type BlockProcessor struct {
4847
}
4948

5049
// NewBlockProcessor will create a new block processor
51-
func NewBlockProcessor(proc Processor) (*BlockProcessor, error) {
50+
func NewBlockProcessor(proc Processor, cache TimedCache) (*BlockProcessor, error) {
5251
if check.IfNil(proc) {
5352
return nil, ErrNilCoreProcessor
5453
}
54+
if check.IfNil(cache) {
55+
return nil, ErrNilTimedCache
56+
}
57+
5558
return &BlockProcessor{
5659
proc: proc,
57-
cache: mock.NewTimedCacheMock(),
60+
cache: cache,
5861
}, nil
5962
}
6063

process/blockProcessorCache.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,13 @@ import (
66
)
77

88
type cacheableBlock interface {
9-
ID() string
109
Hash() string
1110
Nonce() uint64
1211
}
1312

14-
// No error checks or returns in for this cache.
13+
// No error checks for this cache.
1514
// These caching errors should never happen, and if they do, they should not be blocking
1615

17-
func makeHashCacheKey(scope string, hash string, opts interface{}) []byte {
18-
optBytes, _ := json.Marshal(opts)
19-
return []byte(fmt.Sprintf("%s:hash:%s|opts:%s", scope, hash, string(optBytes)))
20-
}
21-
22-
func makeNonceCacheKey(scope string, nonce uint64, opts interface{}) []byte {
23-
optBytes, _ := json.Marshal(opts)
24-
return []byte(fmt.Sprintf("%s:nonce:%d|opts:%s", scope, nonce, string(optBytes)))
25-
}
26-
27-
func makeObjKey(scope string, hash string, opts interface{}) []byte {
28-
optBytes, _ := json.Marshal(opts)
29-
return []byte(scope + ":" + hash + "|" + string(optBytes))
30-
}
31-
3216
func (bp *BlockProcessor) cacheObject(obj cacheableBlock, scope string, opts interface{}) {
3317
objKey := makeObjKey(scope, obj.Hash(), opts)
3418

@@ -40,6 +24,21 @@ func (bp *BlockProcessor) cacheObject(obj cacheableBlock, scope string, opts int
4024
_ = bp.cache.Put(makeNonceCacheKey(scope, obj.Nonce(), opts), objKey)
4125
}
4226

27+
func makeObjKey(scope string, hash string, opts interface{}) []byte {
28+
optBytes, _ := json.Marshal(opts)
29+
return []byte(scope + ":" + hash + "|" + string(optBytes))
30+
}
31+
32+
func makeHashCacheKey(scope string, hash string, opts interface{}) []byte {
33+
optBytes, _ := json.Marshal(opts)
34+
return []byte(fmt.Sprintf("%s:hash:%s|opts:%s", scope, hash, string(optBytes)))
35+
}
36+
37+
func makeNonceCacheKey(scope string, nonce uint64, opts interface{}) []byte {
38+
optBytes, _ := json.Marshal(opts)
39+
return []byte(fmt.Sprintf("%s:nonce:%d|opts:%s", scope, nonce, string(optBytes)))
40+
}
41+
4342
func getObjectFromCacheWithHash[T cacheableBlock](c TimedCache, scope string, hash string, opts interface{}) T {
4443
return getObjFromCache[T](c, makeHashCacheKey(scope, hash, opts))
4544
}

process/blockProcessorCache_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func getObjectFromCache[T cacheableBlock](c TimedCache, scope string, hash strin
2424
func TestBlockProcessorCache(t *testing.T) {
2525
t.Parallel()
2626

27-
bp, _ := NewBlockProcessor(&mock.ProcessorStub{})
27+
mockCache := facadeMock.NewTimedCacheMock()
28+
bp, _ := NewBlockProcessor(&mock.ProcessorStub{}, mockCache)
2829

2930
nonceBlock1 := uint64(1)
3031
nonceBlock2 := uint64(2)
@@ -177,8 +178,6 @@ func TestBlockProcessorCache(t *testing.T) {
177178

178179
opts1Str, _ := json.Marshal(&opts1)
179180
opts2Str, _ := json.Marshal(&opts2)
180-
181-
mockCache := bp.cache.(*facadeMock.TimedCacheMock)
182181
require.Len(t, mockCache.Cache, 12)
183182

184183
expectedObjKeys := []string{

0 commit comments

Comments
 (0)