Skip to content

Commit 93f69bf

Browse files
committed
FEAT: Test block processor cache
1 parent 6d65d6b commit 93f69bf

File tree

3 files changed

+165
-7
lines changed

3 files changed

+165
-7
lines changed

process/blockProcessor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func NewBlockProcessor(proc Processor) (*BlockProcessor, error) {
5757
func (bp *BlockProcessor) GetBlockByHash(shardID uint32, hash string, options common.BlockQueryOptions) (*data.BlockApiResponse, error) {
5858
scope := fmt.Sprintf("block:shardID=%d", shardID)
5959
if cached := getObjectFromCache[*data.BlockApiResponse](bp.cache, scope, hash, nil, options); cached != nil {
60-
return *cached, nil
60+
return cached, nil
6161
}
6262

6363
observers, err := bp.getObserversOrFullHistoryNodes(shardID)
@@ -88,7 +88,7 @@ func (bp *BlockProcessor) GetBlockByHash(shardID uint32, hash string, options co
8888
func (bp *BlockProcessor) GetBlockByNonce(shardID uint32, nonce uint64, options common.BlockQueryOptions) (*data.BlockApiResponse, error) {
8989
scope := fmt.Sprintf("block:shardID=%d", shardID)
9090
if cached := getObjectFromCache[*data.BlockApiResponse](bp.cache, scope, "", &nonce, options); cached != nil {
91-
return *cached, nil
91+
return cached, nil
9292
}
9393

9494
observers, err := bp.getObserversOrFullHistoryNodes(shardID)
@@ -126,7 +126,7 @@ func (bp *BlockProcessor) getObserversOrFullHistoryNodes(shardID uint32) ([]*dat
126126
// GetHyperBlockByHash returns the hyperblock by hash
127127
func (bp *BlockProcessor) GetHyperBlockByHash(hash string, options common.HyperblockQueryOptions) (*data.HyperblockApiResponse, error) {
128128
if cached := getObjectFromCache[*data.HyperblockApiResponse](bp.cache, "hyperblock", hash, nil, options); cached != nil {
129-
return *cached, nil
129+
return cached, nil
130130
}
131131

132132
builder := &hyperblockBuilder{}
@@ -200,7 +200,7 @@ func (bp *BlockProcessor) getAlteredAccountsIfNeeded(options common.HyperblockQu
200200
// GetHyperBlockByNonce returns the hyperblock by nonce
201201
func (bp *BlockProcessor) GetHyperBlockByNonce(nonce uint64, options common.HyperblockQueryOptions) (*data.HyperblockApiResponse, error) {
202202
if cached := getObjectFromCache[*data.HyperblockApiResponse](bp.cache, "hyperblock", "", &nonce, options); cached != nil {
203-
return *cached, nil
203+
return cached, nil
204204
}
205205

206206
builder := &hyperblockBuilder{}

process/blockProcessorCache.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ func (bp *BlockProcessor) cacheObject(obj cacheableBlock, scope string, opts int
3737
bp.cache.Put(makeNonceCacheKey(scope, obj.Nonce(), opts), objKey, 0)
3838
}
3939

40-
func getObjectFromCache[T cacheableBlock](c TimedCache, scope string, hash string, nonce *uint64, opts interface{}) *T {
40+
func getObjectFromCache[T cacheableBlock](c TimedCache, scope string, hash string, nonce *uint64, opts interface{}) T {
41+
var retObj T
42+
4143
var key interface{}
4244
if hash != "" {
4345
key, _ = c.Get(makeHashCacheKey(scope, hash, opts))
@@ -48,8 +50,9 @@ func getObjectFromCache[T cacheableBlock](c TimedCache, scope string, hash strin
4850
if key != nil {
4951
val, ok := c.Get(key.([]byte))
5052
if ok {
51-
return val.(*T)
53+
retObj = val.(T)
5254
}
5355
}
54-
return nil
56+
57+
return retObj
5558
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package process
2+
3+
import (
4+
"testing"
5+
6+
"github.com/multiversx/mx-chain-core-go/data/api"
7+
"github.com/multiversx/mx-chain-proxy-go/common"
8+
"github.com/multiversx/mx-chain-proxy-go/data"
9+
"github.com/multiversx/mx-chain-proxy-go/process/mock"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestBlockProcessorCache(t *testing.T) {
14+
t.Parallel()
15+
16+
bp, _ := NewBlockProcessor(&mock.ProcessorStub{})
17+
18+
nonceBlock1 := uint64(1)
19+
nonceBlock2 := uint64(2)
20+
hashBlock1 := "hashBlock1"
21+
hashBlock2 := "hashBlock2"
22+
scope1 := "block:shard=1"
23+
scope2 := "block:shard=2"
24+
opts1 := common.BlockQueryOptions{WithTransactions: true}
25+
opts2 := common.BlockQueryOptions{WithLogs: true}
26+
27+
blockApi1 := &data.BlockApiResponse{
28+
Data: data.BlockApiResponsePayload{
29+
Block: api.Block{
30+
Nonce: nonceBlock1,
31+
Hash: hashBlock1,
32+
},
33+
},
34+
}
35+
blockApi2 := &data.BlockApiResponse{
36+
Data: data.BlockApiResponsePayload{
37+
Block: api.Block{
38+
Nonce: nonceBlock2,
39+
Hash: hashBlock2,
40+
},
41+
},
42+
}
43+
44+
require.Nil(t, getObjectFromCache[*data.BlockApiResponse](
45+
bp.cache,
46+
scope1,
47+
hashBlock1,
48+
nil,
49+
opts1,
50+
))
51+
require.Nil(t, getObjectFromCache[*data.BlockApiResponse](
52+
bp.cache,
53+
scope1,
54+
"",
55+
&nonceBlock1,
56+
opts1,
57+
))
58+
59+
bp.cacheObject(blockApi1, scope1, opts1)
60+
require.Nil(t, getObjectFromCache[*data.BlockApiResponse](
61+
bp.cache,
62+
scope2,
63+
hashBlock1,
64+
nil,
65+
opts1,
66+
))
67+
require.Nil(t, getObjectFromCache[*data.BlockApiResponse](
68+
bp.cache,
69+
scope1,
70+
hashBlock1,
71+
nil,
72+
opts2,
73+
))
74+
75+
require.Equal(t, blockApi1, getObjectFromCache[*data.BlockApiResponse](
76+
bp.cache,
77+
scope1,
78+
hashBlock1,
79+
nil,
80+
opts1,
81+
))
82+
require.Equal(t, blockApi1, getObjectFromCache[*data.BlockApiResponse](
83+
bp.cache,
84+
scope1,
85+
"",
86+
&nonceBlock1,
87+
opts1,
88+
))
89+
90+
bp.cacheObject(blockApi2, scope2, opts2)
91+
require.Nil(t, getObjectFromCache[*data.BlockApiResponse](
92+
bp.cache,
93+
scope2,
94+
hashBlock2,
95+
nil,
96+
opts1,
97+
))
98+
require.Equal(t, blockApi2, getObjectFromCache[*data.BlockApiResponse](
99+
bp.cache,
100+
scope2,
101+
hashBlock2,
102+
nil,
103+
opts2,
104+
))
105+
require.Equal(t, blockApi2, getObjectFromCache[*data.BlockApiResponse](
106+
bp.cache,
107+
scope2,
108+
"",
109+
&nonceBlock2,
110+
opts2,
111+
))
112+
113+
scopeHyperBlock := "hyperBlock"
114+
hyperBlockNonce := uint64(4)
115+
hyperBlockHash := "hyperBlockHash"
116+
hyperBlock := &data.HyperblockApiResponse{
117+
Data: data.HyperblockApiResponsePayload{
118+
Hyperblock: api.Hyperblock{
119+
Hash: hyperBlockHash,
120+
Nonce: hyperBlockNonce,
121+
},
122+
},
123+
}
124+
125+
bp.cacheObject(hyperBlock, scopeHyperBlock, opts1)
126+
bp.cacheObject(hyperBlock, scopeHyperBlock, opts2)
127+
require.Equal(t, hyperBlock, getObjectFromCache[*data.HyperblockApiResponse](
128+
bp.cache,
129+
scopeHyperBlock,
130+
hyperBlockHash,
131+
nil,
132+
opts2,
133+
))
134+
require.Equal(t, hyperBlock, getObjectFromCache[*data.HyperblockApiResponse](
135+
bp.cache,
136+
scopeHyperBlock,
137+
"",
138+
&hyperBlockNonce,
139+
opts2,
140+
))
141+
require.Equal(t, hyperBlock, getObjectFromCache[*data.HyperblockApiResponse](
142+
bp.cache,
143+
scopeHyperBlock,
144+
hyperBlockHash,
145+
nil,
146+
opts1,
147+
))
148+
require.Equal(t, hyperBlock, getObjectFromCache[*data.HyperblockApiResponse](
149+
bp.cache,
150+
scopeHyperBlock,
151+
"",
152+
&hyperBlockNonce,
153+
opts1,
154+
))
155+
}

0 commit comments

Comments
 (0)