@@ -55,6 +55,11 @@ func NewBlockProcessor(proc Processor) (*BlockProcessor, error) {
5555
5656// GetBlockByHash will return the block based on its hash
5757func (bp * BlockProcessor ) GetBlockByHash (shardID uint32 , hash string , options common.BlockQueryOptions ) (* data.BlockApiResponse , error ) {
58+ scope := fmt .Sprintf ("block:shardID=%d" , shardID )
59+ if cached := getObjectFromCache [* data.BlockApiResponse ](bp .cache , scope , hash , nil , options ); cached != nil {
60+ return * cached , nil
61+ }
62+
5863 observers , err := bp .getObserversOrFullHistoryNodes (shardID )
5964 if err != nil {
6065 return nil , err
@@ -64,23 +69,28 @@ func (bp *BlockProcessor) GetBlockByHash(shardID uint32, hash string, options co
6469
6570 response := data.BlockApiResponse {}
6671 for _ , observer := range observers {
67-
6872 _ , err := bp .proc .CallGetRestEndPoint (observer .Address , path , & response )
6973 if err != nil {
7074 log .Error ("block request" , "observer" , observer .Address , "error" , err .Error ())
7175 continue
7276 }
7377
7478 log .Info ("block request" , "shard id" , observer .ShardId , "hash" , hash , "observer" , observer .Address )
75- return & response , nil
7679
80+ bp .cacheObject (& response , scope , options )
81+ return & response , nil
7782 }
7883
7984 return nil , WrapObserversError (response .Error )
8085}
8186
8287// GetBlockByNonce will return the block based on the nonce
8388func (bp * BlockProcessor ) GetBlockByNonce (shardID uint32 , nonce uint64 , options common.BlockQueryOptions ) (* data.BlockApiResponse , error ) {
89+ scope := fmt .Sprintf ("block:shardID=%d" , shardID )
90+ if cached := getObjectFromCache [* data.BlockApiResponse ](bp .cache , scope , "" , & nonce , options ); cached != nil {
91+ return * cached , nil
92+ }
93+
8494 observers , err := bp .getObserversOrFullHistoryNodes (shardID )
8595 if err != nil {
8696 return nil , err
@@ -90,16 +100,15 @@ func (bp *BlockProcessor) GetBlockByNonce(shardID uint32, nonce uint64, options
90100
91101 response := data.BlockApiResponse {}
92102 for _ , observer := range observers {
93-
94103 _ , err := bp .proc .CallGetRestEndPoint (observer .Address , path , & response )
95104 if err != nil {
96105 log .Error ("block request" , "observer" , observer .Address , "error" , err .Error ())
97106 continue
98107 }
99108
100109 log .Info ("block request" , "shard id" , observer .ShardId , "nonce" , nonce , "observer" , observer .Address )
110+ bp .cacheObject (& response , scope , options )
101111 return & response , nil
102-
103112 }
104113
105114 return nil , WrapObserversError (response .Error )
@@ -116,8 +125,8 @@ func (bp *BlockProcessor) getObserversOrFullHistoryNodes(shardID uint32) ([]*dat
116125
117126// GetHyperBlockByHash returns the hyperblock by hash
118127func (bp * BlockProcessor ) GetHyperBlockByHash (hash string , options common.HyperblockQueryOptions ) (* data.HyperblockApiResponse , error ) {
119- if hyperBlock := bp . getHyperblockFromCache ( hash , nil , options ); hyperBlock != nil {
120- return hyperBlock , nil
128+ if cached := getObjectFromCache [ * data. HyperblockApiResponse ]( bp . cache , "hyperblock" , hash , nil , options ); cached != nil {
129+ return * cached , nil
121130 }
122131
123132 builder := & hyperblockBuilder {}
@@ -143,8 +152,7 @@ func (bp *BlockProcessor) GetHyperBlockByHash(hash string, options common.Hyperb
143152
144153 hyperblock := builder .build (options .NotarizedAtSource )
145154 hyperBlockRsp := data .NewHyperblockApiResponse (hyperblock )
146-
147- bp .cacheHyperblock (hyperBlockRsp , options )
155+ bp .cacheObject (hyperBlockRsp , "hyperblock" , options )
148156
149157 return hyperBlockRsp , nil
150158}
@@ -191,6 +199,10 @@ func (bp *BlockProcessor) getAlteredAccountsIfNeeded(options common.HyperblockQu
191199
192200// GetHyperBlockByNonce returns the hyperblock by nonce
193201func (bp * BlockProcessor ) GetHyperBlockByNonce (nonce uint64 , options common.HyperblockQueryOptions ) (* data.HyperblockApiResponse , error ) {
202+ if cached := getObjectFromCache [* data.HyperblockApiResponse ](bp .cache , "hyperblock" , "" , & nonce , options ); cached != nil {
203+ return * cached , nil
204+ }
205+
194206 builder := & hyperblockBuilder {}
195207
196208 blockQueryOptions := common.BlockQueryOptions {
@@ -213,7 +225,10 @@ func (bp *BlockProcessor) GetHyperBlockByNonce(nonce uint64, options common.Hype
213225 }
214226
215227 hyperblock := builder .build (options .NotarizedAtSource )
216- return data .NewHyperblockApiResponse (hyperblock ), nil
228+ hyperBlockRsp := data .NewHyperblockApiResponse (hyperblock )
229+ bp .cacheObject (hyperBlockRsp , "hyperblock" , options )
230+
231+ return hyperBlockRsp , nil
217232}
218233
219234// GetInternalBlockByHash will return the internal block based on its hash
0 commit comments