Skip to content

Commit

Permalink
Merge pull request #40 from ElrondNetwork/hotfix-caching
Browse files Browse the repository at this point in the history
Hotfix: hold block copies in the cache
  • Loading branch information
andreibancioiu authored Aug 28, 2022
2 parents 1457550 + 55a2f14 commit 3b83d14
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions server/provider/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ type blocksCache interface {
Put(key []byte, value interface{}, size int) (evicted bool)
Len() int
Keys() [][]byte
Clear()
}
6 changes: 4 additions & 2 deletions server/provider/networkProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,17 @@ func (provider *networkProvider) getBlockByNonceCached(nonce uint64) (*data.Bloc
if ok {
block, ok := blockUntyped.(*data.Block)
if ok {
return block, true
blockCopy := *block
return &blockCopy, true
}
}

return nil, false
}

func (provider *networkProvider) cacheBlockByNonce(nonce uint64, block *data.Block) {
_ = provider.blocksCache.Put(blockNonceToBytes(nonce), block, 1)
blockCopy := *block
_ = provider.blocksCache.Put(blockNonceToBytes(nonce), &blockCopy, 1)
}

// GetBlockByHash gets a block by hash
Expand Down
36 changes: 36 additions & 0 deletions server/provider/networkProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,42 @@ func TestNetworkProvider_DoGetBlockByNonce(t *testing.T) {

require.Equal(t, blocksCacheCapacity, provider.blocksCache.Len())
})

t.Run("the cache holds block copies", func(t *testing.T) {
provider.blocksCache.Clear()

observerFacade.GetBlockByNonceCalled = func(shardID uint32, nonce uint64, options common.BlockQueryOptions) (*data.BlockApiResponse, error) {
return &data.BlockApiResponse{
Data: data.BlockApiResponsePayload{
Block: data.Block{
Nonce: nonce,
MiniBlocks: []*data.MiniBlock{
{Hash: "aaaa"},
{Hash: "bbbb"},
},
},
},
}, nil
}

block, err := provider.doGetBlockByNonce(7)
require.Nil(t, err)
require.Equal(t, uint64(7), block.Nonce)
require.Len(t, block.MiniBlocks, 2)
require.Equal(t, 1, provider.blocksCache.Len())

// Simulate mutations performed by downstream handling of blocks, i.e. "simplifyBlockWithScheduledTransactions":
block.MiniBlocks = []*data.MiniBlock{}

cachedBlock, err := provider.doGetBlockByNonce(7)
require.Nil(t, err)
require.Equal(t, uint64(7), cachedBlock.Nonce)
// Miniblocks removal (above) does not reflect in the cached data
require.Len(t, cachedBlock.MiniBlocks, 2)
// ... because the cache holds block copies:
require.False(t, &block == &cachedBlock)
require.Equal(t, 1, provider.blocksCache.Len())
})
}

func createDefaultArgsNewNetworkProvider() ArgsNewNetworkProvider {
Expand Down
2 changes: 1 addition & 1 deletion version/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const (
RosettaVersion = "v1.4.12"

// RosettaMiddlewareVersion is the version of this package (application)
RosettaMiddlewareVersion = "v0.2.2"
RosettaMiddlewareVersion = "v0.2.3"

// NodeVersion is the canonical version of the node runtime
// TODO: We should fetch this from node/status.
Expand Down

0 comments on commit 3b83d14

Please sign in to comment.