Skip to content

Commit

Permalink
add ut for protocols && fix gatewayNodeInfo encode concurrency bug (#24)
Browse files Browse the repository at this point in the history
* fix initialize problems

* add ut for Message

* fix gatewayNodeInfo encode concurrency bug
  • Loading branch information
cyjseagull authored Sep 6, 2024
1 parent 7b77c7b commit 4415b6e
Show file tree
Hide file tree
Showing 73 changed files with 1,104 additions and 418 deletions.
8 changes: 5 additions & 3 deletions cpp/ppc-framework/gateway/IGateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ class IGateway
* @param callback callback
*/
virtual void asyncSendMessage(ppc::protocol::RouteType routeType,
ppc::protocol::MessageOptionalHeader::Ptr const& routeInfo, bcos::bytes&& payload,
long timeout, ppc::protocol::ReceiveMsgFunc callback) = 0;
ppc::protocol::MessageOptionalHeader::Ptr const& routeInfo, std::string const& traceID,
bcos::bytes&& payload, long timeout, ppc::protocol::ReceiveMsgFunc callback) = 0;

virtual void asyncSendbroadcastMessage(ppc::protocol::RouteType routeType,
ppc::protocol::MessageOptionalHeader::Ptr const& routeInfo, bcos::bytes&& payload) = 0;
ppc::protocol::MessageOptionalHeader::Ptr const& routeInfo, std::string const& traceID,
bcos::bytes&& payload) = 0;

virtual bcos::Error::Ptr registerNodeInfo(ppc::protocol::INodeInfo::Ptr const& nodeInfo) = 0;
virtual bcos::Error::Ptr unRegisterNodeInfo(bcos::bytesConstRef nodeID) = 0;
virtual bcos::Error::Ptr registerTopic(
Expand Down
1 change: 1 addition & 0 deletions cpp/ppc-framework/protocol/INodeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class INodeInfoFactory

virtual INodeInfo::Ptr build() = 0;
virtual INodeInfo::Ptr build(bcos::bytesConstRef nodeID, std::string const& endPoint) = 0;
virtual INodeInfo::Ptr build(bcos::bytesConstRef data) = 0;
};

inline std::string printNodeInfo(INodeInfo::Ptr const& nodeInfo)
Expand Down
5 changes: 3 additions & 2 deletions cpp/ppc-framework/protocol/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ class Message : virtual public bcos::boostssl::MessageFace

protected:
MessageHeader::Ptr m_header;
std::shared_ptr<bcos::bytes> m_payload;
MessagePayload::Ptr m_frontMessage;
// Note: allocate here in case of wsService nullptr access caused coredump
std::shared_ptr<bcos::bytes> m_payload = std::make_shared<bcos::bytes>();
MessagePayload::Ptr m_frontMessage = nullptr;
};

class MessageHeaderBuilder
Expand Down
15 changes: 10 additions & 5 deletions cpp/ppc-framework/protocol/PPCMessageFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ namespace ppc
{
namespace front
{
enum MessageExtFlag : uint16_t
{
ResponseFlag = 0x0001,
};
class PPCMessageFace
{
public:
Expand Down Expand Up @@ -86,7 +82,16 @@ class PPCMessageFaceFactory
public:
virtual ~PPCMessageFaceFactory() {}
virtual PPCMessageFace::Ptr buildPPCMessage() = 0;
virtual PPCMessageFace::Ptr buildPPCMessage(ppc::protocol::Message::Ptr msg) = 0;
virtual PPCMessageFace::Ptr decodePPCMessage(ppc::protocol::Message::Ptr msg) = 0;
virtual ppc::protocol::Message::Ptr buildMessage(
ppc::protocol::MessageBuilder::Ptr const& msgBuilder,
ppc::protocol::MessagePayloadBuilder::Ptr const& msgPayloadBuilder,
PPCMessageFace::Ptr const& ppcMessage) = 0;

virtual ppc::protocol::MessagePayload::Ptr buildMessage(
ppc::protocol::MessagePayloadBuilder::Ptr const& msgPayloadBuilder,
PPCMessageFace::Ptr const& ppcMessage) = 0;

virtual PPCMessageFace::Ptr buildPPCMessage(bcos::bytesConstRef _data) = 0;
virtual PPCMessageFace::Ptr buildPPCMessage(bcos::bytesPointer _buffer) = 0;
virtual PPCMessageFace::Ptr buildPPCMessage(uint8_t _taskType, uint8_t _algorithmType,
Expand Down
10 changes: 3 additions & 7 deletions cpp/test-utils/FakeFront.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class FakeFront : public FrontInterface
FakeFront() = default;
~FakeFront() override = default;

void start() override {}
void stop() override {}
void registerMessageHandler(uint8_t _taskType, uint8_t _algorithmType,
std::function<void(front::PPCMessageFace::Ptr)> _handler) override
{}

void registerRA2018(std::string const& _agencyID, TaskFrameworkInterface::Ptr _psi)
{
Expand Down Expand Up @@ -73,11 +74,6 @@ class FakeFront : public FrontInterface
m_agencyToOTPIR[_agencyID] = _pir;
}

void onReceiveMessage(front::PPCMessageFace::Ptr, ErrorCallbackFunc) override
{
throw std::runtime_error("FakeFront: unimplemented interface onReceiveMessage!");
}

void asyncSendMessage(const std::string& _agencyID, front::PPCMessageFace::Ptr _message,
uint32_t _timeout, ErrorCallbackFunc _callback, CallbackFunc _responseCallback) override
{
Expand Down
17 changes: 17 additions & 0 deletions cpp/test-utils/FakePPCMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,22 @@ class FakePPCMessageFactory : public PPCMessageFaceFactory
{
throw std::runtime_error("FakePPCMessageFactory: unimplemented interface!");
}
PPCMessageFace::Ptr decodePPCMessage(ppc::protocol::Message::Ptr msg) override
{
throw std::runtime_error("FakePPCMessageFactory: unimplemented interface!");
}
ppc::protocol::Message::Ptr buildMessage(ppc::protocol::MessageBuilder::Ptr const& msgBuilder,
ppc::protocol::MessagePayloadBuilder::Ptr const& msgPayloadBuilder,
PPCMessageFace::Ptr const& ppcMessage) override
{
throw std::runtime_error("FakePPCMessageFactory: unimplemented interface!");
}

ppc::protocol::MessagePayload::Ptr buildMessage(
ppc::protocol::MessagePayloadBuilder::Ptr const& msgPayloadBuilder,
PPCMessageFace::Ptr const& ppcMessage) override
{
throw std::runtime_error("FakePPCMessageFactory: unimplemented interface!");
}
};
} // namespace ppc::test
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/

#include "FakeCM2020PSIFactory.h"
#include "ppc-protocol/src/JsonTaskImpl.h"
#include "ppc-psi/src/cm2020-psi/CM2020PSIImpl.h"
#include "protocol/src/JsonTaskImpl.h"
#include "test-utils/FakeFront.h"
#include "test-utils/FileTool.h"
#include "test-utils/TaskMock.h"
Expand Down
1 change: 0 additions & 1 deletion cpp/wedpr-computing/ppc-psi/tests/labeled-psi/DataTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

namespace ppc::psi
{

inline void genItemsLabels(
ppc::io::DataBatch::Ptr _items, ppc::io::DataBatch::Ptr _labels, uint32_t _size)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
*/

#include "FakeLabeledPSIFactory.h"
#include "ppc-protocol/src/JsonTaskImpl.h"
#include "ppc-psi/src/labeled-psi/LabeledPSIImpl.h"
#include "ppc-psi/src/labeled-psi/protocol/LabeledPSIResult.h"
#include "protocol/src/JsonTaskImpl.h"
#include "test-utils/FileTool.h"
#include "test-utils/TaskMock.h"
#include <bcos-utilities/testutils/TestPromptFixture.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#include "ppc-crypto-core/src/hash/Sha256Hash.h"
#include "ppc-crypto/src/ecc/Ed25519EccCrypto.h"
#include "ppc-crypto/src/oprf/EcdhOprf.h"
#include "ppc-protocol/src/JsonTaskImpl.h"
#include "ppc-psi/src/labeled-psi/core/LabeledPSIParams.h"
#include "ppc-psi/src/labeled-psi/core/SenderDB.h"
#include "protocol/src/JsonTaskImpl.h"
#include "test-utils/TaskMock.h"
#include <bcos-utilities/testutils/TestPromptFixture.h>
#include <boost/test/unit_test.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "mock/Common.h"
#include "mock/EcdhPSIFixture.h"
// Note: it's better not to depends on the task-impl
#include "ppc-protocol/src/JsonTaskImpl.h"
#include "protocol/src/JsonTaskImpl.h"
#include "test-utils/TaskMock.h"
#include <bcos-utilities/testutils/TestPromptFixture.h>
#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -61,8 +61,8 @@ void testEcdhImplFunc(int64_t _dataBatchSize, std::string const& _serverPSIDataS
auto clientPSI = factory->createEcdhPSI(clientAgencyName, clientConfig);

std::vector<std::string> agencyList = {serverAgencyName, clientAgencyName};
serverPSI->psiConfig()->updateAgenyList(agencyList);
clientPSI->psiConfig()->updateAgenyList(agencyList);
// serverPSI->psiConfig()->updateAgenyList(agencyList);
// clientPSI->psiConfig()->updateAgenyList(agencyList);

// register the server-psi into the front
factory->front()->registerEcdhPSI(serverAgencyName, serverPSI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "mock/Common.h"
#include "mock/RA2018PSIFixture.h"
// Note: it's better not to depends on the task-impl
#include "ppc-protocol/src/JsonTaskImpl.h"
#include "protocol/src/JsonTaskImpl.h"
#include "test-utils/TaskMock.h"
#include <bcos-utilities/testutils/TestPromptFixture.h>
#include <boost/test/unit_test.hpp>
Expand Down
2 changes: 1 addition & 1 deletion cpp/wedpr-computing/ppc-psi/tests/ra2018-psi/mock/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @date 2022-12-29
*/
#pragma once
#include "ppc-protocol/src/JsonTaskImpl.h"
#include "protocol/src/JsonTaskImpl.h"
#include "test-utils/TaskMock.h"
#include <thread>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "test-utils/FakeFront.h"
#include "test-utils/FakePPCMessage.h"
#include <ppc-psi/src/ecdh-psi/EcdhPSIFactory.h>
#include <ppc-psi/src/ecdh-psi/EcdhPSIMessageFactory.h>


using namespace bcos;
using namespace ppc::protocol;
Expand Down
11 changes: 8 additions & 3 deletions cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ void PPCConfig::loadEndpointConfig(EndPoint& endPoint, bool requireHostIp,
endPoint.setPort(listenPort);
}

void PPCConfig::loadFrontConfig(
void PPCConfig::loadFrontConfig(bool requireTransport,
FrontConfigBuilder::Ptr const& frontConfigBuilder, boost::property_tree::ptree const& pt)
{
if (m_frontConfig == nullptr)
{
m_frontConfig = frontConfigBuilder->build();
}
loadEndpointConfig(m_frontConfig->mutableSelfEndPoint(), true, "transport", pt);
// the thread_count
auto threadCount = pt.get<uint16_t>("transport.thread_count", 4);
if (threadCount == 0)
Expand All @@ -128,8 +127,14 @@ void PPCConfig::loadFrontConfig(
InvalidConfig() << errinfo_comment("Must specify the transport.nodeid!"));
}
m_frontConfig->setNodeID(nodeID);

m_frontConfig->setThreadPoolSize(threadCount);

if (!requireTransport)
{
return;
}

loadEndpointConfig(m_frontConfig->mutableSelfEndPoint(), true, "transport", pt);
// the gateway targets
auto gatewayTargets = pt.get<std::string>("transport.service.gateway_target", "");
if (gatewayTargets.empty())
Expand Down
16 changes: 11 additions & 5 deletions cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,16 @@ class PPCConfig
PPCConfig() = default;
virtual ~PPCConfig() = default;
// load the nodeConfig
void loadNodeConfig(ppc::front::FrontConfigBuilder::Ptr const& frontConfigBuilder,
void loadNodeConfig(bool requireTransport,
ppc::front::FrontConfigBuilder::Ptr const& frontConfigBuilder,
std::string const& _configPath)
{
PPCConfig_LOG(INFO) << LOG_DESC("loadNodeConfig") << LOG_KV("path", _configPath);
boost::property_tree::ptree iniConfig;
boost::property_tree::read_ini(_configPath, iniConfig);
// Note: must load common-config firstly since some ra-configs depends on the common-config
loadCommonNodeConfig(iniConfig);
loadFrontConfig(frontConfigBuilder, _configPath);
loadFrontConfig(requireTransport, frontConfigBuilder, _configPath);
loadRA2018Config(iniConfig);
loadStorageConfig(iniConfig);
loadEcdhPSIConfig(iniConfig);
Expand All @@ -158,13 +159,14 @@ class PPCConfig
loadGatewayConfig(iniConfig);
}

void loadFrontConfig(ppc::front::FrontConfigBuilder::Ptr const& frontConfigBuilder,
void loadFrontConfig(bool requireTransport,
ppc::front::FrontConfigBuilder::Ptr const& frontConfigBuilder,
std::string const& _configPath)
{
PPCConfig_LOG(INFO) << LOG_DESC("loadFrontConfig") << LOG_KV("path", _configPath);
boost::property_tree::ptree iniConfig;
boost::property_tree::read_ini(_configPath, iniConfig);
loadFrontConfig(frontConfigBuilder, iniConfig);
loadFrontConfig(requireTransport, frontConfigBuilder, iniConfig);
// load the grpcConfig
m_grpcConfig = loadGrpcConfig("transport", iniConfig);
m_frontConfig->setGrpcConfig(m_grpcConfig);
Expand Down Expand Up @@ -235,11 +237,15 @@ class PPCConfig
// used by cem module
virtual void loadCEMConfig(boost::property_tree::ptree const& _pt);

// for ut
void setAgencyID(std::string const& agencyID) { m_agencyID = agencyID; }

private:
virtual void loadEndpointConfig(ppc::protocol::EndPoint& endPoint, bool requireHostIp,
std::string const& sectionName, boost::property_tree::ptree const& pt);
// load the front config
virtual void loadFrontConfig(ppc::front::FrontConfigBuilder::Ptr const& frontConfigBuilder,
virtual void loadFrontConfig(bool requireTransport,
ppc::front::FrontConfigBuilder::Ptr const& frontConfigBuilder,
boost::property_tree::ptree const& pt);
// load the grpc config
ppc::protocol::GrpcConfig::Ptr loadGrpcConfig(
Expand Down
13 changes: 9 additions & 4 deletions cpp/wedpr-helper/ppc-utilities/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@

namespace ppc
{
template <typename T>
inline uint64_t decodeNetworkBuffer(
bcos::bytes& _result, bcos::byte const* buffer, unsigned int bufferLen, uint64_t const offset)
T& _result, bcos::byte const* buffer, unsigned int bufferLen, uint64_t const offset)
{
uint64_t curOffset = offset;
CHECK_OFFSET_WITH_THROW_EXCEPTION(curOffset, bufferLen);
// Notice: operator* is higher priority than operator+, the () is essential
auto dataLen =
boost::asio::detail::socket_ops::network_to_host_short(*((uint16_t*)buffer + curOffset));
boost::asio::detail::socket_ops::network_to_host_short(*((uint16_t*)(buffer + curOffset)));
curOffset += 2;
if (dataLen == 0)
{
return curOffset;
}
CHECK_OFFSET_WITH_THROW_EXCEPTION(curOffset, bufferLen);
_result.insert(
_result.end(), (bcos::byte*)buffer + curOffset, (bcos::byte*)buffer + curOffset + dataLen);
_result.assign((bcos::byte*)buffer + curOffset, (bcos::byte*)buffer + curOffset + dataLen);
curOffset += dataLen;
return curOffset;
}
Expand Down
20 changes: 14 additions & 6 deletions cpp/wedpr-initializer/Initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,25 @@ using namespace ppc::tools;
using namespace ppc::crypto;
using namespace ppc::sdk;

Initializer::Initializer(std::string const& _configPath) : m_configPath(_configPath)
Initializer::Initializer(ppc::protocol::NodeArch _arch, std::string const& _configPath)
: m_arch(_arch), m_configPath(_configPath)
{
m_transportBuilder = std::make_shared<TransportBuilder>();
// load the config
m_config = std::make_shared<PPCConfig>();
m_config->loadNodeConfig(m_transportBuilder->frontConfigBuilder(), _configPath);
if (m_arch == ppc::protocol::NodeArch::PRO)
{
m_config->loadNodeConfig(true, m_transportBuilder->frontConfigBuilder(), _configPath);
}
else
{
m_config->loadNodeConfig(false, m_transportBuilder->frontConfigBuilder(), _configPath);
}
}

void Initializer::init(ppc::protocol::NodeArch _arch, ppc::gateway::IGateway::Ptr const& gateway)
void Initializer::init(ppc::gateway::IGateway::Ptr const& gateway)
{
INIT_LOG(INFO) << LOG_DESC("init the wedpr-component") << LOG_KV("arch", _arch);
INIT_LOG(INFO) << LOG_DESC("init the wedpr-component") << LOG_KV("arch", m_arch);
// load the protocol
m_protocolInitializer = std::make_shared<ProtocolInitializer>();
m_protocolInitializer->init(m_config);
Expand All @@ -73,7 +81,7 @@ void Initializer::init(ppc::protocol::NodeArch _arch, ppc::gateway::IGateway::Pt

// Note: must set the m_holdingMessageMinutes before init the node
TransportBuilder transportBuilder;
if (_arch == ppc::protocol::NodeArch::AIR)
if (m_arch == ppc::protocol::NodeArch::AIR)
{
m_transport = transportBuilder.build(SDKMode::AIR, m_config->frontConfig(), gateway);
}
Expand All @@ -85,7 +93,7 @@ void Initializer::init(ppc::protocol::NodeArch _arch, ppc::gateway::IGateway::Pt

INIT_LOG(INFO) << LOG_DESC("init the frontService success")
<< LOG_KV("frontDetail", printFrontDesc(m_config->frontConfig()))
<< LOG_KV("arch", _arch);
<< LOG_KV("arch", m_arch);

auto cryptoBox = m_protocolInitializer->cryptoBox();
SQLStorage::Ptr sqlStorage = nullptr;
Expand Down
5 changes: 3 additions & 2 deletions cpp/wedpr-initializer/Initializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class Initializer : public std::enable_shared_from_this<Initializer>
{
public:
using Ptr = std::shared_ptr<Initializer>;
Initializer(std::string const& _configPath);
Initializer(ppc::protocol::NodeArch _arch, std::string const& _configPath);
virtual ~Initializer() { stop(); }

// init the service
virtual void init(ppc::protocol::NodeArch _arch, ppc::gateway::IGateway::Ptr const& gateway);
virtual void init(ppc::gateway::IGateway::Ptr const& gateway);
virtual void stop();
virtual void start();

Expand All @@ -82,6 +82,7 @@ class Initializer : public std::enable_shared_from_this<Initializer>


private:
ppc::protocol::NodeArch m_arch;
std::string m_configPath;
std::shared_ptr<ppc::tools::PPCConfig> m_config;
ProtocolInitializer::Ptr m_protocolInitializer;
Expand Down
4 changes: 2 additions & 2 deletions cpp/wedpr-main/air-node/AirNodeInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ void AirNodeInitializer::init(std::string const& _configPath)
INIT_LOG(INFO) << LOG_DESC("initLog success");

// init the node
m_nodeInitializer = std::make_shared<Initializer>(_configPath);
m_nodeInitializer = std::make_shared<Initializer>(ppc::protocol::NodeArch::AIR, _configPath);

// init the gateway
initGateway(_configPath);
// init the node
m_nodeInitializer->init(ppc::protocol::NodeArch::AIR, m_gateway);
m_nodeInitializer->init(m_gateway);
// set the created front to the builder
m_frontBuilder->setFront(m_nodeInitializer->transport()->getFront());
// register the NodeInfo
Expand Down
Loading

0 comments on commit 4415b6e

Please sign in to comment.