Skip to content

Commit

Permalink
perf: use Node.js crypto sha256 hasher for aggregate building (#107)
Browse files Browse the repository at this point in the history
This PR switches from JS native sha256 hashing to hashing provided by
the Node.js crypto module, which is significantly faster.
  • Loading branch information
alanshaw authored Sep 20, 2024
1 parent 25f9cbb commit 715a4bf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
69 changes: 51 additions & 18 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
"sst": "^2.17.2",
"typescript": "^4.9.4"
},
"dependencies": {},
"dependencies": {
"@storacha/one-webcrypto": "^1.0.1"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
},
Expand Down Expand Up @@ -74,4 +76,4 @@
"workspaces": [
"packages/*"
]
}
}
3 changes: 2 additions & 1 deletion packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
"@ucanto/transport": "^9.1.1",
"@w3filecoin/core": "*",
"@web3-storage/data-segment": "5.0.0",
"@web3-storage/filecoin-api": "^7.2.0",
"@web3-storage/filecoin-api": "^7.3.0",
"@web3-storage/filecoin-client": "3.3.3",
"lru-cache": "^11.0.1",
"multiformats": "^13.3.0",
"uint8arrays": "^4.0.6"
},
"devDependencies": {
Expand Down
15 changes: 13 additions & 2 deletions packages/functions/src/aggregator/handle-buffer-queue-message.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as Sentry from '@sentry/serverless'

import crypto from 'node:crypto'
import { createClient as createBufferStoreClient } from '@w3filecoin/core/src/store/aggregator-buffer-store.js'
import { createClient as createBufferQueueClient, decodeMessage } from '@w3filecoin/core/src/queue/buffer-queue.js'
import { createClient as createAggregateOfferQueueClient } from '@w3filecoin/core/src/queue/aggregate-offer-queue.js'
import * as aggregatorEvents from '@web3-storage/filecoin-api/aggregator/events'
import { Piece } from '@web3-storage/data-segment'
import { LRUCache } from 'lru-cache'
import * as Digest from 'multiformats/hashes/digest'
import { sha256 } from 'multiformats/hashes/sha2'
import { mustGetEnv } from '../utils.js'

Sentry.AWSLambda.init({
Expand Down Expand Up @@ -135,7 +137,16 @@ function getContext () {
// will be prepended, so policy is irrelevant
policy: /** @type {import('@web3-storage/filecoin-api/src/aggregator/api').PiecePolicy} */ (0),
insertedAt: (new Date()).toISOString()
}]
}],
hasher: {
name: sha256.name,
code: sha256.code,
/** @param {Uint8Array} bytes */
digest: bytes => {
const digest = crypto.createHash('sha256').update(bytes).digest()
return Digest.create(sha256.code, digest)
}
}
}
}
}
Expand Down

0 comments on commit 715a4bf

Please sign in to comment.