From 98b1e0fef43c96d7eea7f15db194f92dbafbbcc2 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 9 Oct 2017 10:01:29 -0500 Subject: [PATCH] Return a copy of the block from the exchange, not the block itself Returning the block itself will cause problems when a client deallocates the block. --- exchange/bitswap/bitswap.c | 2 +- exchange/bitswap/network.c | 2 +- test/exchange/test_bitswap.h | 9 +++++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/exchange/bitswap/bitswap.c b/exchange/bitswap/bitswap.c index 8f9de75..cb2e522 100644 --- a/exchange/bitswap/bitswap.c +++ b/exchange/bitswap/bitswap.c @@ -183,7 +183,7 @@ int ipfs_bitswap_get_block(struct Exchange* exchange, struct Cid* cid, struct Bl // loop waiting for it to fill while(1) { if (want_entry->block != NULL) { - *block = want_entry->block; + *block = ipfs_block_copy(want_entry->block); // error or not, we no longer need the block (decrement reference count) ipfs_bitswap_want_manager_remove(bitswapContext, cid); if (*block == NULL) { diff --git a/exchange/bitswap/network.c b/exchange/bitswap/network.c index a5d24f2..c1f05d8 100644 --- a/exchange/bitswap/network.c +++ b/exchange/bitswap/network.c @@ -20,7 +20,7 @@ int ipfs_bitswap_network_send_message(const struct BitswapContext* context, struct Libp2pPeer* peer, const struct BitswapMessage* message) { libp2p_logger_debug("bitswap_network", "Sending bitswap message to %s.\n", libp2p_peer_id_to_string(peer)); // get a connection to the peer - if (peer->connection_type != CONNECTION_TYPE_CONNECTED) { + if (peer->connection_type != CONNECTION_TYPE_CONNECTED || peer->sessionContext == NULL) { libp2p_peer_connect(&context->ipfsNode->identity->private_key, peer, context->ipfsNode->peerstore, context->ipfsNode->repo->config->datastore, 10); if(peer->connection_type != CONNECTION_TYPE_CONNECTED) return 0; diff --git a/test/exchange/test_bitswap.h b/test/exchange/test_bitswap.h index f32f4b0..7ec8a75 100644 --- a/test/exchange/test_bitswap.h +++ b/test/exchange/test_bitswap.h @@ -381,8 +381,9 @@ int test_bitswap_retrieve_file_third_party() { libp2p_logger_add_class("providerstore"); libp2p_logger_add_class("peerstore"); libp2p_logger_add_class("exporter"); - libp2p_logger_add_class("peer"); */ + libp2p_logger_add_class("peer"); + libp2p_logger_add_class("bitswap_network"); libp2p_logger_add_class("test_bitswap"); libp2p_logger_add_class("null"); libp2p_logger_add_class("online"); @@ -398,6 +399,7 @@ int test_bitswap_retrieve_file_third_party() { struct Libp2pVector* ma_vector2 = NULL, *ma_vector3 = NULL; struct HashtableNode* node = NULL; struct Block* result = NULL; + struct HashtableNode* result_node = NULL; struct Cid* cid = NULL; // create peer 1 @@ -468,7 +470,8 @@ int test_bitswap_retrieve_file_third_party() { goto exit; } - if (node->data_size != result->data_length) { + 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; } @@ -498,6 +501,8 @@ int test_bitswap_retrieve_file_third_party() { ipfs_hashtable_node_free(node); if (result != NULL) ipfs_block_free(result); + if (result_node != NULL) + ipfs_hashtable_node_free(result_node); if (cid != NULL) ipfs_cid_free(cid); return retVal;