diff --git a/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts b/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts index 9bf0da1e634..0488372575b 100644 --- a/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts +++ b/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts @@ -9,7 +9,11 @@ module.exports = { development: { client: "sqlite3", connection: { - filename: path.join(__dirname, "data", "/.dev-" + uuidv4() + ".sqlite3"), + filename: path.resolve( + __dirname, + "data", + ".dev.local-" + uuidv4() + ".sqlite3", + ), }, migrations: { directory: path.resolve(__dirname, "migrations"), diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/blo/dispatcher.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/blo/dispatcher.ts index 17399ca618b..9526c0408e1 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/blo/dispatcher.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/blo/dispatcher.ts @@ -32,6 +32,10 @@ import { IntegrationsEndpointV1 } from "../web-services/integrations-endpoint"; import { executeGetHealthCheck } from "./admin/get-healthcheck-handler-service"; import { executeGetStatus } from "./admin/get-status-handler-service"; import { executeTransact } from "./transaction/transact-handler-service"; +import { + ILocalLogRepository, + IRemoteLogRepository, +} from "../repository/interfaces/repository"; export interface BLODispatcherOptions { logger: Logger; @@ -41,6 +45,8 @@ export interface BLODispatcherOptions { signer: JsObjectSigner; bridgesManager: SATPBridgesManager; pubKey: string; + localRepository: ILocalLogRepository; + remoteRepository: IRemoteLogRepository; } export class BLODispatcher { @@ -54,6 +60,8 @@ export class BLODispatcher { private manager: SATPManager; private orchestrator: GatewayOrchestrator; private bridgeManager: SATPBridgesManager; + private localRepository: ILocalLogRepository; + private remoteRepository: IRemoteLogRepository; constructor(public readonly options: BLODispatcherOptions) { const fnTag = `${BLODispatcher.CLASS_NAME}#constructor()`; @@ -61,12 +69,17 @@ export class BLODispatcher { this.level = this.options.logLevel || "INFO"; this.label = this.className; - this.logger = LoggerProvider.getOrCreate({ level: this.level, label: this.label }); + this.logger = LoggerProvider.getOrCreate({ + level: this.level, + label: this.label, + }); this.instanceId = options.instanceId; this.logger.info(`Instantiated ${this.className} OK`); this.orchestrator = options.orchestrator; const signer = options.signer; const ourGateway = this.orchestrator.ourGateway; + this.localRepository = options.localRepository; + this.remoteRepository = options.remoteRepository; this.bridgeManager = options.bridgesManager; @@ -78,6 +91,8 @@ export class BLODispatcher { bridgeManager: this.bridgeManager, orchestrator: this.orchestrator, pubKey: options.pubKey, + localRepository: this.localRepository, + remoteRepository: this.remoteRepository, }; this.manager = new SATPManager(SATPManagerOpts); diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage0-client-service.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage0-client-service.ts index f84684b64db..c8ff6aea64a 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage0-client-service.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage0-client-service.ts @@ -58,6 +58,9 @@ export class Stage0ClientService extends SATPService { serviceName: ops.serviceName, signer: ops.signer, serviceType: Stage0ClientService.SERVICE_TYPE, + localRepository: ops.localRepository, + remoteRepository: ops.remoteRepository, + pubKey: ops.pubKey, }; super(commonOptions); if (ops.bridgeManager == undefined) { diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage1-client-service.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage1-client-service.ts index f793b109a2e..bb22d79d495 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage1-client-service.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage1-client-service.ts @@ -9,7 +9,12 @@ import { TransferClaims, NetworkCapabilities, } from "../../../generated/proto/cacti/satp/v02/common/message_pb"; -import { bufArray2HexStr, getHash, sign } from "../../../gateway-utils"; +import { + bufArray2HexStr, + getHash, + sign, + storeLog, +} from "../../../gateway-utils"; import { getMessageHash, saveHash, @@ -51,6 +56,9 @@ export class Stage1ClientService extends SATPService { serviceName: ops.serviceName, signer: ops.signer, serviceType: Stage1ClientService.SERVICE_TYPE, + localRepository: ops.localRepository, + remoteRepository: ops.remoteRepository, + pubKey: ops.pubKey, }; super(commonOptions); } @@ -196,14 +204,19 @@ export class Stage1ClientService extends SATPService { getHash(transferProposalRequestMessage), ); - /* - await storeLog(gateway, { - sessionID: sessionID, - type: "transferProposalRequest", - operation: "validate", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "transferProposalRequest", + operation: "validate", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); this.Log.info(`${fnTag}, sending TransferProposalRequest...`); return transferProposalRequestMessage; @@ -267,14 +280,20 @@ export class Stage1ClientService extends SATPService { getHash(transferCommenceRequestMessage), ); - /* - await storeLog(gateway, { - sessionID: sessionData.id, - type: "transferCommenceRequest", - operation: "validate", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "transferCommenceRequest", + operation: "validate", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); + this.Log.info(`${fnTag}, sending TransferCommenceRequest...`); return transferCommenceRequestMessage; diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage2-client-service.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage2-client-service.ts index a72238b5e79..a9ce33e67b0 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage2-client-service.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage2-client-service.ts @@ -6,7 +6,12 @@ import { MessageType, } from "../../../generated/proto/cacti/satp/v02/common/message_pb"; import { LockAssertionRequestMessage } from "../../../generated/proto/cacti/satp/v02/stage_2_pb"; -import { bufArray2HexStr, getHash, sign } from "../../../gateway-utils"; +import { + bufArray2HexStr, + getHash, + sign, + storeLog, +} from "../../../gateway-utils"; import { getMessageHash, saveHash, @@ -49,6 +54,9 @@ export class Stage2ClientService extends SATPService { signer: ops.signer, serviceType: Stage2ClientService.SERVICE_TYPE, bridgeManager: ops.bridgeManager, + localRepository: ops.localRepository, + remoteRepository: ops.remoteRepository, + pubKey: ops.pubKey, }; super(commonOptions); @@ -140,14 +148,20 @@ export class Stage2ClientService extends SATPService { getHash(lockAssertionRequestMessage), ); - /* - await storeLog(gateway, { - sessionID: sessionData.id, - type: "lockAssertionRequest", - operation: "lock", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "lockAssertionRequest", + operation: "lock", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); + this.Log.info(`${fnTag}, sending LockAssertionMessage...`); return lockAssertionRequestMessage; diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage3-client-service.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage3-client-service.ts index 1d6db943016..b3463edc150 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage3-client-service.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/client/stage3-client-service.ts @@ -12,7 +12,12 @@ import { CommitReadyResponseMessage, TransferCompleteRequestMessage, } from "../../../generated/proto/cacti/satp/v02/stage_3_pb"; -import { bufArray2HexStr, getHash, sign } from "../../../gateway-utils"; +import { + bufArray2HexStr, + getHash, + sign, + storeLog, +} from "../../../gateway-utils"; import { getMessageHash, saveHash, @@ -56,6 +61,9 @@ export class Stage3ClientService extends SATPService { signer: ops.signer, serviceType: Stage3ClientService.SERVICE_TYPE, bridgeManager: ops.bridgeManager, + localRepository: ops.localRepository, + remoteRepository: ops.remoteRepository, + pubKey: ops.pubKey, }; super(commonOptions); @@ -125,14 +133,19 @@ export class Stage3ClientService extends SATPService { getHash(commitPreparationRequestMessage), ); - /* - await storeLog(gateway, { - sessionID: sessionData.id, - type: "commitPreparation", - operation: "lock", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "commitPreparation", + operation: "lock", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); this.Log.info(`${fnTag}, sending CommitPreparationMessage...`); @@ -213,14 +226,20 @@ export class Stage3ClientService extends SATPService { getHash(commitFinalAssertionRequestMessage), ); - /* - await storeLog(gateway, { - sessionID: sessionData.id, - type: "commitFinalAssertion", - operation: "lock", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "commitFinalAssertion", + operation: "lock", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); + this.Log.info(`${fnTag}, sending CommitFinalAssertionMessage...`); return commitFinalAssertionRequestMessage; @@ -295,14 +314,19 @@ export class Stage3ClientService extends SATPService { getHash(transferCompleteRequestMessage), ); - /* - await storeLog(gateway, { - sessionID: sessionData.id, - type: "transferComplete", - operation: "lock", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "transferComplete", + operation: "lock", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); this.Log.info(`${fnTag}, sending TransferCompleteMessage...`); diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/satp-service.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/satp-service.ts index dc666b1f3cb..67e840eb9d5 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/satp-service.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/satp-service.ts @@ -9,6 +9,10 @@ import { SatpStage0Service } from "../../generated/proto/cacti/satp/v02/stage_0_ import { SatpStage1Service } from "../../generated/proto/cacti/satp/v02/stage_1_connect"; import { SatpStage2Service } from "../../generated/proto/cacti/satp/v02/stage_2_connect"; import { SatpStage3Service } from "../../generated/proto/cacti/satp/v02/stage_3_connect"; +import { + ILocalLogRepository, + IRemoteLogRepository, +} from "../../repository/interfaces/repository"; export enum SATPServiceType { Server = "Server", @@ -24,6 +28,9 @@ export type ISATPServiceOptions = { signer: JsObjectSigner; serviceType: SATPServiceType; bridgeManager?: SATPBridgesManager; + localRepository: ILocalLogRepository; + remoteRepository: IRemoteLogRepository; + pubKey: string; }; export interface SATPServiceStatic { @@ -56,6 +63,9 @@ export abstract class SATPService { readonly serviceType: SATPServiceType; private readonly signer: JsObjectSigner; readonly serviceName: string; + protected localRepository: ILocalLogRepository; + protected remoteRepository: IRemoteLogRepository; + protected pubKey: string; constructor(ops: ISATPServiceOptions) { this.logger = LoggerProvider.getOrCreate(ops.loggerOptions); @@ -63,6 +73,9 @@ export abstract class SATPService { this.serviceType = ops.serviceType; this.stage = ops.stage; this.signer = ops.signer; + this.localRepository = ops.localRepository; + this.remoteRepository = ops.remoteRepository; + this.pubKey = ops.pubKey; this.logger.trace(`Signer logger level: ${this.signer.options.logLevel}`); } @@ -89,4 +102,12 @@ export abstract class SATPService { public get Signer(): JsObjectSigner { return this.signer; } + + public get LocalRepository(): ILocalLogRepository { + return this.localRepository; + } + + public get RemoteRepository(): IRemoteLogRepository { + return this.remoteRepository; + } } diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage0-server-service.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage0-server-service.ts index f2bcf7b36e8..b62a192d48e 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage0-server-service.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage0-server-service.ts @@ -61,6 +61,9 @@ export class Stage0ServerService extends SATPService { serviceName: ops.serviceName, signer: ops.signer, serviceType: Stage0ServerService.SERVICE_TYPE, + localRepository: ops.localRepository, + remoteRepository: ops.remoteRepository, + pubKey: ops.pubKey, }; super(commonOptions); if (ops.bridgeManager == undefined) { diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage1-server-service.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage1-server-service.ts index dfc9e0dde04..edd0f65a96c 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage1-server-service.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage1-server-service.ts @@ -13,7 +13,12 @@ import { } from "../../../generated/proto/cacti/satp/v02/common/message_pb"; // eslint-disable-next-line prettier/prettier import { ACCEPTANCE } from "../../../generated/proto/cacti/satp/v02/common/session_pb"; -import { bufArray2HexStr, getHash, sign } from "../../../gateway-utils"; +import { + bufArray2HexStr, + getHash, + sign, + storeLog, +} from "../../../gateway-utils"; import { TransferClaims } from "../../../generated/proto/cacti/satp/v02/common/message_pb"; import { SessionType, @@ -55,6 +60,9 @@ export class Stage1ServerService extends SATPService { serviceName: ops.serviceName, signer: ops.signer, serviceType: Stage1ServerService.SERVICE_TYPE, + localRepository: ops.localRepository, + remoteRepository: ops.remoteRepository, + pubKey: ops.pubKey, }; super(commonOptions); } @@ -142,14 +150,20 @@ export class Stage1ServerService extends SATPService { ); // TODO: store logs in the database using session ID; refactor storelog not to need gateway as input - /* - await storeLog(gateway, { - sessionID: sessionData.id, - type: "transferProposalResponse", - operation: "lock", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "transferProposalResponse", + operation: "lock", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); + this.Log.info(`${fnTag}, sending TransferProposalResponseMessage...`); return transferProposalReceiptMessage; @@ -209,14 +223,19 @@ export class Stage1ServerService extends SATPService { getHash(transferCommenceResponseMessage), ); - /* - await storeLog(gateway, { - sessionID: sessionData.id, - type: "transferCommenceResponse", - operation: "lock", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "transferCommenceResponse", + operation: "lock", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); this.Log.info(`${fnTag}, sending TransferCommenceResponseMessage...`); diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage2-server-service.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage2-server-service.ts index fdbfbb156fb..0b8abf4b684 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage2-server-service.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage2-server-service.ts @@ -7,7 +7,12 @@ import { CommonSatp, MessageType, } from "../../../generated/proto/cacti/satp/v02/common/message_pb"; -import { bufArray2HexStr, getHash, sign } from "../../../gateway-utils"; +import { + bufArray2HexStr, + getHash, + sign, + storeLog, +} from "../../../gateway-utils"; import { getMessageHash, saveHash, @@ -42,6 +47,9 @@ export class Stage2ServerService extends SATPService { serviceName: ops.serviceName, signer: ops.signer, serviceType: Stage2ServerService.SERVICE_TYPE, + localRepository: ops.localRepository, + remoteRepository: ops.remoteRepository, + pubKey: ops.pubKey, }; super(commonOptions); } @@ -104,14 +112,19 @@ export class Stage2ServerService extends SATPService { getHash(lockAssertionReceiptMessage), ); - /* - await storeLog(gateway, { - sessionID: sessionData.id, - type: "lockAssertionResponse", - operation: "lock", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "lockAssertionResponse", + operation: "lock", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); this.Log.info(`${fnTag}, sending LockAssertionResponseMessage...`); diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage3-server-service.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage3-server-service.ts index 1b4681f5dd4..0b9c0faa305 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage3-server-service.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-services/server/stage3-server-service.ts @@ -14,7 +14,12 @@ import { MintAssertionClaim, MintAssertionClaimFormat, } from "../../../generated/proto/cacti/satp/v02/common/message_pb"; -import { bufArray2HexStr, getHash, sign } from "../../../gateway-utils"; +import { + bufArray2HexStr, + getHash, + sign, + storeLog, +} from "../../../gateway-utils"; import { getMessageHash, saveHash, @@ -58,6 +63,9 @@ export class Stage3ServerService extends SATPService { signer: ops.signer, serviceType: Stage3ServerService.SERVICE_TYPE, bridgeManager: ops.bridgeManager, + localRepository: ops.localRepository, + remoteRepository: ops.remoteRepository, + pubKey: ops.pubKey, }; super(commonOptions); if (ops.bridgeManager == undefined) { @@ -134,14 +142,20 @@ export class Stage3ServerService extends SATPService { getHash(commitReadyMessage), ); - /* - await storeLog(gateway, { - sessionID: sessionData.id, - type: "commitReady", - operation: "lock", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "commitReady", + operation: "lock", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); + this.Log.info(`${fnTag}, sending commitReadyMessage...`); return commitReadyMessage; @@ -222,14 +236,20 @@ export class Stage3ServerService extends SATPService { getHash(commitFinalAcknowledgementReceiptResponseMessage), ); - /* - await storeLog(gateway, { - sessionID: sessionData.id, - type: "commitFinalAcknowledgementReceiptResponse", - operation: "lock", - data: safeStableStringify(sessionData), - }); - */ + await storeLog( + { + sessionID: sessionData.id, + type: "commitFinalAcknowledgementReceiptResponse", + operation: "lock", + data: safeStableStringify(sessionData), + sequenceNumber: Number(sessionData.lastSequenceNumber), + }, + this.localRepository, + this.remoteRepository, + this.Signer, + this.pubKey, + ); + this.Log.info( `${fnTag}, sending commitFinalAcknowledgementReceiptResponseMessage...`, ); diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/types.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/types.ts index 02e69f68ade..63c5831751d 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/types.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/types.ts @@ -12,6 +12,7 @@ import { IMergePolicyValue } from "@hyperledger/cactus-plugin-bungee-hermes/dist import { NetworkBridge } from "./stage-services/satp-bridge/network-bridge"; import { SATPServiceInstance } from "./stage-services/satp-service"; import { NetworkConfig } from "../types/blockchain-interaction"; +import { Knex } from "knex"; export type SATPConnectHandler = ( gateway: SATPGateway, @@ -81,6 +82,7 @@ export interface SATPGatewayConfig { privacyPolicies?: IPrivacyPolicyValue[]; mergePolicies?: IMergePolicyValue[]; bridgesConfig?: NetworkConfig[]; + knexConfig?: Knex.Config; } // export interface SATPBridgeConfig { @@ -110,6 +112,7 @@ export interface LocalLog { operation: string; timestamp?: string; data: string; + sequenceNumber: number; } export interface RemoteLog { key: string; diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/gateway-utils.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/gateway-utils.ts index ec964ebea41..8d56ad29711 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/gateway-utils.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/gateway-utils.ts @@ -1,8 +1,11 @@ import { JsObjectSigner } from "@hyperledger/cactus-common"; import { LocalLog, RemoteLog } from "./core/types"; -import { SATPGateway } from "./plugin-satp-hermes-gateway"; import { SHA256 } from "crypto-js"; import { stringify as safeStableStringify } from "safe-stable-stringify"; +import { + ILocalLogRepository, + IRemoteLogRepository, +} from "./repository/interfaces/repository"; export function bufArray2HexStr(array: Uint8Array): string { return Buffer.from(array).toString("hex"); @@ -62,8 +65,11 @@ export function verifySignature( } export async function storeProof( - gateway: SATPGateway, localLog: LocalLog, + localRepository: ILocalLogRepository, + remoteRepository: IRemoteLogRepository, + signer: JsObjectSigner, + pubKey: string, ): Promise { if (localLog.data == undefined) return; @@ -74,16 +80,19 @@ export async function storeProof( ); localLog.timestamp = Date.now().toString(); - await storeInDatabase(gateway, localLog); + await storeInDatabase(localLog, localRepository); const hash = SHA256(localLog.data).toString(); - await storeRemoteLog(gateway, localLog.key, hash); + await storeRemoteLog(localLog.key, hash, remoteRepository, signer, pubKey); } export async function storeLog( - gateway: SATPGateway, localLog: LocalLog, + localRepository: ILocalLogRepository, + remoteRepository: IRemoteLogRepository, + signer: JsObjectSigner, + pubKey: string, ): Promise { localLog.key = getSatpLogKey( localLog.sessionID, @@ -92,7 +101,7 @@ export async function storeLog( ); localLog.timestamp = Date.now().toString(); - await storeInDatabase(gateway, localLog); + await storeInDatabase(localLog, localRepository); // Keep the order consistent with the order of the fields in the table // so that the hash matches when retrieving from the database @@ -104,40 +113,43 @@ export async function storeLog( "operation", "timestamp", "data", + "sequenceNumber", ]), ).toString(); - await storeRemoteLog(gateway, localLog.key, hash); + await storeRemoteLog(localLog.key, hash, remoteRepository, signer, pubKey); } export async function storeInDatabase( - gateway: SATPGateway, - LocalLog: LocalLog, -) { - await gateway.localRepository?.create(LocalLog); + localLog: LocalLog, + localRepository: ILocalLogRepository, +): Promise { + await localRepository.create(localLog); } export async function storeRemoteLog( - gateway: SATPGateway, key: string, hash: string, -) { - const fnTag = `${gateway.className}#storeInDatabase()`; + remoteRepository: IRemoteLogRepository, + signer: JsObjectSigner, + pubKey: string, +): Promise { + const fnTag = `storeRemoteLog()`; const remoteLog: RemoteLog = { key: key, hash: hash, signature: "", - signerPubKey: gateway.pubKey, + signerPubKey: pubKey, }; remoteLog.signature = bufArray2HexStr( - sign(gateway.Signer, safeStableStringify(remoteLog)), + sign(signer, safeStableStringify(remoteLog)), ); - const response = await gateway.remoteRepository?.create(remoteLog); + const response = await remoteRepository.create(remoteLog); - if (response.status < 200 && response.status > 299) { + if (response.status < 200 || response.status > 299) { throw new Error( `${fnTag}, got response ${response.status} when logging to remote`, ); diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/gol/satp-manager.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/gol/satp-manager.ts index 9b95fef1b17..bd3cd57a3a2 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/gol/satp-manager.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/gol/satp-manager.ts @@ -45,6 +45,10 @@ import { SatpStage3Service } from "../generated/proto/cacti/satp/v02/stage_3_con import { PromiseClient as PromiseConnectClient } from "@connectrpc/connect"; import { SatpStage0Service } from "../generated/proto/cacti/satp/v02/stage_0_connect"; import { HealthCheckResponseStatusEnum } from "../generated/gateway-client/typescript-axios"; +import { + ILocalLogRepository, + IRemoteLogRepository, +} from "../repository/interfaces/repository"; export interface ISATPManagerOptions { logLevel?: LogLevelDesc; @@ -55,6 +59,8 @@ export interface ISATPManagerOptions { supportedDLTs: SupportedChain[]; bridgeManager: SATPBridgesManager; orchestrator: GatewayOrchestrator; + localRepository: ILocalLogRepository; + remoteRepository: IRemoteLogRepository; } export class SATPManager { @@ -79,6 +85,8 @@ export class SATPManager { private readonly orchestrator: GatewayOrchestrator; private gatewaysPubKeys: Map = new Map(); + private localRepository: ILocalLogRepository; + private remoteRepository: IRemoteLogRepository; constructor(public readonly options: ISATPManagerOptions) { const fnTag = `${SATPManager.CLASS_NAME}#constructor()`; @@ -96,6 +104,8 @@ export class SATPManager { this.orchestrator = options.orchestrator; this._pubKey = options.pubKey; this.loadPubKeys(this.orchestrator.getCounterPartyGateways()); + this.localRepository = options.localRepository; + this.remoteRepository = options.remoteRepository; this.sessions = options.sessions || new Map(); const handlersClasses = [ @@ -240,6 +250,9 @@ export class SATPManager { serviceType: serviceClass.SERVICE_TYPE, serviceName: serviceClass.SATP_SERVICE_INTERNAL_NAME, bridgeManager: this.bridgesManager, + localRepository: this.localRepository, + remoteRepository: this.remoteRepository, + pubKey: this.pubKey, })); } diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway.ts index 48e54902e73..27e213eb4a3 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway.ts @@ -43,6 +43,8 @@ import { ILocalLogRepository, IRemoteLogRepository, } from "./repository/interfaces/repository"; +import { KnexRemoteLogRepository as RemoteLogRepository } from "./repository/knex-remote-log-repository"; +import { KnexLocalLogRepository as LocalLogRepository } from "./repository/knex-local-log-repository"; import { BLODispatcher, BLODispatcherOptions } from "./blo/dispatcher"; import swaggerUi, { JsonObject } from "swagger-ui-express"; import { @@ -106,6 +108,8 @@ export class SATPGateway implements IPluginWebService, ICactusPlugin { level: level, label: this.className, }; + this.localRepository = new LocalLogRepository(options.knexConfig); + this.remoteRepository = new RemoteLogRepository(options.knexConfig); this.logger = LoggerProvider.getOrCreate(logOptions); this.logger.info("Initializing Gateway Coordinator"); @@ -162,6 +166,8 @@ export class SATPGateway implements IPluginWebService, ICactusPlugin { signer: this.signer, bridgesManager: this.bridgesManager, pubKey: this.pubKey, + localRepository: this.localRepository, + remoteRepository: this.remoteRepository, }; this.supportedDltIDs = this.config.gid!.supportedDLTs; diff --git a/packages/cactus-plugin-satp-hermes/src/test/typescript/unit/satp-logging.test.ts b/packages/cactus-plugin-satp-hermes/src/test/typescript/unit/satp-logging.test.ts new file mode 100644 index 00000000000..9502896127b --- /dev/null +++ b/packages/cactus-plugin-satp-hermes/src/test/typescript/unit/satp-logging.test.ts @@ -0,0 +1,543 @@ +import * as Utils from "../../../main/typescript/gateway-utils"; +import { + JsObjectSigner, + LogLevelDesc, + Secp256k1Keys, +} from "@hyperledger/cactus-common"; +import { SATPBridgesManager } from "../../../main/typescript/gol/satp-bridges-manager"; +import { SupportedChain } from "../../../main/typescript/core/types"; +import { + ISATPServiceOptions, + SATPService, + SATPServiceType, +} from "../../../main/typescript/core/stage-services/satp-service"; +import { Stage0ClientService } from "../../../main/typescript/core/stage-services/client/stage0-client-service"; +import { Stage0ServerService } from "../../../main/typescript/core/stage-services/server/stage0-server-service"; +import { Stage1ClientService } from "../../../main/typescript/core/stage-services/client/stage1-client-service"; +import { Stage1ServerService } from "../../../main/typescript/core/stage-services/server/stage1-server-service"; +import { Stage2ClientService } from "../../../main/typescript/core/stage-services/client/stage2-client-service"; +import { Stage2ServerService } from "../../../main/typescript/core/stage-services/server/stage2-server-service"; +import { Stage3ClientService } from "../../../main/typescript/core/stage-services/client/stage3-client-service"; +import { Stage3ServerService } from "../../../main/typescript/core/stage-services/server/stage3-server-service"; +import { SATPSession } from "../../../main/typescript/core/satp-session"; +import { SATP_VERSION } from "../../../main/typescript/core/constants"; +import { + Asset, + AssignmentAssertionClaim, + BurnAssertionClaim, + ClaimFormat, + CredentialProfile, + LockAssertionClaim, + LockAssertionClaimFormat, + LockType, + MessageType, + MintAssertionClaim, + SignatureAlgorithm, +} from "../../../main/typescript/generated/proto/cacti/satp/v02/common/message_pb"; +import { + TransferCommenceRequestMessage, + TransferCommenceResponseMessage, + TransferProposalReceiptMessage, + TransferProposalRequestMessage, +} from "../../../main/typescript/generated/proto/cacti/satp/v02/stage_1_pb"; +import { + LockAssertionRequestMessage, + LockAssertionReceiptMessage, +} from "../../../main/typescript/generated/proto/cacti/satp/v02/stage_2_pb"; +import { SessionData } from "../../../main/typescript/generated/proto/cacti/satp/v02/common/session_pb"; +import { + CommitFinalAcknowledgementReceiptResponseMessage, + CommitFinalAssertionRequestMessage, + CommitPreparationRequestMessage, + CommitReadyResponseMessage, + TransferCompleteRequestMessage, +} from "../../../main/typescript/generated/proto/cacti/satp/v02/stage_3_pb"; + +import { getMessageHash } from "../../../main/typescript/core/session-utils"; +import { + NewSessionRequest, + NewSessionResponse, + PreSATPTransferRequest, + PreSATPTransferResponse, +} from "../../../main/typescript/generated/proto/cacti/satp/v02/stage_0_pb"; +import { TokenType } from "../../../main/typescript/core/stage-services/satp-bridge/types/asset"; +import { + ILocalLogRepository, + IRemoteLogRepository, +} from "../../../main/typescript/repository/interfaces/repository"; +import { knexClientConnection, knexRemoteConnection } from "../knex.config"; +import { Knex, knex } from "knex"; +import { KnexLocalLogRepository as LocalLogRepository } from "../../../main/typescript/repository/knex-local-log-repository"; +import { KnexRemoteLogRepository as RemoteLogRepository } from "../../../main/typescript/repository/knex-remote-log-repository"; + +let knexInstanceClient: Knex; +let knexInstanceRemote: Knex; + +const logLevel: LogLevelDesc = "DEBUG"; + +const serviceClasses = [ + Stage0ClientService, + Stage0ServerService, + Stage1ServerService, + Stage1ClientService, + Stage2ServerService, + Stage2ClientService, + Stage3ServerService, + Stage3ClientService, +]; + +const keyPairs = Secp256k1Keys.generateKeyPairsBuffer(); + +const signer = new JsObjectSigner({ + privateKey: keyPairs.privateKey, +}); + +const supportedDLTs = [SupportedChain.FABRIC, SupportedChain.BESU]; +let localRepository: ILocalLogRepository; +let remoteRepository: IRemoteLogRepository; +const pubKey = "MOCK_ORIGINATOR_PUBKEY"; +let bridgeManager: SATPBridgesManager; + +let mockSession: SATPSession; + +let satpClientService0: Stage0ClientService; +let satpServerService0: Stage0ServerService; +let satpClientService1: Stage1ClientService; +let satpClientService2: Stage2ClientService; +let satpClientService3: Stage3ClientService; +let satpServerService1: Stage1ServerService; +let satpServerService2: Stage2ServerService; +let satpServerService3: Stage3ServerService; + +let newSessionRequestMessage: NewSessionRequest; +let newSessionResponseMessage: NewSessionResponse; +let preSATPTransferRequestMessage: PreSATPTransferRequest; +let preSATPTransferResponseMessage: PreSATPTransferResponse; +let transferProposalRequestMessage: TransferProposalRequestMessage; +let transferProposalResponseMessage: TransferProposalReceiptMessage; +let transferCommenceRequestMessage: TransferCommenceRequestMessage; +let transferCommenceResponseMessage: TransferCommenceResponseMessage; +let lockAssertionRequestMessage: LockAssertionRequestMessage; +let lockAssertionReceiptMessage: LockAssertionReceiptMessage; +let commitPreparationRequestMessage: CommitPreparationRequestMessage; +let commitReadyResponseMessage: CommitReadyResponseMessage; +let commitFinalAssertionRequestMessage: CommitFinalAssertionRequestMessage; +let commitFinalAcknowledgementReceiptResponseMessage: CommitFinalAcknowledgementReceiptResponseMessage; +let transferCompleteRequestMessage: TransferCompleteRequestMessage; + +const sessionIDs: string[] = []; + +beforeAll(async () => { + bridgeManager = new SATPBridgesManager({ + supportedDLTs: supportedDLTs, + networks: [], + logLevel: logLevel, + }); + + knexInstanceClient = knex(knexClientConnection); + await knexInstanceClient.migrate.latest(); + + knexInstanceRemote = knex(knexRemoteConnection); + await knexInstanceRemote.migrate.latest(); + + localRepository = new LocalLogRepository(knexClientConnection); + remoteRepository = new RemoteLogRepository(knexRemoteConnection); + + mockSession = new SATPSession({ + contextID: "MOCK_CONTEXT_ID", + server: false, + client: true, + }); + + sessionIDs.push(mockSession.getSessionId()); + + const serviceOptions = initializeServiceOptions( + serviceClasses, + logLevel, + "SATPService", + ); + + for (const service of initializeServices(serviceClasses, serviceOptions)) { + switch (service.constructor) { + case Stage0ClientService: + satpClientService0 = service as Stage0ClientService; + break; + case Stage0ServerService: + satpServerService0 = service as Stage0ServerService; + break; + case Stage1ServerService: + satpServerService1 = service as Stage1ServerService; + break; + case Stage1ClientService: + satpClientService1 = service as Stage1ClientService; + break; + case Stage2ServerService: + satpServerService2 = service as Stage2ServerService; + break; + case Stage2ClientService: + satpClientService2 = service as Stage2ClientService; + break; + case Stage3ServerService: + satpServerService3 = service as Stage3ServerService; + break; + case Stage3ClientService: + satpClientService3 = service as Stage3ClientService; + break; + default: + break; + } + } +}); + +afterAll(async () => { + const services = [ + satpClientService0, + satpServerService0, + satpClientService1, + satpServerService1, + satpClientService2, + satpServerService2, + satpClientService3, + satpServerService3, + ]; + + for (const service of services) { + await service.LocalRepository.destroy(); + await service.RemoteRepository.destroy(); + } + + if (knexInstanceClient) { + await knexInstanceClient.destroy(); + } + if (knexInstanceRemote) { + await knexInstanceRemote.destroy(); + } +}); +function initializeServiceOptions( + serviceClasses: (new (options: ISATPServiceOptions) => SATPService)[], + logLevel: LogLevelDesc, + label: string, +): ISATPServiceOptions[] { + return serviceClasses.map((_, index) => ({ + signer: signer, + stage: index.toString() as "0" | "1" | "2" | "3", + loggerOptions: { level: logLevel, label }, + serviceName: `Service-${index}`, + serviceType: + index % 2 === 0 ? SATPServiceType.Server : SATPServiceType.Client, + bridgeManager: bridgeManager, + localRepository: localRepository, + remoteRepository: remoteRepository, + pubKey: pubKey, + })); +} + +function initializeServices( + serviceClasses: (new (options: ISATPServiceOptions) => SATPService)[], + serviceOptions: ISATPServiceOptions[], +): SATPService[] { + return serviceClasses.map( + (ServiceClass, index) => new ServiceClass(serviceOptions[index]), + ); +} + +describe("SATP Services Testing with storeLog", () => { + let storeLogSpy: jest.SpyInstance; + + beforeAll(() => { + storeLogSpy = jest.spyOn(Utils, "storeLog"); + }); + + afterEach(() => { + storeLogSpy.mockClear(); + }); + + afterAll(() => { + storeLogSpy.mockRestore(); + }); + + it("Satge0 initiation", async () => { + let sessionData = mockSession.getClientSessionData(); + if (!sessionData) { + throw new Error("Session data not found"); + } + + sessionData.version = SATP_VERSION; + sessionData.clientGatewayPubkey = Buffer.from(keyPairs.publicKey).toString( + "hex", + ); + sessionData.serverGatewayPubkey = sessionData.clientGatewayPubkey; + sessionData.originatorPubkey = "MOCK_ORIGINATOR_PUBKEY"; + sessionData.beneficiaryPubkey = "MOCK_BENEFICIARY_PUBKEY"; + sessionData.digitalAssetId = "MOCK_DIGITAL_ASSET_ID"; + sessionData.assetProfileId = "MOCK_ASSET_PROFILE_ID"; + sessionData.verifiedOriginatorEntityId = + "MOCK_VERIFIED_ORIGINATOR_ENTITY_ID"; + sessionData.verifiedBeneficiaryEntityId = + "MOCK_VERIFIED_BENEFICIARY_ENTITY_ID"; + sessionData.receiverGatewayOwnerId = "MOCK_RECEIVER_GATEWAY_OWNER_ID"; + sessionData.recipientGatewayNetworkId = SupportedChain.FABRIC; + sessionData.senderGatewayOwnerId = "MOCK_SENDER_GATEWAY_OWNER_ID"; + sessionData.senderGatewayNetworkId = SupportedChain.BESU; + sessionData.signatureAlgorithm = SignatureAlgorithm.RSA; + sessionData.lockType = LockType.FAUCET; + sessionData.lockExpirationTime = BigInt(1000); + sessionData.credentialProfile = CredentialProfile.X509; + sessionData.loggingProfile = "MOCK_LOGGING_PROFILE"; + sessionData.accessControlProfile = "MOCK_ACCESS_CONTROL_PROFILE"; + sessionData.resourceUrl = "MOCK_RESOURCE_URL"; + sessionData.lockAssertionExpiration = BigInt(99999); + sessionData.receiverContractOntology = "MOCK_RECEIVER_CONTRACT_ONTOLOGY"; + sessionData.senderContractOntology = "MOCK_SENDER_CONTRACT_ONTOLOGY"; + sessionData.sourceLedgerAssetId = "MOCK_SOURCE_LEDGER_ASSET_ID"; + sessionData.senderAsset = new Asset(); + sessionData.senderAsset.tokenId = "MOCK_TOKEN_ID"; + sessionData.senderAsset.tokenType = TokenType.ERC20; + sessionData.senderAsset.amount = BigInt(0); + sessionData.senderAsset.owner = "MOCK_SENDER_ASSET_OWNER"; + sessionData.senderAsset.ontology = "MOCK_SENDER_ASSET_ONTOLOGY"; + sessionData.senderAsset.contractName = "MOCK_SENDER_ASSET_CONTRACT_NAME"; + sessionData.senderAsset.contractAddress = + "MOCK_SENDER_ASSET_CONTRACT_ADDRESS"; + sessionData.receiverAsset = new Asset(); + + sessionData.receiverAsset.tokenType = TokenType.ERC20; + sessionData.receiverAsset.amount = BigInt(0); + sessionData.receiverAsset.owner = "MOCK_RECEIVER_ASSET_OWNER"; + sessionData.receiverAsset.ontology = "MOCK_RECEIVER_ASSET_ONTOLOGY"; + sessionData.receiverAsset.contractName = + "MOCK_RECEIVER_ASSET_CONTRACT_NAME"; + sessionData.receiverAsset.mspId = "MOCK_RECEIVER_ASSET_MSP_ID"; + sessionData.receiverAsset.channelName = "MOCK_CHANNEL_ID"; + + newSessionRequestMessage = await satpClientService0.newSessionRequest( + mockSession, + SupportedChain.BESU, + ); + + await satpServerService0.checkNewSessionRequest( + newSessionRequestMessage, + mockSession, + Buffer.from(keyPairs.publicKey).toString("hex"), + ); + + newSessionResponseMessage = await satpServerService0.newSessionResponse( + newSessionRequestMessage, + mockSession, + ); + + await satpClientService0.checkNewSessionResponse( + newSessionResponseMessage, + mockSession, + sessionIDs, + ); + + sessionData = mockSession.getClientSessionData(); + if (!sessionData) { + throw new Error("Session data not found"); + } + sessionData.senderWrapAssertionClaim = new AssignmentAssertionClaim(); + + preSATPTransferRequestMessage = + await satpClientService0.preSATPTransferRequest(mockSession); + + await satpServerService0.checkPreSATPTransferRequest( + preSATPTransferRequestMessage, + mockSession, + ); + + sessionData = mockSession.getServerSessionData(); + if (!sessionData) { + throw new Error("Session data not found"); + } + sessionData.receiverWrapAssertionClaim = new AssignmentAssertionClaim(); + + preSATPTransferResponseMessage = + await satpServerService0.preSATPTransferResponse( + preSATPTransferRequestMessage, + mockSession, + ); + + await satpClientService1.checkPreSATPTransferResponse( + preSATPTransferResponseMessage, + mockSession, + ); + }); + + it("Service1Client calls storeLog", async () => { + transferProposalRequestMessage = + (await satpClientService1.transferProposalRequest( + mockSession, + supportedDLTs, + )) as TransferProposalRequestMessage; + + await satpServerService1.checkTransferProposalRequestMessage( + transferProposalRequestMessage, + mockSession, + supportedDLTs, + ); + + transferProposalResponseMessage = + (await satpServerService1.transferProposalResponse( + transferProposalRequestMessage, + mockSession, + )) as TransferProposalReceiptMessage; + expect(storeLogSpy).toHaveBeenCalled(); + }); + + it("Service1Client calls storeLog", async () => { + await satpClientService1.checkTransferProposalReceiptMessage( + transferProposalResponseMessage, + mockSession, + ); + + transferCommenceRequestMessage = + (await satpClientService1.transferCommenceRequest( + transferProposalResponseMessage, + mockSession, + )) as TransferCommenceRequestMessage; + expect(storeLogSpy).toHaveBeenCalled(); + }); + + it("Service1Server calls storeLog", async () => { + await satpServerService1.checkTransferCommenceRequestMessage( + transferCommenceRequestMessage, + mockSession, + ); + + transferCommenceResponseMessage = + (await satpServerService1.transferCommenceResponse( + transferCommenceRequestMessage, + mockSession, + )) as TransferCommenceResponseMessage; + expect(storeLogSpy).toHaveBeenCalled(); + }); + + it("Service2Client calls storeLog", async () => { + await satpClientService2.checkTransferCommenceResponseMessage( + transferCommenceResponseMessage, + mockSession, + ); + + const sessionData = mockSession.getClientSessionData(); + if (!sessionData) { + throw new Error("Session data not found"); + } + sessionData.lockAssertionClaim = new LockAssertionClaim(); + sessionData.lockAssertionClaimFormat = new LockAssertionClaimFormat(); + sessionData.lockAssertionClaimFormat.format = ClaimFormat.DEFAULT; + sessionData.lockExpirationTime = BigInt(1000); + + lockAssertionRequestMessage = + (await satpClientService2.lockAssertionRequest( + transferCommenceResponseMessage, + mockSession, + )) as LockAssertionRequestMessage; + expect(storeLogSpy).toHaveBeenCalled(); + }); + + it("Service2Server calls storeLog", async () => { + await satpServerService2.checkLockAssertionRequestMessage( + lockAssertionRequestMessage, + mockSession, + ); + + lockAssertionReceiptMessage = + (await satpServerService2.lockAssertionResponse( + lockAssertionRequestMessage, + mockSession, + )) as LockAssertionReceiptMessage; + expect(storeLogSpy).toHaveBeenCalled(); + }); + + it("Service3Client calls storeLog", async () => { + await satpClientService3.checkLockAssertionReceiptMessage( + lockAssertionReceiptMessage, + mockSession, + ); + + commitPreparationRequestMessage = + (await satpClientService3.commitPreparation( + lockAssertionReceiptMessage, + mockSession, + )) as CommitPreparationRequestMessage; + expect(storeLogSpy).toHaveBeenCalled(); + }); + + it("Service3Server calls storeLog", async () => { + await satpServerService3.checkCommitPreparationRequestMessage( + commitPreparationRequestMessage, + mockSession, + ); + + const sessionData = mockSession.getServerSessionData(); + if (!sessionData) { + throw new Error("Session data not found"); + } + sessionData.mintAssertionClaim = new MintAssertionClaim(); + + commitReadyResponseMessage = (await satpServerService3.commitReady( + commitPreparationRequestMessage, + mockSession, + )) as CommitReadyResponseMessage; + expect(storeLogSpy).toHaveBeenCalled(); + }); + + it("Service3Client calls storeLog", async () => { + await satpClientService3.checkCommitReadyResponseMessage( + commitReadyResponseMessage, + mockSession, + ); + + const sessionData = mockSession.getClientSessionData(); + if (!sessionData) { + throw new Error("Session data not found"); + } + sessionData.burnAssertionClaim = new BurnAssertionClaim(); + + commitFinalAssertionRequestMessage = + (await satpClientService3.commitFinalAssertion( + commitReadyResponseMessage, + mockSession, + )) as CommitFinalAssertionRequestMessage; + expect(storeLogSpy).toHaveBeenCalled(); + }); + + it("Service3Server calls storeLog", async () => { + await satpServerService3.checkCommitFinalAssertionRequestMessage( + commitFinalAssertionRequestMessage, + mockSession, + ); + + const sessionData = mockSession.getServerSessionData(); + if (!sessionData) { + throw new Error("Session data not found"); + } + sessionData.assignmentAssertionClaim = new AssignmentAssertionClaim(); + + commitFinalAcknowledgementReceiptResponseMessage = + (await satpServerService3.commitFinalAcknowledgementReceiptResponse( + commitFinalAssertionRequestMessage, + mockSession, + )) as CommitFinalAcknowledgementReceiptResponseMessage; + expect(storeLogSpy).toHaveBeenCalled(); + }); + + it("Service3Client calls storeLog", async () => { + await satpClientService3.checkCommitFinalAcknowledgementReceiptResponseMessage( + commitFinalAcknowledgementReceiptResponseMessage, + mockSession, + ); + + transferCompleteRequestMessage = (await satpClientService3.transferComplete( + commitFinalAcknowledgementReceiptResponseMessage, + mockSession, + )) as TransferCompleteRequestMessage; + expect(storeLogSpy).toHaveBeenCalled(); + + await satpServerService3.checkTransferCompleteRequestMessage( + transferCompleteRequestMessage, + mockSession, + ); + }); +}); diff --git a/packages/cactus-plugin-satp-hermes/src/test/typescript/unit/services.test.ts b/packages/cactus-plugin-satp-hermes/src/test/typescript/unit/services.test.ts index 26f9df11a76..b59b301a8e4 100644 --- a/packages/cactus-plugin-satp-hermes/src/test/typescript/unit/services.test.ts +++ b/packages/cactus-plugin-satp-hermes/src/test/typescript/unit/services.test.ts @@ -62,6 +62,17 @@ import { STATUS, } from "../../../main/typescript/generated/proto/cacti/satp/v02/stage_0_pb"; import { TokenType } from "../../../main/typescript/core/stage-services/satp-bridge/types/asset"; +import { + ILocalLogRepository, + IRemoteLogRepository, +} from "../../../main/typescript/repository/interfaces/repository"; +import { knexClientConnection, knexRemoteConnection } from "../knex.config"; +import { Knex, knex } from "knex"; +import { KnexLocalLogRepository as LocalLogRepository } from "../../../main/typescript/repository/knex-local-log-repository"; +import { KnexRemoteLogRepository as RemoteLogRepository } from "../../../main/typescript/repository/knex-remote-log-repository"; + +let knexInstanceClient: Knex; +let knexInstanceRemote: Knex; const logLevel: LogLevelDesc = "DEBUG"; @@ -83,7 +94,9 @@ const signer = new JsObjectSigner({ }); const supportedDLTs = [SupportedChain.FABRIC, SupportedChain.BESU]; - +let localRepository: ILocalLogRepository; +let remoteRepository: IRemoteLogRepository; +const pubKey = "MOCK_ORIGINATOR_PUBKEY"; let bridgeManager: SATPBridgesManager; let mockSession: SATPSession; @@ -122,6 +135,15 @@ beforeAll(async () => { logLevel: logLevel, }); + knexInstanceClient = knex(knexClientConnection); + await knexInstanceClient.migrate.latest(); + + knexInstanceRemote = knex(knexRemoteConnection); + await knexInstanceRemote.migrate.latest(); + + localRepository = new LocalLogRepository(knexClientConnection); + remoteRepository = new RemoteLogRepository(knexRemoteConnection); + mockSession = new SATPSession({ contextID: "MOCK_CONTEXT_ID", server: false, @@ -167,6 +189,32 @@ beforeAll(async () => { } } }); + +afterAll(async () => { + const services = [ + satpClientService0, + satpServerService0, + satpClientService1, + satpServerService1, + satpClientService2, + satpServerService2, + satpClientService3, + satpServerService3, + ]; + + for (const service of services) { + await service.LocalRepository.destroy(); + await service.RemoteRepository.destroy(); + } + + if (knexInstanceClient) { + await knexInstanceClient.destroy(); + } + if (knexInstanceRemote) { + await knexInstanceRemote.destroy(); + } +}); + describe("SATP Services Testing", () => { it("Service0Client newSessionRequest", async () => { const sessionData = mockSession.getClientSessionData(); @@ -958,6 +1006,9 @@ function initializeServiceOptions( serviceType: index % 2 === 0 ? SATPServiceType.Server : SATPServiceType.Client, bridgeManager: bridgeManager, + localRepository: localRepository, + remoteRepository: remoteRepository, + pubKey: pubKey, })); }