reilabs / starknet-replay

CLI tool to replay Starknet transactions and profile libfuncs usage.
Apache License 2.0
0 stars 0 forks source link

Implement caching of RPC responses #38

Closed Eagle941 closed 1 month ago

Eagle941 commented 3 months ago

The move from Pathfinder database to RPC protocol for querying Starknet data slowed down the replayer.

An optimisation to speed up the replayer is to cache RPC responses due to some data being queried multiple times during transaction replay.

These endpoints return a single value:

These endpoints returns a complex object:

The following endpoints are not worth caching because the result is used only once:

An appropriate cache takes into consideration:

starknet_getClass could use a LFU policy because a small percentage of classes uses in Starknet is the most frequently called. starknet_getBlockWithReceipts can have a count that after 3 calls the data is evicted because the block header is called only three times. An alternative is to structure the code such that this endpoint is called only once. starknet_getClassHashAt and starknet_getStorageAt can also use an LFU policy. starknet_chainId always returns the same value for a whole transaction replay.

Because of the different objects to be cached, it could be optimal to have multiple caches: one for each endpoint.

Investigate if there is a crate already suitable for this task without rolling our own cache. Moka can be a good candidate to test.

Eagle941 commented 1 month ago

These are all the rpc client endpoints:

Here is the analysis of cache for each endpoint:

For this reason, caching RPC responses during parallel block replay is unlikely to be hit because it requires:

Existing replayer is performing at 7s/block with RPC server in localhost and 34s/block with blastapi.io server with 8 threads. This is meeting requirements.

However, for future exploration, one strategy is to replay blocks in groups of even numbers followed by odd numbers, this ensures that the second group is replayed with the previous block being already replayed.