From 48cb8d81da612f4f0a40dae7a97e99a66368a065 Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Tue, 28 Jan 2025 13:41:17 +0100 Subject: [PATCH] perf: add pass tipset to `StaterMinerInfo` (#105) This makes it easier for Glif to cache this call. --- lib/miner-info.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/miner-info.js b/lib/miner-info.js index 0a091ae..c9cd093 100644 --- a/lib/miner-info.js +++ b/lib/miner-info.js @@ -1,6 +1,27 @@ import { retry } from '../vendor/deno-deps.js' import { RPC_URL, RPC_AUTH } from './constants.js' +async function getChainHead ({ maxAttempts = 5 } = {}) { + try { + const res = await retry(() => rpc('Filecoin.ChainHead'), { + // The maximum amount of attempts until failure. + maxAttempts, + // The initial and minimum amount of milliseconds between attempts. + minTimeout: 5_000, + // How much to backoff after each retry. + multiplier: 1.5 + }) + return res.Cids + } catch (err) { + if (err.name === 'RetryError' && err.cause) { + // eslint-disable-next-line no-ex-assign + err = err.cause + } + err.message = `Cannot obtain chain head: ${err.message}` + throw err + } +} + /** * @param {string} minerId A miner actor id, e.g. `f0142637` * @param {object} options @@ -8,8 +29,9 @@ import { RPC_URL, RPC_AUTH } from './constants.js' * @returns {Promise} Miner's PeerId, e.g. `12D3KooWMsPmAA65yHAHgbxgh7CPkEctJHZMeM3rAvoW8CZKxtpG` */ export async function getMinerPeerId (minerId, { maxAttempts = 5 } = {}) { + const chainHead = await getChainHead({ maxAttempts }) try { - const res = await retry(() => rpc('Filecoin.StateMinerInfo', minerId, null), { + const res = await retry(() => rpc('Filecoin.StateMinerInfo', minerId, chainHead), { // The maximum amount of attempts until failure. maxAttempts, // The initial and minimum amount of milliseconds between attempts.