Skip to content

Commit

Permalink
fix: postgres should not be required in STACKS_API_MODE=offline mode #…
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x authored Mar 27, 2023
1 parent e348ac0 commit 299705f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 35 deletions.
28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@
},
"killBehavior": "polite",
},
{
"type": "node",
"request": "launch",
"name": "Launch: mocknet offline-mode",
"skipFiles": [
"<node_internals>/**"
],
"runtimeArgs": [
"-r",
"ts-node/register/transpile-only",
"-r",
"tsconfig-paths/register"
],
"args": [
"${workspaceFolder}/src/index.ts"
],
"outputCapture": "std",
"internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": "stacks-node:start-mocknet",
"postDebugTask": "stacks-node:stop-mocknet",
"env": {
"STACKS_CHAIN_ID": "0x80000000",
"NODE_ENV": "development",
"STACKS_API_MODE": "offline",
"TS_NODE_SKIP_IGNORE": "true"
},
"killBehavior": "polite",
},
{
"type": "node",
"request": "launch",
Expand Down
22 changes: 22 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@
},
"presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "dedicated", "clear": false }
},
{
"label": "stacks-node:start-mocknet",
"type": "shell",
"command": "docker compose -f docker/docker-compose.dev.stacks-blockchain.yml up --force-recreate -V",
"isBackground": true,
"problemMatcher": {
"pattern": { "regexp": ".", "file": 1, "location": 2, "message": 3, },
"background": { "activeOnStart": true, "beginsPattern": ".", "endsPattern": "." }
},
"presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "dedicated", "clear": false }
},
{
"label": "stacks-node:stop-mocknet",
"type": "shell",
"command": "docker compose -f docker/docker-compose.dev.stacks-blockchain.yml down -v -t 0",
"isBackground": true,
"problemMatcher": {
"pattern": { "regexp": ".", "file": 1, "location": 2, "message": 3, },
"background": { "activeOnStart": true, "beginsPattern": ".", "endsPattern": "." }
},
"presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "dedicated", "clear": false }
},
{
"label": "deploy:krypton",
"type": "shell",
Expand Down
31 changes: 17 additions & 14 deletions src/datastore/offline-dummy-store.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { EventEmitter } from 'events';
import { PgStoreEventEmitter } from './pg-store-event-emitter';
import { PgStore } from './pg-store';
import { PgWriteStore } from './pg-write-store';

export const OfflineDummyStore: PgStore = new Proxy(new EventEmitter() as PgStoreEventEmitter, {
get(target: any, propKey) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (propKey === 'eventEmitter') return target;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (propKey in target) return target[propKey];
return function () {
throw new Error(
`Cannot call function on the Dummy datastore. Check if the application is running in offline mode.`
);
};
},
});
export const OfflineDummyStore: PgWriteStore = new Proxy(
new EventEmitter() as PgStoreEventEmitter,
{
get(target: any, propKey) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (propKey === 'eventEmitter') return target;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (propKey in target) return target[propKey];
return function () {
throw new Error(
`Cannot call function on the Dummy datastore. Check if the application is running in offline mode.`
);
};
},
}
);
51 changes: 30 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,21 @@ async function init(): Promise<void> {
);
}
const apiMode = getApiMode();
const dbStore =
apiMode === StacksApiMode.offline
? OfflineDummyStore
: await PgStore.connect({
usageName: `datastore-${apiMode}`,
});
const dbWriteStore = await PgWriteStore.connect({
usageName: `write-datastore-${apiMode}`,
skipMigrations: apiMode === StacksApiMode.readOnly,
});

registerMempoolPromStats(dbWriteStore.eventEmitter);
let dbStore: PgStore;
let dbWriteStore: PgWriteStore;
if (apiMode === StacksApiMode.offline) {
dbStore = OfflineDummyStore;
dbWriteStore = OfflineDummyStore;
} else {
dbStore = await PgStore.connect({
usageName: `datastore-${apiMode}`,
});
dbWriteStore = await PgWriteStore.connect({
usageName: `write-datastore-${apiMode}`,
skipMigrations: apiMode === StacksApiMode.readOnly,
});
registerMempoolPromStats(dbWriteStore.eventEmitter);
}

if (apiMode === StacksApiMode.default || apiMode === StacksApiMode.writeOnly) {
const configuredChainID = getApiConfiguredChainID();
Expand Down Expand Up @@ -168,7 +171,11 @@ async function init(): Promise<void> {
}
}

if (apiMode === StacksApiMode.default || apiMode === StacksApiMode.readOnly) {
if (
apiMode === StacksApiMode.default ||
apiMode === StacksApiMode.readOnly ||
apiMode === StacksApiMode.offline
) {
const apiServer = await startApiServer({
datastore: dbStore,
writeDatastore: dbWriteStore,
Expand All @@ -193,14 +200,16 @@ async function init(): Promise<void> {
});
}

registerShutdownConfig({
name: 'DB',
handler: async () => {
await dbStore.close();
await dbWriteStore.close();
},
forceKillable: false,
});
if (apiMode !== StacksApiMode.offline) {
registerShutdownConfig({
name: 'DB',
handler: async () => {
await dbStore.close();
await dbWriteStore.close();
},
forceKillable: false,
});
}

if (isProdEnv) {
const prometheusServer = await createPrometheusServer({ port: 9153 });
Expand Down

0 comments on commit 299705f

Please sign in to comment.