Skip to content

Commit 7314b5d

Browse files
authored
Hotfix/identify (#88)
* hotfix to prevent unexpected exception from identify msg processor * secio connection read buffer as member (performance)
1 parent a60298d commit 7314b5d

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

include/libp2p/security/secio/secio_connection.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ namespace libp2p::connection {
162162
boost::optional<std::unique_ptr<crypto::aes::AesCtr>> remote_decryptor_;
163163

164164
std::queue<uint8_t> user_data_buffer_;
165+
166+
std::shared_ptr<common::ByteArray> read_buffer_;
167+
165168
common::Logger log_ = common::createLogger("SECCONN");
166169
};
167170
} // namespace libp2p::connection

src/protocol/identify/identify_msg_processor.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
#include <tuple>
99

10+
#include <generated/protocol/identify/protobuf/identify.pb.h>
1011
#include <boost/assert.hpp>
1112
#include <libp2p/basic/protobuf_message_read_writer.hpp>
1213
#include <libp2p/network/network.hpp>
1314
#include <libp2p/peer/address_repository.hpp>
1415
#include <libp2p/protocol/identify/utils.hpp>
15-
#include <generated/protocol/identify/protobuf/identify.pb.h>
1616

1717
namespace {
1818
inline std::string fromMultiaddrToString(const libp2p::multi::Multiaddress &ma) {
@@ -354,16 +354,15 @@ namespace libp2p::protocol {
354354
log_->debug("can not get addresses for peer {}", peer_id.toBase58());
355355
}
356356

357-
switch (conn_manager_.connectedness({peer_id, addresses.value()})) {
358-
case network::ConnectionManager::Connectedness::CONNECTED:
359-
add_res = addr_repo.upsertAddresses(peer_id, listen_addresses,
360-
peer::ttl::kPermanent);
361-
break;
362-
default:
363-
add_res = addr_repo.upsertAddresses(peer_id, listen_addresses,
364-
peer::ttl::kRecentlyConnected);
365-
break;
366-
}
357+
bool permanent_ttl =
358+
(addresses
359+
&& (conn_manager_.connectedness({peer_id, addresses.value()})
360+
== network::ConnectionManager::Connectedness::CONNECTED));
361+
362+
add_res = addr_repo.upsertAddresses(
363+
peer_id, listen_addresses,
364+
permanent_ttl ? peer::ttl::kPermanent : peer::ttl::kRecentlyConnected);
365+
367366
if (!add_res) {
368367
log_->error("cannot add addresses to peer {}: {}", peer_id.toBase58(),
369368
add_res.error().message());

src/security/secio/secio_connection.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ namespace libp2p::connection {
138138
} else {
139139
return Error::UNSUPPORTED_CIPHER;
140140
}
141+
142+
read_buffer_ = std::make_shared<common::ByteArray>(kMaxFrameSize);
143+
141144
return outcome::success();
142145
}
143146

@@ -260,10 +263,9 @@ namespace libp2p::connection {
260263
}
261264

262265
void SecioConnection::readNextMessage(ReadCallbackFunc cb) {
263-
auto buffer{std::make_shared<common::ByteArray>(kMaxFrameSize)};
264266
raw_connection_->read(
265-
*buffer, kLenMarkerSize,
266-
[self{shared_from_this()}, buffer,
267+
*read_buffer_, kLenMarkerSize,
268+
[self{shared_from_this()}, buffer=read_buffer_,
267269
cb{std::move(cb)}](outcome::result<size_t> read_bytes_res) mutable {
268270
IO_OUTCOME_TRY(len_marker_size, read_bytes_res, cb)
269271
if (len_marker_size != kLenMarkerSize) {

0 commit comments

Comments
 (0)