Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
4a7fd28
Add in schema tables
ejMina226 Feb 14, 2025
5efdbd5
Fix typo in table
ejMina226 Feb 17, 2025
37be1fd
Fix typo in table
ejMina226 Feb 17, 2025
0582c53
Fix typo in table
ejMina226 Feb 17, 2025
a36b23c
Update types to string array in db
ejMina226 Feb 18, 2025
d5be707
Updated schema db to handle the before block
ejMina226 Feb 19, 2025
cdf1102
Update mapOut for StBatch
ejMina226 Feb 19, 2025
06a4a41
Start STMapper
ejMina226 Feb 19, 2025
69d6ba0
Fix mapIn and mapOut
ejMina226 Feb 19, 2025
aa5dd02
Remove unused types
ejMina226 Feb 19, 2025
4598d07
Update schema to remove before and after STs details from block
ejMina226 Feb 20, 2025
ebe7803
Fix TransactionMapper.ts
ejMina226 Feb 20, 2025
c774879
BlockResultsMapper
ejMina226 Feb 20, 2025
4157701
BlockMapper
ejMina226 Feb 20, 2025
798807a
Update BlockMapper
ejMina226 Feb 20, 2025
f641c61
Update TransactionMapper
ejMina226 Feb 20, 2025
8474f7e
Fix PrismaBlockStorage
ejMina226 Feb 20, 2025
4c139c0
Fix BlockFetching.ts
ejMina226 Feb 24, 2025
6ee46ce
Make relations lowercase to be consistent
ejMina226 Feb 24, 2025
9587a53
Make relations lowercase to be consistent processor
ejMina226 Feb 24, 2025
50d5347
Fix type blockfetching
ejMina226 Feb 24, 2025
5019cd9
Export types for indexer
ejMina226 Feb 24, 2025
0d0b4fe
Alias types for readability
ejMina226 Feb 24, 2025
db81cba
Spacing, etc
ejMina226 Feb 24, 2025
250fd85
Indexer fix toJson
ejMina226 Feb 24, 2025
c1c6eda
Fix beforeBlockStateTransitions in pushBlock
ejMina226 Feb 25, 2025
5b8f6a6
Add in migration
ejMina226 Feb 25, 2025
ad6a1bd
Update port in docker-compose for lightnet
ejMina226 Feb 25, 2025
421c9ad
Remove plural
ejMina226 Feb 25, 2025
05284da
Add plural to table
ejMina226 Feb 25, 2025
b586a92
Rename stateTransitions
ejMina226 Feb 25, 2025
23f2b3e
Fixing types
ejMina226 Feb 26, 2025
9237b55
Change ST schema
ejMina226 Feb 26, 2025
9585e37
Remove string
ejMina226 Feb 26, 2025
88a23b2
Add in StMapper.test.ts and fix
ejMina226 Feb 26, 2025
13fd7d9
Fix test typo
ejMina226 Feb 26, 2025
c6e2d12
Add tests
ejMina226 Feb 26, 2025
1283a3b
Code review changes
ejMina226 Mar 4, 2025
fcf12e0
Code review changes
ejMina226 Mar 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/deployment/docker/lightnet/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ services:
- 8080:8080
- 8181:8181
# archive endpoints
- 5432:5432
- 5433:5432
- 8282:8282
83 changes: 70 additions & 13 deletions packages/indexer/src/tasks/IndexBlockTaskParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { BlockWithResult } from "@proto-kit/sequencer";
import {
BlockMapper,
BlockResultMapper,
StateTransitionBatchArrayMapper,
StateTransitionMapper,
TransactionExecutionResultMapper,
STBatchOutput,
STArrayOutput,
} from "@proto-kit/persistance";
import { injectable } from "tsyringe";

Expand All @@ -13,37 +17,90 @@ export class IndexBlockTaskParametersSerializer {
public constructor(
public blockMapper: BlockMapper,
public blockResultMapper: BlockResultMapper,
public transactionResultMapper: TransactionExecutionResultMapper
public transactionResultMapper: TransactionExecutionResultMapper,
public stateTransitionBatchMapper: StateTransitionBatchArrayMapper,
public stateTransitionMapper: StateTransitionMapper
) {}

public toJSON(parameters: IndexBlockTaskParameters): string {
return JSON.stringify({
block: this.blockMapper.mapOut(parameters.block),
transactions: parameters.block.transactions.map((tx) =>
this.transactionResultMapper.mapOut(tx)
),
result: this.blockResultMapper.mapOut(parameters.result),
block: {
...this.blockMapper.mapOut(parameters.block),
beforeBlockStateTransitions:
parameters.block.beforeBlockStateTransitions.map((st) =>
this.stateTransitionMapper.mapOut(st)
),
},
transactions: parameters.block.transactions.map((tx) => {
const txMap = this.transactionResultMapper.mapOut(tx);
const stBatches = this.stateTransitionBatchMapper.mapOut(
tx.stateTransitions
);
return {
...txMap,
stateTransitionBatch: stBatches.map(([batch, sts]) => ({
applied: batch.applied,
stateTransitions: sts,
})),
};
}),
result: {
...this.blockResultMapper.mapOut(parameters.result),
afterBlockStateTransitions:
parameters.result.afterBlockStateTransitions.map((st) =>
this.stateTransitionMapper.mapOut(st)
),
},
});
}

public fromJSON(json: string): IndexBlockTaskParameters {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const parsed = JSON.parse(json) as {
block: ReturnType<BlockMapper["mapOut"]>;
transactions: ReturnType<TransactionExecutionResultMapper["mapOut"]>[];
result: ReturnType<BlockResultMapper["mapOut"]>;
block: ReturnType<BlockMapper["mapOut"]> & {
beforeBlockStateTransitions: ReturnType<
StateTransitionMapper["mapOut"]
>[];
};
transactions: (ReturnType<TransactionExecutionResultMapper["mapOut"]> & {
stateTransitionBatch: (STBatchOutput & {
stateTransitions: STArrayOutput;
})[];
})[];
result: ReturnType<BlockResultMapper["mapOut"]> & {
afterBlockStateTransitions: ReturnType<
StateTransitionMapper["mapOut"]
>[];
};
};

const transactions = parsed.transactions.map((tx) =>
this.transactionResultMapper.mapIn(tx)
);
const transactions = parsed.transactions.map((tx) => {
const txMapped = this.transactionResultMapper.mapIn(tx);
const stBatch = tx.stateTransitionBatch.map<
[STBatchOutput, STArrayOutput]
>((batch) => [{ applied: batch.applied }, batch.stateTransitions]);
return {
...txMapped,
stateTransitions: this.stateTransitionBatchMapper.mapIn(stBatch),
};
});

return {
block: {
...this.blockMapper.mapIn(parsed.block),
beforeBlockStateTransitions:
parsed.block.beforeBlockStateTransitions.map((st) =>
this.stateTransitionMapper.mapIn(st)
),
transactions,
},
result: this.blockResultMapper.mapIn(parsed.result),
result: {
...this.blockResultMapper.mapIn(parsed.result),
afterBlockStateTransitions:
parsed.result.afterBlockStateTransitions.map((st) =>
this.stateTransitionMapper.mapIn(st)
),
},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Warnings:

- You are about to drop the column `beforeBlockStateTransitions` on the `Block` table. All the data in the column will be lost.
- You are about to drop the column `afterBlockStateTransitions` on the `BlockResult` table. All the data in the column will be lost.
- You are about to drop the column `stateTransitions` on the `TransactionExecutionResult` table. All the data in the column will be lost.

*/
-- AlterTable
ALTER TABLE "Block" DROP COLUMN "beforeBlockStateTransitions";

-- AlterTable
ALTER TABLE "BlockResult" DROP COLUMN "afterBlockStateTransitions";

-- AlterTable
ALTER TABLE "TransactionExecutionResult" DROP COLUMN "stateTransitions";

-- CreateTable
CREATE TABLE "StateTransition" (
"id" SERIAL NOT NULL,
"batchId" INTEGER NOT NULL,
"path" TEXT NOT NULL,
"from" TEXT[],
"fromIsSome" BOOLEAN NOT NULL,
"to" TEXT[],
"toIsSome" BOOLEAN NOT NULL,

CONSTRAINT "StateTransition_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "StateTransitionBatch" (
"id" SERIAL NOT NULL,
"txExecutionResultId" TEXT,
"blockId" TEXT,
"blockResultId" TEXT,
"applied" BOOLEAN NOT NULL,

CONSTRAINT "StateTransitionBatch_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "StateTransition" ADD CONSTRAINT "StateTransition_batchId_fkey" FOREIGN KEY ("batchId") REFERENCES "StateTransitionBatch"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "StateTransitionBatch" ADD CONSTRAINT "StateTransitionBatch_txExecutionResultId_fkey" FOREIGN KEY ("txExecutionResultId") REFERENCES "TransactionExecutionResult"("txHash") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "StateTransitionBatch" ADD CONSTRAINT "StateTransitionBatch_blockId_fkey" FOREIGN KEY ("blockId") REFERENCES "Block"("hash") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "StateTransitionBatch" ADD CONSTRAINT "StateTransitionBatch_blockResultId_fkey" FOREIGN KEY ("blockResultId") REFERENCES "BlockResult"("blockHash") ON DELETE SET NULL ON UPDATE CASCADE;
59 changes: 41 additions & 18 deletions packages/persistance/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,42 @@ model Transaction {
IncomingMessageBatchTransaction IncomingMessageBatchTransaction[]
}

model StateTransition {
id Int @id @default(autoincrement())
batchId Int
path String
from String[]
fromIsSome Boolean
to String[]
toIsSome Boolean

batch StateTransitionBatch @relation(fields: [batchId], references: [id])
}

model StateTransitionBatch {
id Int @id @default(autoincrement())
txExecutionResultId String?
blockId String?
blockResultId String?
applied Boolean

transactionExecutionResult TransactionExecutionResult? @relation(fields: [txExecutionResultId], references: [txHash])
block Block? @relation(fields: [blockId], references: [hash])
blockResult BlockResult? @relation(fields: [blockResultId], references: [blockHash])
stateTransitions StateTransition[]
}

model TransactionExecutionResult {
// TODO Make StateTransitionBatch and StateTransition Table
stateTransitions Json @db.Json
status Boolean
statusMessage String?
events Json @db.Json
status Boolean
statusMessage String?
events Json @db.Json

tx Transaction @relation(fields: [txHash], references: [hash])
txHash String @id

block Block @relation(fields: [blockHash], references: [hash])
blockHash String
block Block @relation(fields: [blockHash], references: [hash])
blockHash String
stateTransitionBatch StateTransitionBatch[]
}

model Block {
Expand All @@ -67,17 +91,16 @@ model Block {
toMessagesHash String
fromStateRoot String

beforeBlockStateTransitions Json @db.Json

parentHash String? @unique
parent Block? @relation("Parent", fields: [parentHash], references: [hash])
successor Block? @relation("Parent")

transactions TransactionExecutionResult[]
result BlockResult?

batch Batch? @relation(fields: [batchHeight], references: [height])
batchHeight Int?
batch Batch? @relation(fields: [batchHeight], references: [height])
batchHeight Int?
stateTransitionBatch StateTransitionBatch[]
}

model Batch {
Expand All @@ -94,14 +117,14 @@ model Batch {
model BlockResult {
blockHash String @id @unique

stateRoot String
blockHashRoot String
witnessedRoots String[]
afterNetworkState Json @db.Json
afterBlockStateTransitions Json @db.Json
blockHashWitness Json @db.Json
stateRoot String
blockHashRoot String
witnessedRoots String[]
afterNetworkState Json @db.Json
blockHashWitness Json @db.Json

block Block? @relation(fields: [blockHash], references: [hash])
block Block? @relation(fields: [blockHash], references: [hash])
stateTransitionBatch StateTransitionBatch[]
}

model Settlement {
Expand Down
2 changes: 2 additions & 0 deletions packages/persistance/src/PrismaDatabaseConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export class PrismaDatabaseConnection
"Settlement",
"IncomingMessageBatch",
"IncomingMessageBatchTransaction",
"StateTransition",
"StateTransitionBatch",
];

await this.prismaClient.$transaction(
Expand Down
Loading