Skip to content

Commit

Permalink
remove caching from get timeout height
Browse files Browse the repository at this point in the history
  • Loading branch information
jonator committed Jul 2, 2024
1 parent 12fcb5b commit 9738d08
Showing 1 changed file with 33 additions and 46 deletions.
79 changes: 33 additions & 46 deletions packages/server/src/queries/complex/get-timeout-height.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { Int } from "@keplr-wallet/unit";
import { Chain } from "@osmosis-labs/types";
import { getChain } from "@osmosis-labs/utils";
import cachified, { CacheEntry } from "cachified";
import { LRUCache } from "lru-cache";

import { queryRPCStatus } from "../../queries/cosmos";
import { DEFAULT_LRU_OPTIONS } from "../../utils/cache";

const timeoutCache = new LRUCache<string, CacheEntry>(DEFAULT_LRU_OPTIONS);
export async function getTimeoutHeight({
chainList,
chainId,
Expand All @@ -17,55 +13,46 @@ export async function getTimeoutHeight({
chainId?: string;
destinationAddress?: string;
}) {
return cachified({
cache: timeoutCache,
key: `timeout-height-${chainId}-${destinationAddress}`,
ttl: 1000 * 4, // 4 seconds (just less block time)
getFreshValue: async () => {
const destinationCosmosChain = getChain({
chainList,
chainId,
destinationAddress,
});
const destinationCosmosChain = getChain({
chainList,
chainId,
destinationAddress,
});

if (!destinationCosmosChain) {
throw new Error("Could not find destination Cosmos chain");
}
if (!destinationCosmosChain) {
throw new Error("Could not find destination Cosmos chain");
}

const destinationNodeStatus = await queryRPCStatus({
restUrl: destinationCosmosChain.apis.rpc[0].address,
});
const destinationNodeStatus = await queryRPCStatus({
restUrl: destinationCosmosChain.apis.rpc[0].address,
});

const network = destinationNodeStatus.result.node_info.network;
const latestBlockHeight =
destinationNodeStatus.result.sync_info.latest_block_height;
const network = destinationNodeStatus.result.node_info.network;
const latestBlockHeight =
destinationNodeStatus.result.sync_info.latest_block_height;

if (!network) {
throw new Error(
`Failed to fetch the network chain id of ${destinationCosmosChain.chain_id}`
);
}
if (!network) {
throw new Error(
`Failed to fetch the network chain id of ${destinationCosmosChain.chain_id}`
);
}

if (!latestBlockHeight || latestBlockHeight === "0") {
throw new Error(
`Failed to fetch the latest block of ${destinationCosmosChain.chain_id}`
);
}
if (!latestBlockHeight || latestBlockHeight === "0") {
throw new Error(
`Failed to fetch the latest block of ${destinationCosmosChain.chain_id}`
);
}

const revisionNumber = ChainIdHelper.parse(network).version.toString();
const revisionNumber = ChainIdHelper.parse(network).version.toString();

return {
/**
* Omit the revision_number if the chain's version is 0.
* Sending the value as 0 will cause the transaction to fail.
*/
revisionNumber: revisionNumber !== "0" ? revisionNumber : undefined,
revisionHeight: new Int(latestBlockHeight)
.add(new Int("150"))
.toString(),
};
},
});
return {
/**
* Omit the revision_number if the chain's version is 0.
* Sending the value as 0 will cause the transaction to fail.
*/
revisionNumber: revisionNumber !== "0" ? revisionNumber : undefined,
revisionHeight: new Int(latestBlockHeight).add(new Int("150")).toString(),
};
}
class ChainIdHelper {
// VersionFormatRegExp checks if a chainID is in the format required for parsing versions
Expand Down

0 comments on commit 9738d08

Please sign in to comment.