Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JobExecutionStore In Monitor/Observer #60

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@types/jest": "^29.5.7",
"@types/node": "^20.5.9",
"jest": "^29.7.0",
"prisma": "^5.6.0",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
},
Expand All @@ -27,6 +28,7 @@
"@planetarium/account-aws-kms": "^3.7.0",
"@planetarium/bencodex": "^0.2.2",
"@planetarium/tx": "^3.7.0",
"@prisma/client": "5.6.0",
"@sentry/node": "^7.76.0",
"@slack/web-api": "^6.10.0",
"@urql/core": "^4.2.0",
Expand Down
66 changes: 66 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

enum ActionType {
BURN
MINT
TRANSFER
}

enum TxResult {
INVALID
STAGING
SUCCESS
FAILURE
}

model Job {
id String @id
sender String
recipient String
src_planet String
ticker String
amount String
createdAt DateTime @default(now())
startedAt DateTime?
processedAt DateTime?
updatedAt DateTime @updatedAt

executions JobExecution[]
}

model Transaction {
txId String @id @unique
nonce BigInt @unique
raw Bytes
lastStatus TxResult?
statusUpdatedAt DateTime @default(now())

executions JobExecution[]
}

model JobExecution {
jobId String
job Job @relation(fields: [jobId], references: [id])

transactionId String
transaction Transaction @relation(fields: [transactionId], references: [txId])

actionType ActionType
retries Int @default(0)

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@id([jobId, transactionId])
@@unique([jobId, retries])
}
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { AssetBurner } from "./asset-burner";
import { AssetTransfer } from "./asset-transfer";
import { getRequiredEnv } from "./env";
import { HeadlessGraphQLClient } from "./headless-graphql-client";
import { IJobExecutionStore } from "./interfaces/job-execution-store";
import { IMonitorStateStore } from "./interfaces/monitor-state-store";
import { isBackgroundSyncTxpool } from "./interfaces/txpool";
import { JobExecutionStore } from "./job-execution-store";
import { Minter } from "./minter";
import { getMonitorStateHandler } from "./monitor-state-handler";
import { AssetsTransferredMonitor } from "./monitors/assets-transferred-monitor";
Expand Down Expand Up @@ -44,13 +46,15 @@ const slackBot = new SlackBot(
await Sqlite3MonitorStateStore.open(
getRequiredEnv("MONITOR_STATE_STORE_PATH"),
);
const jobExecutionStore: IJobExecutionStore = new JobExecutionStore();

const upstreamAssetsTransferredMonitorMonitor =
new AssetsTransferredMonitor(
getMonitorStateHandler(
monitorStateStore,
"upstreamAssetTransferMonitor",
),
jobExecutionStore,
upstreamGQLClient,
Address.fromHex(getRequiredEnv("NC_VAULT_ADDRESS")),
);
Expand All @@ -60,6 +64,7 @@ const slackBot = new SlackBot(
monitorStateStore,
"downstreamAssetTransferMonitor",
),
jobExecutionStore,
downstreamGQLClient,
Address.fromHex(getRequiredEnv("NC_VAULT_ADDRESS")),
);
Expand All @@ -68,6 +73,7 @@ const slackBot = new SlackBot(
monitorStateStore,
"upstreamGarageUnloadMonitor",
),
jobExecutionStore,
upstreamGQLClient,
Address.fromHex(getRequiredEnv("NC_VAULT_ADDRESS")),
Address.fromHex(getRequiredEnv("NC_VAULT_AVATAR_ADDRESS")),
Expand Down
15 changes: 15 additions & 0 deletions src/interfaces/job-execution-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Job, PrismaClient, ActionType } from "@prisma/client";
import { GarageUnloadEvent } from "../types/garage-unload-event";
import { IJobExecutionStore } from "./interfaces/job-execution-store";

Check failure on line 3 in src/interfaces/job-execution-store.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module './interfaces/job-execution-store' or its corresponding type declarations.
import { AssetTransferredEvent } from "../types/asset-transferred-event";
import { TransactionLocation } from "../types/transaction-location";

export interface IJobExecutionStore {
putAssetTransferReq(
event: AssetTransferredEvent & TransactionLocation
): Promise<void>;
putGarageUnloadReq(
event: GarageUnloadEvent & TransactionLocation
): Promise<void>;
putJobExec(reqTxId: string, resTxId: string, dstPlanet: string, actionType: ActionType);
}
74 changes: 74 additions & 0 deletions src/job-execution-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { $Enums, PrismaClient } from "@prisma/client";
import { IJobExecutionStore } from "./interfaces/job-execution-store";
import { AssetTransferredEvent } from "./types/asset-transferred-event";
import { GarageUnloadEvent } from "./types/garage-unload-event";
import { TransactionLocation } from "./types/transaction-location";

export class JobExecutionStore implements IJobExecutionStore {
private _prisma: PrismaClient;

constructor() {
this._prisma = new PrismaClient();
}
async putAssetTransferReq(
event: AssetTransferredEvent & TransactionLocation
) {
await this._prisma.request.create({

Check failure on line 16 in src/job-execution-store.ts

View workflow job for this annotation

GitHub Actions / build

Property 'request' does not exist on type 'PrismaClient<PrismaClientOptions, never, DefaultArgs>'.
data: {
req_tx_id: event.txId,
src_planet: event.planetID,
req_type: "TRANSFER_ASSETS",
timestamp: new Date(event.timestamp),

Check failure on line 21 in src/job-execution-store.ts

View workflow job for this annotation

GitHub Actions / build

Property 'timestamp' does not exist on type 'AssetTransferredEvent & TransactionLocation'.
transfer: {
create: {
sender: event.sender.toString(),
recipient: event.memo,
ticker: "NCG", //...need to be adjusted in case of non-NCG bridging?
amount: event.amount.rawValue.toString(),
},
},
job: {
create: {
startedAt: new Date(event.timestamp)

Check failure on line 32 in src/job-execution-store.ts

View workflow job for this annotation

GitHub Actions / build

Property 'timestamp' does not exist on type 'AssetTransferredEvent & TransactionLocation'.
}
}
},
});
}
async putGarageUnloadReq(event: GarageUnloadEvent & TransactionLocation) {
const parsed = JSON.parse(event.memo);

await this._prisma.request.create({

Check failure on line 41 in src/job-execution-store.ts

View workflow job for this annotation

GitHub Actions / build

Property 'request' does not exist on type 'PrismaClient<PrismaClientOptions, never, DefaultArgs>'.
data: {
req_tx_id: event.txId,
src_planet: event.planetID,
req_type: "UNLOAD_GARAGE",
timestamp: new Date(event.timestamp),

Check failure on line 46 in src/job-execution-store.ts

View workflow job for this annotation

GitHub Actions / build

Property 'timestamp' does not exist on type 'GarageUnloadEvent & TransactionLocation'.
garage: {
create: {
sender: event.signer,
recipient: parsed[0],
recipientAvatar: parsed[1],
FungibleAssetValues: JSON.stringify(event.fungibleAssetValues),
FungibleItems: JSON.stringify(event.fungibleItems),
},
},
job: {
create: {
startedAt: new Date(event.timestamp),

Check failure on line 58 in src/job-execution-store.ts

View workflow job for this annotation

GitHub Actions / build

Property 'timestamp' does not exist on type 'GarageUnloadEvent & TransactionLocation'.
}
}
},
});
}
async putJobExec(reqTxId: string, resTxId: string, dstPlanet: string, actionType: $Enums.ActionType) {
await this._prisma.jobExecution.create({
data: {
jobId: reqTxId,
dstPlanetId: dstPlanet,

Check failure on line 68 in src/job-execution-store.ts

View workflow job for this annotation

GitHub Actions / build

Type '{ jobId: string; dstPlanetId: string; transactionId: string; actionType: $Enums.ActionType; }' is not assignable to type '(Without<JobExecutionCreateInput, JobExecutionUncheckedCreateInput> & JobExecutionUncheckedCreateInput) | (Without<...> & JobExecutionCreateInput)'.
transactionId: resTxId,
actionType: actionType,
}
})
}
}
4 changes: 3 additions & 1 deletion src/monitors/assets-transferred-monitor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Address } from "@planetarium/account";
import { Job } from "@prisma/client";
import { IHeadlessGraphQLClient } from "../interfaces/headless-graphql-client";
import { IMonitorStateHandler } from "../interfaces/monitor-state-handler";
import { AssetTransferredEvent } from "../types/asset-transferred-event";
Expand All @@ -10,10 +11,11 @@

constructor(
monitorStateHandler: IMonitorStateHandler,
jobExecutionStore: IJobExecutionStore,

Check failure on line 14 in src/monitors/assets-transferred-monitor.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'IJobExecutionStore'.
headlessGraphQLClient: IHeadlessGraphQLClient,
address: Address,
) {
super(monitorStateHandler, headlessGraphQLClient);
super(monitorStateHandler, jobExecutionStore, headlessGraphQLClient);
this._address = address;
}

Expand Down
3 changes: 2 additions & 1 deletion src/monitors/garage-unload-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

constructor(
monitorStateHandler: IMonitorStateHandler,
jobExecutionStore: IJobExecutionStore,

Check failure on line 14 in src/monitors/garage-unload-monitor.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'IJobExecutionStore'.
headlessGraphQLClient: IHeadlessGraphQLClient,
agentAddress: Address,
avatarAddress: Address,
) {
super(monitorStateHandler, headlessGraphQLClient);
super(monitorStateHandler, jobExecutionStore, headlessGraphQLClient);
this._agentAddress = agentAddress;
this._avatarAddress = avatarAddress;
}
Expand Down
7 changes: 5 additions & 2 deletions src/monitors/ninechronicles-block-monitor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Job } from "@prisma/client";
import { captureException } from "@sentry/node";
import { Monitor } from ".";
import { IHeadlessGraphQLClient } from "../interfaces/headless-graphql-client";
import { IJobExecutionStore } from "../interfaces/job-execution-store";
import { IMonitorStateHandler } from "../interfaces/monitor-state-handler";
import { BlockHash } from "../types/block-hash";
import { TransactionLocation } from "../types/transaction-location";
Expand All @@ -11,25 +13,27 @@ export abstract class NineChroniclesMonitor<TEventData> extends Monitor<
> {
private readonly _monitorStateHandler: IMonitorStateHandler;
private readonly _delayMilliseconds: number;
protected readonly _jobExecutionStore: IJobExecutionStore;
protected readonly _headlessGraphQLClient: IHeadlessGraphQLClient;

private latestBlockNumber: number | undefined;

constructor(
monitorStateHandler: IMonitorStateHandler,
jobExecutionStore: IJobExecutionStore,
headlessGraphQLClient: IHeadlessGraphQLClient,
delayMilliseconds: number = 15 * 1000,
) {
super();

this._monitorStateHandler = monitorStateHandler;
this._headlessGraphQLClient = headlessGraphQLClient;
this._jobExecutionStore = jobExecutionStore;
this._delayMilliseconds = delayMilliseconds;
}

async *loop(): AsyncIterableIterator<{
blockHash: BlockHash;
planetID: string;
events: (TEventData & TransactionLocation)[];
}> {
const nullableLatestBlockHash = await this._monitorStateHandler.load();
Expand Down Expand Up @@ -58,7 +62,6 @@ export abstract class NineChroniclesMonitor<TEventData> extends Monitor<

yield {
blockHash,
planetID,
events: await this.getEvents(blockIndex),
};

Expand Down
2 changes: 2 additions & 0 deletions src/observers/asset-downstream-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address } from "@planetarium/account";
import { IObserver } from ".";
import { IAssetBurner } from "../interfaces/asset-burner";
import { IAssetTransfer } from "../interfaces/asset-transfer";
import { IJobExecutionStore } from "../interfaces/job-execution-store";
import { ISlackMessageSender } from "../slack";
import { SlackBot } from "../slack/bot";
import { BridgeErrorEvent } from "../slack/messages/bridge-error-event";
Expand All @@ -17,6 +18,7 @@ export class AssetDownstreamObserver
events: (AssetTransferredEvent & TransactionLocation)[];
}>
{
private readonly _jobExecutionStore: IJobExecutionStore;
private readonly _slackbot: ISlackMessageSender;
private readonly _transfer: IAssetTransfer;
private readonly _burner: IAssetBurner;
Expand Down
1 change: 1 addition & 0 deletions src/observers/asset-transferred-observer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FungibleAssetValue } from "@planetarium/tx";
import { IObserver } from ".";
import { IJobExecutionStore } from "../interfaces/job-execution-store";
import { IMinter } from "../interfaces/minter";
import { ISlackMessageSender } from "../slack";
import { SlackBot } from "../slack/bot";
Expand Down