hyperlane-xyz / hyperlane-monorepo

The home for Hyperlane core contracts, sdk packages, and other infrastructure
https://hyperlane.xyz
Other
338 stars 373 forks source link

Caching ISM configurations in the relayer #3746

Open tkporter opened 6 months ago

tkporter commented 6 months ago

Problem

ISMs tend to be pretty static, we can afford to cache ISM configurations for a given address

Solution

To have fewer RPCs when building metadata, cache ISM configs

daniel-savu commented 1 month ago

The goal of this task is twofold:

  1. Since we want caches to be short lived, find a robust LRU cache library (maybe lru-rs) that ideally also supports TTLs for invalidating entries. Otherwise, implement wrapper logic to add / check TTLs when writing to the cache and when checking if there's a hit.

    • to test cache effectiveness, either the library or the wrapper should expose metrics that can be plugged into prometheus. Relayer metrics all expose call counts for all evm RPC methods used - those should see a dramatic drop as well.
  2. Given the cache store, optimize the PendingMesage::prepare step for ISM metadata builders as much as possible

    • the cache map schema would look something like (contract_address, method, serialized_params) -> (serialized_call_result, entry_ttl), where method can be a string that's hardcoded in each MetadataBuilder implementation (e.g. validators_and_threshold for the MultisigISM)
    • MetadataBuilder would likely need two new methods to keep things generic:
    cache_call_result<T: Serialize, U: Serialize>(contract_address, method, serializable_params: T, call_result: U, ttl: <likely expressed as the timestamp of expiry>);
    
    get_cached_call_result<T: Deserialize>(contract_address, method, serialized_params) -> T;
    • These main calls to be cached, though there may be other easy wins (which should be cached as well in this task):
      • the validators_and_threshold call (here)
      • highest_known_leaf_index and get_merkle_leaf_id_by_message_id (here)
      • get_merkle_leaf_id_by_message_id (here)
      • get_offchain_verify_info (here)
      • modules_and_threshold (here)
      • in MessageMetadataBuilder, these calls

The implementation should be as generic as possible, to make it easy to add caching to other ISMs / network calls made by the agent in the future. TTLs can be hardcoded to 5 minutes.

Mantas-M commented 3 weeks ago

On it 🫡

Mantas-M commented 2 weeks ago

@daniel-savu Just a small clarification, you want to cache the entire ISMs during the building process? 🤔 Asking as doing so requires, everything of each InterchainSecurityModule implementation to be Serializable