Skip to content

Commit

Permalink
Revert "chore: Expect Merkle/witness CAR files to be always present (#…
Browse files Browse the repository at this point in the history
…1164)"

This reverts commit 4deea40.
  • Loading branch information
gvelez17 committed Sep 20, 2023
1 parent 1c0675b commit 095db35
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@
exports[`present by RequestStatus COMPLETED 1`] = `
{
"anchorCommit": {
"cid": "bafyreid4y5savur6cqzlul6ik35oucgczxkk3pfd62zht5yu54tezsube4",
"cid": "bafyreibfyl5p56xjdarie2p7brjmwktxgiggdm6hxyeugauk6zxg5c6g6m",
},
"cid": "bafyreibfyl5p56xjdarie2p7brjmwktxgiggdm6hxyeugauk6zxg5c6g6m",
"createdAt": 1577934240000,
"id": "889483296",
"message": "Fresh request",
"status": "COMPLETED",
"streamId": "k2t6wyfsu4pfxu08vo93w38oyu9itsuf374ekyeno0wb62ozm2o9sznrn8qp72",
"updatedAt": 1612325100000,
}
`;

exports[`present by RequestStatus COMPLETED but no associated anchor 1`] = `
{
"anchorCommit": {
"cid": "bafyreibfyl5p56xjdarie2p7brjmwktxgiggdm6hxyeugauk6zxg5c6g6m",
},
"cid": "bafyreibfyl5p56xjdarie2p7brjmwktxgiggdm6hxyeugauk6zxg5c6g6m",
"createdAt": 1577934240000,
Expand All @@ -12,7 +27,6 @@ exports[`present by RequestStatus COMPLETED 1`] = `
"status": "COMPLETED",
"streamId": "k2t6wyfsu4pfxu08vo93w38oyu9itsuf374ekyeno0wb62ozm2o9sznrn8qp72",
"updatedAt": 1612325100000,
"witnessCar": "OqJlcm9vdHOB2CpYJQABcRIgfMdkCtI+FDK6L8hW+uoIws3Urbyj9rJ59xTvJkzKgSdndmVyc2lvbgGIAQFxEiB8x2QK0j4UMrovyFb66gjCzdStvKP2snn3FO8mTMqBJ6NjY2lk2CpYJQABcRIgJcL6/vrpGCKCaf8MUssqdzIMYbPHvglDAor2bm6LxvNkcGF0aGEwZXByb29m2CpYJQABcRIgpwbPkSxvbUrkQBQzYF2+AvUKUrt9Wt5mtT0xWy9xGFFTAXESIKcGz5Esb21K5EAUM2BdvgL1ClK7fVreZrU9MVsvcRhRoWRyb2902CpYJQABcRIg8+mIUYRM2fJ1/CL+kpuxTMLn0GJkdC9un23kCXNm4RBOAXESIPPpiFGETNnydfwi/pKbsUzC59BiZHQvbp9t5AlzZuEQgdgqWCUAAXESIATVsKOYecAQpYEE71SWFx7t+6eZMYGJ4U6vxk8bBO2vKAFxEiAE1bCjmHnAEKWBBO9Ulhce7funmTGBieFOr8ZPGwTtr6FhYQM",
}
`;

Expand Down
36 changes: 8 additions & 28 deletions src/services/__tests__/request-presentation-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { describe, expect, jest, test } from '@jest/globals'
import { RequestStatus } from '../../models/request.js'
import {
CompletedAnchorlessError,
RequestPresentationService,
} from '../request-presentation-service.js'
import { RequestPresentationService } from '../request-presentation-service.js'
import type {
AnchorWithRequest,
IAnchorRepository,
Expand All @@ -12,11 +9,6 @@ import { generateRequest } from '../../__tests__/test-utils.js'
import { InMemoryMerkleCarService } from '../merkle-car-service.js'
import { WitnessService } from '../witness-service.js'
import { CID } from 'multiformats/cid'
import { CARFactory } from 'cartonne'
import { pathLine } from '../../ancillary/codecs.js'
import { PathDirection } from '@ceramicnetwork/anchor-utils'

const carFactory = new CARFactory()

const anchorRepository = {
findByRequest: jest.fn(),
Expand Down Expand Up @@ -62,26 +54,13 @@ describe('present by RequestStatus', () => {
status: RequestStatus.COMPLETED,
})
const findByRequestSpy = jest.spyOn(anchorRepository, 'findByRequest')
const merkleCar = carFactory.build()
const witnessElement = merkleCar.put({ a: 3 })
const root = merkleCar.put([witnessElement])
const proofCid = merkleCar.put({ root: root })
const cid = merkleCar.put({
proof: proofCid,
const anchor = {
path: '/some/path',
cid: FAKE_CID,
path: pathLine.encode([PathDirection.L]),
})
proofCid: FAKE_CID,
}
findByRequestSpy.mockImplementationOnce(async () => {
return {
path: pathLine.encode([PathDirection.L]),
cid: cid,
proofCid: proofCid,
request: request,
} as AnchorWithRequest
})
const retrieveCarFileSpy = jest.spyOn(merkleCarService, 'retrieveCarFile')
retrieveCarFileSpy.mockImplementationOnce(async () => {
return merkleCar
return { ...anchor, request: request } as AnchorWithRequest
})
const presentation = await service.body(request)
expect(presentation).toMatchSnapshot()
Expand All @@ -96,6 +75,7 @@ describe('present by RequestStatus', () => {
findByRequestSpy.mockImplementationOnce(async () => {
return null
})
await expect(service.body(request)).rejects.toThrow(CompletedAnchorlessError)
const presentation = await service.body(request)
expect(presentation).toMatchSnapshot()
})
})
28 changes: 10 additions & 18 deletions src/services/request-presentation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AnchorRequestStatusName,
NotCompleteCASResponse,
CASResponse,
CompleteCASResponse,
} from '@ceramicnetwork/codecs'
import { CID } from 'multiformats/cid'
import { StreamID } from '@ceramicnetwork/streamid'
Expand All @@ -30,18 +31,6 @@ const NAME_FROM_STATUS = {

const WITNESS_CAR_CACHE = 1000 // ~2MiB if one witness car is 2KiB

export class CompletedAnchorlessError extends Error {
constructor(cid: string) {
super(`No anchor for COMPLETED request ${cid} found`)
}
}

class NoMerkleCarError extends Error {
constructor(proofCID: CID) {
super(`No MerkleCAR found for proof ${proofCID}`)
}
}

/**
* Render anchoring Request as JSON for a client to consume.
*/
Expand All @@ -56,7 +45,8 @@ export class RequestPresentationService {
private readonly witnessService: WitnessService
) {}

async witnessCAR(anchor: AnchorWithRequest): Promise<CAR> {
async witnessCAR(anchor: AnchorWithRequest | null): Promise<CAR | null> {
if (!anchor) return null // Expected behaviour
const cacheKey = anchor.cid.toString()
const fromCache = this.cache.get(cacheKey)
if (fromCache) {
Expand All @@ -68,7 +58,7 @@ export class RequestPresentationService {
if (!merkleCAR) {
Metrics.count(METRIC_NAMES.NO_MERKLE_CAR_FOR_ANCHOR, 1)
logger.warn(`No Merkle CAR found for anchor ${anchor.cid}`)
throw new NoMerkleCarError(anchor.proofCid)
return null
}
const witnessCAR = this.witnessService.buildWitnessCAR(anchor.cid, merkleCAR)
this.cache.set(cacheKey, witnessCAR)
Expand All @@ -82,15 +72,14 @@ export class RequestPresentationService {
const anchor = await this.anchorRepository.findByRequest(request)
if (!anchor) {
Metrics.count(METRIC_NAMES.NO_ANCHOR_FOR_REQUEST, 1)
throw new CompletedAnchorlessError(request.cid)
}
const witnessCAR = await this.witnessCAR(anchor)
// TODO CDB-2759 Drop this after enough Ceramic nodes can 100% rely on WitnessCAR
// TODO: This is a workaround, fix in CDB-2192
const anchorCommit: AnchorCommitPresentation = {
cid: anchor ? anchor.cid : CID.parse(request.cid),
}

return {
const result: CompleteCASResponse = {
id: request.id,
status: AnchorRequestStatusName.COMPLETED,
cid: CID.parse(request.cid),
Expand All @@ -99,8 +88,11 @@ export class RequestPresentationService {
createdAt: new Date(request.createdAt),
updatedAt: new Date(request.updatedAt),
anchorCommit: anchorCommit,
witnessCar: witnessCAR,
}
if (witnessCAR) {
result.witnessCar = witnessCAR
}
return result
}
case RequestStatus.PENDING:
case RequestStatus.PROCESSING:
Expand Down

0 comments on commit 095db35

Please sign in to comment.