Skip to content

Commit

Permalink
feat: connector-go-ethereum now can report empty blocks
Browse files Browse the repository at this point in the history
- added parameter in monitorOptions what triggers monitor to report
empty blocks

Closes: 2236
Signed-off-by: tomasz awramski <[email protected]>
  • Loading branch information
rwat17 authored and petermetz committed Dec 27, 2022
1 parent 95b1138 commit 6dbe6b5
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ if (!process.env["NODE_CONFIG_DIR"]) {
import { configRead } from "@hyperledger/cactus-cmd-socketio-server";

import fs = require("fs");
import { Server } from "socket.io"
import { Server } from "socket.io";

// Log settings
import { getLogger } from "log4js";
const logger = getLogger("connector_main[" + process.pid + "]");
logger.level = configRead('logLevel', 'info');
logger.level = configRead("logLevel", "info");

// implementation class of a part dependent of end-chains (server plugin)
import { ServerPlugin } from "../../../connector/ServerPlugin";
Expand Down Expand Up @@ -59,25 +59,30 @@ export async function startGoEthereumSocketIOConnector() {
const Smonitor = new ServerMonitorPlugin();

// Get port from environment and store in Express.
const sslport = normalizePort(process.env.PORT || configRead('sslParam.port'));
const sslport = normalizePort(
process.env.PORT || configRead("sslParam.port"),
);
app.set("port", sslport);

// Specify private key and certificate
let keyString: string;
let certString: string;
try {
keyString = configRead<string>('sslParam.keyValue');
certString = configRead<string>('sslParam.certValue');
keyString = configRead<string>("sslParam.keyValue");
certString = configRead<string>("sslParam.certValue");
} catch {
keyString = fs.readFileSync(configRead('sslParam.key'), "ascii");
certString = fs.readFileSync(configRead('sslParam.cert'), "ascii");
keyString = fs.readFileSync(configRead("sslParam.key"), "ascii");
certString = fs.readFileSync(configRead("sslParam.cert"), "ascii");
}

// Create HTTPS server.
const server = https.createServer({
key: keyString,
cert: certString,
}, app); // Start as an https server.
const server = https.createServer(
{
key: keyString,
cert: certString,
},
app,
); // Start as an https server.
const io = new Server(server);

// Event listener for HTTPS server "error" event.
Expand Down Expand Up @@ -126,7 +131,8 @@ export async function startGoEthereumSocketIOConnector() {
// Check for the existence of the specified function and call it if it exists.
if (Splug.isExistFunction(func)) {
// Can be called with Server plugin function name.
(Splug as any)[func](args)
(Splug as any)
[func](args)
.then((respObj: unknown) => {
logger.info("*** RESPONSE ***");
logger.info("Client ID :" + client.id);
Expand Down Expand Up @@ -206,7 +212,8 @@ export async function startGoEthereumSocketIOConnector() {
// Check for the existence of the specified function and call it if it exists.
if (Splug.isExistFunction(func)) {
// Can be called with Server plugin function name.
(Splug as any)[func](args)
(Splug as any)
[func](args)
.then((respObj: unknown) => {
logger.info("*** RESPONSE ***");
logger.info("Client ID :" + client.id);
Expand Down Expand Up @@ -244,8 +251,11 @@ export async function startGoEthereumSocketIOConnector() {
/**
* startMonitor: starting block generation event monitoring
**/
client.on("startMonitor", function () {
Smonitor.startMonitor(client.id, (event) => {
client.on("startMonitor", function (monitorOptions) {

monitorOptions = monitorOptions ?? {allBlocks: false};
logger.debug("monitorOptions", monitorOptions);
Smonitor.startMonitor(client.id, monitorOptions.allBlocks, (event) => {
let emitType = "";
if (event.status == 200) {
emitType = "eventReceived";
Expand All @@ -264,7 +274,7 @@ export async function startGoEthereumSocketIOConnector() {
client.on("stopMonitor", function (reason) {
Smonitor.stopMonitor(client.id).then(() => {
logger.info("stopMonitor completed.");
})
});
});

client.on("disconnect", function (reason) {
Expand All @@ -277,22 +287,27 @@ export async function startGoEthereumSocketIOConnector() {
});

// Listen on provided port, on all network interfaces.
return new Promise<https.Server>((resolve) => server.listen(sslport, () => resolve(server)));
return new Promise<https.Server>((resolve) =>
server.listen(sslport, () => resolve(server)),
);
}

if (require.main === module) {
// When this file executed as a script, not loaded as module - run the connector
startGoEthereumSocketIOConnector().then((server) => {
const addr = server.address();
startGoEthereumSocketIOConnector()
.then((server) => {
const addr = server.address();

if (!addr) {
logger.error("Could not get running server address - exit.");
process.exit(1);
}
if (!addr) {
logger.error("Could not get running server address - exit.");
process.exit(1);
}

const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
logger.debug("Listening on " + bind);
}).catch((err) => {
logger.error("Could not start go-ethereum-socketio connector:", err);
});
const bind =
typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
logger.debug("Listening on " + bind);
})
.catch((err) => {
logger.error("Could not start go-ethereum-socketio connector:", err);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,19 @@ export class ServerMonitorPlugin {
* @param {string} clientId: Client ID from which monitoring start request was made
* @param {function} cb: A callback function that receives monitoring results at any time.
*/
startMonitor(clientId: string, cb: MonitorCallback) {

blockSigner = (blockData: any) => {
const signedBlockData = signMessageJwt({
blockData: blockData,
});
logger.debug(`signedBlockData = ${signedBlockData}`);
return {
status: 200,
blockData: signedBlockData,
};
};

startMonitor(clientId: string, allBlocks = false, cb: MonitorCallback) {
logger.info("*** START MONITOR ***");
logger.info("Client ID :" + clientId);
// Implement handling to receive events from an end-chain and return them in a callback function
Expand All @@ -74,18 +86,21 @@ export class ServerMonitorPlugin {
console.log("##[HL-BC] Validate transactions(D3)");
console.log("##[HL-BC] digital sign on valid transaction(D4)");

if (trLength > 0) {
if (allBlocks == false) {
if (trLength > 0) {
logger.info("*** SEND BLOCK DATA ***");
logger.debug(
"monitor is set to report blocks with at least one transaction",
);
logger.debug(`blockData = ${JSON.stringify(blockData)}`);
const retObj = this.blockSigner(blockData);
cb(retObj);
}
} else {
logger.info("*** SEND BLOCK DATA ***");
logger.debug("monitor is set to report empty blocks");
logger.debug(`blockData = ${JSON.stringify(blockData)}`);
const signedBlockData = signMessageJwt({
blockData: blockData,
});
logger.debug(`signedBlockData = ${signedBlockData}`);
// Notify only if transaction exists
const retObj = {
status: 200,
blockData: signedBlockData,
};
const retObj = this.blockSigner(blockData);
cb(retObj);
}
} else {
Expand Down
Loading

0 comments on commit 6dbe6b5

Please sign in to comment.