More implementation of ipns

This commit is contained in:
John Jones 2017-09-14 14:58:53 -05:00
parent d5c3e01267
commit 395c7d94cf
4 changed files with 115 additions and 11 deletions

View file

@ -85,7 +85,7 @@ void* ipfs_bitswap_engine_peer_request_processor_start(void* ctx) {
if (current_peer_entry->sessionContext == NULL || current_peer_entry->sessionContext->default_stream == NULL) {
current_peer_entry->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
} else {
libp2p_logger_debug("bitswap_engine", "We're connected to %s. Lets see if there is a message waiting for us.\n", libp2p_peer_id_to_string(current_peer_entry));
//libp2p_logger_debug("bitswap_engine", "We're connected to %s. Lets see if there is a message waiting for us.\n", libp2p_peer_id_to_string(current_peer_entry));
int retVal = current_peer_entry->sessionContext->default_stream->peek(current_peer_entry->sessionContext);
if (retVal < 0) {
libp2p_logger_debug("bitswap_engine", "We thought we were connected, but Peek reported an error.\n");

View file

@ -5,6 +5,12 @@
#include "ipfs/namesys/pb.h"
#include "ipfs/namesys/publisher.h"
/**
* Convert an ipns_entry into a char array
*
* @param entry the ipns_entry
* @returns a char array that contains the data from the ipns_entry
*/
char* ipns_entry_data_for_sig (struct ipns_entry *entry)
{
char *ret;
@ -52,6 +58,14 @@ int ipns_selector_func (int *idx, struct ipns_entry ***recs, char *k, char **val
return ipns_select_record(idx, *recs, vals);
}
/***
* selects an ipns_entry record from a list
*
* @param idx the index of the found record
* @param recs the records
* @param vals the search criteria?
* @returns 0 on success, otherwise error code
*/
int ipns_select_record (int *idx, struct ipns_entry **recs, char **vals)
{
int err, i, best_i = -1, best_seq = 0;
@ -94,8 +108,14 @@ int ipns_select_record (int *idx, struct ipns_entry **recs, char **vals)
return 0;
}
// ipns_validate_ipns_record implements ValidatorFunc and verifies that the
// given 'val' is an IpnsEntry and that that entry is valid.
/****
* implements ValidatorFunc and verifies that the
* given 'val' is an IpnsEntry and that that entry is valid.
*
* @param k
* @param val
* @returns 0 on success, otherwise an error code
*/
int ipns_validate_ipns_record (char *k, char *val)
{
int err = 0;
@ -124,3 +144,88 @@ int ipns_validate_ipns_record (char *k, char *val)
}
return 0;
}
/**
* Helper to copy values from one to another, allocating memory
*
* @param from the value to copy
* @param from_size the size of from
* @param to where to allocate memory and copy
* @param to_size where to put the value of from_size in the new structure
* @returns true(1) on success, false(0) otherwise
*/
int ipfs_namesys_copy_bytes(uint8_t* from, int from_size, uint8_t** to, int* to_size) {
*to = (uint8_t*) malloc(from_size);
if (*to == NULL) {
return 0;
}
memcpy(*to, from, from_size);
*to_size = from_size;
return 1;
}
/**
* Store the hash locally, and notify the network
*
* @param local_node the context
* @param cid the hash
* @returns true(1) on success, false(0) otherwise
*/
int ipfs_namesys_publish(struct IpfsNode* local_node, struct Cid* cid) {
// store locally
struct DatastoreRecord* record = libp2p_datastore_record_new();
if (record == NULL)
return 0;
// key
if (!ipfs_namesys_copy_bytes(cid->hash, cid->hash_length, &record->key, &record->key_size)) {
libp2p_datastore_record_free(record);
return 0;
}
// value
if (!ipfs_namesys_copy_bytes(local_node->identity->peer->id, local_node->identity->peer->id_size, &record->value, &record->value_size)) {
libp2p_datastore_record_free(record);
return 0;
}
if (!local_node->repo->config->datastore->datastore_put(record, local_node->repo->config->datastore)) {
libp2p_datastore_record_free(record);
return 0;
}
libp2p_datastore_record_free(record);
// propagate to network
// build the KademliaMessage
struct KademliaMessage* msg = libp2p_message_new();
if (msg == NULL) {
libp2p_message_free(msg);
return 0;
}
msg->provider_peer_head = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(msg->provider_peer_head, local_node->identity->peer);
// msg->Libp2pRecord
msg->record = libp2p_record_new();
if (msg->record == NULL) {
libp2p_message_free(msg);
return 0;
}
// KademliaMessage->Libp2pRecord->author
if (!ipfs_namesys_copy_bytes(local_node->identity->peer->id, local_node->identity->peer->id_size, &msg->record->author, &msg->record->author_size)) {
libp2p_message_free(msg);
return 0;
}
// KademliaMessage->Libp2pRecord->key
if (!ipfs_namesys_copy_bytes(cid->hash, cid->hash_length, &msg->record->key, &msg->record->key_size)) {
libp2p_message_free(msg);
return 0;
}
// KademliaMessage->Libp2pRecord->value
if (!ipfs_namesys_copy_bytes(local_node->identity->peer->id, local_node->identity->peer->id_size, &msg->record->value, &msg->record->value_size)) {
libp2p_message_free(msg);
return 0;
}
int retVal = libp2p_routing_send_message(local_node->identity->peer, local_node->providerstore, msg);
libp2p_message_free(msg);
return retVal;
}

View file

@ -147,7 +147,6 @@ int ipfs_namesys_hex_string_to_bytes(const unsigned char* hex, unsigned char** b
* @param path the path
* @param name the name (b58 encoded)
* @param depth
* @param depth
* @param prefix
* @param pb
* @returns 0 on success, otherwise error code

View file

@ -38,13 +38,13 @@ int test_routing_put_value() {
struct Libp2pVector* ma_vector = NULL;
// fire up the "publisher"
drop_and_build_respository(ipfs_path_publisher, 4001, NULL, &peer_id_publisher);
drop_and_build_repository(ipfs_path_publisher, 4001, NULL, &peer_id_publisher);
char multiaddress_string[255];
sprintf(multiaddress_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s", peer_id_publisher);
ma_publisher = multiaddress_new_from_string(multiaddress_string);
char* args[] = { "ipfs", "--config", ipfs_path_publisher, "add", "-r", "~/site"};
ipfs_import_files(args, 6);
if (!pthread_create(&thread_publisher, test_routing_daemon_start, (void*)ipfs_path_publisher)) {
ipfs_import_files(6, args);
if (!pthread_create(&thread_publisher, NULL, test_routing_daemon_start, (void*)ipfs_path_publisher)) {
goto exit;
}
publisher_thread_started = 1;
@ -62,7 +62,7 @@ int test_routing_put_value() {
// now "publish" to publisher, and verify that "consumer" receives the message
char* args2[] = {"ipfs" "--config", ipfs_path_publisher, "name", "publish", "QmZtAEqmnXMZkwVPKdyMGxUoo35cQMzNhmq6CN3DvgRwAD" };
ipfs_name_publish(args2, 6);
ipfs_name_publish(6, args2);
// wait for everything to settle in
sleep(3);
@ -70,16 +70,16 @@ int test_routing_put_value() {
// see if we have what we should...
char* args3[] = {"ipfs", "--config", ipfs_path_consumer, "resolve", peer_id_publisher};
char* results = NULL;
ipfs_resolve(args3, 5, &results);
ipfs_resolve(5, args3, &results);
retVal = 1;
exit:
ipfs_daemon_stop();
if (publisher_thread_started) {
pthread_join(thread_publisher);
pthread_join(thread_publisher, NULL);
}
if (consumer_thread_started) {
pthread_join(thread_consumer);
pthread_join(thread_consumer, NULL);
}
multiaddress_free(ma_publisher);
return retVal;