@@ -175,6 +175,22 @@ func unmarshalBlock(pbBlock *pb.RawFullBlock) (*types.BlockWithExtractedData, er
175175 return raw .DecodeBytes ()
176176}
177177
178+ type blockByNumberCache struct {
179+ blockByHash * execution.BlockByHashAccessor
180+ blockHashByNumber * execution.BlockHashByNumberAccessor
181+ }
182+
183+ func (b blockByNumberCache ) Get (
184+ tx db.RoTx , shardId types.ShardId , num types.BlockNumber ,
185+ ) (* types.RawBlockWithExtractedData , error ) {
186+ hash , err := b .blockHashByNumber .Get (tx , shardId , num )
187+ if err != nil {
188+ return nil , err
189+ }
190+
191+ return b .blockByHash .Get (tx , shardId , hash )
192+ }
193+
178194func SetBlockRequestHandler (
179195 ctx context.Context , networkManager network.Manager , shardId types.ShardId , database db.DB , logger logging.Logger ,
180196) {
@@ -183,8 +199,10 @@ func SetBlockRequestHandler(
183199 return
184200 }
185201
186- // Sharing accessor between all handlers enables caching.
187- accessor := execution .NewStateAccessor (128 , 0 )
202+ accessor := & blockByNumberCache {
203+ blockByHash : execution .NewBlockByHashAccessor (128 ),
204+ blockHashByNumber : execution .NewBlockHashByNumberAccessor (),
205+ }
188206 handler := func (s network.Stream ) {
189207 if err := s .SetDeadline (time .Now ().Add (requestTimeout )); err != nil {
190208 return
@@ -212,15 +230,8 @@ func SetBlockRequestHandler(
212230 }
213231 defer tx .Rollback ()
214232
215- acc := accessor .RawAccess (tx , shardId ).
216- GetBlock ().
217- WithOutTransactions ().
218- WithInTransactions ().
219- WithChildBlocks ().
220- WithConfig ()
221-
222233 for id := blockReq .GetId (); ; id ++ {
223- resp , err := acc . ByNumber ( types .BlockNumber (id ))
234+ resp , err := accessor . Get ( tx , shardId , types .BlockNumber (id ))
224235 if err != nil {
225236 if ! errors .Is (err , db .ErrKeyNotFound ) {
226237 logError (logger , err , "DB error" )
@@ -229,11 +240,11 @@ func SetBlockRequestHandler(
229240 }
230241
231242 b := & pb.RawFullBlock {
232- BlockBytes : resp .Block () ,
233- OutTransactionsBytes : resp .OutTransactions () ,
234- InTransactionsBytes : resp .InTransactions () ,
235- ChildBlocks : pb .PackHashes (resp .ChildBlocks () ),
236- Config : resp .Config () ,
243+ BlockBytes : resp .Block ,
244+ OutTransactionsBytes : resp .OutTransactions ,
245+ InTransactionsBytes : resp .InTransactions ,
246+ ChildBlocks : pb .PackHashes (resp .ChildBlocks ),
247+ Config : resp .Config ,
237248 }
238249
239250 if err := writeBlockToStream (s , b ); err != nil {
0 commit comments