Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peerstore Updates (WIP) #56

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions cmake/libraries/peerstore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ target_link_libraries(libpeerstore-test libpeerstore)
target_link_libraries(libpeerstore-test cmocka)
target_link_libraries(libpeerstore-test libtestutils)
target_link_libraries(libpeerstore-test libcrypto)
target_link_libraries(libpeerstore-test libmultiaddr)
add_test(NAME LibPeerstoreTest COMMAND libpeerstore-test)
4 changes: 4 additions & 0 deletions include/network/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ typedef struct message_hello {
size_t peer_id_len;
/*! @brief the size of public_key */
size_t public_key_len;
/*! @brief the number of multiaddrs being sent */
size_t num_addrs;
/*! @brief a peer's identifier */
unsigned char *peer_id;
/*! @brief the corresponding public key for the peerid */
unsigned char *public_key;
/*! @brief the peers multiaddrs */
multi_addr_t **addrs;
} message_hello_t;

/*!
Expand Down
5 changes: 4 additions & 1 deletion include/network/socket_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ typedef struct socket_server {
public_key_t *public_key;
/*! @brief our peer id information */
peer_id_t *peer_id;
multi_addr_t **listen_addrs;
} socket_server_t;

/*! @typedef client_conn
Expand Down Expand Up @@ -229,4 +230,6 @@ int socket_server_send(socket_server_t *srv, multi_addr_t *to_address,
/*!
* @brief helper function to return a message_hello_t using our server values
*/
message_hello_t *new_server_message_hello_t(socket_server_t *srv);
message_hello_t *new_server_message_hello_t(socket_server_t *srv);

multi_addr_t **socket_server_config_copy_multi_addrs(socket_server_config_t *cfg);
7 changes: 5 additions & 2 deletions include/peerstore/peerstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@

#pragma once

#include "multiaddr/multiaddr.h"
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

/*!
* @struct peer
* @struct peer
* @typedef peer_t
* @brief contains identification information about a single peer
* @details bundles together a peers identifier (hash of public key) and the public
Expand All @@ -43,8 +44,10 @@
typedef struct peer {
size_t peer_id_len; /*! @note the size of the peer id */
size_t public_key_len; /*! @note the size of the public key */
size_t num_addrs;
unsigned char *peer_id; /*! @note the actual peer id data */
unsigned char *public_key; /*! @note the actual public key data */
multi_addr_t **addrs;
} peer_t;

/*!
Expand Down Expand Up @@ -120,7 +123,7 @@ bool peerstore_have_peer(peerstore_t *pst, unsigned char *peer_id);
*/
bool peerstore_insert_peer(peerstore_t *pst, unsigned char *peer_id,
unsigned char *public_key, size_t peer_id_len,
size_t public_key_len);
size_t public_key_len, size_t num_addrs, multi_addr_t **addrs);

/*!
* @brief performs a check to see if we need to allocate more memory
Expand Down
16 changes: 15 additions & 1 deletion src/network/messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ message_hello_t *new_message_hello_t(unsigned char *peer_id,
* @details the message_t instance to send peer information to another peer
*/
cbor_encoded_data_t *cbor_encode_hello_t(message_hello_t *msg_hello) {
size_t maddr_size;
for (size_t i = 0; i < msg_hello->num_addrs; i++) {
maddr_size += msg_hello->addrs[i]->bsize;
}
// maddr_size covers the actual size of the multiaddrs, and sizeof(size_t)
// covers the size of the size_t variable
uint8_t buf[sizeof(message_hello_t) + msg_hello->peer_id_len +
msg_hello->public_key_len];
msg_hello->public_key_len + maddr_size + sizeof(size_t)];
CborEncoder encoder, array_encoder;
CborError err;

Expand All @@ -134,6 +140,13 @@ cbor_encoded_data_t *cbor_encode_hello_t(message_hello_t *msg_hello) {
return NULL;
}

// encode the total number of multiaddresses
err = cbor_encode_int(&array_encoder, (int64_t)msg_hello->num_addrs);
if (err != CborNoError) {
printf("failed to encode int: %s\n", cbor_error_string(err));
return NULL;
}

err = cbor_encode_byte_string(&array_encoder, msg_hello->peer_id,
msg_hello->peer_id_len);
if (err != CborNoError) {
Expand All @@ -148,6 +161,7 @@ cbor_encoded_data_t *cbor_encode_hello_t(message_hello_t *msg_hello) {
return NULL;
}


err = cbor_encoder_close_container(&encoder, &array_encoder);
if (err != CborNoError) {
printf("failed to close container: %s\n", cbor_error_string(err));
Expand Down
12 changes: 10 additions & 2 deletions src/network/socket_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ socket_server_t *new_socket_server(thread_logger *thl,
config->max_peers = 100;
}

if (config->private_key_path == NULL) {
if (config->private_key_path == NULL) {
LOG_ERROR(thl, 0, "no private key path in config");
return NULL;
}
Expand Down Expand Up @@ -525,7 +525,7 @@ bool handle_hello_protocol(conn_handle_data_t *data, message_t *msg) {
// insert the peers information into our peerstore
bool ok = peerstore_insert_peer(data->srv->pstore, msg_hello->peer_id,
msg_hello->public_key, msg_hello->peer_id_len,
msg_hello->public_key_len);
msg_hello->public_key_len, msg_hello->num_addrs, msg_hello->addrs);

free_message_hello_t(msg_hello);

Expand Down Expand Up @@ -634,4 +634,12 @@ int socket_server_send(socket_server_t *srv, multi_addr_t *to_address,
message_hello_t *new_server_message_hello_t(socket_server_t *srv) {
return new_message_hello_t(srv->peer_id->data, srv->public_key->data,
srv->peer_id->len, srv->public_key->data_size);
}

multi_addr_t **socket_server_config_copy_multi_addrs(socket_server_config_t *cfg) {
multi_addr_t **addrs = calloc(1, sizeof(multi_addr_t) * cfg->num_addrs);
for (int i = 0; i < cfg->num_addrs; i++) {
addrs[i] = multi_address_copy(cfg->addrs[i]);
}
return addrs;
}
4 changes: 2 additions & 2 deletions src/peerstore/peerstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ bool peerstore_have_peer(peerstore_t *pst, unsigned char *peer_id) {
*/
bool peerstore_insert_peer(peerstore_t *pst, unsigned char *peer_id,
unsigned char *public_key, size_t peer_id_len,
size_t public_key_len) {
size_t public_key_len, size_t num_addrs, multi_addr_t **addrs) {

bool ok = false;

Expand Down Expand Up @@ -148,7 +148,7 @@ bool peerstore_insert_peer(peerstore_t *pst, unsigned char *peer_id,

/*! @note right now im not sure if we should store this in the peerstore */
/*! @note or use a pointer. i think like this would consume less memory */
peer_t pt = {.peer_id_len = peer_id_len, .public_key_len = public_key_len};
peer_t pt = {.peer_id_len = peer_id_len, .public_key_len = public_key_len, .num_addrs = num_addrs, .addrs = addrs};

// allocate memory to hold peer id
pt.peer_id = calloc(1, peer_id_len);
Expand Down
18 changes: 15 additions & 3 deletions tests/peerstore_test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "peerstore/peerstore.h"
#include "multiaddr/multiaddr.h"
// #include "testutils/testutils.h"
#include "crypto/ecdsa.h"
#include <stdio.h>
Expand Down Expand Up @@ -57,8 +58,15 @@ void peerstore_test_insert_peer(void **state) {
assert(pid != NULL);
public_key_t *pub_key = libp2p_crypto_ecdsa_keypair_public(priv_key);
assert(pub_key != NULL);

bool ok = peerstore_insert_peer(pst, pid->data, pub_key->data, pid->len, pub_key->data_size);

char addr[1024];
sprintf(addr, "/ip4/127.0.0.1/tcp/%i", i);

multi_addr_t **maddrs = calloc(1, sizeof(multi_addr_t) * 1);
multi_addr_t *maddr = multi_address_new_from_string((const char *)addr);
maddrs[0] = maddr;

bool ok = peerstore_insert_peer(pst, pid->data, pub_key->data, pid->len, pub_key->data_size, 1, maddrs);
assert(ok == true);

unsigned char output[1024];
Expand Down Expand Up @@ -90,8 +98,12 @@ void peerstore_test_insert_peer(void **state) {
assert(pid != NULL);
public_key_t *pub_key = libp2p_crypto_ecdsa_keypair_public(priv_key);
assert(pub_key != NULL);

multi_addr_t **maddrs = calloc(1, sizeof(multi_addr_t) * 1);
multi_addr_t *maddr = multi_address_new_from_string("/ip4/127.0.0.1/tcp/5001");
maddrs[0] = maddr;

bool ok = peerstore_insert_peer(pst, pid->data, pub_key->data, pid->len, pub_key->data_size);
bool ok = peerstore_insert_peer(pst, pid->data, pub_key->data, pid->len, pub_key->data_size, 1, maddrs);
assert(ok == false);

libp2p_crypto_ecdsa_free(priv_key);
Expand Down