Skip to content

Commit

Permalink
feat(chain-follower): add WIP Dgraph client and service
Browse files Browse the repository at this point in the history
Also implements envalid for process.env handling
  • Loading branch information
rhyslbw committed Oct 21, 2021
1 parent 33d3962 commit 962f4b7
Show file tree
Hide file tree
Showing 52 changed files with 515 additions and 32 deletions.
Binary file added packages-cache/@grpc-grpc-js-1.4.1.tgz
Binary file not shown.
Binary file added packages-cache/@grpc-proto-loader-0.6.6.tgz
Binary file not shown.
Binary file added packages-cache/@mapbox-node-pre-gyp-1.0.5.tgz
Binary file not shown.
Binary file added packages-cache/@types-bytebuffer-5.0.42.tgz
Binary file not shown.
Binary file added packages-cache/@types-google-protobuf-3.15.5.tgz
Binary file not shown.
Binary file added packages-cache/@types-node-16.11.1.tgz
Binary file not shown.
Binary file added packages-cache/@types-protobufjs-6.0.0.tgz
Binary file not shown.
Binary file added packages-cache/@types-url-parse-1.4.4.tgz
Binary file not shown.
Binary file added packages-cache/ansi-regex-5.0.1.tgz
Binary file not shown.
Binary file added packages-cache/aproba-1.2.0.tgz
Binary file not shown.
Binary file added packages-cache/are-we-there-yet-1.1.7.tgz
Binary file not shown.
Binary file added packages-cache/ascli-1.0.1.tgz
Binary file not shown.
Binary file added packages-cache/bytebuffer-5.0.1.tgz
Binary file not shown.
Binary file added packages-cache/chownr-2.0.0.tgz
Binary file not shown.
Binary file added packages-cache/cliui-7.0.4.tgz
Binary file not shown.
Binary file added packages-cache/colour-0.7.1.tgz
Binary file not shown.
Binary file added packages-cache/console-control-strings-1.1.0.tgz
Binary file not shown.
Binary file added packages-cache/delegates-1.0.0.tgz
Binary file not shown.
Binary file added packages-cache/detect-libc-1.0.3.tgz
Binary file not shown.
Binary file added packages-cache/dgraph-js-21.3.1.tgz
Binary file not shown.
Binary file added packages-cache/escalade-3.1.1.tgz
Binary file not shown.
Binary file added packages-cache/fs-extra-10.0.0.tgz
Binary file not shown.
Binary file added packages-cache/fs-minipass-2.1.0.tgz
Binary file not shown.
Binary file added packages-cache/gauge-2.7.4.tgz
Binary file not shown.
Binary file added packages-cache/google-protobuf-3.18.1.tgz
Binary file not shown.
Binary file added packages-cache/grpc-1.24.11.tgz
Binary file not shown.
Binary file added packages-cache/has-unicode-2.0.1.tgz
Binary file not shown.
Binary file added packages-cache/is-base64-1.1.0.tgz
Binary file not shown.
Binary file added packages-cache/long-3.2.0.tgz
Binary file not shown.
Binary file added packages-cache/minipass-3.1.5.tgz
Binary file not shown.
Binary file added packages-cache/minizlib-2.1.2.tgz
Binary file not shown.
Binary file added packages-cache/nan-2.15.0.tgz
Binary file not shown.
Binary file added packages-cache/nopt-5.0.0.tgz
Binary file not shown.
Binary file added packages-cache/npmlog-4.1.2.tgz
Binary file not shown.
Binary file added packages-cache/optjs-3.2.2.tgz
Binary file not shown.
Binary file added packages-cache/protobufjs-5.0.3.tgz
Binary file not shown.
Binary file added packages-cache/protobufjs-6.11.2.tgz
Binary file not shown.
Binary file added packages-cache/string-width-4.2.3.tgz
Binary file not shown.
Binary file added packages-cache/strip-ansi-6.0.1.tgz
Binary file not shown.
Binary file added packages-cache/tar-6.1.11.tgz
Binary file not shown.
Binary file added packages-cache/wide-align-1.1.5.tgz
Binary file not shown.
Binary file added packages-cache/y18n-5.0.8.tgz
Binary file not shown.
Binary file added packages-cache/yargs-16.2.0.tgz
Binary file not shown.
Binary file added packages-cache/yargs-parser-20.2.9.tgz
Binary file not shown.
8 changes: 8 additions & 0 deletions packages/chain-follower/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@
"@cardano-ogmios/client": "^4.1.0",
"@cardanosolutions/json-bigint": "^1.0.0",
"@emurgo/cip14-js": "^2.0.0",
"bunyan": "^1.8.15",
"death": "^1.1.0",
"dgraph-js": "^21.3.1",
"envalid": "^7.2.1",
"fs-extra": "^10.0.0",
"grpc": "^1.24.11",
"ts-custom-error": "^3.2.0",
"ts-log": "^2.2.3"
},
"devDependencies": {
"@cardano-graphql/util-dev": "6.0.0",
"@types/google-protobuf": "^3.15.5",
"@types/node": "^14.0.13",
"@types/protobufjs": "^6.0.0",
"shx": "^0.3.2",
"typescript": "^3.9.5"
}
Expand Down
24 changes: 5 additions & 19 deletions packages/chain-follower/src/ChainFollower.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import {
ChainSync,
ConnectionConfig,
createChainSyncClient,
createInteractionContext,
Schema
} from '@cardano-ogmios/client'
import { errors, RunnableModuleState } from '@cardano-graphql/util'
import { dummyLogger, Logger } from 'ts-log'
import { dummyLogger } from 'ts-log'

const MODULE_NAME = 'ChainFollower'

interface Config {
cardanoNodeConfigPath: string;
db: {
database: string;
host: string;
password: string;
port: number;
user: string;
};
ogmios?: {
host?: string;
port?: number;
};
}

export class ChainFollower {
private chainSyncClient: ChainSync.ChainSyncClient;
private state: RunnableModuleState;

constructor (private logger: Logger = dummyLogger) {
constructor (private logger = dummyLogger) {
this.state = null
}

public async initialize (ogmiosConfig: Config['ogmios']) {
public async initialize (ogmiosConnection: ConnectionConfig) {
if (this.state !== null) return
this.state = 'initializing'
this.logger.info({ module: MODULE_NAME }, 'Initializing')
Expand All @@ -43,7 +29,7 @@ export class ChainFollower {
this.logger.error({ module: MODULE_NAME, code }, reason)
},
{
connection: ogmiosConfig,
connection: ogmiosConnection,
interactionType: 'LongRunning'
}
)
Expand Down
20 changes: 20 additions & 0 deletions packages/chain-follower/src/DgraphClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { dummyLogger } from 'ts-log'
import dgraph from 'dgraph-js'

export interface DgraphClient {
setSchema: (schema: string) => Promise<void>
}

export const createDgraphClient = (address: string, logger = dummyLogger): DgraphClient => {
const clientStub = new dgraph.DgraphClientStub(address)
const dgraphClient = new dgraph.DgraphClient(clientStub)

return {
async setSchema (schema) {
const op = new dgraph.Operation()
op.setSchema(schema)
await dgraphClient.alter(op)
logger.info('Dgraph schema set')
}
}
}
18 changes: 18 additions & 0 deletions packages/chain-follower/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { LogLevelString } from 'bunyan'
import { cleanEnv, EnvError, host, makeValidator, port, Spec } from 'envalid'

// Todo: Hoist to util package, dedupe
// custom validator same as str from envalid but with typed return type
const str = <T extends string = string> (spec?: Spec<T>) => makeValidator((input: string) => {
if (typeof input === 'string') return input as T
throw new EnvError(`Not a string: "${input}"`)
})(spec)

export const getConfig = () => cleanEnv(process.env, {
dgraphAddress: host(),
loggerMinSeverity: str<LogLevelString>({ choices: ['trace', 'debug', 'info', 'warn', 'error', 'fatal'] }),
ogmiosHost: host({ default: undefined }),
ogmiosPort: port({ default: undefined })
})

export type Config = ReturnType<typeof getConfig>
1 change: 1 addition & 0 deletions packages/chain-follower/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './ChainFollower'
export * from './DgraphClient'
35 changes: 35 additions & 0 deletions packages/chain-follower/src/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createDgraphClient } from './DgraphClient'
import { getConfig } from './config'
import { Logger } from 'ts-log'
import { createLogger } from 'bunyan'
import onDeath from 'death'
import { ChainFollower } from './ChainFollower'
import { loadSchema} from './util'

(async () => {
const config = getConfig()

const logger: Logger = createLogger({
name: 'chain-follower',
level: config.loggerMinSeverity
})

try {
const dgraphClient = createDgraphClient(config.dgraphAddress)
await dgraphClient.setSchema(await loadSchema())

const chainFollower = new ChainFollower(logger)

onDeath(async (signal) => {
logger.error({ signal }, 'About to exit the process')
await chainFollower.shutdown()
process.exit(1)
})

await chainFollower.initialize({ host: config.ogmiosHost, port: config.ogmiosPort })
await chainFollower.start(['origin'])
} catch (error) {
logger.error(error)
process.exit(1)
}
})()
3 changes: 3 additions & 0 deletions packages/chain-follower/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { readFile } from 'fs-extra'

export const loadSchema = async (): Promise<string> => readFile('./schema', 'utf-8')
Loading

0 comments on commit 962f4b7

Please sign in to comment.