Skip to content

Commit

Permalink
chore: iterate on schema design (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingloh authored Jun 10, 2024
1 parent 9fefd43 commit 29dd424
Show file tree
Hide file tree
Showing 43 changed files with 1,704 additions and 1,011 deletions.
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2
updates:
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: 'weekly'
- package-ecosystem: npm
directory: '/'
schedule:
interval: 'weekly'
groups:
eslint:
patterns:
- 'eslint'
- '@eslint/*'
4 changes: 0 additions & 4 deletions .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
"matchPackageNames": ["node", "typescript", "@types/node"],
"enabled": false
},
{
"matchPackagePatterns": ["^@contentedjs/"],
"groupName": "@contentedjs"
},
{
"matchPackagePatterns": ["^@eslint/", "^eslint$"],
"groupName": "@eslint"
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,22 @@ to accelerate the adoption of blockchain technology.
```js
import hardhat from '@chainfile/eip-155-31337/hardhat.json';

let testcontainers: ChainfileTestcontainers;
const testcontainers = new ChainfileTestcontainers(hardhat);

beforeAll(async () => {
testcontainers = await ChainfileTestcontainers.start(hardhat);
await testcontainers.start();
});

afterAll(async () => {
await testcontainers.stop();
});

it('should rpc(eth_blockNumber)', async () => {
const hardhat = testcontainers.get('hardhat');

const response = await hardhat.rpc({
const response = await testcontainers.get('hardhat').rpc({
method: 'eth_blockNumber',
});

expect(response.status).toStrictEqual(200);

expect(await response.json()).toMatchObject({
result: '0x0',
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"jest": "29.7.0",
"lint-staged": "^15.2.5",
"prettier": "^3.3.0",
"turbo": "^2.0.1",
"turbo": "^2.0.3",
"typescript": "5.4.5",
"wait-for-expect": "^3.0.2"
},
Expand Down
29 changes: 11 additions & 18 deletions packages/chainfile-agent/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
FROM node:20-alpine as base
RUN apk add --no-cache libc6-compat python3 make g++ && \
apk update
WORKDIR /app

RUN corepack enable pnpm

ENV PNPM_HOME="/root/.local/share/pnpm/global" \
PATH="$PATH:/root/.local/share/pnpm/global"
ENV PNPM_HOME="/pnpm" \
PATH="/pnpm:$PATH"

COPY package.json package.json
RUN pnpm -v
ENV COREPACK_ENABLE_NETWORK=0

RUN pnpm config set store-dir /root/.local/share/pnpm/global/store/v3
ENV COREPACK_ENABLE_NETWORK=0 \
TURBO_TELEMETRY_DISABLED=1

RUN pnpm config set store-dir /root/.local/share/pnpm/global/store/v3 \
pnpm config set update-notifier false

# Isolate workspace by pruning non-related services.
FROM base AS pruner

# renovate: datasource=npm depName=turbo
ENV TURBO_VERSION=2.0.1
RUN pnpm add -g turbo@${TURBO_VERSION}
RUN pnpm add -g turbo@2

COPY . .

Expand All @@ -33,25 +31,20 @@ COPY .gitignore pnpm-workspace.yaml ./
COPY --from=pruner /app/out/json/ .
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml

# Mount cache to copy from content-addressable store
RUN --mount=type=cache,target=/root/.local/share/pnpm/global/store/v3\
pnpm install --frozen-lockfile
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile

COPY --from=pruner /app/out/full/ .

# To relink dependencies with ./bin links
RUN pnpm install --frozen-lockfile --offline

RUN pnpm turbo run build --filter=@chainfile/agent

# Run agent
FROM node:20-alpine AS runner
WORKDIR /app/packages/chainfile-agent

RUN addgroup --system --gid 1001 chainfile && \
adduser --system --uid 1001 chainfile
USER chainfile

EXPOSE 1569
WORKDIR /app/packages/chainfile-agent

COPY --from=builder --chown=chainfile:chainfile /app /app
CMD node "dist/server.js"
6 changes: 6 additions & 0 deletions packages/chainfile-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
"@trpc/server": "^10.45.2",
"ajv": "^8.16.0",
"ajv-formats": "^3.0.1",
"debug": "^4.3.4",
"lodash": "^4.17.21",
"trpc-openapi": "^1.2.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/debug": "^4.1.12",
"@types/lodash": "^4.17.4"
}
}
7 changes: 2 additions & 5 deletions packages/chainfile-agent/src/routers/_app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { createCallerFactory, router } from '../trpc';
import { createCallerFactory, mergeRouters } from '../trpc';
import { agentRouter } from './agent';
import { probesRouter } from './probes';

export const appRouter = router({
Probes: probesRouter,
Agent: agentRouter,
});
export const appRouter = mergeRouters(probesRouter, agentRouter);

export const createCaller = createCallerFactory(appRouter);
10 changes: 10 additions & 0 deletions packages/chainfile-agent/src/routers/_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ function getChainfile(): Chainfile {
throw new Error(`Invalid Chainfile: ${ajv.errorsText(validateFunction.errors)}`);
}

function getValues() {
const CHAINFILE_VALUES = process.env.CHAINFILE_VALUES;
if (CHAINFILE_VALUES === undefined) {
throw new Error('CHAINFILE_VALUES is not defined, cannot start @chainfile/agent.');
}
return JSON.parse(CHAINFILE_VALUES);
}

const chainfile = getChainfile();
const values = getValues();

export const createContext = async () => {
return {
chainfile: chainfile,
values: values,
};
};

Expand Down
20 changes: 8 additions & 12 deletions packages/chainfile-agent/src/routers/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ const chainfile: Chainfile = {
$schema: 'https://chainfile.org/schema.json',
caip2: 'bip122:0f9188f13cb7b2c71f2a335e3a4fc328',
name: 'Bitcoin Regtest',
env: {
RPC_USER: {
type: 'Value',
value: 'agent',
},
RPC_PASSWORD: {
type: 'Value',
value: 'agent',
},
values: {
rpc_user: 'agent',
rpc_password: 'agent',
},
containers: {
bitcoind: {
image: 'docker.io/kylemanna/bitcoind@sha256:1492fa0306cb7eb5de8d50ba60367cff8d29b00b516e45e93e05f8b54fa2970e',
image: 'docker.io/kylemanna/bitcoind',
tag: 'latest',
source: 'https://github.com/kylemanna/docker-bitcoind',
endpoints: {
rpc: {
Expand Down Expand Up @@ -64,9 +59,10 @@ const chainfile: Chainfile = {

const caller = createCaller({
chainfile: chainfile,
values: {},
});

it('should call GetChainfile', async () => {
const result = await caller.Agent.GetChainfile();
it('should getChainfile', async () => {
const result = await caller.getChainfile();
expect(result).toEqual(JSON.parse(JSON.stringify(chainfile)));
});
4 changes: 2 additions & 2 deletions packages/chainfile-agent/src/routers/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { z } from 'zod';
import { publicProcedure, router } from '../trpc';

export const agentRouter = router({
GetChainfile: publicProcedure
getChainfile: publicProcedure
.meta({ openapi: { method: 'GET', path: '/chainfile', tags: ['agent'] } })
.input(z.void())
.output(
z.object({
$schema: z.string(),
caip2: z.string(),
name: z.string(),
env: z.any().optional(),
values: z.any().optional(),
containers: z.any(),
}),
)
Expand Down
Loading

0 comments on commit 29dd424

Please sign in to comment.