Skip to content

Commit

Permalink
fix: use ipfs peerId as pin location (#966)
Browse files Browse the repository at this point in the history
Cluster now provides the peerId for the underlying ipfs node in pin status response, so we fix things to use that as our pin location peer id. Previously we used the cluster peerId which should only be used for internal cluster admin. With this change users will be able to use the peerId in the /status response to connect to the ipfs node that has their stuff via `ipfs swarm connect <peerId>`

- Fix toPin to use ipfsPeerId
- Update tests to verify that status response for new content has a peerId of the ipfs node for one of the cluster nodes.
- Update ipfs-cluster client to [4.0.0](https://github.com/nftstorage/ipfs-cluster/releases/tag/v4.0.0)
- Update ipfs-cluster in docker-compose to [0.14.5-rc1](https://github.com/ipfs/ipfs-cluster/releases/tag/v0.14.5-rc1)
- Tweak test hooks to not wait for things to shut down before printing the test results overview, to reduce debugging friction.

Fixes: #414

TODO:
- [] db migration script to update PinLocation peerIds
- [] verify cluster infra is updated to >= ipfs-cluster >= 0.14.5-rc1
- [] schedule read-only maintenance

License: (Apache-2.0 AND MIT)
Signed-off-by: Oli Evans <oli@tableflip.io>
  • Loading branch information
olizilla authored Feb 24, 2022
1 parent 049f9b8 commit 5ae212d
Show file tree
Hide file tree
Showing 8 changed files with 667 additions and 7,302 deletions.
7,923 changes: 630 additions & 7,293 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@ipld/dag-pb": "^2.0.2",
"@magic-ext/oauth": "^0.8.0",
"@magic-sdk/admin": "^1.3.0",
"@nftstorage/ipfs-cluster": "^3.5.0",
"@nftstorage/ipfs-cluster": "^4.0.0",
"@web3-storage/db": "^4.0.0",
"@web3-storage/multipart-parser": "^1.0.0",
"cborg": "^1.6.0",
Expand Down
13 changes: 9 additions & 4 deletions packages/api/src/utils/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ export async function getOKpins (cid, cluster, peerMap) {
* @return {Array.<import('@web3-storage/db/db-client-types').PinUpsertInput>}
*/
export function toPins (peerMap) {
return Object.entries(peerMap).map(([peerId, { peerName, status }]) => ({
status: toPinStatusEnum(status),
location: { peerId, peerName }
}))
// Note: `clusterPeerId` is an internal id, and is only used for cluster admin.
// The `ipfsPeerId` which we rename to `peerId` can be used to connect to the underlying ipfs node
// that stores a given pin, by passing it to `ipfs swarm connect <peerid>`.
return Object.entries(peerMap).map(([clusterPeerId, { peerName, status, ipfsPeerId: peerId }]) => {
return {
status: toPinStatusEnum(status),
location: { peerId, peerName }
}
})
}

/**
Expand Down
16 changes: 15 additions & 1 deletion packages/api/test/car.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sha256 } from 'multiformats/hashes/sha2'
import * as pb from '@ipld/dag-pb'
import { CarWriter } from '@ipld/car'
import fetch, { Blob } from '@web-std/fetch'
import { endpoint } from './scripts/constants.js'
import { endpoint, clusterApi, clusterApiAuthHeader } from './scripts/constants.js'
import { createCar } from './scripts/car.js'
import { MAX_BLOCK_SIZE } from '../src/constants.js'
import { getTestJWT } from './scripts/helpers.js'
Expand Down Expand Up @@ -38,6 +38,20 @@ describe('POST /car', () => {
const { cid } = await res.json()
assert(cid, 'Server response payload has `cid` property')
assert.strictEqual(cid, expectedCid, 'Server responded with expected CID')

const statusRes = await fetch(new URL(`status/${cid}`, endpoint))
const status = await statusRes.json()
const pinned = status.pins.find(pin => pin.status === 'Pinned')
assert(pinned, 'CID is Pinned')

const clusterPeersRes = await fetch(new URL('peers', clusterApi), {
headers: {
Authorization: clusterApiAuthHeader
}
})
const clusterPeers = await clusterPeersRes.json()
// assert that peerId from the status belongs to one of the cluster ipfs nodes.
assert(clusterPeers.some(peer => peer.ipfs.id === pinned.peerId))
})

it('should throw for blocks bigger than the maximum permitted size', async () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/api/test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@ export const mochaHooks = () => {
await delay(2000)
},
async afterAll () {
// Note: not awaiting promises here so we see the test results overview sooner.
this.timeout(60_000)
if (srv) {
console.log('🛑 Stopping Miniflare')
srv.close()
}
if (projectCluster) {
console.log('🛑 Stopping IPFS Cluster')
await execa(toolsCli, ['cluster', '--stop', '--clean', '--project', projectCluster])
execa(toolsCli, ['cluster', '--stop', '--clean', '--project', projectCluster])
}
if (projectDb) {
console.log('🛑 Stopping PostgreSQL and PostgREST')
await execa(dbCli, ['db', '--stop', '--clean', '--project', projectDb])
execa(dbCli, ['db', '--stop', '--clean', '--project', projectDb])
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/api/test/scripts/constants.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const endpoint = 'http://localhost:8787'
export const clusterApi = 'http://localhost:9094'
export const clusterApiAuthHeader = `Basic ${Buffer.from('test:test').toString('base64')}`
2 changes: 1 addition & 1 deletion packages/cron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"author": "Alan Shaw",
"license": "(Apache-2.0 OR MIT)",
"dependencies": {
"@nftstorage/ipfs-cluster": "^3.3.1",
"@nftstorage/ipfs-cluster": "^4.0.0",
"@web-std/fetch": "^2.0.1",
"@web3-storage/db": "^4.0.0",
"debug": "^4.3.1",
Expand Down
6 changes: 6 additions & 0 deletions packages/db/postgres/fix-peer_location.peer_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
UPDATE pin_location SET peer_id='12D3KooWPySxxWQjBgX9Jp6uAHQfVmdq8HG1gVvS1fRawHNSrmqW' WHERE peer_id='12D3KooWF6uxxqZf4sXpQEbNE4BfbVJWAKWrFSKamxmWm4E9vyzd';
UPDATE pin_location SET peer_id='12D3KooWR19qPPiZH4khepNjS3CLXiB7AbrbAD4ZcDjN1UjGUNE1' WHERE peer_id='12D3KooWFe387JFDpgNEVCP5ARut7gRkX7YuJCXMStpkq714ziK6';
UPDATE pin_location SET peer_id='12D3KooWKytRAd2ujxhGzaLHKJuje8sVrHXvjGNvHXovpar5KaKQ' WHERE peer_id='12D3KooWHdBjmicdXu5X57mQ65oBpfy4p3ca5p31kfJT9FSUFu3P';
UPDATE pin_location SET peer_id='12D3KooWEDMw7oRqQkdCJbyeqS5mUmWGwTp8JJ2tjCzTkHboF6wK' WHERE peer_id='12D3KooWJeRQfPbiv5U2RqQ9yK3qijbNxKarKEGLMkGrfXJuZ2Bo';
UPDATE pin_location SET peer_id='12D3KooWNuoVEfVLJvU3jWY2zLYjGUaathsecwT19jhByjnbQvkj' WHERE peer_id='12D3KooWLWFUri36dmTkki6o9PwfQNwGb2gsHuKD5FdcwzCXYnwc';
UPDATE pin_location SET peer_id='12D3KooWSnniGsyAF663gvHdqhyfJMCjWJv54cGSzcPiEMAfanvU' WHERE peer_id='12D3KooWMbibcXHwkSjgV7VZ8TMfDKi6pZvmi97P83ZwHm9LEsvV';

0 comments on commit 5ae212d

Please sign in to comment.