lisk-framework
Version:
Lisk blockchain application platform
166 lines • 6.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ABIServer = void 0;
const lisk_codec_1 = require("@liskhq/lisk-codec");
const zeromq_1 = require("zeromq");
const abi_1 = require("../abi");
class ABIServer {
constructor(logger, socketPath, abi) {
this._abiHandlers = {};
this._socketPath = socketPath;
this._logger = logger;
this._router = new zeromq_1.Router();
this._abiHandlers[abi.init.name] = {
request: abi_1.initRequestSchema,
response: abi_1.initResponseSchema,
func: abi.init.bind(abi),
};
this._abiHandlers[abi.initStateMachine.name] = {
request: abi_1.initStateMachineRequestSchema,
response: abi_1.initStateMachineResponseSchema,
func: abi.initStateMachine.bind(abi),
};
this._abiHandlers[abi.initGenesisState.name] = {
request: abi_1.initGenesisStateRequestSchema,
response: abi_1.initGenesisStateResponseSchema,
func: abi.initGenesisState.bind(abi),
};
this._abiHandlers[abi.insertAssets.name] = {
request: abi_1.insertAssetsRequestSchema,
response: abi_1.insertAssetsResponseSchema,
func: abi.insertAssets.bind(abi),
};
this._abiHandlers[abi.verifyAssets.name] = {
request: abi_1.verifyAssetsRequestSchema,
response: abi_1.verifyAssetsResponseSchema,
func: abi.verifyAssets.bind(abi),
};
this._abiHandlers[abi.beforeTransactionsExecute.name] = {
request: abi_1.beforeTransactionsExecuteRequestSchema,
response: abi_1.beforeTransactionsExecuteResponseSchema,
func: abi.beforeTransactionsExecute.bind(abi),
};
this._abiHandlers[abi.afterTransactionsExecute.name] = {
request: abi_1.afterTransactionsExecuteRequestSchema,
response: abi_1.afterTransactionsExecuteResponseSchema,
func: abi.afterTransactionsExecute.bind(abi),
};
this._abiHandlers[abi.verifyTransaction.name] = {
request: abi_1.verifyTransactionRequestSchema,
response: abi_1.verifyTransactionResponseSchema,
func: abi.verifyTransaction.bind(abi),
};
this._abiHandlers[abi.executeTransaction.name] = {
request: abi_1.executeTransactionRequestSchema,
response: abi_1.executeTransactionResponseSchema,
func: abi.executeTransaction.bind(abi),
};
this._abiHandlers[abi.commit.name] = {
request: abi_1.commitRequestSchema,
response: abi_1.commitResponseSchema,
func: abi.commit.bind(abi),
};
this._abiHandlers[abi.revert.name] = {
request: abi_1.revertRequestSchema,
response: abi_1.revertResponseSchema,
func: abi.revert.bind(abi),
};
this._abiHandlers[abi.clear.name] = {
request: abi_1.clearRequestSchema,
response: abi_1.clearResponseSchema,
func: abi.clear.bind(abi),
};
this._abiHandlers[abi.finalize.name] = {
request: abi_1.finalizeRequestSchema,
response: abi_1.finalizeResponseSchema,
func: abi.finalize.bind(abi),
};
this._abiHandlers[abi.getMetadata.name] = {
request: abi_1.metadataRequestSchema,
response: abi_1.metadataResponseSchema,
func: abi.getMetadata.bind(abi),
};
this._abiHandlers[abi.query.name] = {
request: abi_1.queryRequestSchema,
response: abi_1.queryResponseSchema,
func: abi.query.bind(abi),
};
this._abiHandlers[abi.prove.name] = {
request: abi_1.proveRequestSchema,
response: abi_1.proveResponseSchema,
func: abi.prove.bind(abi),
};
}
async start() {
await this._router.bind(this._socketPath);
this._listenToRequest().catch(err => {
this._logger.error({ err: err }, 'Fail to listen to ABI request');
});
}
stop() {
this._router.close();
}
async _listenToRequest() {
for await (const [sender, message] of this._router) {
let request;
try {
request = lisk_codec_1.codec.decode(abi_1.ipcRequestSchema, message);
}
catch (error) {
await this._replyError(sender, 'Failed to decode message');
this._logger.debug({ err: error }, 'Failed to decode ABI request');
continue;
}
this._logger.debug({ method: request.method, id: request.id, file: 'abi_server' }, 'Received and decoded request');
const handler = this._abiHandlers[request.method];
if (!handler) {
await this._replyError(sender, `Method ${request.method} is not registered.`, request.id);
continue;
}
try {
const params = Object.keys(handler.request.properties).length > 0
? lisk_codec_1.codec.decode(handler.request, request.params)
: {};
const resp = await handler.func(params);
await this._router.send([
sender,
lisk_codec_1.codec.encode(abi_1.ipcResponseSchema, {
id: request.id,
success: true,
error: {
message: '',
},
result: Object.keys(handler.response.properties).length > 0
? lisk_codec_1.codec.encode(handler.response, resp)
: Buffer.alloc(0),
}),
]);
this._logger.debug({ method: request.method, id: request.id, file: 'abi_server' }, 'Responded request');
}
catch (error) {
this._logger.error({ err: error, method: request.method, id: request.id }, 'Failed to respond');
await this._replyError(sender, error.message, request.id);
continue;
}
}
}
async _replyError(sender, msg, id) {
await this._router
.send([
sender,
lisk_codec_1.codec.encode(abi_1.ipcResponseSchema, {
id: id !== null && id !== void 0 ? id : BigInt(0),
success: false,
error: {
message: msg,
},
result: Buffer.alloc(0),
}),
])
.catch(sendErr => {
this._logger.error({ err: sendErr }, 'Failed to send response to the ABI request');
});
}
}
exports.ABIServer = ABIServer;
//# sourceMappingURL=abi_server.js.map