Unverified Commit c064a774 authored by kladko's avatar kladko

bug/SKALE-3662 Adding libzmq

parent d09a4f4c
......@@ -70,7 +70,7 @@ bin_PROGRAMS = sgxwallet testw sgx_util
## have to be explicitly listed
COMMON_SRC = ZMQClient.cpp BLSSignRspMessage.cpp ECDSASignRspMessage.cpp ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp ServerWorker.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \
COMMON_SRC = SGXException.cpp ZMQClient.cpp BLSSignRspMessage.cpp ECDSASignRspMessage.cpp ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp ServerWorker.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \
SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp \
DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp \
third_party/intel/sgx_stub.c third_party/intel/sgx_detect_linux.c third_party/intel/create_enclave.c third_party/intel/oc_alloc.c \
......@@ -116,7 +116,7 @@ nodist_testw_SOURCES=${nodist_sgxwallet_SOURCES}
EXTRA_testw_DEPENDENCIES=${EXTRA_sgxwallet_DEPENDENCIES}
testw_LDADD= ${sgxwallet_LDADD}
sgx_util_SOURCES= InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp sgx_util.cpp stubclient.cpp LevelDB.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp
sgx_util_SOURCES= SGXException.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp sgx_util.cpp stubclient.cpp LevelDB.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp
sgx_util_LDADD=-LlibBLS/deps/deps_inst/x86_or_x64/lib -Lleveldb/build -LlibBLS/build \
-LlibBLS/build/libff/libff \
......
/*
Copyright (C) 2021-Present SKALE Labs
This file is part of sgxwallet.
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file SGXException.cpp
@author Stan Kladko
@date 2021
*/
#include "SGXException.h"
const char* SGXException::what() const noexcept {
return errString.c_str();
}
......@@ -16,13 +16,13 @@
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file RPCException.h
@file SGXException.h
@author Stan Kladko
@date 2019
*/
#ifndef SGXD_RPCEXCEPTION_H
#define SGXD_RPCEXCEPTION_H
#ifndef SGXD_SGXEXCEPTION_H
#define SGXD_SGXEXCEPTION_H
#include <string>
#include <exception>
......@@ -46,11 +46,11 @@ public:
return errString;
}
const char* what() const noexcept override;
const int32_t getStatus() const {
return status;
}
};
#endif //SGXD_RPCEXCEPTION_H
#endif //SGXD_SGXEXCEPTION_H
......@@ -18,16 +18,18 @@ ServerWorker::ServerWorker(zmq::context_t &ctx, int sock_type) : ctx_(ctx),
void ServerWorker::work() {
worker_.connect("inproc://backend");
try {
std::string replyStr;
while (true) {
try {
zmq::message_t msg;
zmq::message_t copied_msg;
worker_.recv(&msg);
vector<uint8_t> msgData(msg.size() + 1, 0);
vector <uint8_t> msgData(msg.size() + 1, 0);
memcpy(msgData.data(), msg.data(), msg.size());
auto parsedMsg = ZMQMessage::parse(msgData, true);
CHECK_STATE(parsedMsg);
......@@ -36,21 +38,35 @@ void ServerWorker::work() {
Json::FastWriter fastWriter;
std::string replyStr = fastWriter.write(reply);
replyStr = fastWriter.write(reply);
zmq::message_t replyMsg(replyStr.c_str(),replyStr.size() + 1);
worker_.send(replyMsg);
}
}
catch (std::exception &e) {
spdlog::info("Exiting zmq server worker:{}", e.what());
return;
spdlog::error("Exception in zmq server worker:{}", e.what());
replyStr = "";
} catch (...) {
spdlog::error("Error in zmq server worker");
return;
replyStr = "";
}
try {
zmq::message_t replyMsg(replyStr.c_str(), replyStr.size() + 1);
worker_.send(replyMsg);
} catch (std::exception &e) {
spdlog::error("Exception in zmq server send :{}", e.what());
} catch (...) {
spdlog::error("Unklnown exception in zmq server send");
}
}
}
......@@ -47,37 +47,39 @@ string ZMQMessage::getStringRapid(const char *_name) {
shared_ptr <ZMQMessage> ZMQMessage::parse(vector <uint8_t> &_msg, bool _isRequest) {
return parse((const char *) _msg.data(), _msg.size(), _isRequest);
CHECK_STATE(_msg.size() > 2);
CHECK_STATE(_msg.back() == 0);
return parse((const char *) _msg.data(), _msg.size() - 1, _isRequest);
}
shared_ptr <ZMQMessage> ZMQMessage::parse(const char* _msg,
size_t _size, bool _isRequest) {
CHECK_STATE(_size > 0);
CHECK_STATE(_size > 2);
CHECK_STATE(_msg);
// CHECK NULL TERMINATED
CHECK_STATE(_msg[_size - 1] == 0);
auto d = make_shared<rapidjson::Document>();
CHECK_STATE(_msg[_size] == 0);
CHECK_STATE(_msg[_size - 1] == '}');
CHECK_STATE(_msg[0] == '{');
cerr << "parsing" << endl;
cerr << _msg << endl;
d->Parse(_msg);
cerr << "parsing" << endl ;
auto d = make_shared<rapidjson::Document>();
d->Parse(_msg);
CHECK_STATE(!d->HasParseError());
CHECK_STATE(d->IsObject())
CHECK_STATE(d->HasMember("type"));
CHECK_STATE((*d)["type"].IsString());
string type = (*d)["type"].GetString();
shared_ptr <ZMQMessage> result;
if (_isRequest) {
......
......@@ -56,7 +56,7 @@ inline std::string className(const std::string &prettyFunction) {
#include <execinfo.h>
inline void print_stack() {
inline void print_stack(int _line) {
void *array[10];
size_t size;
......@@ -64,16 +64,16 @@ inline void print_stack() {
size = backtrace(array, 10);
// print out all the frames to stderr
fprintf(stderr, "Error: signal \n");
fprintf(stderr, "Backtrace on line %d: \n", _line);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(-1);
}
#define CHECK_STATE(_EXPRESSION_) \
if (!(_EXPRESSION_)) { \
auto __msg__ = std::string("State check failed::") + #_EXPRESSION_ + " " + std::string(__FILE__) + ":" + std::to_string(__LINE__); \
print_stack(); \
print_stack(__LINE__); \
\
BOOST_THROW_EXCEPTION(SGXException(-100, string(__CLASS_NAME__) + ":" + __msg__));}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment