Skip to content

Commit

Permalink
feat: add satp logging in all satges
Browse files Browse the repository at this point in the history
Signed-off-by: Yogesh01000100 <[email protected]>
  • Loading branch information
Yogesh01000100 committed Dec 4, 2024
1 parent 3039f23 commit 4c23272
Show file tree
Hide file tree
Showing 19 changed files with 635 additions and 138 deletions.
4 changes: 3 additions & 1 deletion packages/cactus-plugin-satp-hermes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"db:migrate": "knex migrate:latest --knexfile src/knex/knexfile.js",
"db:migrate:production": "knex migrate:latest --env production --knexfile src/knex/knexfile.ts",
"db:seed": "knex seed:run --knexfile src/knex/knexfile.ts",
"db:cleanup": "find src/knex/data -name '.dev-*.sqlite3' -delete"
"db:cleanup": "find src/knex/ -name '.dev.local-*.sqlite3' -delete"
},
"jest": {
"moduleNameMapper": {
Expand Down Expand Up @@ -134,6 +134,7 @@
"kubo-rpc-client": "3.0.1",
"npm-run-all": "4.1.5",
"openzeppelin-solidity": "3.4.2",
"pg": "^8.13.1",
"safe-stable-stringify": "2.5.0",
"secp256k1": "4.0.3",
"socket.io": "4.6.2",
Expand All @@ -157,6 +158,7 @@
"@types/fs-extra": "11.0.4",
"@types/google-protobuf": "3.15.5",
"@types/node": "18.18.2",
"@types/pg": "^8.11.10",
"@types/swagger-ui-express": "4.1.6",
"@types/tape": "4.13.4",
"@types/uuid": "10.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import path from "path";
import { v4 as uuidv4 } from "uuid";
import dotenv from "dotenv";
import { Knex } from "knex";

const envPath = process.env.ENV_PATH;
dotenv.config({ path: envPath });

module.exports = {
const config: { [key: string]: Knex.Config } = {
development: {
client: "sqlite3",
connection: {
Expand All @@ -20,7 +21,7 @@ module.exports = {
client: "pg",
connection: {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
port: Number(process.env.DB_PORT),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
Expand All @@ -30,3 +31,5 @@ module.exports = {
},
},
};

export default config;
9 changes: 6 additions & 3 deletions packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import path from "path";
import { v4 as uuidv4 } from "uuid";
import dotenv from "dotenv";
import { Knex } from "knex";

const envPath = process.env.ENV_PATH;
dotenv.config({ path: envPath });

module.exports = {
const config: { [key: string]: Knex.Config } = {
development: {
client: "sqlite3",
connection: {
filename: path.join(__dirname, "data", "/.dev-" + uuidv4() + ".sqlite3"),
filename: path.resolve(__dirname, `.dev.local-${uuidv4()}.sqlite3`),
},
migrations: {
directory: path.resolve(__dirname, "migrations"),
Expand All @@ -23,7 +24,7 @@ module.exports = {
client: "pg",
connection: {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
port: Number(process.env.DB_PORT),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
Expand All @@ -33,3 +34,5 @@ module.exports = {
},
},
};

export default config;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +45,8 @@ export interface BLODispatcherOptions {
signer: JsObjectSigner;
bridgesManager: SATPBridgesManager;
pubKey: string;
localRepository: ILocalLogRepository;
remoteRepository: IRemoteLogRepository;
}

export class BLODispatcher {
Expand All @@ -54,19 +60,26 @@ 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()`;
Checks.truthy(options, `${fnTag} arg options`);

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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { bufArray2HexStr, getHash, sign } from "../../../gateway-utils";
import {
bufArray2HexStr,
getHash,
sign,
storeLog,
} from "../../../gateway-utils";
import {
MessageType,
WrapAssertionClaim,
Expand Down Expand Up @@ -58,6 +63,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) {
Expand Down Expand Up @@ -109,6 +117,20 @@ export class Stage0ClientService extends SATPService {
getHash(newSessionRequestMessage),
);

await storeLog(
{
sessionID: sessionData.id,
type: String(MessageType.NEW_SESSION_REQUEST),
operation: "create",
data: safeStableStringify(sessionData),
sequenceNumber: Number(sessionData.lastSequenceNumber),
},
this.localRepository,
this.remoteRepository,
this.Signer,
this.pubKey,
);

this.Log.info(`${fnTag}, sending NewSessionRequest...`);

return newSessionRequestMessage;
Expand Down Expand Up @@ -270,6 +292,20 @@ export class Stage0ClientService extends SATPService {
getHash(preSATPTransferRequest),
);

await storeLog(
{
sessionID: sessionData.id,
type: String(MessageType.PRE_SATP_TRANSFER_REQUEST),
operation: "create",
data: safeStableStringify(sessionData),
sequenceNumber: Number(sessionData.lastSequenceNumber),
},
this.localRepository,
this.remoteRepository,
this.Signer,
this.pubKey,
);

this.Log.info(`${fnTag}, sending PreSATPTransferRequest...`);

return preSATPTransferRequest;
Expand Down Expand Up @@ -323,6 +359,19 @@ export class Stage0ClientService extends SATPService {
sessionData.senderWrapAssertionClaim.signature = bufArray2HexStr(
sign(this.Signer, sessionData.senderWrapAssertionClaim.receipt),
);
await storeLog(
{
sessionID: sessionData.id,
type: "wrap-token",
operation: "done",
data: safeStableStringify(sessionData),
sequenceNumber: Number(sessionData.lastSequenceNumber),
},
this.localRepository,
this.remoteRepository,
this.Signer,
this.pubKey,
);
} catch (error) {
this.logger.debug(`Crash in ${fnTag}`, error);
throw new FailedToProcessError(fnTag, "WrapToken");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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: String(MessageType.INIT_PROPOSAL),
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;
Expand Down Expand Up @@ -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: String(MessageType.TRANSFER_COMMENCE_REQUEST),
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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: String(MessageType.LOCK_ASSERT),
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;
Expand Down Expand Up @@ -240,6 +254,19 @@ export class Stage2ClientService extends SATPService {
sessionData.lockAssertionClaim.signature = bufArray2HexStr(
sign(this.Signer, sessionData.lockAssertionClaim.receipt),
);
await storeLog(
{
sessionID: sessionData.id,
type: "lock-asset",
operation: "done",
data: safeStableStringify(sessionData),
sequenceNumber: Number(sessionData.lastSequenceNumber),
},
this.localRepository,
this.remoteRepository,
this.Signer,
this.pubKey,
);
} catch (error) {
this.logger.debug(`Crash in ${fnTag}`, error);
throw new FailedToProcessError(fnTag, "LockAsset");
Expand Down
Loading

0 comments on commit 4c23272

Please sign in to comment.