Making providerstore smarter

This commit is contained in:
jmjatlanta 2017-07-31 12:50:12 -05:00
parent e22da601ea
commit e4f1c9b39c
17 changed files with 76 additions and 72 deletions

View file

@ -60,7 +60,7 @@ int do_init(FILE* out_file, char* repo_root, int empty, int num_bits_for_keypair
if (fs_repo_is_initialized(repo_root))
return 0;
//TODO: If the conf is null, make one
if ( conf->identity->peer_id == NULL) {
if ( conf->identity->peer == NULL || conf->identity->peer->id == NULL) {
int retVal = ipfs_repo_config_init(conf, num_bits_for_keypair, repo_root, 4001, NULL);
if (retVal == 0)
return 0;

View file

@ -39,7 +39,7 @@ int ipfs_daemon_start(char* repo_path) {
local_node->routing->Bootstrap(local_node->routing);
libp2p_logger_info("daemon", "Daemon for %s is ready on port %d\n", listen_param.local_node->identity->peer_id, listen_param.port);
libp2p_logger_info("daemon", "Daemon for %s is ready on port %d\n", listen_param.local_node->identity->peer->id, listen_param.port);
// Wait for pthreads to finish.
while (count_pths) {

View file

@ -40,8 +40,8 @@ int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node) {
// fill in the node
local_node->repo = fs_repo;
local_node->identity = fs_repo->config->identity;
local_node->peerstore = libp2p_peerstore_new(local_node->identity->peer_id);
local_node->providerstore = libp2p_providerstore_new();
local_node->peerstore = libp2p_peerstore_new(local_node->identity->peer);
local_node->providerstore = libp2p_providerstore_new(fs_repo->config->datastore, local_node->identity->peer);
local_node->blockstore = ipfs_blockstore_new(fs_repo);
local_node->mode = MODE_OFFLINE;
local_node->routing = ipfs_routing_new_online(local_node, &fs_repo->config->identity->private_key);

View file

@ -132,7 +132,7 @@ void ipfs_null_connection (void *ptr)
session->insecure_stream = libp2p_net_multistream_stream_new(connection_param->file_descriptor, connection_param->ip, connection_param->port);
libp2p_logger_debug("null", "%s null has a file descriptor of %d\n", connection_param->local_node->identity->peer_id, *((int*)session->insecure_stream->socket_descriptor) );
libp2p_logger_debug("null", "%s null has a file descriptor of %d\n", connection_param->local_node->identity->peer->id, *((int*)session->insecure_stream->socket_descriptor) );
session->default_stream = session->insecure_stream;
session->datastore = connection_param->local_node->repo->config->datastore;
@ -142,23 +142,27 @@ void ipfs_null_connection (void *ptr)
if (libp2p_net_multistream_negotiate(session)) {
// Someone has connected and successfully negotiated multistream. Now talk to them...
int unsuccessful_max = 30;
int unsuccessful_counter = 0;
for(;;) {
// Wait for them to ask something...
unsigned char* results = NULL;
size_t bytes_read = 0;
if (null_shutting_down) {
libp2p_logger_debug("null", "%s null shutting down before read.\n", connection_param->local_node->identity->peer_id);
libp2p_logger_debug("null", "%s null shutting down before read.\n", connection_param->local_node->identity->peer->id);
// this service is shutting down. Ignore the request and exit the loop
break;
}
if (!session->default_stream->read(session, &results, &bytes_read, DEFAULT_NETWORK_TIMEOUT) ) {
// the read was unsuccessful wait a sec
sleep(1);
unsuccessful_counter++;
if (unsuccessful_counter >= unsuccessful_max)
break;
continue;
}
if (null_shutting_down) {
libp2p_logger_debug("null", "%s null shutting down after read.\n", connection_param->local_node->identity->peer_id);
libp2p_logger_debug("null", "%s null shutting down after read.\n", connection_param->local_node->identity->peer->id);
// this service is shutting down. Ignore the request and exit the loop
break;
}
@ -168,7 +172,7 @@ void ipfs_null_connection (void *ptr)
}
// We actually got something. Process the request...
unsuccessful_counter = 0;
libp2p_logger_debug("null", "Read %lu bytes from a stream tranaction\n", bytes_read);
int retVal = ipfs_null_marshal(results, bytes_read, session, connection_param);
free(results);
@ -181,7 +185,7 @@ void ipfs_null_connection (void *ptr)
libp2p_logger_log("null", LOGLEVEL_DEBUG, "Multistream negotiation failed\n");
}
libp2p_logger_debug("null", "%s Freeing session context.\n", connection_param->local_node->identity->peer_id);
libp2p_logger_debug("null", "%s Freeing session context.\n", connection_param->local_node->identity->peer->id);
(*(connection_param->count))--; // update counter.
if (connection_param->ip != NULL)
free(connection_param->ip);
@ -212,10 +216,10 @@ void* ipfs_null_listen (void *ptr)
libp2p_logger_error("null", "Ipfs listening on %d\n", listen_param->port);
for (;;) {
libp2p_logger_debug("null", "%s Attempting socket read\n", listen_param->local_node->identity->peer_id);
libp2p_logger_debug("null", "%s Attempting socket read\n", listen_param->local_node->identity->peer->id);
int numDescriptors = socket_read_select4(socketfd, 2);
if (null_shutting_down) {
libp2p_logger_debug("null", "%s null_listen shutting down.\n", listen_param->local_node->identity->peer_id);
libp2p_logger_debug("null", "%s null_listen shutting down.\n", listen_param->local_node->identity->peer->id);
break;
}
if (numDescriptors > 0) {

View file

@ -51,8 +51,8 @@ int ipfs_ping (int argc, char **argv)
local_node.repo = fs_repo;
local_node.mode = MODE_ONLINE;
local_node.routing = ipfs_routing_new_online(&local_node, &fs_repo->config->identity->private_key);
local_node.peerstore = libp2p_peerstore_new(local_node.identity->peer_id);
local_node.providerstore = libp2p_providerstore_new();
local_node.peerstore = libp2p_peerstore_new(local_node.identity->peer);
local_node.providerstore = libp2p_providerstore_new(fs_repo->config->datastore, fs_repo->config->identity->peer);
if (local_node.routing->Bootstrap(local_node.routing) != 0)
goto exit;

View file

@ -57,8 +57,8 @@ const char* ipfs_resolver_remove_path_prefix(const char* path, const struct FSRe
if (pos == first_non_slash && (strncmp(&path[pos], "ipfs", 4) == 0 || strncmp(&path[pos], "ipns", 4) == 0) ) {
// ipfs or ipns should be up front. Otherwise, it could be part of the path
pos += 4;
} else if (strncmp(&path[pos], fs_repo->config->identity->peer_id, strlen(fs_repo->config->identity->peer_id)) == 0) {
pos += strlen(fs_repo->config->identity->peer_id) + 1; // the slash
} else if (strncmp(&path[pos], fs_repo->config->identity->peer->id, fs_repo->config->identity->peer->id_size) == 0) {
pos += fs_repo->config->identity->peer->id_size + 1; // the slash
} else {
return &path[pos];
}
@ -91,7 +91,7 @@ int ipfs_resolver_is_remote(const char* path, const struct FSRepo* fs_repo) {
// if this is a Qm code, see if it is a local Qm code
if (path[pos] == 'Q' && path[pos+1] == 'm') {
if (strncmp(&path[pos], fs_repo->config->identity->peer_id, strlen(fs_repo->config->identity->peer_id)) != 0) {
if (strncmp(&path[pos], fs_repo->config->identity->peer->id, fs_repo->config->identity->peer->id_size) != 0) {
return 1;
}
}

View file

@ -2,9 +2,10 @@
#define __REPO_CONFIG_IDENTITY_H__
#include "libp2p/crypto/rsa.h"
#include "libp2p/peer/peer.h"
struct Identity {
char* peer_id; // a pretty-printed hash of the public key
struct Libp2pPeer* peer; // a Peer object
struct RsaPrivateKey private_key; // a private key
};

View file

@ -77,7 +77,7 @@ int repo_config_get_file_name(char* path, char** result) {
}
int ipfs_repo_config_is_valid_identity(struct Identity* identity) {
if (identity->peer_id == NULL || identity->peer_id[0] != 'Q' || identity->peer_id[1] != 'm')
if (identity == NULL || identity->peer == NULL || identity->peer->id == NULL || identity->peer->id[0] != 'Q' || identity->peer->id[1] != 'm')
return 0;
return 1;
}
@ -101,8 +101,8 @@ int ipfs_repo_config_init(struct RepoConfig* config, unsigned int num_bits_for_k
free(config->identity->private_key.public_key_der);
if (config->identity->private_key.der != NULL)
free(config->identity->private_key.der);
if (config->identity->peer_id != NULL)
free(config->identity->peer_id);
if (config->identity->peer != NULL)
libp2p_peer_free(config->identity->peer);
}
if (!repo_config_identity_init(config->identity, num_bits_for_keypair))
return 0;

View file

@ -24,8 +24,9 @@ int repo_config_identity_build_peer_id(struct Identity* identity) {
public_key.data = (unsigned char*)identity->private_key.public_key_der;
public_key.data_size = identity->private_key.public_key_length;
public_key.type = KEYTYPE_RSA;
if (!libp2p_crypto_public_key_to_peer_id(&public_key, &identity->peer_id))
if (!libp2p_crypto_public_key_to_peer_id(&public_key, &identity->peer->id))
return 0;
identity->peer->id_size = strlen(identity->peer->id);
return 1;
}
@ -61,7 +62,7 @@ int repo_config_identity_new(struct Identity** identity) {
memset(*identity, 0, sizeof(struct Identity));
(*identity)->peer_id = NULL;
(*identity)->peer = NULL;
(*identity)->private_key.public_key_der = NULL;
(*identity)->private_key.der = NULL;
@ -74,8 +75,8 @@ int repo_config_identity_free(struct Identity* identity) {
free(identity->private_key.public_key_der);
if (identity->private_key.der != NULL)
free(identity->private_key.der);
if (identity->peer_id != NULL)
free(identity->peer_id);
if (identity->peer != NULL)
libp2p_peer_free(identity->peer);
free(identity);
}
return 1;

View file

@ -3,6 +3,7 @@
#include "libp2p/crypto/encoding/base64.h"
#include "libp2p/crypto/key.h"
#include "libp2p/peer/peer.h"
#include "libp2p/utils/vector.h"
#include "ipfs/blocks/blockstore.h"
#include "ipfs/datastore/ds_helper.h"
@ -31,7 +32,7 @@ int repo_config_write_config_file(char* full_filename, struct RepoConfig* config
fprintf(out_file, "{\n");
fprintf(out_file, " \"Identity\": {\n");
fprintf(out_file, " \"PeerID\": \"%s\",\n", config->identity->peer_id);
fprintf(out_file, " \"PeerID\": \"%s\",\n", config->identity->peer->id);
// print correct format of private key
// first put it in a protobuf
struct PrivateKey* priv_key = libp2p_crypto_private_key_new();
@ -396,19 +397,20 @@ int fs_repo_open_config(struct FSRepo* repo) {
// the next should be the array, then string "PeerID"
//NOTE: the code below compares the peer id of the file with the peer id generated
// by the key. If they don't match, we fail.
char* peer_id = NULL;
_get_json_string_value(data, tokens, num_tokens, curr_pos, "PeerID", &peer_id);
repo->config->identity->peer = libp2p_peer_new();
_get_json_string_value(data, tokens, num_tokens, curr_pos, "PeerID", &repo->config->identity->peer->id);
// build the Libp2pPeer object
repo->config->identity->peer->id_size = strlen(repo->config->identity->peer->id);
repo->config->identity->peer->is_local = 1;
char* priv_key_base64;
// then PrivKey
_get_json_string_value(data, tokens, num_tokens, curr_pos, "PrivKey", &priv_key_base64);
retVal = repo_config_identity_build_private_key(repo->config->identity, priv_key_base64);
if (retVal == 0 || strcmp(peer_id, repo->config->identity->peer_id) != 0) {
if (retVal == 0) {
free(data);
free(priv_key_base64);
free(peer_id);
return 0;
}
free(peer_id);
// now the datastore
//int datastore_position = _find_token(data, tokens, num_tokens, 0, "Datastore");
_get_json_string_value(data, tokens, num_tokens, curr_pos, "Type", &repo->config->datastore->type);

View file

@ -88,10 +88,10 @@ int make_ipfs_repository(const char* path, int swarm_port, struct Libp2pVector*
goto exit;
// give some results to the user
printf("peer identity: %s\n", fs_repo->config->identity->peer_id);
printf("peer identity: %s\n", fs_repo->config->identity->peer->id);
if (peer_id != NULL) {
*peer_id = malloc(strlen(fs_repo->config->identity->peer_id) + 1);
strcpy(*peer_id, fs_repo->config->identity->peer_id);
*peer_id = malloc(fs_repo->config->identity->peer->id_size + 1);
strcpy(*peer_id, fs_repo->config->identity->peer->id);
}
// make sure the repository exists

View file

@ -108,7 +108,7 @@ int ipfs_routing_kademlia_provide(struct IpfsRouting* routing, const unsigned ch
//TODO: Announce to the network that I can provide this file
// save in a cache
// store key and address in cache. Key is the hash, peer id is the value
libp2p_providerstore_add(routing->local_node->providerstore, (unsigned char*)key, key_size, (unsigned char*)routing->local_node->identity->peer_id, strlen(routing->local_node->identity->peer_id));
libp2p_providerstore_add(routing->local_node->providerstore, (unsigned char*)key, key_size, (unsigned char*)routing->local_node->identity->peer->id, routing->local_node->identity->peer->id_size);
return 1;
}
@ -151,10 +151,10 @@ int ipfs_routing_kademlia_bootstrap(struct IpfsRouting* routing) {
struct IpfsRouting* ipfs_routing_new_kademlia(struct IpfsNode* local_node, struct RsaPrivateKey* private_key) {
char kademlia_id[21];
// generate kademlia compatible id by getting first 20 chars of peer id
if (local_node->identity->peer_id == NULL || strlen(local_node->identity->peer_id) < 20) {
if (local_node->identity->peer->id == NULL || local_node->identity->peer->id_size < 20) {
return NULL;
}
strncpy(kademlia_id, local_node->identity->peer_id, 20);
strncpy(kademlia_id, local_node->identity->peer->id, 20);
kademlia_id[20] = 0;
struct IpfsRouting* routing = (struct IpfsRouting*)malloc(sizeof(struct IpfsRouting));
if (routing != NULL) {

View file

@ -219,17 +219,14 @@ int ipfs_routing_online_find_peer(struct IpfsRouting* routing, const unsigned ch
struct Libp2pPeer* ipfs_routing_online_build_local_peer(struct IpfsRouting* routing) {
// create the local_peer to be attached to the message
struct Libp2pPeer* local_peer = libp2p_peer_new();
local_peer->id_size = strlen(routing->local_node->identity->peer_id);
local_peer->id = malloc(local_peer->id_size);
memcpy(local_peer->id, routing->local_node->identity->peer_id, local_peer->id_size);
struct Libp2pPeer* local_peer = libp2p_peer_copy(routing->local_node->identity->peer);
local_peer->connection_type = CONNECTION_TYPE_CONNECTED;
local_peer->addr_head = libp2p_utils_linked_list_new();
struct MultiAddress* temp_ma = multiaddress_new_from_string((char*)routing->local_node->repo->config->addresses->swarm_head->item);
int port = multiaddress_get_ip_port(temp_ma);
multiaddress_free(temp_ma);
char str[255];
sprintf(str, "/ip4/127.0.0.1/tcp/%d/ipfs/%s/", port, routing->local_node->identity->peer_id);
sprintf(str, "/ip4/127.0.0.1/tcp/%d/ipfs/%s/", port, routing->local_node->identity->peer->id);
struct MultiAddress* ma = multiaddress_new_from_string(str);
libp2p_logger_debug("online", "Adding local MultiAddress %s to peer.\n", ma->string);
local_peer->addr_head->item = ma;
@ -261,17 +258,16 @@ int ipfs_routing_online_provide(struct IpfsRouting* routing, const unsigned char
while (current != NULL) {
struct PeerEntry* current_peer_entry = (struct PeerEntry*)current->item;
struct Libp2pPeer* current_peer = current_peer_entry->peer;
if (current_peer->is_local) {
// don't bother adding it
} else {
// notify everyone we're connected to
if (current_peer->connection_type == CONNECTION_TYPE_CONNECTED) {
// ignoring results is okay this time
struct Libp2pMessage* rslt = ipfs_routing_online_send_receive_message(current_peer->sessionContext, msg);
if (rslt != NULL)
libp2p_message_free(rslt);
} else if(current_peer->addr_head == NULL
&& strlen(routing->local_node->identity->peer_id) == current_peer->id_size
&& strncmp(routing->local_node->identity->peer_id, current_peer->id, current_peer->id_size) == 0) {
//this is the local node, add it
libp2p_providerstore_add(routing->local_node->providerstore, key, key_size, (unsigned char*)current_peer->id, current_peer->id_size);
}
}
current = current->next;
}
@ -309,7 +305,7 @@ int ipfs_routing_online_ping(struct IpfsRouting* routing, struct Libp2pPeer* pee
msg = NULL;
// connect using a dialer
struct Dialer *dialer = libp2p_conn_dialer_new(routing->local_node->identity->peer_id, libp2p_crypto_rsa_to_private_key(routing->sk));
struct Dialer *dialer = libp2p_conn_dialer_new(routing->local_node->identity->peer->id, libp2p_crypto_rsa_to_private_key(routing->sk));
struct Connection *conn = libp2p_conn_dialer_get_connection(dialer, peer->addr_head->item);
// send the record
conn->write(conn, (char*)protobuf, protobuf_size);
@ -389,7 +385,7 @@ int ipfs_routing_online_get_value (ipfs_routing* routing, const unsigned char *k
for(int i = 0; i < peers->total; i++) {
struct Libp2pPeer* current_peer = libp2p_peerstore_get_or_add_peer(routing->local_node->peerstore, libp2p_utils_vector_get(peers, i));
if (libp2p_peer_matches_id(current_peer, (unsigned char*)routing->local_node->identity->peer_id)) {
if (current_peer->is_local) {
// it's a local fetch. Retrieve it
if (ipfs_routing_generic_get_value(routing, key, key_size, buffer, buffer_size) == 0) {
retVal = 1;
@ -407,7 +403,7 @@ int ipfs_routing_online_get_value (ipfs_routing* routing, const unsigned char *k
// we didn't get the file. Try to connect to the peers we're not connected to, and ask for the file
for(int i = 0; i < peers->total; i++) {
struct Libp2pPeer* current_peer = (struct Libp2pPeer*)libp2p_utils_vector_get(peers, i);
if (libp2p_peer_matches_id(current_peer, (unsigned char*)routing->local_node->identity->peer_id)) {
if (current_peer->is_local) {
// we tried this once, it didn't work. Skip it.
continue;
}

View file

@ -57,7 +57,7 @@ int test_ping() {
remote_peer->addr_head->item = multiaddress_new_from_string("/ip4/192.168.43.234/tcp/4001/");
// connect using a dialer
dialer = libp2p_conn_dialer_new(fs_repo->config->identity->peer_id, libp2p_crypto_rsa_to_private_key(&fs_repo->config->identity->private_key));
dialer = libp2p_conn_dialer_new(fs_repo->config->identity->peer->id, libp2p_crypto_rsa_to_private_key(&fs_repo->config->identity->private_key));
conn = libp2p_conn_dialer_get_connection(dialer, remote_peer->addr_head->item);
//TODO: Dialer should know the protocol

View file

@ -51,7 +51,7 @@ int test_resolver_get() {
// find something where path includes the local node
char path[255];
strcpy(path, "/ipfs/");
strcat(path, fs_repo->config->identity->peer_id);
strcat(path, fs_repo->config->identity->peer->id);
strcat(path, "/QmbMecmXESf96ZNry7hRuzaRkEBhjqXpoYfPCwgFzVGDzB");
result = ipfs_resolver_get(path, NULL, &ipfs_node);
if (result == NULL) {
@ -119,14 +119,14 @@ int test_resolver_remote_get() {
ipfs_repo_fsrepo_open(fs_repo);
// put the server in the peer store and change our peer id so we think it is remote (hack for now)
strcpy(remote_peer_id, fs_repo->config->identity->peer_id);
strcpy(remote_peer_id, fs_repo->config->identity->peer->id);
char multiaddress_string[100];
sprintf(multiaddress_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s", remote_peer_id);
struct MultiAddress* remote_addr = multiaddress_new_from_string(multiaddress_string);
struct Peerstore* peerstore = libp2p_peerstore_new(fs_repo->config->identity->peer_id);
struct Peerstore* peerstore = libp2p_peerstore_new(fs_repo->config->identity->peer);
struct Libp2pPeer* peer = libp2p_peer_new_from_multiaddress(remote_addr);
libp2p_peerstore_add_peer(peerstore, peer);
strcpy(fs_repo->config->identity->peer_id, "QmABCD");
strcpy(fs_repo->config->identity->peer->id, "QmABCD");
struct IpfsNode local_node;
local_node.mode = MODE_ONLINE;

View file

@ -89,7 +89,7 @@ int test_routing_find_peer() {
// We know peer 1, try to find peer 2
local_node.mode = MODE_ONLINE;
local_node.peerstore = libp2p_peerstore_new(fs_repo->config->identity->peer_id);
local_node.peerstore = libp2p_peerstore_new(fs_repo->config->identity->peer);
local_node.providerstore = NULL;
local_node.repo = fs_repo;
local_node.identity = fs_repo->config->identity;
@ -211,8 +211,8 @@ int test_routing_find_providers() {
// We know peer 1, try to find peer 2
local_node.mode = MODE_ONLINE;
local_node.peerstore = libp2p_peerstore_new(fs_repo->config->identity->peer_id);
local_node.providerstore = libp2p_providerstore_new();
local_node.peerstore = libp2p_peerstore_new(fs_repo->config->identity->peer);
local_node.providerstore = libp2p_providerstore_new(fs_repo->config->datastore, fs_repo->config->identity->peer);
local_node.repo = fs_repo;
local_node.identity = fs_repo->config->identity;
local_node.routing = ipfs_routing_new_online(&local_node, &fs_repo->config->identity->private_key);

View file

@ -83,11 +83,11 @@ int test_routing_supernode_get_remote_value() {
ipfs_node->mode = MODE_ONLINE;
ipfs_node->identity = fs_repo->config->identity;
ipfs_node->repo = fs_repo;
ipfs_node->providerstore = libp2p_providerstore_new();
ipfs_node->peerstore = libp2p_peerstore_new(ipfs_node->identity->peer_id);
ipfs_node->providerstore = libp2p_providerstore_new(fs_repo->config->datastore, fs_repo->config->identity->peer);
ipfs_node->peerstore = libp2p_peerstore_new(ipfs_node->identity->peer);
// add the local peer to the peerstore
this_peer.id = fs_repo->config->identity->peer_id;
this_peer.id_size = strlen(fs_repo->config->identity->peer_id);
this_peer.id = fs_repo->config->identity->peer->id;
this_peer.id_size = fs_repo->config->identity->peer->id_size;
this_peer.addr_head = libp2p_utils_linked_list_new();
this_peer.addr_head->item = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/4001");
libp2p_peerstore_add_peer(ipfs_node->peerstore, &this_peer);
@ -184,11 +184,11 @@ int test_routing_supernode_get_value() {
ipfs_node->mode = MODE_ONLINE;
ipfs_node->identity = fs_repo->config->identity;
ipfs_node->repo = fs_repo;
ipfs_node->providerstore = libp2p_providerstore_new();
ipfs_node->peerstore = libp2p_peerstore_new(ipfs_node->identity->peer_id);
ipfs_node->providerstore = libp2p_providerstore_new(fs_repo->config->datastore, fs_repo->config->identity->peer);
ipfs_node->peerstore = libp2p_peerstore_new(ipfs_node->identity->peer);
struct Libp2pPeer this_peer;
this_peer.id = fs_repo->config->identity->peer_id;
this_peer.id_size = strlen(fs_repo->config->identity->peer_id);
this_peer.id = fs_repo->config->identity->peer->id;
this_peer.id_size = fs_repo->config->identity->peer->id_size;
this_peer.addr_head = libp2p_utils_linked_list_new();
this_peer.addr_head->item = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/4001");
libp2p_peerstore_add_peer(ipfs_node->peerstore, &this_peer);