This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add index provider library #7
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b24b508
add library
00729f2
add dependencies
13eb682
revert multiaddr test
f66700e
resolve PR threads
8ddcbdc
fmt
6a1990f
changed import naming
49bbe56
changed import naming
2b56a25
changed default naming
998257f
change test naming
9a6b053
Merge branch 'main' into nhaimerl-add-index-probier-library
c024010
lint
cb0d03a
add abort signal
1f35d65
removed error message
8d747b3
merged with main
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,89 +1,39 @@ | ||
| import { retry } from '../vendor/deno-deps.js' | ||
| import { RPC_URL, RPC_AUTH } from './constants.js' | ||
| import { | ||
| getIndexProviderPeerId as getPeerId, | ||
| MINER_TO_PEERID_CONTRACT_ADDRESS, | ||
| MINER_TO_PEERID_CONTRACT_ABI | ||
| , ethers | ||
| } from '../vendor/deno-deps.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 | ||
| } | ||
| } | ||
| // Initialize your ethers contract instance | ||
| const fetchRequest = new ethers.FetchRequest(RPC_URL) | ||
| fetchRequest.setHeader('Authorization', `Bearer ${RPC_AUTH}`) | ||
| const provider = new ethers.JsonRpcProvider(fetchRequest) | ||
| const smartContractClient = new ethers.Contract( | ||
| MINER_TO_PEERID_CONTRACT_ADDRESS, | ||
| MINER_TO_PEERID_CONTRACT_ABI, | ||
| provider | ||
| ) | ||
|
|
||
| /** | ||
| * @param {string} minerId A miner actor id, e.g. `f0142637` | ||
| * @param {object} options | ||
| * @param {number} [options.maxAttempts] | ||
| * @returns {Promise<string>} Miner's PeerId, e.g. `12D3KooWMsPmAA65yHAHgbxgh7CPkEctJHZMeM3rAvoW8CZKxtpG` | ||
| * @param {string} minerId - The ID of the miner. | ||
| * @param {object} options - Options for the function. | ||
| * @param {number} options.maxAttempts - The maximum number of attempts to fetch the peer ID. | ||
| * @returns {Promise<string>} The peer ID of the miner. | ||
| */ | ||
| export async function getMinerPeerId (minerId, { maxAttempts = 5 } = {}) { | ||
| const chainHead = await getChainHead({ maxAttempts }) | ||
| export async function getIndexProviderPeerId (minerId, { maxAttempts = 5 } = {}) { | ||
| try { | ||
| const res = await retry(() => rpc('Filecoin.StateMinerInfo', minerId, chainHead), { | ||
| // The maximum amount of attempts until failure. | ||
| const { peerId, source } = await getPeerId(minerId, smartContractClient, { | ||
| rpcUrl: RPC_URL, | ||
| rpcAuth: RPC_AUTH, | ||
| maxAttempts, | ||
| // The initial and minimum amount of milliseconds between attempts. | ||
| minTimeout: 5_000, | ||
| // How much to backoff after each retry. | ||
| multiplier: 1.5 | ||
| signal: AbortSignal.timeout(60_000) | ||
| }) | ||
| return res.PeerId | ||
| console.log(`Peer ID fetched from ${source}.`) | ||
| return peerId | ||
| } catch (err) { | ||
| if (err.name === 'RetryError' && err.cause) { | ||
| // eslint-disable-next-line no-ex-assign | ||
| err = err.cause | ||
| } | ||
| err.message = `Cannot obtain miner info for ${minerId}: ${err.message}` | ||
| throw err | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} method | ||
| * @param {unknown[]} params | ||
| */ | ||
| async function rpc (method, ...params) { | ||
| const req = new Request(RPC_URL, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'content-type': 'application/json', | ||
| accepts: 'application/json', | ||
| authorization: `Bearer ${RPC_AUTH}` | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method, | ||
| params | ||
| }) | ||
| }) | ||
| const res = await fetch(req, { | ||
| signal: AbortSignal.timeout(60_000) | ||
| }) | ||
|
|
||
| if (!res.ok) { | ||
| throw new Error(`JSON RPC failed with ${res.code}: ${(await res.text()).trimEnd()}`) | ||
| } | ||
|
|
||
| const body = await res.json() | ||
| if (body.error) { | ||
| const err = new Error(body.error.message) | ||
| err.name = 'FilecoinRpcError' | ||
| err.code = body.code | ||
| console.error(err) | ||
| throw err | ||
| } | ||
|
|
||
| return body.result | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,21 @@ | ||
| import { test } from 'zinnia:test' | ||
| import { assertMatch, AssertionError } from 'zinnia:assert' | ||
| import { getMinerPeerId } from '../lib/miner-info.js' | ||
| import { getIndexProviderPeerId } from '../lib/miner-info.js' | ||
|
|
||
| const KNOWN_MINER_ID = 'f0142637' | ||
|
|
||
| test('get peer id of a known miner', async () => { | ||
| const result = await getMinerPeerId(KNOWN_MINER_ID) | ||
| const result = await getIndexProviderPeerId(KNOWN_MINER_ID) | ||
| assertMatch(result, /^12D3KooW/) | ||
| }) | ||
|
|
||
| test('get peer id of a miner that does not exist', async () => { | ||
| try { | ||
| const result = await getMinerPeerId('f010', { maxAttempts: 1 }) | ||
| const result = await getIndexProviderPeerId('f010', { maxAttempts: 1 }) | ||
| throw new AssertionError( | ||
| `Expected "getMinerPeerId()" to fail, but it resolved with "${result}" instead.` | ||
| `Expected "getIndexProviderPeerId()" to fail, but it resolved with "${result}" instead.` | ||
| ) | ||
| } catch (err) { | ||
| assertMatch(err.toString(), /\bf010\b.*\bactor code is not miner/) | ||
| assertMatch(err.toString(), /Error fetching index provider PeerID for miner f010/) | ||
| } | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the caller of the function should log. With that removed, we can remove the
try/catchaltogether.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the use case scenario here different to CheckerNetwork/spark-checker#129 (comment) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I don't think so. It also seems to me that your commit wasn't yet approved by Miro, right?
My understanding is that it makes sense to wrap a function body in try/catch if you want to modify the error to be consistent, and then rethrow it. Here we're just logging the error and then throwing it. That's not providing any extra value, so should be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I approved CheckerNetwork/spark-checker#129, it was already landed.
I agree we may not need the try/catch block here, but I consider it a detail I can live with.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@juliangruber do you want a change here or should we keep the code in sync with
spark-checker?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Up to you, I would prefer to change here and in spark-checker, but don't want to delay the work stream any further. If we keep this as is, I'm sure someone will eventually refactor it.