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: check if unpinned then downgrade to v0 #919

Merged
merged 13 commits into from
Jan 31, 2022
16 changes: 9 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/api/src/car.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import retry from 'p-retry'
import { GATEWAY, LOCAL_ADD_THRESHOLD, MAX_BLOCK_SIZE } from './constants.js'
import { JSONResponse } from './utils/json-response.js'
import { getPins, PIN_OK_STATUS, waitAndUpdateOkPins } from './utils/pin.js'
import { normalizeCid } from './utils/normalize-cid.js'
import { normalizeCid } from './utils/cid.js'
LeslieOA marked this conversation as resolved.
Show resolved Hide resolved

/**
* @typedef {import('multiformats/cid').CID} CID
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/pins.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSONResponse, notFound } from './utils/json-response.js'
import { normalizeCid } from './utils/normalize-cid.js'
import { normalizeCid } from './utils/cid.js'
import { getPins, PIN_OK_STATUS, waitAndUpdateOkPins } from './utils/pin.js'

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/status.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSONResponse, notFound } from './utils/json-response.js'
import { normalizeCid } from './utils/normalize-cid.js'
import { normalizeCid } from './utils/cid.js'

/**
* Returns pin and deal status info for a given CID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ export function normalizeCid (cid) {
throw new InvalidCidError(cid)
}
}

export function denormalizeCid (cid) {
try {
return CID.parse(cid).toV0().toString()
} catch (err) {
throw new InvalidCidError(cid)
}
}
LeslieOA marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion packages/cron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
"debug": "^4.3.1",
"dotenv": "^9.0.2",
"limiter": "2.0.1",
"pg": "^8.7.1",
"multiformats": "^9.6.2",
"node-fetch": "^2.6.1",
"p-retry": "^4.6.1",
"pg": "^8.7.1",
"piggybacker": "^2.0.0"
},
"devDependencies": {
Expand Down
16 changes: 15 additions & 1 deletion packages/cron/src/jobs/pins.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import debug from 'debug'
import { toPinStatusEnum } from '@web3-storage/api/src/utils/pin.js'
import retry from 'p-retry'
import { piggyback } from 'piggybacker'
import { denormalizeCid } from '../lib/cid.js'

const MAX_PIN_REQUESTS_PER_RUN = 400
const log = debug('pins:updatePinStatuses')
Expand Down Expand Up @@ -63,9 +64,22 @@ export async function updatePinStatuses ({ cluster, db }) {
return null // not tracked by our cluster
}

const status = toPinStatusEnum(peerMap[pin.location.peerId].status)
let status = toPinStatusEnum(peerMap[pin.location.peerId].status)

if (status !== 'Pinned' && status !== 'Remote') {
LeslieOA marked this conversation as resolved.
Show resolved Hide resolved
const cidV0 = denormalizeCid(pin.contentCid)
LeslieOA marked this conversation as resolved.
Show resolved Hide resolved

try {
peerMap = await getPinStatus(cidV0)
} catch (err) {
reSyncPins.push(pin)
return null
}

if (!peerMap[pin.location.peerId]) return null

status = toPinStatusEnum(peerMap[pin.location.peerId].status)

reSyncPins.push(pin)
}

Expand Down
24 changes: 24 additions & 0 deletions packages/cron/src/lib/cid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CID } from 'multiformats/cid'
import { InvalidCidError } from './errors.js'

/**
* Parse CID and return normalized b32 v1
*
* @param {string} cid
*/
export function normalizeCid (cid) {
try {
const c = CID.parse(cid)
return c.toV1().toString()
} catch (err) {
throw new InvalidCidError(cid)
}
}

export function denormalizeCid (cid) {
LeslieOA marked this conversation as resolved.
Show resolved Hide resolved
try {
return CID.parse(cid).toV0().toString()
} catch (err) {
throw new InvalidCidError(cid)
}
LeslieOA marked this conversation as resolved.
Show resolved Hide resolved
}
12 changes: 12 additions & 0 deletions packages/cron/src/lib/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class InvalidCidError extends Error {
/**
* @param {string} cid
*/
constructor (cid) {
super(`Invalid CID: ${cid}`)
this.name = 'InvalidCid'
this.status = 400
this.code = InvalidCidError.CODE
}
}
InvalidCidError.CODE = 'ERROR_INVALID_CID'
LeslieOA marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion packages/db/test/pinning.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha, browser */
import assert from 'assert'
import { normalizeCid } from '../../api/src/utils/normalize-cid'
import { normalizeCid } from '../../api/src/utils/cid'
import { DBClient } from '../index'
import { createUpload, createUser, createUserAuthKey, token } from './utils.js'
import { CID } from 'multiformats/cid'
Expand Down