diff --git a/rhizome.h b/rhizome.h index 9f53dfdc..b4fa9e08 100644 --- a/rhizome.h +++ b/rhizome.h @@ -622,7 +622,8 @@ int rhizome_manifest_extract_signature(rhizome_manifest *m, unsigned *ofs); enum rhizome_bundle_status rhizome_find_duplicate(const rhizome_manifest *m, rhizome_manifest **found); int rhizome_manifest_to_bar(rhizome_manifest *m, rhizome_bar_t *bar); enum rhizome_bundle_status rhizome_is_bar_interesting(const rhizome_bar_t *bar); -enum rhizome_bundle_status rhizome_is_manifest_interesting(rhizome_manifest *m); +enum rhizome_bundle_status rhizome_is_interesting(const rhizome_bid_t *bid, uint64_t version); +#define rhizome_is_manifest_interesting(M) rhizome_is_interesting(&(M)->keypair.public_key, (M)->version) enum rhizome_bundle_status rhizome_retrieve_manifest(const rhizome_bid_t *bid, rhizome_manifest *m); enum rhizome_bundle_status rhizome_retrieve_manifest_by_prefix(const unsigned char *prefix, unsigned prefix_len, rhizome_manifest *m); enum rhizome_bundle_status rhizome_retrieve_manifest_by_hash_prefix(const uint8_t *prefix, unsigned prefix_len, rhizome_manifest *m); diff --git a/rhizome_database.c b/rhizome_database.c index 8386b62a..6ff091f1 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -2050,7 +2050,7 @@ static enum rhizome_bundle_status is_interesting(const char *id_hex, uint64_t ve // do we have this bundle [or later]? sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT; sqlite3_stmt *statement = sqlite_prepare_bind(&retry, - "SELECT filehash FROM MANIFESTS WHERE id LIKE ? AND version >= ?", + "SELECT version, filehash FROM MANIFESTS WHERE id LIKE ? AND version >= ?", TEXT_TOUPPER, id_hex, INT64, version, END); @@ -2060,31 +2060,36 @@ static enum rhizome_bundle_status is_interesting(const char *id_hex, uint64_t ve enum rhizome_bundle_status status = RHIZOME_BUNDLE_STATUS_ERROR; int stepcode; if ((stepcode = sqlite_step_retry(&retry, statement)) == SQLITE_ROW){ - const char *q_filehash = (const char *) sqlite3_column_text(statement, 0); - if (q_filehash && *q_filehash) { - rhizome_filehash_t hash; - if (str_to_rhizome_filehash_t(&hash, q_filehash) == -1) { - WHYF("Malformed filehash %s", q_filehash); - status = RHIZOME_BUNDLE_STATUS_ERROR; - }else{ - enum rhizome_payload_status pstatus; - switch((pstatus = rhizome_exists(&hash))){ - case RHIZOME_PAYLOAD_STATUS_NEW: - status = RHIZOME_BUNDLE_STATUS_NEW; - break; - case RHIZOME_PAYLOAD_STATUS_STORED: - status = RHIZOME_BUNDLE_STATUS_SAME; - break; - case RHIZOME_PAYLOAD_STATUS_BUSY: - status = RHIZOME_BUNDLE_STATUS_BUSY; - break; - default: - status = RHIZOME_BUNDLE_STATUS_ERROR; - break; - } - } + uint64_t q_version = sqlite3_column_int64(statement, 0); + const char *q_filehash = (const char *) sqlite3_column_text(statement, 1); + + if (q_version > version){ + status = RHIZOME_BUNDLE_STATUS_OLD; }else{ status = RHIZOME_BUNDLE_STATUS_SAME; + // unless we are missing the payload... + if (q_filehash && *q_filehash) { + rhizome_filehash_t hash; + if (str_to_rhizome_filehash_t(&hash, q_filehash) == -1) { + WHYF("Malformed filehash %s", q_filehash); + status = RHIZOME_BUNDLE_STATUS_ERROR; + }else{ + enum rhizome_payload_status pstatus; + switch((pstatus = rhizome_exists(&hash))){ + case RHIZOME_PAYLOAD_STATUS_NEW: + status = RHIZOME_BUNDLE_STATUS_NEW; + break; + case RHIZOME_PAYLOAD_STATUS_STORED: + break; + case RHIZOME_PAYLOAD_STATUS_BUSY: + status = RHIZOME_BUNDLE_STATUS_BUSY; + break; + default: + status = RHIZOME_BUNDLE_STATUS_ERROR; + break; + } + } + } } }else if (sqlite_code_busy(stepcode)){ status = RHIZOME_BUNDLE_STATUS_BUSY; @@ -2106,7 +2111,7 @@ enum rhizome_bundle_status rhizome_is_bar_interesting(const rhizome_bar_t *bar) return is_interesting(id_hex, rhizome_bar_version(bar)); } -enum rhizome_bundle_status rhizome_is_manifest_interesting(rhizome_manifest *m) +enum rhizome_bundle_status rhizome_is_interesting(const rhizome_bid_t *bid, uint64_t version) { - return is_interesting(alloca_tohex_rhizome_bid_t(m->keypair.public_key), m->version); + return is_interesting(alloca_tohex_rhizome_bid_t(*bid), version); } diff --git a/rhizome_fetch.c b/rhizome_fetch.c index 2e618254..f05d8360 100644 --- a/rhizome_fetch.c +++ b/rhizome_fetch.c @@ -729,7 +729,7 @@ rhizome_fetch(struct rhizome_fetch_slot *slot, rhizome_manifest *m, } // If we already have this version or newer, do not fetch. - if (rhizome_is_manifest_interesting(m) == RHIZOME_BUNDLE_STATUS_SAME) { + if (rhizome_is_manifest_interesting(m) != RHIZOME_BUNDLE_STATUS_NEW) { DEBUG(rhizome_rx, " fetch not started -- already have that version or newer"); RETURN(SUPERSEDED); } @@ -878,7 +878,7 @@ int rhizome_suggest_queue_manifest_import(rhizome_manifest *m, const struct sock DEBUGF(rhizome_rx, "Considering import bid=%s version=%"PRIu64" size=%"PRIu64, alloca_tohex_rhizome_bid_t(m->keypair.public_key), m->version, m->filesize); - if (rhizome_is_manifest_interesting(m) == RHIZOME_BUNDLE_STATUS_SAME) { + if (rhizome_is_manifest_interesting(m) != RHIZOME_BUNDLE_STATUS_NEW) { DEBUG(rhizome_rx, " already stored that version or newer"); rhizome_manifest_free(m); RETURN(-1); diff --git a/rhizome_sync.c b/rhizome_sync.c index 0fc06f13..d45f35d2 100644 --- a/rhizome_sync.c +++ b/rhizome_sync.c @@ -288,7 +288,8 @@ static int sync_cache_bar(struct rhizome_sync *state, const rhizome_bar_t *bar, state->bars[state->bar_count].tries = MAX_TRIES; state->bar_count++; ret=1; - }else if(status != RHIZOME_BUNDLE_STATUS_SAME){ + }else if(status != RHIZOME_BUNDLE_STATUS_SAME + && status != RHIZOME_BUNDLE_STATUS_OLD){ return -1; } } diff --git a/rhizome_sync_keys.c b/rhizome_sync_keys.c index b31c3ba8..af67fda6 100644 --- a/rhizome_sync_keys.c +++ b/rhizome_sync_keys.c @@ -660,7 +660,7 @@ static int process_transfer_message(struct subscriber *peer, struct rhizome_sync break; enum rhizome_bundle_status status = rhizome_is_bar_interesting(&bar); - if (status == RHIZOME_BUNDLE_STATUS_SAME){ + if (status == RHIZOME_BUNDLE_STATUS_SAME || status == RHIZOME_BUNDLE_STATUS_OLD){ DEBUGF(rhizome_sync_keys, "Ignoring BAR %s:%"PRIu64" (hash %s), (Uninteresting)", alloca_tohex_rhizome_bar_prefix(&bar), rhizome_bar_version(&bar), @@ -721,7 +721,7 @@ static int process_transfer_message(struct subscriber *peer, struct rhizome_sync } enum rhizome_bundle_status bstatus = rhizome_is_manifest_interesting(m); - if (bstatus == RHIZOME_BUNDLE_STATUS_SAME){ + if (bstatus == RHIZOME_BUNDLE_STATUS_SAME || bstatus == RHIZOME_BUNDLE_STATUS_OLD){ DEBUGF(rhizome_sync_keys, "Ignoring manifest %s:%u"PRIu64" (hash %s), (Uninteresting)", alloca_tohex_rhizome_bid_t(m->keypair.public_key), m->version,