Skip to content

Commit

Permalink
fix: increment used storage (#501)
Browse files Browse the repository at this point in the history
- increment the users used storage amount in the Fauna function that creates the upload, to avoid double counting when handling split CARs
-use .bytelength insted of .length when computing cumulativeSize
  • Loading branch information
vasco-santos authored Sep 28, 2021
1 parent aeeebbc commit 1becd09
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
24 changes: 2 additions & 22 deletions packages/api/src/car.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ const CREATE_OR_UPDATE_PIN = gql`
}
`

const INCREMENT_USER_USED_STORAGE = gql`
mutation IncrementUserUsedStorage($user: ID!, $amount: Long!) {
incrementUserUsedStorage(user: $user, amount: $amount) {
usedStorage
}
}
`

// Duration between status check polls in ms.
const PIN_STATUS_CHECK_INTERVAL = 5000
// Max time in ms to spend polling for an OK status.
Expand Down Expand Up @@ -188,19 +180,6 @@ export async function handleCarUpload (request, env, ctx, car, uploadType = 'Car
/** @type {(() => Promise<any>)[]} */
const tasks = []

// TODO: this should be handled in the db code as part of the CREATE_UPLOAD
// Update the user's used storage
tasks.push(async () => {
try {
await env.db.query(INCREMENT_USER_USED_STORAGE, {
user: user._id,
amount: dagSize
})
} catch (err) {
console.error(`failed to update user used storage: ${err.stack}`)
}
})

// Retrieve current pin status and info about the nodes pinning the content.
// Keep querying Cluster until one of the nodes reports something other than
// Unpinned i.e. PinQueued or Pinning or Pinned.
Expand Down Expand Up @@ -369,14 +348,15 @@ async function carStat (carBlob) {

/**
* The sum of the node size and size of each link
* @param {Uint8Array} pbNodeBytes
* @param {import('@ipld/dag-pb/src/interface').PBNode} pbNode
* @returns {number} the size of the DAG in bytes
*/
function cumulativeSize (pbNodeBytes, pbNode) {
// NOTE: Tsize is optional, but all ipfs implementations we know of set it.
// It's metadata, that could be missing or deliberately set to an incorrect value.
// This logic is the same as used by go/js-ipfs to display the cumulative size of a dag-pb dag.
return pbNodeBytes.length + pbNode.Links.reduce((acc, curr) => acc + (curr.Tsize || 0), 0)
return pbNodeBytes.byteLength + pbNode.Links.reduce((acc, curr) => acc + (curr.Tsize || 0), 0)
}

/**
Expand Down
16 changes: 14 additions & 2 deletions packages/db/fauna/resources/Function/createUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const {
Do,
Call,
Foreach,
Equals
Equals,
Add
} = fauna

const name = 'createUpload'
Expand All @@ -47,7 +48,8 @@ const body = Query(
Let(
{
cid: Select('cid', Var('data')),
contentMatch: Match(Index('unique_Content_cid'), Var('cid'))
contentMatch: Match(Index('unique_Content_cid'), Var('cid')),
usedStorage: Select(['data', 'usedStorage'], Get(Var('userRef')), 0)
},
If(
IsNonEmpty(Var('contentMatch')),
Expand Down Expand Up @@ -104,6 +106,11 @@ const body = Query(
type: Select('type', Var('data')),
created: Now()
}
}),
user: Update(Var('userRef'), {
data: {
usedStorage: Add(Var('usedStorage'), Select('dagSize', Var('data'), 0))
}
})
},
Do(
Expand Down Expand Up @@ -169,6 +176,11 @@ const body = Query(
created: Now()
}
}),
user: Update(Var('userRef'), {
data: {
usedStorage: Add(Var('usedStorage'), Select('dagSize', Var('data'), 0))
}
}),
pinRequest: Create('PinRequest', {
data: {
cid: Var('cid'),
Expand Down

0 comments on commit 1becd09

Please sign in to comment.