-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use IPNI advertisements from the miner only (#55)
1. Resolve `minerId` to miner's PeerId, which is the same value as `Provider.ID` in the IPNI records 2. Ignore IPNI records advertised by different miners 3. Report `minerId` and `providerId` as the new measurement fields Signed-off-by: Miroslav Bajtoš <[email protected]>
- Loading branch information
Showing
8 changed files
with
146 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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,3 +1,9 @@ | ||
export const SPARK_VERSION = '1.9.1' | ||
export const MAX_CAR_SIZE = 200 * 1024 * 1024 // 200 MB | ||
export const APPROX_ROUND_LENGTH_IN_MS = 20 * 60_000 // 20 minutes | ||
|
||
export const RPC_REQUEST = new Request('https://api.node.glif.io/', { | ||
headers: { | ||
authorization: 'Bearer 6bbc171ebfdd78b2644602ce7463c938' | ||
} | ||
}) |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { RPC_REQUEST } from './constants.js' | ||
|
||
/** | ||
* @param {string} minerId A miner actor id, e.g. `f0142637` | ||
* @returns {Promise<string>} Miner's PeerId, e.g. `12D3KooWMsPmAA65yHAHgbxgh7CPkEctJHZMeM3rAvoW8CZKxtpG` | ||
*/ | ||
export async function getMinerPeerId (minerId) { | ||
try { | ||
const res = await rpc('Filecoin.StateMinerInfo', minerId, null) | ||
return res.PeerId | ||
} catch (err) { | ||
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_REQUEST, { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
accepts: 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
jsonrpc: '2.0', | ||
id: 1, | ||
method, | ||
params | ||
}) | ||
}) | ||
const res = await fetch(req) | ||
|
||
if (!res.ok) { | ||
throw new Error(`JSON RPC failed with ${res.code}: ${await res.text()}`) | ||
} | ||
|
||
const body = await res.json() | ||
if (body.error) { | ||
const err = new Error(body.error.message) | ||
err.name = 'FilecoinRpcError' | ||
err.code = body.code | ||
throw err | ||
} | ||
|
||
return body.result | ||
} |
This file contains 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 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,3 +1,4 @@ | ||
import './test/ipni-client.test.js' | ||
import './test/miner-info.test.js' | ||
import './test/integration.js' | ||
import './test/spark.js' |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { test } from 'zinnia:test' | ||
import { assertMatch, AssertionError } from 'zinnia:assert' | ||
import { getMinerPeerId } 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) | ||
assertMatch(result, /^12D3KooW/) | ||
}) | ||
|
||
test('get peer id of a miner that does not exist', async () => { | ||
try { | ||
const result = await getMinerPeerId('f010') | ||
throw new AssertionError( | ||
`Expected "getMinerPeerId()" to fail, but it resolved with "${result}" instead.` | ||
) | ||
} catch (err) { | ||
assertMatch(err.toString(), /\bf010\b.*\bactor code is not miner/) | ||
} | ||
}) |