Skip to content

Commit 427d4ef

Browse files
authored
add exception handle when send message failed for ecdh-multi-psi (#92)
* add exception handle when send message failed for ecdh-multi-psi * python transport sdk support initLog
1 parent c3be9d5 commit 427d4ef

File tree

16 files changed

+146
-41
lines changed

16 files changed

+146
-41
lines changed

cpp/wedpr-computing/ppc-pir/tests/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ set(TEST_BINARY_NAME test-ppc-pir)
55

66
add_executable(${TEST_BINARY_NAME} ${SOURCES})
77
target_include_directories(${TEST_BINARY_NAME} PRIVATE .)
8-
# target_link_libraries(${PIR_TARGET} PUBLIC ${IO_TARGET} ${FRONT_TARGET} ${BCOS_UTILITIES_TARGET} ${TARS_PROTOCOL_TARGET} TBB::tbb TCMalloc)
98

10-
# target_link_libraries(${TEST_BINARY_NAME} ${PIR_TARGET} ${RPC_TARGET} ${CRYPTO_TARGET} ${BOOST_UNIT_TEST})
11-
target_link_libraries(${TEST_BINARY_NAME} PUBLIC ${IO_TARGET} ${FRONT_TARGET} ${BCOS_UTILITIES_TARGET} ${TARS_PROTOCOL_TARGET} ${PIR_TARGET} ${RPC_TARGET} ${CRYPTO_TARGET} ${PROTOCOL_TARGET} ${BOOST_UNIT_TEST})
9+
target_link_libraries(${TEST_BINARY_NAME} PUBLIC ${CRYPTO_TARGET} ${BCOS_UTILITIES_TARGET} ${TARS_PROTOCOL_TARGET} ${PIR_TARGET} ${PROTOCOL_TARGET} ${BOOST_UNIT_TEST})
1210
add_test(NAME test-pir WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COMMAND ${TEST_BINARY_NAME})

cpp/wedpr-computing/ppc-psi/src/cm2020-psi/core/CM2020PSIReceiver.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ void CM2020PSIReceiver::prepareInputs()
170170
m_originLocations.resize(m_rInputSize);
171171
m_oprfOutputs.reserve(m_rInputSize);
172172
m_oprfOutputs.resize(m_rInputSize);
173-
173+
CM2020_PSI_LOG(INFO) << LOG_BADGE("prepareInputs success") << LOG_KV("taskID", m_taskID)
174+
<< LOG_KV("inputSize", m_rInputSize);
174175
syncInputsSize();
175176
}
176177
catch (const std::exception& e)
@@ -187,7 +188,7 @@ void CM2020PSIReceiver::syncInputsSize()
187188
encodeUnsignedNum(data, m_rInputSize);
188189
CM2020_PSI_LOG(TRACE) << LOG_BADGE("syncInputsSize") << LOG_KV("taskID", m_taskID)
189190
<< LOG_KV("data", *bcos::toHexString(*data))
190-
<< LOG_KV("m_rInputSize", m_rInputSize);
191+
<< LOG_KV("inputSize", m_rInputSize);
191192
auto message = m_config->ppcMsgFactory()->buildPPCMessage(
192193
uint8_t(TaskType::PSI), uint8_t(TaskAlgorithmType::CM_PSI_2PC), m_taskID, data);
193194
message->setMessageType(uint8_t(CM2020PSIMessageType::RECEIVER_SIZE));
@@ -242,7 +243,8 @@ void CM2020PSIReceiver::onBatchPointBReceived(PPCMessageFace::Ptr _message)
242243
{
243244
return;
244245
}
245-
CM2020_PSI_LOG(INFO) << LOG_BADGE("handleBatchPointB") << LOG_KV("taskID", m_taskID);
246+
CM2020_PSI_LOG(INFO) << LOG_BADGE("handleBatchPointB") << LOG_KV("taskID", m_taskID)
247+
<< LOG_KV("payloadSize", _message->data()->size());
246248

247249
try
248250
{
@@ -261,7 +263,8 @@ void CM2020PSIReceiver::onSenderSizeReceived(front::PPCMessageFace::Ptr _message
261263
{
262264
return;
263265
}
264-
CM2020_PSI_LOG(INFO) << LOG_BADGE("onSenderSizeReceived") << LOG_KV("taskID", m_taskID);
266+
CM2020_PSI_LOG(INFO) << LOG_BADGE("onSenderSizeReceived") << LOG_KV("taskID", m_taskID)
267+
<< LOG_KV("payloadSize", _message->data()->size());
265268
try
266269
{
267270
decodeUnsignedNum(m_sInputSize, _message->data());

cpp/wedpr-computing/ppc-psi/src/cm2020-psi/core/CM2020PSISender.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ void CM2020PSISender::onReceiverSizeReceived(front::PPCMessageFace::Ptr _message
209209
{
210210
decodeUnsignedNum(m_rInputSize, _message->data());
211211
m_progress->mark<std::string>("RECEIVE_SIZE");
212+
CM2020_PSI_LOG(INFO) << LOG_BADGE("onReceiverSizeReceived") << LOG_KV("taskID", m_taskID)
213+
<< LOG_KV("inputSize", m_rInputSize);
212214
}
213215
catch (const std::exception& e)
214216
{
@@ -238,6 +240,10 @@ void CM2020PSISender::onPointAReceived(front::PPCMessageFace::Ptr _message)
238240

239241
auto retPair = m_ot->receiverGeneratePointsB(m_otChoices, pointA);
240242

243+
CM2020_PSI_LOG(INFO) << LOG_BADGE("onPointAReceived and send pointB")
244+
<< LOG_KV("taskID", m_taskID)
245+
<< LOG_KV("dataSize", retPair.first->size())
246+
<< LOG_KV("otNumber", m_otChoices->size());
241247
// send batch point B to ot sender
242248
auto message = m_config->ppcMsgFactory()->buildPPCMessage(uint8_t(TaskType::PSI),
243249
uint8_t(TaskAlgorithmType::CM_PSI_2PC), m_taskID, retPair.first);

cpp/wedpr-computing/ppc-psi/src/ecdh-multi-psi/EcdhMultiCache.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,26 @@ void MasterCache::encryptAndSendIntersection(uint64_t dataBatchIdx, bcos::bytes
244244
});
245245
auto taskID = m_taskState->task()->id();
246246
auto self = weak_from_this();
247-
for (auto const& calcultor : calculators)
247+
for (auto const& calculator : calculators)
248248
{
249249
m_config->generateAndSendPPCMessage(
250-
calcultor.first, taskID, message,
251-
[self](bcos::Error::Ptr&& _error) {
252-
if (!_error)
250+
calculator.first, taskID, message,
251+
[self, calculator](bcos::Error::Ptr&& _error) {
252+
if (!_error || _error->errorCode() == 0)
253253
{
254254
return;
255255
}
256+
auto cache = self.lock();
257+
if (!cache)
258+
{
259+
return;
260+
}
261+
ECDH_MULTI_LOG(WARNING)
262+
<< LOG_DESC("encryptAndSendIntersection: send message to calcultor failed")
263+
<< LOG_KV("task", cache->m_taskState->task()->id())
264+
<< LOG_KV("calculator", calculator.first) << LOG_KV("code", _error->errorCode())
265+
<< LOG_KV("msg", _error->errorMessage());
266+
cache->m_taskState->onTaskException(_error->errorMessage());
256267
},
257268
dataBatchIdx);
258269
}
@@ -317,10 +328,16 @@ bool CalculatorCache::tryToFinalize()
317328
{
318329
continue;
319330
}
320-
if (it.second.plainDataIndex >= 0)
331+
if (it.second.plainDataIndex > 0)
321332
{
322333
m_intersectionResult.emplace_back(getPlainDataByIndex(it.second.plainDataIndex));
323334
}
335+
// means the header field, swap with the first element
336+
if (it.second.plainDataIndex == 0)
337+
{
338+
m_intersectionResult.emplace_back(m_intersectionResult[0]);
339+
m_intersectionResult[0] = getPlainDataByIndex(it.second.plainDataIndex);
340+
}
324341
}
325342
m_cacheState = CacheState::Finalized;
326343
ECDH_MULTI_LOG(INFO) << LOG_DESC("* tryToFinalize: compute intersection success")
@@ -356,6 +373,7 @@ void CalculatorCache::syncIntersections()
356373
message->setVersion(-1);
357374
for (auto& peer : peers)
358375
{
376+
// Note: sync task failed will not change the task status
359377
m_config->generateAndSendPPCMessage(
360378
peer.first, taskID, message,
361379
[taskID, peer](bcos::Error::Ptr&& _error) {
@@ -381,6 +399,7 @@ void CalculatorCache::syncIntersections()
381399
message->setVersion(0);
382400
for (auto& peer : peers)
383401
{
402+
// Note: sync task failed will not change the task status
384403
m_config->generateAndSendPPCMessage(
385404
peer.first, taskID, message,
386405
[taskID, peer](bcos::Error::Ptr&& _error) {

cpp/wedpr-computing/ppc-psi/src/ecdh-multi-psi/core/EcdhMultiPSICalculator.cpp

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,36 @@ bcos::bytes EcdhMultiPSICalculator::generateRandomA(std::string _taskID)
4949
message->setData(std::vector<bcos::bytes>{randomValue});
5050
message->setDataBatchCount(randomValue.size());
5151
message->setFrom(m_taskState->task()->selfParty()->id());
52+
auto self = weak_from_this();
5253
for (auto const& partner : m_partnerParties)
5354
{
54-
ECDH_CAL_LOG(INFO) << LOG_KV("PART1: Calculator generateRandomA to ", partner.first)
55+
ECDH_CAL_LOG(INFO) << LOG_DESC("send generateRandomA to partner")
56+
<< LOG_KV("partner", partner.first)
5557
<< LOG_KV(" Random: ", *toHexString(randomValue));
5658
m_config->generateAndSendPPCMessage(
5759
partner.first, _taskID, message,
58-
[self = weak_from_this(), partner](bcos::Error::Ptr&& _error) {
59-
if (!_error)
60+
[self, _taskID, partner](bcos::Error::Ptr&& _error) {
61+
if (!_error || _error->errorCode() == 0)
6062
{
6163
ECDH_CAL_LOG(INFO)
62-
<< LOG_KV("PART1: Calculator generateRandomA success to ", partner.first);
64+
<< LOG_DESC("send generated randomA to partner success")
65+
<< LOG_KV("task", _taskID) << LOG_KV("partner", partner.first);
6366
return;
6467
}
6568
auto psi = self.lock();
6669
if (!psi)
6770
{
6871
return;
6972
}
73+
if (!psi->m_taskState)
74+
{
75+
return;
76+
}
77+
ECDH_CAL_LOG(WARNING)
78+
<< LOG_DESC("send generated randomA to partner failed")
79+
<< LOG_KV("task", _taskID) << LOG_KV("partner", partner.first)
80+
<< LOG_KV("code", _error->errorCode()) << LOG_KV("msg", _error->errorMessage());
81+
psi->m_taskState->onTaskException(_error->errorMessage());
7082
},
7183
0);
7284
}
@@ -147,12 +159,13 @@ void EcdhMultiPSICalculator::blindData(std::string _taskID, bcos::bytes _randA)
147159
message->setDataBatchCount(0);
148160
}
149161
// send cipher
162+
auto self = weak_from_this();
150163
for (auto const& master : m_masterParties)
151164
{
152165
m_config->generateAndSendPPCMessage(
153166
master.first, _taskID, message,
154-
[self = weak_from_this(), master](bcos::Error::Ptr&& _error) {
155-
if (!_error)
167+
[self, _taskID, master](bcos::Error::Ptr&& _error) {
168+
if (!_error || _error->errorCode() == 0)
156169
{
157170
return;
158171
}
@@ -161,6 +174,12 @@ void EcdhMultiPSICalculator::blindData(std::string _taskID, bcos::bytes _randA)
161174
{
162175
return;
163176
}
177+
ECDH_CAL_LOG(WARNING)
178+
<< LOG_DESC("send blindedData to master failed")
179+
<< LOG_KV("task", _taskID) << LOG_KV("master", master.first)
180+
<< LOG_KV("code", _error->errorCode())
181+
<< LOG_KV("msg", _error->errorMessage());
182+
psi->m_taskState->onTaskException(_error->errorMessage());
164183
},
165184
seq);
166185
dataOffset += dataBatch->size();
@@ -215,22 +234,23 @@ void EcdhMultiPSICalculator::onReceiveIntersecCipher(PSIMessageInterface::Ptr _m
215234
message->setFrom(m_taskState->task()->selfParty()->id());
216235
// try to finalize
217236
auto ret = m_calculatorCache->tryToFinalize();
237+
auto taskID = m_taskID;
218238
for (auto const& master : m_masterParties)
219239
{
220240
ECDH_CAL_LOG(INFO) << LOG_DESC("onReceiveIntersecCipher: send response to the master")
221241
<< LOG_KV("master", master.first) << printPSIMessage(_msg);
242+
// no any bad influences when send response failed
222243
m_config->generateAndSendPPCMessage(
223-
master.first, m_taskID, message,
224-
[self = weak_from_this()](bcos::Error::Ptr&& _error) {
225-
if (!_error)
226-
{
227-
return;
228-
}
229-
auto psi = self.lock();
230-
if (!psi)
244+
master.first, taskID, message,
245+
[taskID](bcos::Error::Ptr&& _error) {
246+
if (!_error || _error->errorCode() == 0)
231247
{
232248
return;
233249
}
250+
ECDH_CAL_LOG(WARNING)
251+
<< LOG_DESC("onReceiveIntersecCipher: send response to the master failed")
252+
<< LOG_KV("task", taskID) << LOG_KV("code", _error->errorCode())
253+
<< LOG_KV("msg", _error->errorMessage());
234254
},
235255
0);
236256
}

cpp/wedpr-computing/ppc-psi/src/ecdh-multi-psi/core/EcdhMultiPSIMaster.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,28 @@ void EcdhMultiPSIMaster::blindData()
185185
message->setDataBatchCount(0);
186186
}
187187
message->setFrom(m_taskState->task()->selfParty()->id());
188+
auto self = weak_from_this();
188189
for (auto const& calcultor : m_calculatorParties)
189190
{
190-
// TODO: handle the send failed case
191191
m_config->generateAndSendPPCMessage(
192192
calcultor.first, m_taskID, message,
193-
[](bcos::Error::Ptr&& _error) {
194-
if (_error)
193+
[self, seq, calcultor](bcos::Error::Ptr&& _error) {
194+
if (!_error || _error->errorCode() == 0)
195195
{
196196
return;
197197
}
198+
auto psi = self.lock();
199+
if (!psi)
200+
{
201+
return;
202+
}
203+
ECDH_MASTER_LOG(WARNING)
204+
<< LOG_DESC("send blindedData to calculator failed")
205+
<< LOG_KV("calculator", calcultor.first) << LOG_KV("seq", seq)
206+
<< LOG_KV("task", psi->m_taskState->task()->id())
207+
<< LOG_KV("code", _error->errorCode())
208+
<< LOG_KV("msg", _error->errorMessage());
209+
psi->m_taskState->onTaskException(_error->errorMessage());
198210
},
199211
seq);
200212
}

cpp/wedpr-computing/ppc-psi/src/ecdh-multi-psi/core/EcdhMultiPSIPartner.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ void EcdhMultiPSIPartner::onReceiveRandomA(bcos::bytesPointer _randA)
117117
message->setDataBatchCount(0);
118118
}
119119
// generate and send encryptedHashSet
120+
auto self = weak_from_this();
120121
for (auto const& master : m_masterParties)
121122
{
122123
m_config->generateAndSendPPCMessage(
123124
master.first, m_taskState->task()->id(), message,
124-
[self = weak_from_this()](bcos::Error::Ptr&& _error) {
125-
if (!_error)
125+
[self, master, seq](bcos::Error::Ptr&& _error) {
126+
if (!_error || _error->errorCode() == 0)
126127
{
127128
return;
128129
}
@@ -131,6 +132,13 @@ void EcdhMultiPSIPartner::onReceiveRandomA(bcos::bytesPointer _randA)
131132
{
132133
return;
133134
}
135+
ECDH_PARTNER_LOG(WARNING)
136+
<< LOG_DESC("send blinded data to master failed") << LOG_KV("seq", seq)
137+
<< LOG_KV("master", master.first)
138+
<< LOG_KV("task", psi->m_taskState->task()->id())
139+
<< LOG_KV("code", _error->errorCode())
140+
<< LOG_KV("msg", _error->errorMessage());
141+
psi->m_taskState->onTaskException(_error->errorMessage());
134142
},
135143
seq);
136144
}

cpp/wedpr-computing/ppc-psi/src/psi-framework/TaskState.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,19 @@ void TaskState::onTaskException(std::string const& _errorMsg)
306306
<< LOG_KV("msg", boost::diagnostic_information(e));
307307
}
308308
}
309+
if (m_notifyPeerFinishHandler)
310+
{
311+
try
312+
{
313+
m_notifyPeerFinishHandler();
314+
}
315+
catch (std::exception const& e)
316+
{
317+
PSI_LOG(WARNING) << LOG_DESC("notifyPeerFinish exception")
318+
<< LOG_KV("taskID", m_task->id())
319+
<< LOG_KV("msg", boost::diagnostic_information(e));
320+
}
321+
}
309322
auto taskResult = std::make_shared<TaskResult>(m_task->id());
310323
auto msg = "Task " + m_task->id() + " exception, error : " + _errorMsg;
311324
auto error = std::make_shared<bcos::Error>(-1, msg);

cpp/wedpr-protocol/protocol/src/PPCMessage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class PPCMessage : public PPCMessageFace
4343

4444
using Ptr = std::shared_ptr<PPCMessage>;
4545
PPCMessage() { m_data = std::make_shared<bcos::bytes>(); }
46-
~PPCMessage() override { releasePayload(); }
46+
// Note: the payload passed in by the upper layer cannot be released at will
47+
~PPCMessage() override = default;
4748

4849
uint8_t version() const override { return m_version; }
4950
void setVersion(uint8_t _version) override { m_version = _version; }

cpp/wedpr-transport/ppc-front/ppc-front/Front.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ void Front::asyncSendMessage(const std::string& _agencyID, front::PPCMessageFace
139139
m_front->asyncSendMessage((uint16_t)RouteType::ROUTE_THROUGH_TOPIC, routeInfo,
140140
bcos::bytesConstRef((bcos::byte*)data.data(), data.size()), _message->seq(), _timeout,
141141
_callback, msgCallback);
142+
// release the data
143+
bcos::bytes().swap(data);
142144
}
143145

144146
// send response when receiving message from given agencyID

0 commit comments

Comments
 (0)