From ac5a6224003695ae08d62b056b6708831a567333 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 31 Jul 2017 13:54:09 -0500 Subject: [PATCH] Passing SessionContext instead of Stream --- cid/cid.c | 2 +- exchange/bitswap/message.c | 2 ++ exchange/bitswap/network.c | 2 +- include/ipfs/cid/cid.h | 2 +- routing/online.c | 4 ++++ test/exchange/test_bitswap.h | 2 -- 6 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cid/cid.c b/cid/cid.c index d559ee4..b774771 100644 --- a/cid/cid.c +++ b/cid/cid.c @@ -16,7 +16,7 @@ enum WireType ipfs_cid_message_fields[] = { WIRETYPE_VARINT, WIRETYPE_VARINT, WIRETYPE_LENGTH_DELIMITED }; -size_t ipfs_cid_protobuf_encode_size(struct Cid* cid) { +size_t ipfs_cid_protobuf_encode_size(const struct Cid* cid) { if (cid != NULL) return 11+12+cid->hash_length+11; return 0; diff --git a/exchange/bitswap/message.c b/exchange/bitswap/message.c index d39d621..b4739ec 100644 --- a/exchange/bitswap/message.c +++ b/exchange/bitswap/message.c @@ -658,6 +658,8 @@ int ipfs_bitswap_message_add_wantlist_items(struct BitswapMessage* message, stru for(int i = 0; i < cids->total; i++) { const struct Cid* cid = (const struct Cid*)libp2p_utils_vector_get(cids, i); struct WantlistEntry* entry = ipfs_bitswap_wantlist_entry_new(); + entry->block_size = ipfs_cid_protobuf_encode_size(cid); + entry->block = (unsigned char*) malloc(entry->block_size); if (!ipfs_cid_protobuf_encode(cid, entry->block, entry->block_size, &entry->block_size)) { // TODO: we should do more than return a half-baked list return 0; diff --git a/exchange/bitswap/network.c b/exchange/bitswap/network.c index 6b7ca3f..a3ba511 100644 --- a/exchange/bitswap/network.c +++ b/exchange/bitswap/network.c @@ -34,7 +34,7 @@ int ipfs_bitswap_network_send_message(const struct BitswapContext* context, stru memcpy(buf, "/ipfs/bitswap/1.1.0\n", 20); buf_size += 20; // send it - int bytes_written = peer->sessionContext->default_stream->write(peer->sessionContext->default_stream, buf, buf_size); + int bytes_written = peer->sessionContext->default_stream->write(peer->sessionContext, buf, buf_size); if (bytes_written <= 0) { free(buf); return 0; diff --git a/include/ipfs/cid/cid.h b/include/ipfs/cid/cid.h index 10e50d2..4a8b55b 100644 --- a/include/ipfs/cid/cid.h +++ b/include/ipfs/cid/cid.h @@ -61,7 +61,7 @@ int ipfs_cid_protobuf_decode(unsigned char* buffer, size_t buffer_length, struct * @param incoming the struct to encode * @returns the number of approximate bytes */ -size_t ipfs_cid_protobuf_encode_size(struct Cid* incoming); +size_t ipfs_cid_protobuf_encode_size(const struct Cid* incoming); /** * Create a new CID based on the given hash diff --git a/routing/online.c b/routing/online.c index 4791c81..f664fd5 100644 --- a/routing/online.c +++ b/routing/online.c @@ -87,6 +87,10 @@ int ipfs_routing_online_find_remote_providers(struct IpfsRouting* routing, const struct Libp2pLinkedList * current_provider_peer_list_item = return_message->provider_peer_head; while (current_provider_peer_list_item != NULL) { struct Libp2pPeer *current_peer = current_provider_peer_list_item->item; + // if we can find the peer in the peerstore, use that one instead + struct Libp2pPeer* peerstorePeer = libp2p_peerstore_get_peer(routing->local_node->peerstore, (unsigned char*)current_peer->id, current_peer->id_size); + if (peerstorePeer != NULL) + current_peer = peerstorePeer; libp2p_utils_vector_add(*peers, libp2p_peer_copy(current_peer)); current_provider_peer_list_item = current_provider_peer_list_item->next; } diff --git a/test/exchange/test_bitswap.h b/test/exchange/test_bitswap.h index cc2aaaf..5ecd7f7 100644 --- a/test/exchange/test_bitswap.h +++ b/test/exchange/test_bitswap.h @@ -304,8 +304,6 @@ int test_bitswap_retrieve_file_known_remote() { multiaddress_free(ma_peer1); ipfs_node_online_new(ipfs_path, &ipfs_node2); - ipfs_node2->routing->Bootstrap(ipfs_node2->routing); - if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hello_world_hash, strlen(hello_world_hash), &cid)) goto exit;