Skip to content

Commit 87c66f8

Browse files
authored
Merge pull request #126 from proto-kit/feature/devops-rampup
Feature/devops rampup
2 parents 72f0fac + c8624a7 commit 87c66f8

File tree

12 files changed

+650
-12
lines changed

12 files changed

+650
-12
lines changed

.github/workflows/pull-request-develop.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,46 @@ jobs:
9191

9292
- name: "Test"
9393
run: npm run test:ci
94+
95+
integration:
96+
runs-on: ubuntu-latest
97+
needs: test
98+
99+
env:
100+
POSTGRES_URL: localhost
101+
REDIS_URL: localhost
102+
REDIS_CI: true
103+
DATABASE_URL: "postgresql://admin:password@localhost:5432/protokit?schema=public"
104+
105+
services:
106+
postgres:
107+
image: postgres:14-alpine
108+
env:
109+
POSTGRES_PASSWORD: password
110+
POSTGRES_USER: admin
111+
POSTGRES_DB: protokit
112+
ports:
113+
- 5432:5432
114+
redis:
115+
image: redis:6.2-alpine
116+
ports:
117+
- 6379:6379
118+
119+
steps:
120+
- uses: actions/checkout@v3
121+
- uses: actions/setup-node@v3
122+
with:
123+
node-version: 18
124+
cache: npm
125+
126+
- name: "Install dependencies"
127+
run: npm ci --workspaces --include-workspace-root
128+
129+
- name: "Build"
130+
run: npm run build
131+
132+
- name: "Migrate DB"
133+
run: npm run migrate
134+
135+
- name: "Integration tests"
136+
run: npm run test:integration

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"lint:staged": "eslint",
1111
"test": "npx lerna run test --scope=\"*/sdk\" -- --passWithNoTests",
1212
"test:ci": "npx lerna run test -- --passWithNoTests --forceExit",
13+
"test:integration": "npx lerna run integration -- --passWithNoTests --forceExit",
1314
"test:watch": "npx lerna run test:watch",
15+
"migrate": "npx lerna run prisma-migrate",
1416
"commit": "cz",
1517
"publish:canary": "npx lerna publish prerelease --no-private --exact --yes --canary --preid develop --dist-tag latest --loglevel verbose --force-git-tag --force-publish"
1618
},

packages/persistance/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"lint": "eslint ./src ./test",
1313
"test:file": "node --experimental-vm-modules --experimental-wasm-modules --experimental-wasm-threads ../../node_modules/jest/bin/jest.js",
1414
"test": "npm run test:file -- ./test/**",
15+
"integration": "npm run test:file -- ./test-integration/** --runInBand",
1516
"test:watch": "npm run test:file -- ./test/** --watch"
1617
},
1718
"main": "dist/index.js",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `args` on the `Transaction` table. All the data in the column will be lost.
5+
- Added the required column `fromMessagesHash` to the `Block` table without a default value. This is not possible if the table is not empty.
6+
- Added the required column `toMessagesHash` to the `Block` table without a default value. This is not possible if the table is not empty.
7+
- Added the required column `isMessage` to the `Transaction` table without a default value. This is not possible if the table is not empty.
8+
9+
*/
10+
-- AlterTable
11+
ALTER TABLE "Batch" ADD COLUMN "settlementTransactionHash" TEXT;
12+
13+
-- AlterTable
14+
ALTER TABLE "Block" ADD COLUMN "fromMessagesHash" TEXT NOT NULL,
15+
ADD COLUMN "toMessagesHash" TEXT NOT NULL;
16+
17+
-- AlterTable
18+
ALTER TABLE "Transaction" DROP COLUMN "args",
19+
ADD COLUMN "argsFields" TEXT[],
20+
ADD COLUMN "argsJSON" TEXT[],
21+
ADD COLUMN "isMessage" BOOLEAN NOT NULL;
22+
23+
-- CreateTable
24+
CREATE TABLE "Settlement" (
25+
"transactionHash" TEXT NOT NULL,
26+
"promisedMessagesHash" TEXT NOT NULL,
27+
28+
CONSTRAINT "Settlement_pkey" PRIMARY KEY ("transactionHash")
29+
);
30+
31+
-- CreateTable
32+
CREATE TABLE "IncomingMessageBatchTransaction" (
33+
"transactionHash" TEXT NOT NULL,
34+
"batchId" INTEGER NOT NULL,
35+
36+
CONSTRAINT "IncomingMessageBatchTransaction_pkey" PRIMARY KEY ("transactionHash","batchId")
37+
);
38+
39+
-- CreateTable
40+
CREATE TABLE "IncomingMessageBatch" (
41+
"id" SERIAL NOT NULL,
42+
"fromMessageHash" TEXT NOT NULL,
43+
"toMessageHash" TEXT NOT NULL,
44+
45+
CONSTRAINT "IncomingMessageBatch_pkey" PRIMARY KEY ("id")
46+
);
47+
48+
-- AddForeignKey
49+
ALTER TABLE "Batch" ADD CONSTRAINT "Batch_settlementTransactionHash_fkey" FOREIGN KEY ("settlementTransactionHash") REFERENCES "Settlement"("transactionHash") ON DELETE SET NULL ON UPDATE CASCADE;
50+
51+
-- AddForeignKey
52+
ALTER TABLE "IncomingMessageBatchTransaction" ADD CONSTRAINT "IncomingMessageBatchTransaction_transactionHash_fkey" FOREIGN KEY ("transactionHash") REFERENCES "Transaction"("hash") ON DELETE RESTRICT ON UPDATE CASCADE;
53+
54+
-- AddForeignKey
55+
ALTER TABLE "IncomingMessageBatchTransaction" ADD CONSTRAINT "IncomingMessageBatchTransaction_batchId_fkey" FOREIGN KEY ("batchId") REFERENCES "IncomingMessageBatch"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

packages/persistance/src/PrismaDatabaseConnection.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ export class PrismaDatabaseConnection
7676
};
7777
}
7878

79+
public async clearDatabase(): Promise<void> {
80+
const tables = [
81+
"TransactionExecutionResult",
82+
"Transaction",
83+
"Block",
84+
"Batch",
85+
"UnprovenBlockMetadata",
86+
"State",
87+
"Settlement",
88+
"IncomingMessageBatch",
89+
"IncomingMessageBatchTransaction",
90+
];
91+
92+
await this.prismaClient.$transaction(
93+
tables.map((table) =>
94+
this.prismaClient.$executeRawUnsafe(`TRUNCATE TABLE "${table}" CASCADE`)
95+
)
96+
);
97+
}
98+
7999
public async start(): Promise<void> {
80100
const { connection } = this.config;
81101
if (connection !== undefined) {
@@ -102,4 +122,8 @@ export class PrismaDatabaseConnection
102122
this.initializedClient = new PrismaClient();
103123
}
104124
}
125+
126+
public async close() {
127+
await this.prismaClient.$disconnect();
128+
}
105129
}

packages/persistance/src/PrismaRedisDatabase.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ export class PrismaRedisDatabase
6666
this.redis.config = this.config.redis;
6767
await this.redis.start();
6868
}
69+
70+
public async close() {
71+
await this.prisma.close();
72+
await this.redis.close();
73+
}
6974
}

packages/persistance/src/RedisConnection.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { RedisMerkleTreeStore } from "./services/redis/RedisMerkleTreeStore";
99

1010
export interface RedisConnectionConfig {
1111
host: string;
12-
password: string;
12+
password?: string;
1313
port?: number;
14-
username?: string
14+
username?: string;
1515
}
1616

1717
export interface RedisConnection {
@@ -50,6 +50,10 @@ export class RedisConnectionModule
5050
};
5151
}
5252

53+
public async clearDatabase() {
54+
await this.redisClient.flushAll();
55+
}
56+
5357
public async init() {
5458
const { host, port, password, username } = this.config;
5559
this.client = createClient({
@@ -70,4 +74,8 @@ export class RedisConnectionModule
7074
public async start(): Promise<void> {
7175
await this.init();
7276
}
77+
78+
public async close() {
79+
await this.redisClient.disconnect();
80+
}
7381
}

packages/persistance/src/services/prisma/PrismaStateService.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import { noop } from "@proto-kit/common";
66
import type { PrismaConnection } from "../../PrismaDatabaseConnection";
77
import { injectable } from "tsyringe";
88

9+
// We need to create a correctly configured Decimal constructor
10+
// with our parameters
11+
const Decimal = Prisma.Decimal.clone({
12+
precision: 78,
13+
});
14+
915
@injectable()
1016
export class PrismaStateService implements AsyncStateService {
1117
private cache: StateEntry[] = [];
@@ -25,18 +31,16 @@ export class PrismaStateService implements AsyncStateService {
2531
const data = this.cache
2632
.filter((entry) => entry.value !== undefined)
2733
.map((entry) => ({
28-
path: new Prisma.Decimal(entry.key.toString()),
29-
values: entry.value!.map(
30-
(field) => new Prisma.Decimal(field.toString())
31-
),
34+
path: new Decimal(entry.key.toString()),
35+
values: entry.value!.map((field) => new Decimal(field.toString())),
3236
mask: this.mask,
3337
}));
3438

3539
await prismaClient.$transaction([
3640
prismaClient.state.deleteMany({
3741
where: {
3842
path: {
39-
in: this.cache.map((x) => new Prisma.Decimal(x.key.toString())),
43+
in: this.cache.map((x) => new Decimal(x.key.toString())),
4044
},
4145
mask: this.mask,
4246
},
@@ -55,7 +59,7 @@ export class PrismaStateService implements AsyncStateService {
5559
AND: [
5660
{
5761
path: {
58-
in: keys.map((key) => new Prisma.Decimal(key.toString())),
62+
in: keys.map((key) => new Decimal(key.toString())),
5963
},
6064
},
6165
{
@@ -65,8 +69,8 @@ export class PrismaStateService implements AsyncStateService {
6569
},
6670
});
6771
return records.map((record) => ({
68-
key: Field(record.path.toNumber()),
69-
value: record.values.map((x) => Field(x.toString())),
72+
key: Field(record.path.toFixed()),
73+
value: record.values.map((x) => Field(x.toFixed())),
7074
}));
7175
}
7276

0 commit comments

Comments
 (0)