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

fix(satp-hermes): when a db is not provided it creates a local repository, remote db is not required anymore #3728

Merged
merged 1 commit into from
Jan 22, 2025
Merged
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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -335,5 +335,9 @@
"built": false
}
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"dependencies": {
"deasync": "^0.1.30",
"deasync-promise": "^1.0.1"
}
}
22 changes: 21 additions & 1 deletion packages/cactus-plugin-satp-hermes/gateway-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,25 @@
"logLevel": "DEBUG",
"counterPartyGateways": [],
"environment": "development",
"enableOpenAPI": true
"enableOpenAPI": true,
"knexLocalConfig": {
"client": "sqlite3",
"connection": {
"filename": "${knex_dir}/.dev.client-123e4567-e89b-12d3-a456-426614174000.sqlite3"
},
"migrations": {
"directory": "./packages/cactus-plugin-satp-hermes/src/knex/migrations"
},
"useNullAsDefault": true
},
"knexRemoteConfig": {
"client": "sqlite3",
"connection": {
"filename": "./packages/cactus-plugin-satp-hermes/src/knex/.dev.server-123e4567-e89b-12d3-a456-426614174000.sqlite3"
},
"migrations": {
"directory": "./packages/cactus-plugin-satp-hermes/src/knex/migrations"
},
"useNullAsDefault": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ export interface BLODispatcherOptions {
signer: JsObjectSigner;
bridgesManager: SATPBridgesManager;
pubKey: string;
defaultRepository: boolean;
localRepository: ILocalLogRepository;
remoteRepository: IRemoteLogRepository;
remoteRepository?: IRemoteLogRepository;
}

export class BLODispatcher {
Expand All @@ -60,8 +61,9 @@ export class BLODispatcher {
private manager: SATPManager;
private orchestrator: GatewayOrchestrator;
private bridgeManager: SATPBridgesManager;
private defaultRepository: boolean;
private localRepository: ILocalLogRepository;
private remoteRepository: IRemoteLogRepository;
private remoteRepository: IRemoteLogRepository | undefined;

constructor(public readonly options: BLODispatcherOptions) {
const fnTag = `${BLODispatcher.CLASS_NAME}#constructor()`;
Expand All @@ -78,6 +80,7 @@ export class BLODispatcher {
this.orchestrator = options.orchestrator;
const signer = options.signer;
const ourGateway = this.orchestrator.ourGateway;
this.defaultRepository = options.defaultRepository;
this.localRepository = options.localRepository;
this.remoteRepository = options.remoteRepository;

Expand All @@ -91,6 +94,7 @@ export class BLODispatcher {
bridgeManager: this.bridgeManager,
orchestrator: this.orchestrator,
pubKey: options.pubKey,
defaultRepository: this.defaultRepository,
localRepository: this.localRepository,
remoteRepository: this.remoteRepository,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Type } from "../../generated/proto/cacti/satp/v02/common/session_pb";
export class CrashRecoveryServerService {
constructor(
private readonly bridgesManager: SATPBridgesManager,
private readonly defaultRepository: boolean,
private readonly logRepository: ILocalLogRepository,
private readonly sessions: Map<string, SATPSession>,
private readonly signer: JsObjectSigner,
Expand Down Expand Up @@ -56,6 +57,10 @@ export class CrashRecoveryServerService {
throw new SignatureVerificationError(fnTag);
}

if (this.defaultRepository && !this.logRepository.getCreated()) {
this.logRepository.createKnex();
}

const recoveredLogs = await this.logRepository.fetchLogsFromSequence(
req.sessionId,
req.sequenceNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { create, isMessage } from "@bufbuild/protobuf";
import {
AssetSchema,
CredentialProfile,
Error as SATPError,
LockType,
MessageType,
SignatureAlgorithm,
Expand Down Expand Up @@ -703,7 +704,12 @@ export function setError(
}

sessionData.state = State.ERROR;
sessionData.errorCode = error.getSATPErrorType();

try {
sessionData.errorCode = error.getSATPErrorType();
} catch (e) {
sessionData.errorCode = SATPError.UNSPECIFIED;
}
sessionData.phaseError = stageMessage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ import { verifySignature } from "../gateway-utils";

export interface ICrashRecoveryManagerOptions {
logLevel?: LogLevelDesc;
defaultRepository: boolean;
localRepository: ILocalLogRepository;
remoteRepository: IRemoteLogRepository;
remoteRepository?: IRemoteLogRepository;
instanceId: string;
bridgeConfig: SATPBridgesManager;
orchestrator: GatewayOrchestrator;
Expand All @@ -55,8 +56,9 @@ export class CrashManager {
public sessions: Map<string, SATPSession>;
private crashRecoveryHandler: CrashRecoveryHandler;
private factory: RollbackStrategyFactory;
private defaultRepository: boolean;
public localRepository: ILocalLogRepository;
public remoteRepository: IRemoteLogRepository;
public remoteRepository: IRemoteLogRepository | undefined;
private crashScheduler?: Job;
private isSchedulerPaused = false;
private crashRecoveryServerService: CrashRecoveryServerService;
Expand All @@ -76,6 +78,7 @@ export class CrashManager {
this.log.info(`Instantiated ${this.className} OK`);
this.instanceId = options.instanceId;
this.sessions = new Map<string, SATPSession>();
this.defaultRepository = options.defaultRepository;
this.localRepository = options.localRepository;
this.remoteRepository = options.remoteRepository;
this.signer = options.signer;
Expand All @@ -87,6 +90,7 @@ export class CrashManager {

this.crashRecoveryServerService = new CrashRecoveryServerService(
this.bridgesManager,
this.defaultRepository,
this.localRepository,
this.sessions,
this.signer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ export interface ISATPManagerOptions {
supportedDLTs: SupportedChain[];
bridgeManager: SATPBridgesManager;
orchestrator: GatewayOrchestrator;
defaultRepository: boolean;
localRepository: ILocalLogRepository;
remoteRepository: IRemoteLogRepository;
remoteRepository?: IRemoteLogRepository;
}

export class SATPManager {
Expand All @@ -118,8 +119,9 @@ export class SATPManager {
private readonly orchestrator: GatewayOrchestrator;

private gatewaysPubKeys: Map<string, string> = new Map();
private defaultRepository: boolean;
private localRepository: ILocalLogRepository;
private remoteRepository: IRemoteLogRepository;
private remoteRepository: IRemoteLogRepository | undefined;
private readonly dbLogger: SATPLogger;

constructor(public readonly options: ISATPManagerOptions) {
Expand All @@ -138,6 +140,7 @@ export class SATPManager {
this.orchestrator = options.orchestrator;
this._pubKey = options.pubKey;
this.loadPubKeys(this.orchestrator.getCounterPartyGateways());
this.defaultRepository = options.defaultRepository;
this.localRepository = options.localRepository;
this.remoteRepository = options.remoteRepository;

Expand All @@ -150,6 +153,7 @@ export class SATPManager {
];

const satpLoggerConfig: ISATPLoggerConfig = {
defaultRepository: this.defaultRepository,
localRepository: this.localRepository,
remoteRepository: this.remoteRepository,
signer: this.signer,
Expand Down
62 changes: 37 additions & 25 deletions packages/cactus-plugin-satp-hermes/src/main/typescript/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@ interface SATPLogEntry {
}

export interface ISATPLoggerConfig {
defaultRepository: boolean;
localRepository: ILocalLogRepository;
remoteRepository: IRemoteLogRepository;
remoteRepository?: IRemoteLogRepository;
signer: JsObjectSigner;
pubKey: string;
}

export class SATPLogger {
private defaultRepository: boolean = true;
public localRepository: ILocalLogRepository;
public remoteRepository: IRemoteLogRepository;
public remoteRepository: IRemoteLogRepository | undefined;
private signer: JsObjectSigner;
private pubKey: string;
private readonly log: Logger;

constructor(config: ISATPLoggerConfig) {
this.defaultRepository = config.defaultRepository;
this.localRepository = config.localRepository;
this.remoteRepository = config.remoteRepository;
this.signer = config.signer;
Expand Down Expand Up @@ -136,38 +139,47 @@ export class SATPLogger {
const fnTag = `SATPLogger#storeInDatabase()`;
this.log.info(`${fnTag} - Storing log entry with key: ${localLog.key}`);

if (this.defaultRepository && !this.localRepository.getCreated()) {
this.log.debug(
`${fnTag} - Default configuration detected. Creating local repository.`,
);
await this.localRepository.createKnex();
}

await this.localRepository.create(localLog);
}

private async storeRemoteLog(key: string, hash: string): Promise<void> {
const fnTag = `SATPLogger#storeRemoteLog()`;
this.log.info(
`${fnTag} - Storing remote log with key: ${key} and hash: ${hash}`,
);
if (!!this.remoteRepository) {
this.log.info(
`${fnTag} - Storing remote log with key: ${key} and hash: ${hash}`,
);

const remoteLog: RemoteLog = {
key: key,
hash: hash,
signature: "",
signerPubKey: this.pubKey,
};
const remoteLog: RemoteLog = {
key: key,
hash: hash,
signature: "",
signerPubKey: this.pubKey,
};

remoteLog.signature = bufArray2HexStr(
sign(this.signer, safeStableStringify(remoteLog)),
);
remoteLog.signature = bufArray2HexStr(
sign(this.signer, safeStableStringify(remoteLog)),
);

this.log.debug(`${fnTag} - Generated signature: ${remoteLog.signature}`);
const response = await this.remoteRepository.create(remoteLog);
this.log.debug(`${fnTag} - Generated signature: ${remoteLog.signature}`);
const response = await this.remoteRepository.create(remoteLog);

if (response.status < 200 || response.status > 299) {
this.log.error(
`${fnTag} - Failed to store remote log. Response status: ${response.status}`,
);
throw new Error(
`${fnTag} - Got response ${response.status} when logging to remote`,
);
}
if (response.status < 200 || response.status > 299) {
this.log.error(
`${fnTag} - Failed to store remote log. Response status: ${response.status}`,
);
throw new Error(
`${fnTag} - Got response ${response.status} when logging to remote`,
);
}

this.log.info(`${fnTag} - Successfully stored remote log.`);
this.log.info(`${fnTag} - Successfully stored remote log.`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import cors from "cors";

import * as OAS from "../json/openapi-blo-bundled.json";
import { knexLocalInstance } from "../../knex/knexfile";
import { knexRemoteInstance } from "../../knex/knexfile-remote";

export class SATPGateway implements IPluginWebService, ICactusPlugin {
// todo more checks; example port from config is between 3000 and 9000
Expand Down Expand Up @@ -100,6 +99,8 @@ export class SATPGateway implements IPluginWebService, ICactusPlugin {
private signer: JsObjectSigner;
private _pubKey: string;

// Flag to create a db repository when not given
public defaultRepository: boolean = true;
public localRepository?: ILocalLogRepository;
public remoteRepository?: IRemoteLogRepository;
private readonly shutdownHooks: ShutdownHook[];
Expand All @@ -117,10 +118,23 @@ export class SATPGateway implements IPluginWebService, ICactusPlugin {
};
this.logger = LoggerProvider.getOrCreate(logOptions);
this.logger.info("Initializing Gateway Coordinator");
this.localRepository = new LocalLogRepository(this.config.knexLocalConfig);
this.remoteRepository = new RemoteLogRepository(
this.config.knexRemoteConfig,
);

if (this.config.knexLocalConfig) {
this.defaultRepository = false;
this.localRepository = new LocalLogRepository(
this.config.knexLocalConfig,
);
} else {
this.localRepository = new LocalLogRepository(knexLocalInstance.default);
}

if (this.config.knexRemoteConfig) {
this.remoteRepository = new RemoteLogRepository(
this.config.knexRemoteConfig,
);
} else {
this.logger.warn("Remote repository is not defined");
}

if (this.config.keyPair == undefined) {
throw new Error("Key pair is undefined");
Expand Down Expand Up @@ -176,6 +190,7 @@ export class SATPGateway implements IPluginWebService, ICactusPlugin {
signer: this.signer,
bridgesManager: this.bridgesManager,
pubKey: this.pubKey,
defaultRepository: this.defaultRepository,
localRepository: this.localRepository,
remoteRepository: this.remoteRepository,
};
Expand All @@ -197,6 +212,7 @@ export class SATPGateway implements IPluginWebService, ICactusPlugin {
logLevel: this.config.logLevel,
bridgeConfig: this.bridgesManager,
orchestrator: this.gatewayOrchestrator,
defaultRepository: this.defaultRepository,
localRepository: this.localRepository,
remoteRepository: this.remoteRepository,
signer: this.signer,
Expand Down Expand Up @@ -408,14 +424,6 @@ export class SATPGateway implements IPluginWebService, ICactusPlugin {
pluginOptions.bridgesConfig = [];
}

if (!pluginOptions.knexLocalConfig) {
pluginOptions.knexLocalConfig = knexLocalInstance.default;
}

if (!pluginOptions.knexRemoteConfig) {
pluginOptions.knexRemoteConfig = knexRemoteInstance.default;
}

if (!pluginOptions.enableCrashManager) {
pluginOptions.enableCrashManager = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface ILocalLogRepository extends IRepository<LocalLog, string> {
): Promise<LocalLog[]>;
destroy(): any;
reset(): any;
createKnex(): any;
getCreated(): boolean;
}

export interface IRemoteLogRepository extends IRepository<RemoteLog, string> {
Expand Down
Loading
Loading