From 5910d63c3d51bdcff368b3b07a41522bddb90280 Mon Sep 17 00:00:00 2001 From: John Jones Date: Wed, 2 Aug 2017 10:53:14 -0500 Subject: [PATCH] Clean up when we receive a block we wanted --- exchange/bitswap/bitswap.c | 12 +++++++++--- exchange/bitswap/engine.c | 1 - exchange/bitswap/peer_request_queue.c | 14 ++++++++++---- exchange/bitswap/wantlist_queue.c | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/exchange/bitswap/bitswap.c b/exchange/bitswap/bitswap.c index 39a09c7..b8808fb 100644 --- a/exchange/bitswap/bitswap.c +++ b/exchange/bitswap/bitswap.c @@ -87,9 +87,15 @@ int ipfs_bitswap_close(struct Exchange* exchange) { * adds the block to the blockstore. This still has to be sorted. */ int ipfs_bitswap_has_block(struct Exchange* exchange, struct Block* block) { - //TODO: Implement this method - // NOTE: The GO version adds the block to the blockstore. I have yet to - // understand the flow and if this is correct for us. + // add the block to the blockstore + struct BitswapContext* context = exchange->exchangeContext; + context->ipfsNode->blockstore->Put(context->ipfsNode->blockstore->blockstoreContext, block); + // update requests + struct WantListQueueEntry* queueEntry = ipfs_bitswap_wantlist_queue_find(context->localWantlist, block->cid); + if (queueEntry != NULL) { + queueEntry->block = block; + } + // TODO: Announce to world that we now have the block return 0; } diff --git a/exchange/bitswap/engine.c b/exchange/bitswap/engine.c index ea08d7d..be6ab1c 100644 --- a/exchange/bitswap/engine.c +++ b/exchange/bitswap/engine.c @@ -60,7 +60,6 @@ void* ipfs_bitswap_engine_peer_request_processor_start(void* ctx) { while (!context->bitswap_engine->shutting_down) { struct PeerRequest* item = ipfs_bitswap_peer_request_queue_pop(context->peerRequestQueue); if (item != NULL) { - // Do they have something on the network to process? // did they send us something over the network? unsigned char* buffer = NULL; size_t buffer_len = 0; diff --git a/exchange/bitswap/peer_request_queue.c b/exchange/bitswap/peer_request_queue.c index 2af20af..f0bc8b3 100644 --- a/exchange/bitswap/peer_request_queue.c +++ b/exchange/bitswap/peer_request_queue.c @@ -206,12 +206,14 @@ struct PeerRequest* ipfs_bitswap_peer_request_queue_pop(struct PeerRequestQueue* pthread_mutex_lock(&queue->queue_mutex); struct PeerRequestEntry* entry = queue->first; if (entry != NULL) { + retVal = entry->current; if (ipfs_bitswap_peer_request_something_to_do(entry)) { - retVal = entry->current; // move to the end of the queue - queue->first = queue->first->next; - queue->last->next = entry; - queue->last = entry; + if (queue->first->next != NULL) { + queue->first = queue->first->next; + queue->last->next = entry; + queue->last = entry; + } } } pthread_mutex_unlock(&queue->queue_mutex); @@ -348,9 +350,13 @@ struct PeerRequest* ipfs_peer_request_queue_find_peer(struct PeerRequestQueue* q } } + // we didn't find one, so create one entry = ipfs_bitswap_peer_request_entry_new(); entry->current = ipfs_bitswap_peer_request_new(); entry->current->peer = peer; + // attach it to the queue + if (queue->first == NULL) + queue->first = entry; entry->prior = queue->last; queue->last = entry; diff --git a/exchange/bitswap/wantlist_queue.c b/exchange/bitswap/wantlist_queue.c index 0d850f6..b6f2ecf 100644 --- a/exchange/bitswap/wantlist_queue.c +++ b/exchange/bitswap/wantlist_queue.c @@ -293,7 +293,7 @@ int ipfs_bitswap_wantlist_process_entry(struct BitswapContext* context, struct W // TODO: Review this code. struct WantListSession* session = (struct WantListSession*) libp2p_utils_vector_get(entry->sessionsRequesting, i); if (session->type == WANTLIST_SESSION_TYPE_LOCAL) { - context->ipfsNode->exchange->HasBlock(context->ipfsNode->exchange, entry->block); + //context->ipfsNode->exchange->HasBlock(context->ipfsNode->exchange, entry->block); } else { struct Libp2pPeer* peer = (struct Libp2pPeer*) session->context; ipfs_bitswap_peer_request_queue_fill(context->peerRequestQueue, peer, entry->block);