-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: trigger aggregator on computed piece
- Loading branch information
1 parent
2429663
commit e7df15f
Showing
20 changed files
with
635 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import * as Sentry from '@sentry/serverless' | ||
import { Config } from '@serverless-stack/node/config/index.js' | ||
import { unmarshall } from '@aws-sdk/util-dynamodb' | ||
import { Piece } from '@web3-storage/data-segment' | ||
|
||
import { reportPieceCid } from '../index.js' | ||
import { getServiceConnection, getServiceSigner } from '../service.js' | ||
import { mustGetEnv } from './utils.js' | ||
|
||
Sentry.AWSLambda.init({ | ||
environment: process.env.SST_STAGE, | ||
dsn: process.env.SENTRY_DSN, | ||
tracesSampleRate: 1.0, | ||
}) | ||
|
||
/** | ||
* @param {import('aws-lambda').DynamoDBStreamEvent} event | ||
*/ | ||
async function pieceCidReport (event) { | ||
const { aggregatorDid, aggregatorUrl } = getEnv() | ||
const { PRIVATE_KEY: privateKey } = Config | ||
|
||
const records = parseDynamoDbEvent(event) | ||
if (records.length > 1) { | ||
throw new Error('Should only receive one ferry to update') | ||
} | ||
|
||
// @ts-expect-error can't figure out type of new | ||
const pieceRecord = unmarshall(records[0].new) | ||
const piece = Piece.fromString(pieceRecord.piece).link | ||
|
||
const aggregateServiceConnection = getServiceConnection({ | ||
did: aggregatorDid, | ||
url: aggregatorUrl | ||
}) | ||
const issuer = getServiceSigner({ | ||
privateKey | ||
}) | ||
const audience = aggregateServiceConnection.id | ||
/** @type {import('@web3-storage/filecoin-client/types').InvocationConfig} */ | ||
const invocationConfig = { | ||
issuer, | ||
audience, | ||
with: issuer.did(), | ||
} | ||
|
||
const { ok, error } = await reportPieceCid({ | ||
piece, | ||
group: issuer.did(), | ||
aggregateServiceConnection, | ||
invocationConfig | ||
}) | ||
|
||
if (error) { | ||
return { | ||
statusCode: 500, | ||
body: error.message || 'failed to add aggregate' | ||
} | ||
} | ||
|
||
return { | ||
statusCode: 200, | ||
body: ok | ||
} | ||
} | ||
|
||
export const handler = Sentry.AWSLambda.wrapHandler(pieceCidReport) | ||
|
||
/** | ||
* Get Env validating it is set. | ||
*/ | ||
function getEnv() { | ||
return { | ||
aggregatorDid: mustGetEnv('AGGREGATOR_DID'), | ||
aggregatorUrl: mustGetEnv('AGGREGATOR_URL'), | ||
} | ||
} | ||
|
||
/** | ||
* @param {import('aws-lambda').DynamoDBStreamEvent} event | ||
*/ | ||
function parseDynamoDbEvent (event) { | ||
return event.Records.map(r => ({ | ||
new: r.dynamodb?.NewImage, | ||
old: r.dynamodb?.OldImage | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @param {string} name | ||
* @returns {string} | ||
*/ | ||
export function mustGetEnv (name) { | ||
const value = process.env[name] | ||
if (!value) throw new Error(`Missing env var: ${name}`) | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as ed25519 from '@ucanto/principal/ed25519' | ||
import * as DID from '@ipld/dag-ucan/did' | ||
import { CAR, HTTP } from '@ucanto/transport' | ||
import { connect } from '@ucanto/client' | ||
|
||
/** | ||
* Given a config, return a ucanto Signer object representing the service | ||
* | ||
* @param {object} config | ||
* @param {string} config.privateKey - multiformats private key of primary signing key | ||
* @returns {import('@ucanto/principal/ed25519').Signer.Signer} | ||
*/ | ||
export function getServiceSigner(config) { | ||
return ed25519.parse(config.privateKey) | ||
} | ||
|
||
/** | ||
* | ||
* @param {{ did: string, url: string }} config | ||
* @returns | ||
*/ | ||
export function getServiceConnection (config) { | ||
const servicePrincipal = DID.parse(config.did) // 'did:web:filecoin.web3.storage' | ||
const serviceURL = new URL(config.url) // 'https://filecoin.web3.storage' | ||
|
||
const serviceConnection = connect({ | ||
id: servicePrincipal, | ||
codec: CAR.outbound, | ||
channel: HTTP.open({ | ||
url: serviceURL, | ||
method: 'POST', | ||
}), | ||
}) | ||
|
||
return serviceConnection | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { encode } from 'multiformats/block' | ||
import { identity } from 'multiformats/hashes/identity' | ||
import { sha256 as hasher } from 'multiformats/hashes/sha2' | ||
import * as pb from '@ipld/dag-pb' | ||
import { CarBufferWriter } from '@ipld/car' | ||
import { toString } from 'uint8arrays' | ||
import { Piece } from '@web3-storage/data-segment' | ||
|
||
export async function createCar () { | ||
const id = await encode({ | ||
value: pb.prepare({ Data: 'a red car on the street!' }), | ||
codec: pb, | ||
hasher: identity, | ||
}) | ||
|
||
const parent = await encode({ | ||
value: pb.prepare({ Links: [id.cid] }), | ||
codec: pb, | ||
hasher, | ||
}) | ||
const car = CarBufferWriter.createWriter(Buffer.alloc(1000), { | ||
roots: [parent.cid], | ||
}) | ||
car.write(parent) | ||
|
||
const body = car.close() | ||
const digest = await hasher.digest(body) | ||
const checksum = toString(digest.digest, 'base64pad') | ||
|
||
const key = `${parent.cid.toString()}/${parent.cid.toString()}` | ||
const piece = Piece.fromPayload(body) | ||
|
||
return { | ||
body, | ||
checksum, | ||
key, | ||
link: parent.cid, | ||
piece: piece.link | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as Server from '@ucanto/server' | ||
|
||
export const OperationErrorName = /** @type {const} */ ('OperationFailed') | ||
export class OperationFailed extends Server.Failure { | ||
/** | ||
* @param {string} message | ||
* @param {import('@web3-storage/data-segment').PieceLink} piece | ||
*/ | ||
constructor(message, piece) { | ||
super(message) | ||
this.piece = piece | ||
} | ||
|
||
get reason() { | ||
return this.message | ||
} | ||
|
||
get name() { | ||
return OperationErrorName | ||
} | ||
} |
Oops, something went wrong.