Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use ipfs peerId as pin location #966

Merged
merged 4 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for me. But one question first. Running consecutively the tests is "safe", or can we easily start the tests while the containers are still being stopped/clenned?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no, we might have to revert this if we ever need that.

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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We sadly don't have yet a migration mechanism out of the box. Perhaps, we can just create a migrations folder here, and prepend name of the sql script with date. What do you think?

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';
2 changes: 1 addition & 1 deletion packages/tools/docker/cluster/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ services:

cluster0:
container_name: cluster0
image: ipfs/ipfs-cluster:latest
image: ipfs/ipfs-cluster:v0.14.5-rc1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alanshaw I'm going to revert this change to the docker-compose to as the latest is now v0.14.5 https://github.com/ipfs/ipfs-cluster/releases

depends_on:
- ipfs0
environment:
Expand Down