Skip to content

Commit

Permalink
Feature/Include pushing the different event types (#3126)
Browse files Browse the repository at this point in the history
* feat: add enum

* feat: update event type parsing

* chore: add tests

* chore: CommitType and eliminate dependency cycle (#3129)

* chore: CommitType and eliminate dependency cycle

* chore: Eliminate confusion

* test: Make tests run and fix an error

* test: Make tests run

* test: Make tests run

* fix: circular dependency

---------

Co-authored-by: Sergey Ukustov <sergey@ukstv.me>
  • Loading branch information
JulissaDantes and ukstv authored Jan 31, 2024
1 parent 8827948 commit eae99ad
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 57 deletions.
3 changes: 2 additions & 1 deletion packages/codecs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"author": "3Box Labs",
"license": "(Apache-2.0 OR MIT)",
"dependencies": {
"@ceramicnetwork/streamid": "^3.4.0",
"@ceramicnetwork/common": "^4.0.0",
"@ceramicnetwork/streamid": "^3.4.0-rc.0",
"cartonne": "^3.0.1",
"codeco": "^1.1.0",
"dag-jose": "^4.0.0",
Expand Down
13 changes: 2 additions & 11 deletions packages/codecs/src/anchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ import { carAsUint8Array, cidAsString } from './ipld.js'
import { streamIdAsString } from './stream.js'
import { uint8ArrayAsBase64 } from './binary.js'
import { dateAsUnix } from './date.js'

export enum AnchorRequestStatusName {
PENDING = 'PENDING',
PROCESSING = 'PROCESSING',
COMPLETED = 'COMPLETED',
FAILED = 'FAILED',
READY = 'READY',
REPLACED = 'REPLACED',
}
import { AnchorRequestStatusName } from '@ceramicnetwork/common'

/**
* Part of CAS response that sends AnchorCommit content. Effectively a historical artefact.
Expand All @@ -35,14 +27,13 @@ export const AnchorCommitPresentation = sparse(
)
export type AnchorCommitPresentation = TypeOf<typeof AnchorCommitPresentation>

export const NotCompleteStatusName = union([
const NotCompleteStatusName = union([
literal(AnchorRequestStatusName.PENDING),
literal(AnchorRequestStatusName.PROCESSING),
literal(AnchorRequestStatusName.FAILED),
literal(AnchorRequestStatusName.READY),
literal(AnchorRequestStatusName.REPLACED),
])
export type NotCompleteStatusName = TypeOf<typeof NotCompleteStatusName>

export const NotCompleteCASResponse = sparse(
{
Expand Down
3 changes: 2 additions & 1 deletion packages/codecs/src/feed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Context, Type, type } from 'codeco'
import { commitIdAsString, StreamMetadata } from './stream.js'
import { commitIdAsString, CommitTypeAsNumber, StreamMetadata } from './stream.js'

export const JsonAsString = new Type<unknown, string, string>(
'JSON-as-string',
Expand All @@ -18,4 +18,5 @@ export const AggregationDocument = type({
commitId: commitIdAsString,
content: JsonAsString,
metadata: StreamMetadata,
eventType: CommitTypeAsNumber,
})
4 changes: 4 additions & 0 deletions packages/codecs/src/stream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CommitID, StreamID } from '@ceramicnetwork/streamid'
import { CommitType } from '@ceramicnetwork/common'
import { type Context, Type, refinement, string, sparse, array, optional, boolean } from 'codeco'
import { enumCodec } from './enum.js'

/**
* Verify if `input` is a StreamID string.
Expand Down Expand Up @@ -80,3 +82,5 @@ export const StreamMetadata = sparse({
tags: optional(array(string)),
forbidControllerChange: optional(boolean),
})

export const CommitTypeAsNumber = enumCodec<CommitType>('CommitType', CommitType)
1 change: 0 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"clean": "npx rimraf ./lib"
},
"dependencies": {
"@ceramicnetwork/codecs": "^2.4.0",
"@ceramicnetwork/streamid": "^3.4.0",
"@didtools/cacao": "^3.0.0",
"@didtools/pkh-ethereum": "^0.2.0",
Expand Down
18 changes: 16 additions & 2 deletions packages/common/src/anchor-service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import type { CID } from 'multiformats/cid'
import type { StreamID } from '@ceramicnetwork/streamid'
import type { CAR } from 'cartonne'
import type { NotCompleteStatusName } from '@ceramicnetwork/codecs'
import { AnchorRequestStatusName } from '@ceramicnetwork/codecs'

export enum AnchorRequestStatusName {
PENDING = 'PENDING',
PROCESSING = 'PROCESSING',
COMPLETED = 'COMPLETED',
FAILED = 'FAILED',
READY = 'READY',
REPLACED = 'REPLACED',
}

export type NotCompleteStatusName =
| AnchorRequestStatusName.PENDING
| AnchorRequestStatusName.PROCESSING
| AnchorRequestStatusName.FAILED
| AnchorRequestStatusName.READY
| AnchorRequestStatusName.REPLACED

/**
* Describes all anchor statuses
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/__tests__/ceramic-anchor.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { jest } from '@jest/globals'
import { Ceramic } from '../ceramic.js'
import { AnchorStatus, IpfsApi } from '@ceramicnetwork/common'
import { AnchorStatus, IpfsApi, AnchorRequestStatusName } from '@ceramicnetwork/common'
import { Utils as CoreUtils } from '../utils.js'
import { createIPFS, swarmConnect } from '@ceramicnetwork/ipfs-daemon'
import { TileDocument } from '@ceramicnetwork/stream-tile'
import { InMemoryAnchorService } from '../anchor/memory/in-memory-anchor-service.js'
import { createCeramic as vanillaCreateCeramic } from './create-ceramic.js'
import { AnchorRequestStatusName } from '@ceramicnetwork/codecs'
import { CommonTestUtils as TestUtils } from '@ceramicnetwork/common-test-utils'

const SEED = '6e34b2e1a9624113d81ece8a8a22e6e97f0e145c25c1d4d2d0e62753b4060c83'
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/__tests__/ceramic-feed.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, describe, test, beforeEach, afterEach } from '@jest/globals'
import { type IpfsApi } from '@ceramicnetwork/common'
import { expect, describe, test, beforeAll, afterAll } from '@jest/globals'
import { CommitType, type IpfsApi } from '@ceramicnetwork/common'
import { Utils as CoreUtils } from '@ceramicnetwork/core'
import { TileDocument } from '@ceramicnetwork/stream-tile'
import { createIPFS, swarmConnect } from '@ceramicnetwork/ipfs-daemon'
Expand Down Expand Up @@ -49,6 +49,8 @@ describe('Ceramic feed', () => {
expect(feed[0].metadata).toStrictEqual(feed[1].metadata)
expect(feed[0].content).toStrictEqual({ hello: original })
expect(feed[1].content).toStrictEqual({ hello: updated })
expect(feed[0].eventType).toBe(CommitType.GENESIS)
expect(feed[1].eventType).toBe(CommitType.SIGNED)
})

test('add entry after loading pinned stream/pubsub', async () => {
Expand All @@ -74,7 +76,11 @@ describe('Ceramic feed', () => {
await TestUtils.delay(500)

expect(feed1.length).toEqual(2) // create + update
expect(feed1[0].eventType).toBe(CommitType.GENESIS)
expect(feed1[1].eventType).toBe(CommitType.SIGNED)
expect(feed2.length).toEqual(2) //load + pubsub update
expect(feed2[0].eventType).toBe(CommitType.GENESIS)
expect(feed2[1].eventType).toBe(CommitType.SIGNED)
expect(feed2[0].content.test).toBe(content.test)
expect(feed2[0].commitId).toStrictEqual(feed1[0].commitId)
// test pubsub propagating the update from stream1 being inside the feed
Expand Down Expand Up @@ -108,6 +114,7 @@ describe('Ceramic feed', () => {
expect(feed[0].content).toEqual(model.state.content)
expect(feed[0].metadata).toEqual(model.state.metadata)
expect(feed[0].commitId).toEqual(model.commitId)
expect(feed[0].eventType).toBe(CommitType.GENESIS)
s1.unsubscribe()
})

Expand All @@ -130,6 +137,8 @@ describe('Ceramic feed', () => {
expect(feed[0].content).toEqual(feed[1].content)
expect(feed[0].metadata).toEqual(feed[1].metadata)
expect(feed[0].commitId.equals(feed[1].commitId)).toEqual(false)
expect(feed[0].eventType).toBe(CommitType.GENESIS)
expect(feed[1].eventType).toBe(CommitType.ANCHOR)
s.unsubscribe()
})
})
8 changes: 6 additions & 2 deletions packages/core/src/__tests__/state-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import {
jest,
test,
} from '@jest/globals'
import { AnchorStatus, IpfsApi, SignatureStatus } from '@ceramicnetwork/common'
import {
AnchorStatus,
IpfsApi,
AnchorRequestStatusName,
SignatureStatus,
} from '@ceramicnetwork/common'
import { Utils as CoreUtils } from '@ceramicnetwork/core'
import { createIPFS } from '@ceramicnetwork/ipfs-daemon'
import { createCeramic } from './create-ceramic.js'
import { Ceramic } from '../ceramic.js'
import { TileDocument } from '@ceramicnetwork/stream-tile'
import { InMemoryAnchorService } from '../anchor/memory/in-memory-anchor-service.js'
import { AnchorRequestStatusName } from '@ceramicnetwork/codecs'
import { CommonTestUtils as TestUtils } from '@ceramicnetwork/common-test-utils'

const INITIAL_CONTENT = { abc: 123, def: 456 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { expect, jest } from '@jest/globals'
import { LoggerProvider, type fetchJson, IpfsApi } from '@ceramicnetwork/common'
import {
LoggerProvider,
AnchorRequestStatusName,
type fetchJson,
type IpfsApi,
} from '@ceramicnetwork/common'
import { createIPFS } from '@ceramicnetwork/ipfs-daemon'
import { createCeramic } from '../../../__tests__/create-ceramic.js'
import { createDidAnchorServiceAuth } from '../../../__tests__/create-did-anchor-service-auth.js'
import { AuthenticatedEthereumAnchorService } from '../ethereum-anchor-service.js'
import { generateFakeCarFile } from './generateFakeCarFile.js'
import { AnchorRequestStatusName } from '@ceramicnetwork/codecs'
import { AnchorRequestStore } from '../../../store/anchor-request-store.js'
import type { AnchorLoopHandler } from '../../anchor-service.js'
import { CARFactory, type CAR } from 'cartonne'
Expand Down Expand Up @@ -52,13 +56,7 @@ describe('AuthenticatedEthereumAnchorServiceTest', () => {

const auth = createDidAnchorServiceAuth(url, ceramic.signer, diagnosticsLogger, fauxFetchJson)
const signRequestSpy = jest.spyOn(auth, 'signRequest')
const anchorService = new AuthenticatedEthereumAnchorService(
auth,
url,
url,
diagnosticsLogger,
100
)
const anchorService = new AuthenticatedEthereumAnchorService(auth, url, url, diagnosticsLogger)

jest.spyOn(anchorService.validator, 'init').mockImplementation(async () => {
// Do Nothing
Expand Down Expand Up @@ -87,13 +85,7 @@ describe('AuthenticatedEthereumAnchorServiceTest', () => {

const auth = createDidAnchorServiceAuth(url, ceramic.signer, diagnosticsLogger, fauxFetchJson)
const signRequestSpy = jest.spyOn(auth, 'signRequest')
const anchorService = new AuthenticatedEthereumAnchorService(
auth,
url,
url,
diagnosticsLogger,
100
)
const anchorService = new AuthenticatedEthereumAnchorService(auth, url, url, diagnosticsLogger)
jest.spyOn(anchorService.validator, 'init').mockImplementation(async () => {
// Do Nothing
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, expect, jest, test } from '@jest/globals'
import { RemoteCAS } from '../remote-cas.js'
import { fetchJson } from '@ceramicnetwork/common'
import { fetchJson, AnchorRequestStatusName } from '@ceramicnetwork/common'
import { CommonTestUtils as TestUtils } from '@ceramicnetwork/common-test-utils'
import { AnchorRequestCarFileReader } from '../../anchor-request-car-file-reader.js'
import { generateFakeCarFile } from './generateFakeCarFile.js'
import { AnchorRequestStatusName, dateAsUnix } from '@ceramicnetwork/codecs'
import { dateAsUnix } from '@ceramicnetwork/codecs'

const ANCHOR_SERVICE_URL = 'http://example.com'
const POLL_INTERVAL = 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type {
DiagnosticsLogger,
CeramicSigner,
} from '@ceramicnetwork/common'
import { AnchorRequestStatusName } from '@ceramicnetwork/common'
import { Subject, type Observable } from 'rxjs'
import type { CAR } from 'cartonne'
import { AnchorRequestCarFileReader } from '../anchor-request-car-file-reader.js'
import { AnchorRequestStatusName } from '@ceramicnetwork/codecs'
import { EthereumAnchorValidator } from './ethereum-anchor-validator.js'
import type {
AnchorService,
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/anchor/ethereum/remote-cas.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import type { CASClient } from '../anchor-service.js'
import type { AnchorEvent, FetchRequest } from '@ceramicnetwork/common'
import { AnchorRequestStatusName } from '@ceramicnetwork/common'
import type { AnchorRequestCarFileReader } from '../anchor-request-car-file-reader.js'
import {
AnchorRequestStatusName,
CASResponseOrError,
ErrorResponse,
SupportedChainsResponse,
} from '@ceramicnetwork/codecs'
import { CASResponseOrError, ErrorResponse, SupportedChainsResponse } from '@ceramicnetwork/codecs'
import type { StreamID } from '@ceramicnetwork/streamid'
import type { CID } from 'multiformats/cid'
import { validate, isValid, decode } from 'codeco'
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/anchor/memory/in-memory-anchor-service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { AnchorEvent, CeramicSigner } from '@ceramicnetwork/common'
import { type DiagnosticsLogger } from '@ceramicnetwork/common'
import {
AnchorRequestStatusName,
type NotCompleteStatusName,
type DiagnosticsLogger,
} from '@ceramicnetwork/common'
import { type CAR } from 'cartonne'
import { AnchorRequestCarFileReader } from '../anchor-request-car-file-reader.js'
import { AnchorRequestStatusName, NotCompleteStatusName } from '@ceramicnetwork/codecs'
import type { AnchorLoopHandler, AnchorService, AnchorValidator } from '../anchor-service.js'
import { InMemoryAnchorValidator } from './in-memory-anchor-validator.js'
import type { AnchorRequestStore } from '../../store/anchor-request-store.js'
import { InMemoryCAS } from './in-memory-cas.js'
import { CID } from 'multiformats'
import { AnchorProcessingLoop } from '../anchor-processing-loop.js'
import { doNotWait } from '../../ancillary/do-not-wait.js'
import { NamedTaskQueue } from '../../state-management/named-task-queue.js'
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/anchor/memory/in-memory-cas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { CASClient } from '../anchor-service.js'
import { AnchorCommit, AnchorEvent, AnchorProof } from '@ceramicnetwork/common'
import { AnchorRequestStatusName, NotCompleteStatusName } from '@ceramicnetwork/codecs'
import { AnchorRequestStatusName } from '@ceramicnetwork/common'
import type {
AnchorCommit,
AnchorEvent,
NotCompleteStatusName,
AnchorProof,
} from '@ceramicnetwork/common'
import { AnchorRequestCarFileReader } from '../anchor-request-car-file-reader.js'
import { randomCID, StreamID } from '@ceramicnetwork/streamid'
import { CARFactory } from 'cartonne'
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/feed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { StreamMetadata, StreamState } from '@ceramicnetwork/common'
import { CommitType, StreamMetadata, StreamState } from '@ceramicnetwork/common'
import { Subject, type Observable } from 'rxjs'
import { CommitID } from '@ceramicnetwork/streamid'
import { StreamUtils } from '@ceramicnetwork/common'
Expand All @@ -7,14 +7,16 @@ export class FeedDocument {
constructor(
readonly commitId: CommitID,
readonly content: any,
readonly metadata: StreamMetadata
readonly metadata: StreamMetadata,
readonly eventType: CommitType
) {}

static fromStreamState(streamState: StreamState): FeedDocument {
return {
commitId: StreamUtils.commitIdFromStreamState(streamState),
content: streamState.next ? streamState.next.content : streamState.content,
metadata: streamState.next ? streamState.next.metadata : streamState.metadata,
eventType: streamState.log[streamState.log.length - 1].type,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
StreamState,
StreamUtils,
SyncOptions,
AnchorRequestStatusName,
} from '@ceramicnetwork/common'
import { Utils as CoreUtils } from '../../utils.js'
import { TileDocument } from '@ceramicnetwork/stream-tile'
Expand All @@ -32,7 +33,6 @@ import cloneDeep from 'lodash.clonedeep'
import { CID } from 'multiformats/cid'
import { StateLink } from '../state-link.js'
import { OperationType } from '../operation-type.js'
import { AnchorRequestStatusName } from '@ceramicnetwork/codecs'
import { generateFakeCarFile } from '../../anchor/ethereum/__tests__/generateFakeCarFile.js'
import type { FeedDocument } from '../../feed.js'
import { CommonTestUtils as TestUtils } from '@ceramicnetwork/common-test-utils'
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/state-management/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { StreamUpdater } from '../stream-loading/stream-updater.js'
import { CID } from 'multiformats/cid'
import type { AnchorLoopHandler, AnchorService } from '../anchor/anchor-service.js'
import type { AnchorRequestCarBuilder } from '../anchor/anchor-request-car-builder.js'
import { AnchorRequestStatusName } from '@ceramicnetwork/codecs'
import { AnchorRequestStatusName } from '@ceramicnetwork/common'
import { CAR } from 'cartonne'
import { FeedDocument, type Feed } from '../feed.js'
import { doNotWait } from '../ancillary/do-not-wait.js'
Expand Down
4 changes: 3 additions & 1 deletion packages/indexing/src/tables-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ export class TablesManager {

if (validSchema != actualSchema) {
throw new Error(
`Schema verification failed for config table: ${table.tableName}. Please make sure node has been setup correctly.
`Schema verification failed for config table: ${
table.tableName
}. Please make sure node has been setup correctly.
Expected=${JSON.stringify(validSchema)}
Actual=${JSON.stringify(actualSchema)}
`
Expand Down

0 comments on commit eae99ad

Please sign in to comment.