|
1 | 1 | import { retry } from '../vendor/deno-deps.js'
|
2 | 2 | import { RPC_URL, RPC_AUTH } from './constants.js'
|
3 | 3 |
|
| 4 | +async function getChainHead ({ maxAttempts = 5 } = {}) { |
| 5 | + try { |
| 6 | + const res = await retry(() => rpc('Filecoin.ChainHead'), { |
| 7 | + // The maximum amount of attempts until failure. |
| 8 | + maxAttempts, |
| 9 | + // The initial and minimum amount of milliseconds between attempts. |
| 10 | + minTimeout: 5_000, |
| 11 | + // How much to backoff after each retry. |
| 12 | + multiplier: 1.5 |
| 13 | + }) |
| 14 | + return res.Cids |
| 15 | + } catch (err) { |
| 16 | + if (err.name === 'RetryError' && err.cause) { |
| 17 | + // eslint-disable-next-line no-ex-assign |
| 18 | + err = err.cause |
| 19 | + } |
| 20 | + err.message = `Cannot obtain chain head: ${err.message}` |
| 21 | + throw err |
| 22 | + } |
| 23 | +} |
| 24 | + |
4 | 25 | /**
|
5 | 26 | * @param {string} minerId A miner actor id, e.g. `f0142637`
|
6 | 27 | * @param {object} options
|
7 | 28 | * @param {number} [options.maxAttempts]
|
8 | 29 | * @returns {Promise<string>} Miner's PeerId, e.g. `12D3KooWMsPmAA65yHAHgbxgh7CPkEctJHZMeM3rAvoW8CZKxtpG`
|
9 | 30 | */
|
10 | 31 | export async function getMinerPeerId (minerId, { maxAttempts = 5 } = {}) {
|
| 32 | + const chainHead = await getChainHead({ maxAttempts }) |
11 | 33 | try {
|
12 |
| - const res = await retry(() => rpc('Filecoin.StateMinerInfo', minerId, null), { |
| 34 | + const res = await retry(() => rpc('Filecoin.StateMinerInfo', minerId, chainHead), { |
13 | 35 | // The maximum amount of attempts until failure.
|
14 | 36 | maxAttempts,
|
15 | 37 | // The initial and minimum amount of milliseconds between attempts.
|
|
0 commit comments