Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion external/c-libp2p
Submodule c-libp2p updated 125 files
2 changes: 1 addition & 1 deletion include/lantern/networking/libp2p.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int lantern_libp2p_enr_to_multiaddr(
const struct lantern_enr_record *record,
char *buffer,
size_t buffer_len,
peer_id_t *peer_id);
peer_id_t **peer_id);
int lantern_libp2p_encode_secp256k1_private_key_proto(
const uint8_t *secret,
size_t secret_len,
Expand Down
77 changes: 48 additions & 29 deletions src/core/client_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ static const uint32_t LANTERN_PING_FAILURES_BEFORE_DISCONNECT = 1u;
*
* @note Thread safety: This function is thread-safe
*/
static int write_legacy_peer_id_text(const peer_id_t *peer, char *out, size_t out_len)
{
if (!peer || !out || out_len == 0)
{
return -1;
}
size_t written = 0;
peer_id_error_t rc = peer_id_text_write(
peer,
PEER_ID_TEXT_LEGACY_BASE58,
out,
out_len,
&written);
if (rc != PEER_ID_OK)
{
out[0] = '\0';
return -1;
}
return (int)written;
}

static void format_peer_id_text(const peer_id_t *peer, char *out, size_t out_len)
{
if (!out || out_len == 0)
Expand All @@ -69,7 +90,7 @@ static void format_peer_id_text(const peer_id_t *peer, char *out, size_t out_len
return;
}

if (peer_id_to_string(peer, PEER_ID_FMT_BASE58_LEGACY, out, out_len) < 0)
if (write_legacy_peer_id_text(peer, out, out_len) < 0)
{
out[0] = '\0';
}
Expand Down Expand Up @@ -652,7 +673,7 @@ void redial_peer_on_timeout(struct lantern_client *client, const peer_id_t *peer
}

char multiaddr[256];
peer_id_t enr_peer_id = {0};
peer_id_t *enr_peer_id = NULL;
if (lantern_libp2p_enr_to_multiaddr(
record,
multiaddr,
Expand All @@ -662,8 +683,8 @@ void redial_peer_on_timeout(struct lantern_client *client, const peer_id_t *peer
continue;
}

int eq = peer_id_equals(peer, &enr_peer_id);
peer_id_destroy(&enr_peer_id);
int eq = peer_id_equal(peer, enr_peer_id);
peer_id_free(enr_peer_id);

if (eq == 1)
{
Expand Down Expand Up @@ -784,7 +805,7 @@ static size_t compute_peer_dial_target(
}

size_t target = enrs->count;
if (local_peer && local_peer->bytes && local_peer->size && target > 0)
if (local_peer && target > 0)
{
target -= 1;
}
Expand Down Expand Up @@ -815,25 +836,24 @@ static void peer_dialer_handle_record(
}

char multiaddr[256];
peer_id_t peer_id = {0};
peer_id_t *peer_id = NULL;
if (lantern_libp2p_enr_to_multiaddr(record, multiaddr, sizeof(multiaddr), &peer_id) != 0)
{
peer_id_destroy(&peer_id);
return;
}

if (local_peer && peer_id_equals(local_peer, &peer_id) == 1)
if (local_peer && peer_id_equal(local_peer, peer_id) == 1)
{
peer_id_destroy(&peer_id);
peer_id_free(peer_id);
return;
}

char peer_text[128];
format_peer_id_text(&peer_id, peer_text, sizeof(peer_text));
format_peer_id_text(peer_id, peer_text, sizeof(peer_text));

if (peer_text[0] && connected_snapshot && string_list_contains(connected_snapshot, peer_text))
{
peer_id_destroy(&peer_id);
peer_id_free(peer_id);
return;
}

Expand All @@ -857,7 +877,7 @@ static void peer_dialer_handle_record(
{
libp2p_err_t perr = libp2p_gossipsub_peering_add(
client->gossip.gossipsub,
&peer_id);
peer_id);
if (perr == LIBP2P_ERR_OK)
{
if (peer_text[0])
Expand All @@ -875,7 +895,7 @@ static void peer_dialer_handle_record(
}
}

peer_id_destroy(&peer_id);
peer_id_free(peer_id);
}


Expand Down Expand Up @@ -927,8 +947,7 @@ void peer_dialer_attempt(struct lantern_client *client)
cleanup:
if (local_peer)
{
peer_id_destroy(local_peer);
free(local_peer);
peer_id_free(local_peer);
}

lantern_string_list_reset(&connected_snapshot);
Expand Down Expand Up @@ -1140,11 +1159,11 @@ static void ping_on_stream_open(libp2p_stream_t *s, void *user_data, int err)
uint32_t failures = record_peer_ping_failure(client, peer_text);
if (failures >= LANTERN_PING_FAILURES_BEFORE_DISCONNECT)
{
peer_id_t peer_id = {0};
if (peer_id_create_from_string(peer_text, &peer_id) == 0)
peer_id_t *peer_id = NULL;
if (peer_id_new_from_text(peer_text, &peer_id) == PEER_ID_OK && peer_id)
{
connection_counter_update(client, -1, &peer_id, false, reason);
peer_id_destroy(&peer_id);
connection_counter_update(client, -1, peer_id, false, reason);
peer_id_free(peer_id);
}
clear_peer_ping_failures(client, peer_text);
}
Expand Down Expand Up @@ -1191,11 +1210,11 @@ static void ping_on_stream_open(libp2p_stream_t *s, void *user_data, int err)
uint32_t failures = record_peer_ping_failure(client, peer_text);
if (failures >= LANTERN_PING_FAILURES_BEFORE_DISCONNECT)
{
peer_id_t peer_id = {0};
if (peer_id_create_from_string(peer_text, &peer_id) == 0)
peer_id_t *peer_id = NULL;
if (peer_id_new_from_text(peer_text, &peer_id) == PEER_ID_OK && peer_id)
{
connection_counter_update(client, -1, &peer_id, false, reason);
peer_id_destroy(&peer_id);
connection_counter_update(client, -1, peer_id, false, reason);
peer_id_free(peer_id);
}
clear_peer_ping_failures(client, peer_text);
}
Expand Down Expand Up @@ -1279,27 +1298,27 @@ static void ping_all_peers(struct lantern_client *client)
{
continue;
}
peer_id_t peer = {0};
if (peer_id_create_from_string(peer_str, &peer) != 0)
peer_id_t *peer = NULL;
if (peer_id_new_from_text(peer_str, &peer) != PEER_ID_OK || !peer)
{
continue;
}
if (peer_status_needs_refresh(client, peer_str, now_ms))
{
request_status_now(client, &peer, peer_str);
request_status_now(client, peer, peer_str);
}
struct ping_dial_ctx *ctx = (struct ping_dial_ctx *)calloc(1, sizeof(*ctx));
if (!ctx)
{
peer_id_destroy(&peer);
peer_id_free(peer);
continue;
}
ctx->client = client;
strncpy(ctx->peer_text, peer_str, sizeof(ctx->peer_text) - 1);
ctx->peer_text[sizeof(ctx->peer_text) - 1] = '\0';
int rc = libp2p_host_open_stream_async(
client->network.host,
&peer,
peer,
LIBP2P_PING_PROTO_ID,
ping_on_stream_open,
ctx);
Expand All @@ -1315,7 +1334,7 @@ static void ping_all_peers(struct lantern_client *client)
rc);
free(ctx);
}
peer_id_destroy(&peer);
peer_id_free(peer);
}
lantern_string_list_reset(&peers);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/client_network_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct block_request_ctx
{
struct lantern_client *client; /**< Client instance */
uint64_t request_id; /**< Internal request tracking ID */
peer_id_t peer_id; /**< Peer ID structure */
peer_id_t *peer_id; /**< Peer ID structure */
char peer_text[128]; /**< Peer ID as text */
LanternRoot *roots; /**< Roots being requested */
uint32_t *depths; /**< Backfill depth per root */
Expand Down
6 changes: 3 additions & 3 deletions src/core/client_reqresp_blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static void block_request_ctx_free(struct block_request_ctx *ctx)
{
return;
}
peer_id_destroy(&ctx->peer_id);
peer_id_free(ctx->peer_id);
free(ctx->roots);
free(ctx->depths);
free(ctx);
Expand Down Expand Up @@ -844,7 +844,7 @@ static int schedule_blocks_request_batch(
}
}

if (peer_id_create_from_string(peer_id_text, &ctx->peer_id) != PEER_ID_SUCCESS)
if (peer_id_new_from_text(peer_id_text, &ctx->peer_id) != PEER_ID_OK || !ctx->peer_id)
{
lantern_log_warn(
"reqresp",
Expand All @@ -870,7 +870,7 @@ static int schedule_blocks_request_batch(

int rc = libp2p_host_open_stream_async(
client->network.host,
&ctx->peer_id,
ctx->peer_id,
ctx->protocol_id,
block_request_on_open,
ctx);
Expand Down
17 changes: 13 additions & 4 deletions src/core/client_reqresp_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,19 @@ static void init_peer_log_metadata(
if (stream)
{
const peer_id_t *peer = libp2p_stream_remote_peer(stream);
if (peer
&& peer_id_to_string(peer, PEER_ID_FMT_BASE58_LEGACY, peer_text, peer_text_len) < 0)
{
peer_text[0] = '\0';
if (peer)
{
size_t written = 0;
peer_id_error_t rc = peer_id_text_write(
peer,
PEER_ID_TEXT_LEGACY_BASE58,
peer_text,
peer_text_len,
&written);
if (rc != PEER_ID_OK)
{
peer_text[0] = '\0';
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/core/client_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ static const char *peer_id_to_text(const peer_id_t *from, char *out, size_t out_
return NULL;
}

if (peer_id_to_string(from, PEER_ID_FMT_BASE58_LEGACY, out, out_len) < 0)
size_t written = 0;
peer_id_error_t rc = peer_id_text_write(
from,
PEER_ID_TEXT_LEGACY_BASE58,
out,
out_len,
&written);
if (rc != PEER_ID_OK)
{
out[0] = '\0';
return NULL;
Expand Down
33 changes: 27 additions & 6 deletions src/genesis/genesis_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ static int parse_validator_config_entry(
struct lantern_validator_config_entry *entry);
static void free_validator_config_entry(struct lantern_validator_config_entry *entry);

static int write_legacy_peer_id_text(const peer_id_t *peer_id, char *buffer, size_t buffer_len)
{
if (!peer_id || !buffer || buffer_len == 0)
{
return -1;
}
size_t written = 0;
peer_id_error_t rc = peer_id_text_write(
peer_id,
PEER_ID_TEXT_LEGACY_BASE58,
buffer,
buffer_len,
&written);
if (rc != PEER_ID_OK)
{
buffer[0] = '\0';
return -1;
}
return (int)written;
}


/**
* Parse an unsigned 64-bit integer from a string, allowing a trailing comment.
Expand Down Expand Up @@ -365,26 +386,26 @@ static int derive_peer_id_from_privkey_hex(const char *hex, char **out_peer_id)
}
lantern_secure_zero(secret, sizeof(secret));

peer_id_t peer_id = {0};
peer_id_error_t perr = peer_id_create_from_private_key(encoded, encoded_len, &peer_id);
peer_id_t *peer_id = NULL;
peer_id_error_t perr = peer_id_new_from_private_key_pb(encoded, encoded_len, &peer_id);
if (encoded)
{
lantern_secure_zero(encoded, encoded_len);
}
free(encoded);

if (perr != PEER_ID_SUCCESS)
if (perr != PEER_ID_OK || !peer_id)
{
return LANTERN_GENESIS_ERR_PARSE;
}

char buffer[GENESIS_PEER_ID_BUFFER_LEN];
if (peer_id_to_string(&peer_id, PEER_ID_FMT_BASE58_LEGACY, buffer, sizeof(buffer)) < 0)
if (write_legacy_peer_id_text(peer_id, buffer, sizeof(buffer)) < 0)
{
peer_id_destroy(&peer_id);
peer_id_free(peer_id);
return LANTERN_GENESIS_ERR_PARSE;
}
peer_id_destroy(&peer_id);
peer_id_free(peer_id);

char *dup = lantern_string_duplicate(buffer);
if (!dup)
Expand Down
10 changes: 8 additions & 2 deletions src/networking/gossipsub_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ static void describe_peer_id(const peer_id_t *peer, char *buffer, size_t length)
buffer[0] = '\0';
return;
}
int written = peer_id_to_string(peer, PEER_ID_FMT_BASE58_LEGACY, buffer, length);
if (written < 0) {
size_t written = 0;
peer_id_error_t rc = peer_id_text_write(
peer,
PEER_ID_TEXT_LEGACY_BASE58,
buffer,
length,
&written);
if (rc != PEER_ID_OK) {
buffer[0] = '\0';
}
}
Expand Down
Loading