Skip to content

Commit

Permalink
fix(satp): make satp a cacti plugin
Browse files Browse the repository at this point in the history
 * changed cbdc example to use cacti api-server
 * added test to test the compatibility
 * TODO: check OAS (detailed comment in main plugin)

Signed-off-by: Eduardo Vasques <[email protected]>
  • Loading branch information
eduv09 authored and RafaelAPB committed Dec 6, 2024
1 parent 0f9fb71 commit ad241ba
Show file tree
Hide file tree
Showing 10 changed files with 1,324 additions and 609 deletions.
4 changes: 2 additions & 2 deletions examples/cactus-example-cbdc-bridging-backend/process.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
API_HOST=localhost
API_SERVER_1_PORT=4000
API_SERVER_2_PORT=4100
API_GATEWAY_1_BLO_PORT=4010
API_GATEWAY_2_BLO_PORT=4110
API_GATEWAY_1_BLO_PORT=4000
API_GATEWAY_2_BLO_PORT=4100
API_GATEWAY_1_CLIENT_PORT=3011
API_GATEWAY_2_CLIENT_PORT=3111
API_GATEWAY_1_SERVER_PORT=3010
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { AddressInfo } from "net";
import exitHook, { IAsyncExitHookDoneCallback } from "async-exit-hook";
import { PluginRegistry } from "@hyperledger/cactus-core";
import { v4 as uuidv4 } from "uuid";
import CryptoMaterial from "../../crypto-material/crypto-material.json";

import {
IListenOptions,
LogLevelDesc,
Logger,
LoggerProvider,
Expand All @@ -21,13 +23,9 @@ import {
import { CbdcBridgingAppDummyInfrastructure } from "./infrastructure/cbdc-bridging-app-dummy-infrastructure";
import { DefaultApi as FabricApi } from "@hyperledger/cactus-plugin-ledger-connector-fabric";
import { DefaultApi as BesuApi } from "@hyperledger/cactus-plugin-ledger-connector-besu";
import express from "express";
import bodyParser from "body-parser";
import http, { Server } from "http";
import { Constants } from "@hyperledger/cactus-core-api";
import cors from "cors";
import { Server } from "http";

import { Server as SocketIoServer } from "socket.io";
import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";

export interface ICbdcBridgingApp {
apiHost: string;
Expand Down Expand Up @@ -92,49 +90,57 @@ export class CbdcBridgingApp {
await this.infrastructure.createFabricLedgerConnector();
this.log.info("Creating Besu Connector");
const besuPlugin = await this.infrastructure.createBesuLedgerConnector();
const gatways = await this.infrastructure.createSATPGateways();

let addressInfoA: AddressInfo;
let addressInfoB: AddressInfo;

this.log.info(`SATP Gateways started.`);
// Reserve the ports where the API Servers will run
{
const expressApp = express();
expressApp.use(cors());
expressApp.use(bodyParser.json({ limit: "250mb" }));
const fabricServer = http.createServer(expressApp);

const listenOptions: IListenOptions = {
hostname: this.options.apiHost,
port: this.options.apiServer1Port,
server: fabricServer,
};
addressInfoA = (await Servers.listen(listenOptions)) as AddressInfo;

await fabricPlugin.getOrCreateWebServices();
await fabricPlugin.registerWebServices(expressApp);
}

{
const expressApp = express();
expressApp.use(bodyParser.json({ limit: "250mb" }));
expressApp.use(cors());
const besuServer = http.createServer(expressApp);
const listenOptions: IListenOptions = {
hostname: this.options.apiHost,
port: this.options.apiServer2Port,
server: besuServer,
};
addressInfoB = (await Servers.listen(listenOptions)) as AddressInfo;
await besuPlugin.getOrCreateWebServices();
const wsApi = new SocketIoServer(besuServer, {
path: Constants.SocketIoConnectionPathV1,
});
await besuPlugin.registerWebServices(expressApp, wsApi);
}

const httpApiA = await Servers.startOnPort(
this.options.apiServer1Port,
this.options.apiHost,
);
const httpApiB = await Servers.startOnPort(
this.options.apiServer2Port,
this.options.apiHost,
);
const addressInfoA = httpApiA.address() as AddressInfo;
const nodeApiHostA = `http://${this.options.apiHost}:${addressInfoA.port}`;

const addressInfoB = httpApiB.address() as AddressInfo;
const nodeApiHostB = `http://${this.options.apiHost}:${addressInfoB.port}`;

const clientPluginRegistry = new PluginRegistry({
plugins: [
new PluginKeychainMemory({
keychainId: CryptoMaterial.keychains.keychain1.id,
instanceId: uuidv4(),
logLevel: "INFO",
}),
],
});
const serverPluginRegistry = new PluginRegistry({
plugins: [
new PluginKeychainMemory({
keychainId: CryptoMaterial.keychains.keychain2.id,
instanceId: uuidv4(),
logLevel: "INFO",
}),
],
});

clientPluginRegistry.add(fabricPlugin);
clientPluginRegistry.add(gatways[0]);
await gatways[0].onPluginInit();
serverPluginRegistry.add(besuPlugin);
serverPluginRegistry.add(gatways[1]);
await gatways[1].onPluginInit();

const crpcOptionsServer1 = {host: 'localhost', port: 6000};
const apiServer1 = await this.startNode(httpApiA, clientPluginRegistry, crpcOptionsServer1, 5101);
const crpcOptionsServer2 = {host: 'localhost', port: 6001};

const apiServer2 = await this.startNode(httpApiB, serverPluginRegistry,crpcOptionsServer2, 5100);

const fabricApiClient = new FabricApi(
new Configuration({ basePath: nodeApiHostA }),
);
Expand All @@ -158,13 +164,7 @@ export class CbdcBridgingApp {

this.log.info(`Chaincode and smart Contracts deployed.`);

const gatways = await this.infrastructure.createSATPGateways();

for (const gateway of gatways) {
await gateway.startup();
}

this.log.info(`SATP Gateways started.`);


return {
fabricApiClient,
Expand All @@ -186,33 +186,36 @@ export class CbdcBridgingApp {
httpServerApi: Server,
pluginRegistry: PluginRegistry,
crpcOptions: ICrpcOptions,
grpcPort: number,
): Promise<ApiServer> {
this.log.info(`Starting API Server node...`);

const addressInfoApi = httpServerApi.address() as AddressInfo;

let config;
if (this.options.apiServerOptions) {
config = this.options.apiServerOptions;
} else {
const configService = new ConfigService();
const convictConfig = await configService.getOrCreate();
config = convictConfig.getProperties();
config.configFile = "";
config.apiCorsDomainCsv = `http://${process.env.API_HOST_FRONTEND}:${process.env.API_PORT_FRONTEND}`;
config.cockpitCorsDomainCsv = `http://${process.env.API_HOST_FRONTEND}:${process.env.API_PORT_FRONTEND}`;
config.apiPort = addressInfoApi.port;
config.apiHost = addressInfoApi.address;
config.grpcPort = 0;
config.logLevel = this.options.logLevel || "INFO";
config.authorizationProtocol = AuthorizationProtocol.NONE;
config.crpcHost = crpcOptions.host;
config.crpcPort = crpcOptions.port;
}



const configService = new ConfigService();
const apiServerOptions = await configService.newExampleConfig();
apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE;
apiServerOptions.configFile = "";
apiServerOptions.apiCorsDomainCsv = "*";
apiServerOptions.apiPort = addressInfoApi.port;
apiServerOptions.apiHost = addressInfoApi.address;
apiServerOptions.logLevel = this.options.logLevel || "INFO";
apiServerOptions.apiTlsEnabled = false;
apiServerOptions.grpcPort = grpcPort;
apiServerOptions.crpcHost = crpcOptions.host;
apiServerOptions.crpcPort = crpcOptions.port;
const config =
await configService.newExampleConfigConvict(apiServerOptions);
const prop = config.getProperties();
prop.grpcPort = grpcPort
prop.apiPort = addressInfoApi.port;
prop.crpcPort = crpcOptions.port;
this.log.info(prop);
const apiServer = new ApiServer({
config,
httpServerApi,
httpServerApi: httpServerApi,
config: prop,
pluginRegistry,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import { GetAmountApprovedEndpointV1 } from "../web-services/get-amount-approved
import {
AdminApi,
TransactionApi,
TransactRequest,
} from "@hyperledger/cactus-plugin-satp-hermes/src/main/typescript/generated/gateway-client/typescript-axios/api";
import { ClaimFormat } from "@hyperledger/cactus-plugin-satp-hermes/src/main/typescript/generated/proto/cacti/satp/v02/common/message_pb";

Expand Down Expand Up @@ -399,7 +400,7 @@ export class CbdcBridgingAppDummyInfrastructure {
address: `http://localhost`,
gatewayServerPort: 3010,
gatewayClientPort: 3011,
gatewayOpenAPIPort: 4010,
gatewayOpenAPIPort: 4000,
} as GatewayIdentity;

const besuGatewayIdentity = {
Expand All @@ -411,7 +412,7 @@ export class CbdcBridgingAppDummyInfrastructure {
address: `http://localhost`,
gatewayServerPort: 3110,
gatewayClientPort: 3111,
gatewayOpenAPIPort: 4110,
gatewayOpenAPIPort: 4100,
} as GatewayIdentity;

const pluginBungeeFabricOptions = {
Expand Down Expand Up @@ -472,7 +473,7 @@ export class CbdcBridgingAppDummyInfrastructure {
address: `http://localhost`,
gatewayServerPort: 3010,
gatewayClientPort: 3011,
gatewayOpenAPIPort: 4010,
gatewayOpenAPIPort: 4000,
pubKey: bufArray2HexStr(fabricGatewayKeyPair.publicKey),
} as GatewayIdentity,
],
Expand All @@ -494,7 +495,7 @@ export class CbdcBridgingAppDummyInfrastructure {
address: `http://localhost`,
gatewayServerPort: 3110,
gatewayClientPort: 3111,
gatewayOpenAPIPort: 4110,
gatewayOpenAPIPort: 4100,
pubKey: bufArray2HexStr(besuGatewayKeyPair.publicKey),
} as GatewayIdentity,
],
Expand Down Expand Up @@ -1256,7 +1257,7 @@ export class CbdcBridgingAppDummyInfrastructure {
}

try {
await api.transact({
const request: TransactRequest = {
contextID: "MockID",
fromDLTNetworkID,
toDLTNetworkID,
Expand All @@ -1265,11 +1266,12 @@ export class CbdcBridgingAppDummyInfrastructure {
originatorPubkey: senderAddress,
beneficiaryPubkey: receiverAddress,
sourceAsset,
receiverAsset: receiverAsset,
});
receiverAsset,
}
await api.transact(request);
} catch (error) {
this.log.error(
`Error bridging tokens from ${sourceChain} to ${receiverAsset}`,
`Error bridging tokens from ${sourceChain} to ${destinationChain}`,
);
throw error;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"dependencies": {
"@emotion/react": "11.11.1",
"@emotion/styled": "11.11.0",
"@hyperledger/cactus-example-cbdc-bridging-backend": "2.0.0-rc.7",
"@hyperledger/cactus-example-cbdc-bridging-backend": "2.0.0",
"@mui/icons-material": "6.1.1",
"@mui/material": "6.1.1",
"@testing-library/jest-dom": "5.17.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SessionReference } from "@hyperledger/cactus-example-cbdc-bridging-back
import {
GetSessionsReferencesApi,
TransactApi,
TransactRequest,
} from "@hyperledger/cactus-example-cbdc-bridging-backend/src/main/typescript/generated/openapi/typescript-axios/api";
import { Configuration } from "@hyperledger/cactus-example-cbdc-bridging-backend/src/main/typescript/generated/openapi/typescript-axios/configuration";

Expand Down Expand Up @@ -55,7 +56,7 @@ export async function transactTokens(
assetType: receiverChain,
},
amount,
});
} as TransactRequest);

if (response.status !== 200) {
throw Error(response.status + " :" + response.data);
Expand Down
1 change: 1 addition & 0 deletions packages/cactus-plugin-satp-hermes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
"@bufbuild/buf": "^1.47.2",
"@bufbuild/protoc-gen-es": "2.2.2",
"@grpc/proto-loader": "0.7.13",
"@hyperledger/cactus-api-client": "2.0.0",
"@hyperledger/cactus-test-geth-ledger": "2.0.0-rc.7",
"@quobix/vacuum": "0.9.16",
"@types/body-parser": "1.19.4",
Expand Down
Loading

0 comments on commit ad241ba

Please sign in to comment.