2017-07-26 14:48:04 +00:00
|
|
|
#include <pthread.h>
|
2017-08-03 22:46:20 +00:00
|
|
|
#include <signal.h>
|
2017-07-26 14:48:04 +00:00
|
|
|
#include "../test_helper.h"
|
|
|
|
#include "../routing/test_routing.h" // for test_routing_daemon_start
|
2017-07-20 21:10:31 +00:00
|
|
|
#include "libp2p/utils/vector.h"
|
2017-07-26 14:48:04 +00:00
|
|
|
#include "libp2p/utils/logger.h"
|
2017-10-09 14:00:48 +00:00
|
|
|
#include "ipfs/merkledag/merkledag.h" // for block to node conversion
|
2017-07-26 14:48:04 +00:00
|
|
|
#include "ipfs/exchange/bitswap/bitswap.h"
|
|
|
|
#include "ipfs/exchange/bitswap/message.h"
|
|
|
|
#include "ipfs/importer/importer.h"
|
2017-07-20 21:10:31 +00:00
|
|
|
|
|
|
|
uint8_t* generate_bytes(size_t size) {
|
|
|
|
uint8_t* buffer = (uint8_t*) malloc(size);
|
|
|
|
for(size_t i = 0; i < size; i++) {
|
|
|
|
buffer[i] = i % 255;
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare_generated_bytes(uint8_t* buffer, size_t size) {
|
|
|
|
for(size_t i = 0; i < size; i++) {
|
|
|
|
if (buffer[i] != (i % 255)) {
|
|
|
|
fprintf(stderr, "compare_generated_bytes: Mismatch in position %lu", i);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:16:59 +00:00
|
|
|
/**
|
|
|
|
* Test the protobufing of a BitswapMessage
|
|
|
|
* this should be run with valgrind
|
|
|
|
*/
|
|
|
|
int test_bitswap_new_free() {
|
|
|
|
int retVal = 0;
|
2017-07-20 21:10:31 +00:00
|
|
|
struct BitswapMessage* message = NULL;
|
|
|
|
struct WantlistEntry* wantlist_entry = NULL;
|
|
|
|
struct Block* block = NULL;
|
2017-07-20 20:16:59 +00:00
|
|
|
|
2017-07-20 21:10:31 +00:00
|
|
|
// Test 1, create a simple BitswapMessage
|
|
|
|
message = ipfs_bitswap_message_new();
|
|
|
|
ipfs_bitswap_message_free(message);
|
|
|
|
message = NULL;
|
|
|
|
|
|
|
|
// Test 2, okay, that worked, now make one more complicated
|
|
|
|
message = ipfs_bitswap_message_new();
|
|
|
|
message->wantlist = ipfs_bitswap_wantlist_new();
|
2017-07-20 20:16:59 +00:00
|
|
|
ipfs_bitswap_message_free(message);
|
2017-07-20 21:10:31 +00:00
|
|
|
message = NULL;
|
|
|
|
|
|
|
|
// Test 3, now add some more
|
|
|
|
message = ipfs_bitswap_message_new();
|
|
|
|
// wantlist
|
|
|
|
message->wantlist = ipfs_bitswap_wantlist_new();
|
|
|
|
wantlist_entry = ipfs_bitswap_wantlist_entry_new();
|
|
|
|
wantlist_entry->priority = 24;
|
|
|
|
message->wantlist->entries = libp2p_utils_vector_new(1);
|
|
|
|
libp2p_utils_vector_add(message->wantlist->entries, wantlist_entry);
|
|
|
|
wantlist_entry = NULL;
|
2017-07-26 12:38:47 +00:00
|
|
|
wantlist_entry = (struct WantlistEntry*)libp2p_utils_vector_get(message->wantlist->entries, 0);
|
2017-07-20 21:10:31 +00:00
|
|
|
if (wantlist_entry == NULL) {
|
|
|
|
fprintf(stderr, "Vector didn't have entry\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (wantlist_entry->priority != 24) {
|
|
|
|
fprintf(stderr, "Unable to retrieve item from vector after an external free\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
// payload
|
|
|
|
message->payload = libp2p_utils_vector_new(1);
|
2017-07-27 17:05:41 +00:00
|
|
|
block = ipfs_block_new();
|
2017-07-20 21:10:31 +00:00
|
|
|
block->data_length = 25;
|
|
|
|
libp2p_utils_vector_add(message->payload, block);
|
2017-07-26 12:38:47 +00:00
|
|
|
block = (struct Block*)libp2p_utils_vector_get(message->payload, 0);
|
2017-07-20 21:10:31 +00:00
|
|
|
if (block == NULL) {
|
|
|
|
fprintf(stderr, "Vector didn't have payload entry\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (block->data_length != 25) {
|
|
|
|
fprintf(stderr, "Unable to retrieve block->data_length\n");
|
|
|
|
}
|
2017-07-20 20:16:59 +00:00
|
|
|
|
2017-07-20 21:10:31 +00:00
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
|
|
|
|
if (message != NULL)
|
|
|
|
{
|
|
|
|
ipfs_bitswap_message_free(message);
|
|
|
|
}
|
2017-07-20 20:16:59 +00:00
|
|
|
|
2017-07-20 21:10:31 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_bitswap_protobuf() {
|
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
// create a complicated BitswapMessage
|
|
|
|
// first the basics...
|
|
|
|
struct BitswapMessage* message = ipfs_bitswap_message_new();
|
|
|
|
// add a WantList
|
|
|
|
struct BitswapWantlist* want_list = ipfs_bitswap_wantlist_new();
|
|
|
|
message->wantlist = want_list;
|
|
|
|
// add something to the WantList
|
|
|
|
message->wantlist->entries = libp2p_utils_vector_new(1);
|
|
|
|
struct WantlistEntry* wantlist_entry = ipfs_bitswap_wantlist_entry_new();
|
|
|
|
wantlist_entry->block_size = 1;
|
|
|
|
wantlist_entry->priority = 100;
|
|
|
|
wantlist_entry->block = generate_bytes(100);
|
|
|
|
wantlist_entry->block_size = 100;
|
|
|
|
libp2p_utils_vector_add(message->wantlist->entries, wantlist_entry);
|
|
|
|
message->wantlist->full = 1;
|
2017-07-20 20:16:59 +00:00
|
|
|
|
|
|
|
retVal = 1;
|
|
|
|
return retVal;
|
|
|
|
}
|
2017-07-26 14:48:04 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Put a file in ipfs and attempt to retrieve it using bitswap's Exchange interface
|
|
|
|
*/
|
|
|
|
int test_bitswap_retrieve_file()
|
|
|
|
{
|
|
|
|
int retVal = 0;
|
|
|
|
struct IpfsNode* localNode = NULL;
|
|
|
|
const char* ipfs_path = "/tmp/ipfstest1";
|
|
|
|
struct HashtableNode* node = NULL; // the node created by adding the file
|
|
|
|
size_t bytes_written = 0;
|
|
|
|
struct Block* block = NULL;
|
|
|
|
struct Cid* cid = NULL;
|
|
|
|
|
|
|
|
// build and open the new IPFS repository with no bootstrap peers
|
|
|
|
drop_and_build_repository(ipfs_path, 4001, NULL, NULL);
|
2017-10-09 14:00:48 +00:00
|
|
|
ipfs_node_offline_new(ipfs_path, &localNode);
|
2017-07-26 14:48:04 +00:00
|
|
|
|
|
|
|
// add a file
|
2017-10-09 14:00:48 +00:00
|
|
|
unsigned char bytes[256];
|
|
|
|
create_bytes(&bytes[0], 256);
|
|
|
|
create_file("/tmp/test_file.bin", &bytes[0], 256);
|
|
|
|
ipfs_import_file(NULL, "/tmp/test_file.bin", &node, localNode, &bytes_written, 0);
|
2017-07-26 14:48:04 +00:00
|
|
|
|
|
|
|
// build the Cid from the node information
|
2017-09-04 18:33:56 +00:00
|
|
|
cid = ipfs_cid_new(0, node->hash, node->hash_size, CID_DAG_PROTOBUF);
|
2017-07-26 14:48:04 +00:00
|
|
|
if (cid == NULL)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
// attempt to retrieve the file
|
2017-07-27 19:33:19 +00:00
|
|
|
if (!localNode->exchange->GetBlock(localNode->exchange, cid, &block)) {
|
2017-07-26 14:48:04 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
// cleanup
|
|
|
|
if (block != NULL)
|
|
|
|
ipfs_block_free(block);
|
|
|
|
if (cid != NULL)
|
|
|
|
ipfs_cid_free(cid);
|
|
|
|
if (node != NULL)
|
|
|
|
ipfs_hashtable_node_free(node);
|
2017-09-25 13:55:42 +00:00
|
|
|
ipfs_node_free(localNode);
|
2017-07-26 14:48:04 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:01:06 +00:00
|
|
|
/***
|
|
|
|
* Attempt to retrieve a file from a known node
|
|
|
|
*/
|
|
|
|
int test_bitswap_retrieve_file_remote() {
|
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
libp2p_logger_add_class("dht_protocol");
|
|
|
|
libp2p_logger_add_class("providerstore");
|
|
|
|
libp2p_logger_add_class("peerstore");
|
|
|
|
libp2p_logger_add_class("exporter");
|
|
|
|
libp2p_logger_add_class("peer");
|
|
|
|
*/
|
|
|
|
libp2p_logger_add_class("test_bitswap");
|
|
|
|
libp2p_logger_add_class("null");
|
|
|
|
libp2p_logger_add_class("online");
|
|
|
|
libp2p_logger_add_class("multistream");
|
|
|
|
|
|
|
|
// clean out repository
|
|
|
|
char* ipfs_path = "/tmp/test1";
|
|
|
|
char* peer_id_1 = NULL, *peer_id_2 = NULL;
|
|
|
|
struct IpfsNode* ipfs_node1 = NULL, *ipfs_node2 = NULL;
|
|
|
|
pthread_t thread1;
|
|
|
|
int thread1_started = 0;
|
|
|
|
struct MultiAddress* ma_peer1 = NULL;
|
|
|
|
struct Libp2pVector* ma_vector2 = NULL;
|
2017-10-09 14:00:48 +00:00
|
|
|
struct HashtableNode* node = NULL; // the file that was put in
|
|
|
|
struct Block* result = NULL; // the block that comes back
|
|
|
|
struct HashtableNode* result_node = NULL; // the HashtableNode of the block that comes back
|
2017-07-31 15:01:06 +00:00
|
|
|
struct Cid* cid = NULL;
|
|
|
|
|
|
|
|
// create peer 1
|
|
|
|
libp2p_logger_debug("test_bitswap", "Firing up daemon 1.\n");
|
|
|
|
drop_and_build_repository(ipfs_path, 4001, NULL, &peer_id_1);
|
|
|
|
char multiaddress_string[255];
|
|
|
|
sprintf(multiaddress_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s", peer_id_1);
|
|
|
|
ma_peer1 = multiaddress_new_from_string(multiaddress_string);
|
|
|
|
// add a file
|
|
|
|
size_t bytes_written = 0;
|
2017-10-09 14:00:48 +00:00
|
|
|
ipfs_node_offline_new(ipfs_path, &ipfs_node1);
|
|
|
|
unsigned char bytes[256];
|
|
|
|
create_bytes(bytes, 256);
|
|
|
|
create_file("/tmp/hello.bin", bytes, 256);
|
|
|
|
ipfs_import_file(NULL, "/tmp/hello.bin", &node, ipfs_node1, &bytes_written, 0);
|
2017-07-31 15:01:06 +00:00
|
|
|
// start the daemon in a separate thread
|
2017-09-19 00:04:52 +00:00
|
|
|
if (pthread_create(&thread1, NULL, test_daemon_start, (void*)ipfs_path) < 0) {
|
2017-07-31 15:01:06 +00:00
|
|
|
libp2p_logger_error("test_bitswap", "Unable to start thread 1\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
thread1_started = 1;
|
|
|
|
// wait for everything to start up
|
|
|
|
sleep(3);
|
|
|
|
|
|
|
|
// create my peer, peer 2
|
2017-10-09 14:00:48 +00:00
|
|
|
libp2p_logger_debug("test_bitswap", "Firing up the client\n");
|
2017-07-31 15:01:06 +00:00
|
|
|
ipfs_path = "/tmp/test2";
|
|
|
|
ma_peer1 = multiaddress_new_from_string(multiaddress_string);
|
|
|
|
ma_vector2 = libp2p_utils_vector_new(1);
|
|
|
|
libp2p_utils_vector_add(ma_vector2, ma_peer1);
|
|
|
|
drop_and_build_repository(ipfs_path, 4002, ma_vector2, &peer_id_2);
|
|
|
|
multiaddress_free(ma_peer1);
|
2017-10-09 14:00:48 +00:00
|
|
|
ipfs_node_offline_new(ipfs_path, &ipfs_node2);
|
2017-07-31 15:01:06 +00:00
|
|
|
ipfs_node2->routing->Bootstrap(ipfs_node2->routing);
|
|
|
|
|
|
|
|
// this does the heavy lifting...
|
2017-09-04 18:33:56 +00:00
|
|
|
cid = ipfs_cid_new(0, node->hash, node->hash_size, CID_DAG_PROTOBUF);
|
2017-07-31 15:01:06 +00:00
|
|
|
if (!ipfs_node2->exchange->GetBlock(ipfs_node2->exchange, cid, &result)) {
|
2017-10-09 14:00:48 +00:00
|
|
|
libp2p_logger_error("test_bitswap", "GetBlock returned false\n");
|
|
|
|
goto exit;
|
2017-07-31 15:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node->hash_size != result->cid->hash_length) {
|
2017-10-09 14:00:48 +00:00
|
|
|
libp2p_logger_error("test_bitswap", "Node hash sizes do not match. Should be %lu but is %lu\n", node->hash_size, result->cid->hash_length);
|
|
|
|
goto exit;
|
2017-07-31 15:01:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 14:00:48 +00:00
|
|
|
// to compare sizes, we must convert the block back to a hashtable node
|
|
|
|
ipfs_merkledag_convert_block_to_node(result, &result_node);
|
|
|
|
if (node->data_size != result_node->data_size) {
|
|
|
|
libp2p_logger_error("test_bitswap", "Result sizes do not match. Should be %lu but is %lu\n", node->data_size, result->data_length);
|
|
|
|
goto exit;
|
2017-07-31 15:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
ipfs_daemon_stop();
|
|
|
|
if (thread1_started)
|
|
|
|
pthread_join(thread1, NULL);
|
|
|
|
if (peer_id_1 != NULL)
|
|
|
|
free(peer_id_1);
|
|
|
|
if (peer_id_2 != NULL)
|
|
|
|
free(peer_id_2);
|
|
|
|
if (ma_vector2 != NULL) {
|
|
|
|
libp2p_utils_vector_free(ma_vector2);
|
|
|
|
}
|
|
|
|
if (node != NULL)
|
|
|
|
ipfs_hashtable_node_free(node);
|
2017-10-09 14:00:48 +00:00
|
|
|
if (result_node != NULL)
|
|
|
|
ipfs_hashtable_node_free(result_node);
|
2017-07-31 15:01:06 +00:00
|
|
|
if (result != NULL)
|
|
|
|
ipfs_block_free(result);
|
|
|
|
if (cid != NULL)
|
|
|
|
ipfs_cid_free(cid);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Attempt to retrieve a file from a known node
|
|
|
|
*/
|
|
|
|
int test_bitswap_retrieve_file_known_remote() {
|
|
|
|
int retVal = 0;
|
2017-08-03 22:46:20 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2017-07-31 15:01:06 +00:00
|
|
|
/***
|
|
|
|
* This assumes a remote server with the hello_world.txt file already in its database
|
|
|
|
*/
|
|
|
|
int remote_port = 4001;
|
2017-08-03 19:35:06 +00:00
|
|
|
// mac
|
2017-08-31 21:41:10 +00:00
|
|
|
char* remote_peer_id = "QmPTDqsHMn3GkLBUHBofB5M6wBGzLwHAxqZpLvcda6hF6Q";
|
2017-08-03 22:46:20 +00:00
|
|
|
char* remote_ip = "10.211.55.2";
|
2017-08-03 19:35:06 +00:00
|
|
|
// linux
|
2017-08-03 22:46:20 +00:00
|
|
|
//char* remote_peer_id = "QmRKm1d9kSCRpMFtLYpfhhCQ3DKuSSPJa3qn9wWXfwnWnY";
|
|
|
|
//char* remote_ip = "10.211.55.4";
|
2017-07-31 15:01:06 +00:00
|
|
|
char* hello_world_hash = "QmTUFTVgkHT3Qdd9ospVjSLi2upd6VdkeNXZQH66cVmzja";
|
|
|
|
|
|
|
|
/*
|
|
|
|
libp2p_logger_add_class("dht_protocol");
|
|
|
|
libp2p_logger_add_class("providerstore");
|
|
|
|
libp2p_logger_add_class("peerstore");
|
|
|
|
libp2p_logger_add_class("exporter");
|
|
|
|
libp2p_logger_add_class("peer");
|
|
|
|
*/
|
|
|
|
libp2p_logger_add_class("test_bitswap");
|
|
|
|
libp2p_logger_add_class("null");
|
|
|
|
libp2p_logger_add_class("online");
|
|
|
|
libp2p_logger_add_class("multistream");
|
2017-08-09 17:26:26 +00:00
|
|
|
libp2p_logger_add_class("secio");
|
2017-08-31 21:41:10 +00:00
|
|
|
libp2p_logger_add_class("bitswap");
|
|
|
|
libp2p_logger_add_class("bitswap_engine");
|
|
|
|
libp2p_logger_add_class("bitswap_network");
|
2017-07-31 15:01:06 +00:00
|
|
|
|
|
|
|
char* ipfs_path = "/tmp/test1";
|
|
|
|
char* peer_id_1 = NULL, *peer_id_2 = NULL;
|
|
|
|
struct IpfsNode* ipfs_node2 = NULL;
|
|
|
|
struct MultiAddress* ma_peer1 = NULL;
|
|
|
|
struct Libp2pVector* ma_vector2 = NULL;
|
|
|
|
struct Block* result = NULL;
|
|
|
|
struct Cid* cid = NULL;
|
|
|
|
|
|
|
|
// create peer 1
|
|
|
|
char multiaddress_string[255];
|
|
|
|
sprintf(multiaddress_string, "/ip4/%s/tcp/%d/ipfs/%s", remote_ip, remote_port, remote_peer_id);
|
|
|
|
ma_peer1 = multiaddress_new_from_string(multiaddress_string);
|
|
|
|
|
|
|
|
// create my peer, peer 2
|
|
|
|
libp2p_logger_debug("test_routing", "Firing up the client\n");
|
|
|
|
ipfs_path = "/tmp/test2";
|
|
|
|
ma_vector2 = libp2p_utils_vector_new(1);
|
|
|
|
libp2p_utils_vector_add(ma_vector2, ma_peer1);
|
|
|
|
drop_and_build_repository(ipfs_path, 4002, ma_vector2, &peer_id_2);
|
|
|
|
multiaddress_free(ma_peer1);
|
2017-09-25 13:55:42 +00:00
|
|
|
ipfs_node_online_new(ipfs_path, &ipfs_node2);
|
2017-07-31 15:01:06 +00:00
|
|
|
|
2017-07-31 18:32:09 +00:00
|
|
|
if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hello_world_hash, strlen(hello_world_hash), &cid))
|
2017-08-03 19:35:06 +00:00
|
|
|
goto exit;
|
2017-07-31 18:32:09 +00:00
|
|
|
|
2017-09-25 20:11:44 +00:00
|
|
|
sleep(3);
|
2017-08-31 21:41:10 +00:00
|
|
|
|
2017-07-31 15:01:06 +00:00
|
|
|
// this does the heavy lifting...
|
|
|
|
if (!ipfs_node2->exchange->GetBlock(ipfs_node2->exchange, cid, &result)) {
|
|
|
|
libp2p_logger_error("test_bitswap", "GetBlock returned false\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2017-08-03 22:46:20 +00:00
|
|
|
if (result == NULL) {
|
|
|
|
libp2p_logger_error("test_bitswap", "GetBlock returned NULL");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result->cid == NULL) {
|
|
|
|
libp2p_logger_error("test_bitswap", "GetBlock returned an object with no CID");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2017-08-02 16:43:48 +00:00
|
|
|
if (cid->hash_length != result->cid->hash_length) {
|
2017-07-31 15:01:06 +00:00
|
|
|
libp2p_logger_error("test_bitswap", "Node hash sizes do not match. Should be %lu but is %lu\n", strlen(hello_world_hash), result->cid->hash_length);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
if (peer_id_1 != NULL)
|
|
|
|
free(peer_id_1);
|
|
|
|
if (peer_id_2 != NULL)
|
|
|
|
free(peer_id_2);
|
|
|
|
if (ma_vector2 != NULL) {
|
|
|
|
libp2p_utils_vector_free(ma_vector2);
|
|
|
|
}
|
2017-08-09 01:40:35 +00:00
|
|
|
// this is freed by ipfs_node_free
|
|
|
|
//if (result != NULL)
|
|
|
|
// ipfs_block_free(result);
|
2017-07-31 15:01:06 +00:00
|
|
|
if (cid != NULL)
|
|
|
|
ipfs_cid_free(cid);
|
2017-09-25 13:55:42 +00:00
|
|
|
ipfs_node_free(ipfs_node2);
|
2017-07-31 15:01:06 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-26 14:48:04 +00:00
|
|
|
/***
|
|
|
|
* Attempt to retrieve a file from a previously unknown node
|
|
|
|
*/
|
|
|
|
int test_bitswap_retrieve_file_third_party() {
|
|
|
|
int retVal = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
libp2p_logger_add_class("dht_protocol");
|
|
|
|
libp2p_logger_add_class("providerstore");
|
|
|
|
libp2p_logger_add_class("peerstore");
|
|
|
|
libp2p_logger_add_class("exporter");
|
|
|
|
*/
|
2017-10-09 15:01:29 +00:00
|
|
|
libp2p_logger_add_class("peer");
|
|
|
|
libp2p_logger_add_class("bitswap_network");
|
2017-07-27 17:05:41 +00:00
|
|
|
libp2p_logger_add_class("test_bitswap");
|
2017-07-31 11:43:15 +00:00
|
|
|
libp2p_logger_add_class("null");
|
|
|
|
libp2p_logger_add_class("online");
|
|
|
|
libp2p_logger_add_class("multistream");
|
2017-07-26 14:48:04 +00:00
|
|
|
|
|
|
|
// clean out repository
|
|
|
|
char* ipfs_path = "/tmp/test1";
|
|
|
|
char* peer_id_1 = NULL, *peer_id_2 = NULL, *peer_id_3 = NULL;
|
|
|
|
struct IpfsNode* ipfs_node2 = NULL, *ipfs_node3 = NULL;
|
|
|
|
pthread_t thread1, thread2;
|
|
|
|
int thread1_started = 0, thread2_started = 0;
|
|
|
|
struct MultiAddress* ma_peer1 = NULL;
|
|
|
|
struct Libp2pVector* ma_vector2 = NULL, *ma_vector3 = NULL;
|
2017-07-27 17:05:41 +00:00
|
|
|
struct HashtableNode* node = NULL;
|
|
|
|
struct Block* result = NULL;
|
2017-10-09 15:01:29 +00:00
|
|
|
struct HashtableNode* result_node = NULL;
|
2017-07-27 17:05:41 +00:00
|
|
|
struct Cid* cid = NULL;
|
2017-07-26 14:48:04 +00:00
|
|
|
|
|
|
|
// create peer 1
|
2017-07-27 17:05:41 +00:00
|
|
|
libp2p_logger_debug("test_bitswap", "Firing up daemon 1.\n");
|
2017-07-26 14:48:04 +00:00
|
|
|
drop_and_build_repository(ipfs_path, 4001, NULL, &peer_id_1);
|
|
|
|
char multiaddress_string[255];
|
|
|
|
sprintf(multiaddress_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s", peer_id_1);
|
|
|
|
ma_peer1 = multiaddress_new_from_string(multiaddress_string);
|
|
|
|
// start the daemon in a separate thread
|
2017-09-19 00:04:52 +00:00
|
|
|
if (pthread_create(&thread1, NULL, test_daemon_start, (void*)ipfs_path) < 0) {
|
2017-07-27 17:05:41 +00:00
|
|
|
libp2p_logger_error("test_bitswap", "Unable to start thread 1\n");
|
2017-07-26 14:48:04 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
thread1_started = 1;
|
|
|
|
// wait for everything to start up
|
|
|
|
sleep(3);
|
|
|
|
|
|
|
|
// create peer 2
|
2017-07-27 17:05:41 +00:00
|
|
|
libp2p_logger_debug("test_routing", "Firing up daemon 2.\n");
|
2017-07-26 14:48:04 +00:00
|
|
|
ipfs_path = "/tmp/test2";
|
|
|
|
// create a vector to hold peer1's multiaddress so we can connect as a peer
|
|
|
|
ma_vector2 = libp2p_utils_vector_new(1);
|
|
|
|
libp2p_utils_vector_add(ma_vector2, ma_peer1);
|
|
|
|
// note: this destroys some things, as it frees the fs_repo_3:
|
|
|
|
drop_and_build_repository(ipfs_path, 4002, ma_vector2, &peer_id_2);
|
|
|
|
multiaddress_free(ma_peer1);
|
|
|
|
// add a file, to prime the connection to peer 1
|
|
|
|
//TODO: Find a better way to do this...
|
|
|
|
size_t bytes_written = 0;
|
2017-09-25 13:55:42 +00:00
|
|
|
if (!ipfs_node_online_new(ipfs_path, &ipfs_node2))
|
2017-07-26 14:48:04 +00:00
|
|
|
goto exit;
|
|
|
|
ipfs_node2->routing->Bootstrap(ipfs_node2->routing);
|
2017-10-09 14:00:48 +00:00
|
|
|
unsigned char bytes[256];
|
|
|
|
create_bytes(&bytes[0], 256);
|
|
|
|
create_file("/tmp/hello.bin", &bytes[0], 256);
|
|
|
|
ipfs_import_file(NULL, "/tmp/hello.bin", &node, ipfs_node2, &bytes_written, 0);
|
2017-09-25 13:55:42 +00:00
|
|
|
ipfs_node_free(ipfs_node2);
|
2017-07-26 14:48:04 +00:00
|
|
|
// start the daemon in a separate thread
|
2017-09-19 00:04:52 +00:00
|
|
|
if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) {
|
2017-07-27 17:05:41 +00:00
|
|
|
libp2p_logger_error("test_bitswap", "Unable to start thread 2\n");
|
2017-07-26 14:48:04 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
thread2_started = 1;
|
|
|
|
// wait for everything to start up
|
|
|
|
sleep(3);
|
|
|
|
|
|
|
|
// create my peer, peer 3
|
2017-07-27 17:05:41 +00:00
|
|
|
libp2p_logger_debug("test_routing", "Firing up the 3rd client\n");
|
2017-07-26 14:48:04 +00:00
|
|
|
ipfs_path = "/tmp/test3";
|
|
|
|
ma_peer1 = multiaddress_new_from_string(multiaddress_string);
|
|
|
|
ma_vector3 = libp2p_utils_vector_new(1);
|
|
|
|
libp2p_utils_vector_add(ma_vector3, ma_peer1);
|
|
|
|
drop_and_build_repository(ipfs_path, 4003, ma_vector3, &peer_id_3);
|
|
|
|
multiaddress_free(ma_peer1);
|
2017-10-09 14:00:48 +00:00
|
|
|
ipfs_node_offline_new(ipfs_path, &ipfs_node3);
|
2017-07-26 14:48:04 +00:00
|
|
|
|
|
|
|
ipfs_node3->routing->Bootstrap(ipfs_node3->routing);
|
|
|
|
|
2017-07-27 17:05:41 +00:00
|
|
|
// this does the heavy lifting...
|
2017-09-04 18:33:56 +00:00
|
|
|
cid = ipfs_cid_new(0, node->hash, node->hash_size, CID_DAG_PROTOBUF);
|
2017-07-27 17:05:41 +00:00
|
|
|
if (!ipfs_node3->exchange->GetBlock(ipfs_node3->exchange, cid, &result)) {
|
|
|
|
libp2p_logger_error("test_bitswap", "GetBlock returned false\n");
|
2017-07-26 14:48:04 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2017-07-27 17:05:41 +00:00
|
|
|
if (node->hash_size != result->cid->hash_length) {
|
|
|
|
libp2p_logger_error("test_bitswap", "Node hash sizes do not match. Should be %lu but is %lu\n", node->hash_size, result->cid->hash_length);
|
2017-07-26 14:48:04 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2017-10-09 15:01:29 +00:00
|
|
|
ipfs_merkledag_convert_block_to_node(result, &result_node);
|
|
|
|
if (node->data_size != result_node->data_size) {
|
2017-07-27 17:05:41 +00:00
|
|
|
libp2p_logger_error("test_bitswap", "Result sizes do not match. Should be %lu but is %lu\n", node->data_size, result->data_length);
|
2017-07-26 14:48:04 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
ipfs_daemon_stop();
|
|
|
|
if (thread1_started)
|
|
|
|
pthread_join(thread1, NULL);
|
|
|
|
if (thread2_started)
|
|
|
|
pthread_join(thread2, NULL);
|
|
|
|
if (ipfs_node3 != NULL)
|
2017-09-25 13:55:42 +00:00
|
|
|
ipfs_node_free(ipfs_node3);
|
2017-07-26 14:48:04 +00:00
|
|
|
if (peer_id_1 != NULL)
|
|
|
|
free(peer_id_1);
|
|
|
|
if (peer_id_2 != NULL)
|
|
|
|
free(peer_id_2);
|
|
|
|
if (peer_id_3 != NULL)
|
|
|
|
free(peer_id_3);
|
|
|
|
if (ma_vector2 != NULL) {
|
|
|
|
libp2p_utils_vector_free(ma_vector2);
|
|
|
|
}
|
|
|
|
if (ma_vector3 != NULL) {
|
|
|
|
libp2p_utils_vector_free(ma_vector3);
|
|
|
|
}
|
|
|
|
if (node != NULL)
|
|
|
|
ipfs_hashtable_node_free(node);
|
2017-07-27 17:05:41 +00:00
|
|
|
if (result != NULL)
|
|
|
|
ipfs_block_free(result);
|
2017-10-09 15:01:29 +00:00
|
|
|
if (result_node != NULL)
|
|
|
|
ipfs_hashtable_node_free(result_node);
|
2017-07-27 17:05:41 +00:00
|
|
|
if (cid != NULL)
|
|
|
|
ipfs_cid_free(cid);
|
2017-07-26 14:48:04 +00:00
|
|
|
return retVal;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-04 11:34:37 +00:00
|
|
|
/***
|
|
|
|
* Attempt to retrieve a file from a known node
|
|
|
|
*/
|
|
|
|
int test_bitswap_retrieve_file_go_remote() {
|
|
|
|
int retVal = 0;
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
/***
|
|
|
|
* This assumes a remote server with the hello_world.txt file already in its database
|
|
|
|
*/
|
|
|
|
int remote_port = 4001;
|
|
|
|
// mac
|
|
|
|
char* remote_peer_id = "QmNgGtj3Dk7RPRFgyp9exmq2WjaUf3gkwhMF1vkmcyXcBD";
|
|
|
|
char* remote_ip = "10.211.55.2";
|
|
|
|
// linux
|
|
|
|
//char* remote_peer_id = "QmRKm1d9kSCRpMFtLYpfhhCQ3DKuSSPJa3qn9wWXfwnWnY";
|
|
|
|
//char* remote_ip = "10.211.55.4";
|
|
|
|
char* hello_world_hash = "QmYAXgX8ARiriupMQsbGXtKdDyGzWry1YV3sycKw1qqmgH";
|
|
|
|
|
|
|
|
/*
|
|
|
|
libp2p_logger_add_class("dht_protocol");
|
|
|
|
libp2p_logger_add_class("providerstore");
|
|
|
|
libp2p_logger_add_class("peerstore");
|
|
|
|
libp2p_logger_add_class("exporter");
|
|
|
|
libp2p_logger_add_class("peer");
|
|
|
|
*/
|
|
|
|
libp2p_logger_add_class("test_bitswap");
|
|
|
|
libp2p_logger_add_class("null");
|
|
|
|
libp2p_logger_add_class("online");
|
|
|
|
libp2p_logger_add_class("multistream");
|
|
|
|
libp2p_logger_add_class("secio");
|
|
|
|
libp2p_logger_add_class("bitswap");
|
|
|
|
libp2p_logger_add_class("bitswap_engine");
|
|
|
|
libp2p_logger_add_class("bitswap_network");
|
|
|
|
|
|
|
|
char* ipfs_path = "/tmp/test1";
|
|
|
|
char peer_ma_1[80] = "", *peer_id_2 = NULL;
|
|
|
|
struct IpfsNode* ipfs_node2 = NULL;
|
|
|
|
struct MultiAddress* ma_peer1 = NULL;
|
|
|
|
struct Libp2pVector* ma_vector2 = NULL;
|
|
|
|
struct Block* result = NULL;
|
|
|
|
struct Cid* cid = NULL;
|
|
|
|
|
|
|
|
// create a MultiAddress of the GO peer (peer 1)
|
|
|
|
sprintf(peer_ma_1, "/ip4/%s/tcp/%d/ipfs/%s", remote_ip, remote_port, remote_peer_id);
|
|
|
|
ma_peer1 = multiaddress_new_from_string(peer_ma_1);
|
|
|
|
|
|
|
|
// create my peer, peer 2
|
|
|
|
libp2p_logger_debug("test_routing", "Firing up the client\n");
|
|
|
|
ipfs_path = "/tmp/test2";
|
|
|
|
ma_vector2 = libp2p_utils_vector_new(1);
|
|
|
|
libp2p_utils_vector_add(ma_vector2, ma_peer1);
|
|
|
|
drop_and_build_repository(ipfs_path, 4002, ma_vector2, &peer_id_2);
|
|
|
|
multiaddress_free(ma_peer1);
|
|
|
|
ipfs_node_online_new(ipfs_path, &ipfs_node2);
|
|
|
|
|
|
|
|
if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hello_world_hash, strlen(hello_world_hash), &cid))
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
|
|
// this does the heavy lifting...
|
|
|
|
if (!ipfs_node2->exchange->GetBlock(ipfs_node2->exchange, cid, &result)) {
|
|
|
|
libp2p_logger_error("test_bitswap", "GetBlock returned false\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
libp2p_logger_error("test_bitswap", "GetBlock returned NULL");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result->cid == NULL) {
|
|
|
|
libp2p_logger_error("test_bitswap", "GetBlock returned an object with no CID");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cid->hash_length != result->cid->hash_length) {
|
|
|
|
libp2p_logger_error("test_bitswap", "Node hash sizes do not match. Should be %lu but is %lu\n", strlen(hello_world_hash), result->cid->hash_length);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
if (peer_id_2 != NULL)
|
|
|
|
free(peer_id_2);
|
|
|
|
if (ma_vector2 != NULL) {
|
|
|
|
libp2p_utils_vector_free(ma_vector2);
|
|
|
|
}
|
|
|
|
if (cid != NULL)
|
|
|
|
ipfs_cid_free(cid);
|
|
|
|
ipfs_node_free(ipfs_node2);
|
|
|
|
if (ma_peer1 != NULL)
|
|
|
|
multiaddress_free(ma_peer1);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Attempt to retrieve a file from a known node
|
|
|
|
*/
|
|
|
|
int test_bitswap_serve_file_go_remote() {
|
|
|
|
int retVal = 0;
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
/***
|
|
|
|
* This assumes a remote client asking for hello.txt
|
|
|
|
*/
|
|
|
|
// linux
|
|
|
|
//char* remote_peer_id = "QmRKm1d9kSCRpMFtLYpfhhCQ3DKuSSPJa3qn9wWXfwnWnY";
|
|
|
|
//char* remote_ip = "10.211.55.4";
|
|
|
|
//char* hello_world_hash = "QmYAXgX8ARiriupMQsbGXtKdDyGzWry1YV3sycKw1qqmgH";
|
|
|
|
|
|
|
|
/*
|
|
|
|
libp2p_logger_add_class("dht_protocol");
|
|
|
|
libp2p_logger_add_class("providerstore");
|
|
|
|
libp2p_logger_add_class("peerstore");
|
|
|
|
libp2p_logger_add_class("exporter");
|
|
|
|
libp2p_logger_add_class("peer");
|
|
|
|
*/
|
|
|
|
libp2p_logger_add_class("test_bitswap");
|
|
|
|
libp2p_logger_add_class("null");
|
|
|
|
libp2p_logger_add_class("online");
|
|
|
|
libp2p_logger_add_class("multistream");
|
|
|
|
libp2p_logger_add_class("secio");
|
|
|
|
libp2p_logger_add_class("bitswap");
|
|
|
|
libp2p_logger_add_class("bitswap_engine");
|
|
|
|
libp2p_logger_add_class("bitswap_network");
|
|
|
|
|
|
|
|
char* ipfs_path = "/tmp/ipfs_2";
|
|
|
|
char *peer_id_2 = NULL;
|
|
|
|
struct FSRepo* fs_repo;
|
|
|
|
pthread_t thread2;
|
|
|
|
|
|
|
|
// create my peer, peer 2
|
|
|
|
libp2p_logger_debug("test_bitswap", "Firing up the client\n");
|
|
|
|
drop_build_open_repo(ipfs_path, &fs_repo, "config.test2.wo_journal");
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
|
|
|
|
// add file
|
|
|
|
char* bytes = "Hello, World!";
|
|
|
|
char* filename = "hello.txt";
|
|
|
|
create_file(filename, (unsigned char*)bytes, strlen(bytes));
|
|
|
|
char* argv[] = { "ipfs" , "--config", ipfs_path, "add", filename};
|
|
|
|
struct CliArguments args;
|
|
|
|
args.argc = 5;
|
|
|
|
args.argv = argv;
|
|
|
|
args.config_dir = ipfs_path;
|
|
|
|
args.verb_index = 3;
|
|
|
|
ipfs_import_files(&args);
|
|
|
|
|
|
|
|
if (pthread_create(&thread2, NULL, test_daemon_start, (void*)ipfs_path) < 0) {
|
|
|
|
libp2p_logger_error("test_bitswap", "Unable to start thread 2\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(120);
|
|
|
|
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
ipfs_daemon_stop();
|
|
|
|
if (peer_id_2 != NULL)
|
|
|
|
free(peer_id_2);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|