Skip to content

Commit

Permalink
fix(cli,http-client,stream-model-instance): encode genesis commit in …
Browse files Browse the repository at this point in the history
…multiquery (#3172)
  • Loading branch information
PaulLeCam authored Feb 23, 2024
1 parent 9c50238 commit cb5c2fc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 54 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"devDependencies": {
"@ceramicnetwork/common-test-utils": "^3.1.0",
"@ceramicnetwork/stream-model": "^4.1.0",
"@ceramicnetwork/stream-model-instance": "^4.1.0",
"@ceramicnetwork/stream-tile-handler": "^5.1.0",
"@stablelib/sha256": "^1.0.1",
"@types/express": "^4.17.21",
Expand Down
30 changes: 30 additions & 0 deletions packages/cli/src/__tests__/ceramic-daemon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { CommonTestUtils as TestUtils } from '@ceramicnetwork/common-test-utils'
import { EventSource } from 'cross-eventsource'
import { AggregationDocument, JsonAsString } from '@ceramicnetwork/codecs'
import { Model, ModelDefinition } from '@ceramicnetwork/stream-model'
import { ModelInstanceDocument } from '@ceramicnetwork/stream-model-instance'
import { decode } from 'codeco'

const seed = 'SEED'
Expand Down Expand Up @@ -607,6 +608,35 @@ describe('Ceramic interop: core <> http-client', () => {
expect(resCore[streamId.toString()].metadata).toEqual(metadata)
expect(resClient[streamId.toString()].metadata).toEqual(metadata)
})

it('handles genesis commit encoding', async () => {
const model = await Model.create(client, {
name: 'TestModel',
version: '2.0',
accountRelation: { type: 'set', fields: ['unique'] },
interface: false,
implements: [],
schema: {
type: 'object',
properties: {
unique: { type: 'string' },
},
required: ['unique'],
additionalProperties: false,
},
})

const meta = { controller: core.did?.id.toString(), deterministic: true, model: model.id }
const genesis = await ModelInstanceDocument.makeGenesis(client, null, meta, ['test'])
const streamId = await StreamID.fromGenesis(ModelInstanceDocument.STREAM_TYPE_ID, genesis)
const id = streamId.toString()

const resCore = await core.multiQuery([{ genesis, streamId }])
expect(resCore[id]).toBeInstanceOf(ModelInstanceDocument)

const resClient = await client.multiQuery([{ genesis, streamId }])
expect(resClient[id]).toBeInstanceOf(ModelInstanceDocument)
})
})

describeIfV3('pin api', () => {
Expand Down
13 changes: 7 additions & 6 deletions packages/cli/src/ceramic-daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,12 +911,13 @@ export class CeramicDaemon {
* Load multiple streams and paths using an array of multiqueries
*/
async multiQuery(req: Request, res: Response): Promise<void> {
const { queries } = <MultiQueries>req.body
const { timeout } = <MultiQueries>req.body

const results = await this.ceramic.multiQuery(queries, timeout)
const response = Object.entries(results).reduce((acc, e) => {
const [k, v] = e
const body = req.body as MultiQueries
const queries = body.queries.map((q) => ({
...q,
genesis: q.genesis ? StreamUtils.deserializeCommit(q.genesis) : undefined,
}))
const results = await this.ceramic.multiQuery(queries, body.timeout)
const response = Object.entries(results).reduce((acc, [k, v]) => {
acc[k] = StreamUtils.serializeState(v.state)
return acc
}, {})
Expand Down
1 change: 1 addition & 0 deletions packages/http-client/src/ceramic-http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class CeramicClient implements StreamReaderWriter {
const queriesJSON = queries.map((q) => {
return {
...q,
genesis: q.genesis ? StreamUtils.serializeCommit(q.genesis) : undefined,
streamId: typeof q.streamId === 'string' ? q.streamId : q.streamId.toString(),
}
})
Expand Down
Loading

0 comments on commit cb5c2fc

Please sign in to comment.