Skip to content

Commit

Permalink
feat: do not write to ipfs (#1197)
Browse files Browse the repository at this point in the history
* feat: do not write to ipfs

* fix: attempt to fix unit tests

* fix: attempt to fix unit tests

* fix: attempt to fix unit tests
  • Loading branch information
smrz2001 authored May 2, 2024
1 parent 9ba2fce commit 5fad14b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
33 changes: 23 additions & 10 deletions src/__tests__/ceramic_integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,24 @@ describe('Ceramic Integration Test', () => {
beforeAll(async () => {
ipfsApiPort1 = await getPort()
ipfsApiPort2 = await getPort()
;[ipfs1, ipfs2, ipfs3, ipfs4] = await Promise.all([
createIPFS(ipfsApiPort1),
createIPFS(ipfsApiPort2),
createIPFS(),
createIPFS(),
])
if (process.env['CAS_USE_IPFS_STORAGE']) {
;[ipfs1, ipfs2, ipfs3, ipfs4] = await Promise.all([
createIPFS(ipfsApiPort1),
createIPFS(ipfsApiPort2),
createIPFS(),
createIPFS(),
])
} else {
;[ipfs3, ipfs4] = await Promise.all([
createIPFS(ipfsApiPort1),
createIPFS(ipfsApiPort2),
createIPFS(),
createIPFS(),
])
}

// Now make sure all ipfs nodes are connected to all other ipfs nodes
const ipfsNodes = [ipfs1, ipfs2, ipfs3, ipfs4]
const ipfsNodes = process.env['CAS_USE_IPFS_STORAGE'] ? [ipfs1, ipfs2, ipfs3, ipfs4] : [ipfs3, ipfs4]
for (const [i] of ipfsNodes.entries()) {
for (const [j] of ipfsNodes.entries()) {
if (i == j) {
Expand All @@ -296,8 +305,11 @@ describe('Ceramic Integration Test', () => {
})

afterAll(async () => {
// await Promise.all([ipfsServer1.stop(), ipfsServer2.stop()])
await Promise.all([ipfs1.stop(), ipfs2.stop(), ipfs3.stop(), ipfs4.stop()])
if (process.env['CAS_USE_IPFS_STORAGE']) {
await Promise.all([ipfs1.stop(), ipfs2.stop(), ipfs3.stop(), ipfs4.stop()])
} else {
await Promise.all([ipfs3.stop(), ipfs4.stop()])
}
await ganacheServer.close()
await anchorLauncher.stop()
})
Expand Down Expand Up @@ -364,7 +376,8 @@ describe('Ceramic Integration Test', () => {
})

describe('Multiple CAS instances in same process works', () => {
test(
// This test uses a very old approach for anchoring. Moreover, we will never be deploying multiple CAS instances.
test.skip(
'Anchors on different CAS instances are independent',
async () => {
const doc1 = await TileDocument.create(ceramic1, { foo: 1 }, null, { anchor: true })
Expand Down
50 changes: 27 additions & 23 deletions src/services/__tests__/anchor-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,12 @@ describe('anchor service', () => {
expectPresent(request)
expect(anchor.requestId).toEqual(request.id)

const anchorRecord = await ipfsService.retrieveRecord(anchor.cid)
expect(anchorRecord.prev.toString()).toEqual(request.cid)
expect(anchorRecord.proof).toEqual(ipfsProofCid)
expect(anchorRecord.path).toEqual(anchor.path)
if (process.env['CAS_USE_IPFS_STORAGE']) {
const anchorRecord = await ipfsService.retrieveRecord(anchor.cid)
expect(anchorRecord.prev.toString()).toEqual(request.cid)
expect(anchorRecord.proof).toEqual(ipfsProofCid)
expect(anchorRecord.path).toEqual(anchor.path)
}
}

expectPresent(anchors[0])
Expand Down Expand Up @@ -702,29 +704,31 @@ describe('anchor service', () => {
}
})

test('fail a batch if can not import Merkle CAR to IPFS', async () => {
const numRequests = 4
const requests = await fake.multipleRequests(numRequests)
if (process.env['CAS_USE_IPFS_STORAGE']) {
test('fail a batch if can not import Merkle CAR to IPFS', async () => {
const numRequests = 4
const requests = await fake.multipleRequests(numRequests)

const batch = new MockQueueMessage({
bid: uuidv4(),
rids: requests.map(({ id }) => id),
})
anchorBatchQueueService.receiveMessage.mockReturnValue(Promise.resolve(batch))
const batch = new MockQueueMessage({
bid: uuidv4(),
rids: requests.map(({id}) => id),
})
anchorBatchQueueService.receiveMessage.mockReturnValue(Promise.resolve(batch))

const original = blockchainService.sendTransaction
blockchainService.sendTransaction = () => {
return Promise.resolve(fakeTransaction)
}
const original = blockchainService.sendTransaction
blockchainService.sendTransaction = () => {
return Promise.resolve(fakeTransaction)
}

jest.spyOn(ipfsService, 'importCAR').mockImplementation(async () => {
throw new Error(`Can not import merkle CAR`)
jest.spyOn(ipfsService, 'importCAR').mockImplementation(async () => {
throw new Error(`Can not import merkle CAR`)
})
await expect(anchorService.anchorRequests()).rejects.toThrow()
const retrieved = await requestRepository.findByIds(requests.map((r) => r.id))
expect(retrieved.every((r) => r.status === RequestStatus.PENDING)).toBeTruthy()
blockchainService.sendTransaction = original
})
await expect(anchorService.anchorRequests()).rejects.toThrow()
const retrieved = await requestRepository.findByIds(requests.map((r) => r.id))
expect(retrieved.every((r) => r.status === RequestStatus.PENDING)).toBeTruthy()
blockchainService.sendTransaction = original
})
}

test('fail a batch if can not store Merkle CAR to S3', async () => {
const numRequests = 4
Expand Down
25 changes: 15 additions & 10 deletions src/services/anchor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,17 @@ export class AnchorService {
logger.debug('Creating anchor commits')
const anchors = await this._createAnchorCommits(ipfsProofCid, merkleTree)

logger.debug('Importing Merkle CAR to IPFS')
try {
await this.ipfsService.importCAR(merkleTree.car)
} catch (e) {
Metrics.count(METRIC_NAMES.MERKLE_CAR_STORAGE_FAILURE_IPFS, 1)
const message = `Can not store Merkle CAR to IPFS. Batch failed: ${e}`
logger.err(message)
throw e
// Do not store CAR file in IPFS by default
if (process.env['CAS_USE_IPFS_STORAGE']) {
logger.debug('Importing Merkle CAR to IPFS')
try {
await this.ipfsService.importCAR(merkleTree.car)
} catch (e) {
Metrics.count(METRIC_NAMES.MERKLE_CAR_STORAGE_FAILURE_IPFS, 1)
const message = `Can not store Merkle CAR to IPFS. Batch failed: ${e}`
logger.err(message)
throw e
}
}

logger.debug('Storing Merkle CAR file')
Expand Down Expand Up @@ -504,8 +507,10 @@ export class AnchorService {
}

try {
await this.ipfsService.storeRecord(ipfsAnchorCommit)

// Do not store in IPFS by default
if (process.env['CAS_USE_IPFS_STORAGE']) {
await this.ipfsService.storeRecord(ipfsAnchorCommit)
}
// Do not publish to pubsub by default
if (process.env['CAS_PUBSUB_PUBLISH']) {
// TODO: Remove this case entirely after js-ceramic no longer supports pubsub
Expand Down

0 comments on commit 5fad14b

Please sign in to comment.