Skip to content

Commit

Permalink
Added acknowledge function for lastseen entries
Browse files Browse the repository at this point in the history
Added function to acknowledge the observation of a lastseen entry in DB.

Ticket: ENT-11838
Changelog: None
Signed-off-by: Lars Erik Wik <[email protected]>
  • Loading branch information
larsewi committed Oct 15, 2024
1 parent da6674e commit cf243d6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
51 changes: 51 additions & 0 deletions libpromises/lastseen.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,54 @@ int RemoveKeysFromLastSeen(const char *input, bool must_be_coherent,

return 0;
}

static bool OnlyRewriteIfChanged(KeyHostSeen *entry, ARG_UNUSED size_t size, KeyHostSeen *new)
{
assert(entry != NULL);
assert(new != NULL);

if (entry->acknowledged)
{
return false;
}

new->acknowledged = true;
new->lastseen = entry->lastseen;
new->Q = entry->Q;

return true;
}

bool LastSeenHostAcknowledge(const char *host_key, bool incoming)
{
DBHandle *db = NULL;
if (!OpenDB(&db, dbid_lastseen))
{
Log(LOG_LEVEL_ERR, "Unable to open lastseen DB");
return false;
}

// Update quality-of-connection entry
char key[CF_BUFSIZE];
NDEBUG_UNUSED int ret = snprintf(key, CF_BUFSIZE, "q%c%s", incoming ? 'i' : 'o', host_key);
assert(ret > 0 && ret < CF_BUFSIZE);

KeyHostSeen value;
value.lastseen = 0; /* To distinguish between entry not found and error */
if (OverwriteDB(db, key, &value, sizeof(value), (OverwriteCondition)OnlyRewriteIfChanged, &value))
{
Log(LOG_LEVEL_DEBUG, "Acknowledged observation of key '%s' to lastseen DB", key);
}
else if (value.lastseen != 0 /* was found */ &&
!value.acknowledged /* should update */)
{
/* In this case, the entry was found and should have been updated.
* However, false was returned. Hence, this must be due to error. */
Log(LOG_LEVEL_ERR, "Unable to overwrite key '%s' to lastseen DB", key);
CloseDB(db);
return false;
}

CloseDB(db);
return true;
}
8 changes: 8 additions & 0 deletions libpromises/lastseen.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ bool IsLastSeenCoherent(void);
int RemoveKeysFromLastSeen(const char *input, bool must_be_coherent,
char *equivalent, size_t equivalent_size);

/**
* @brief Acknowledge that lastseen host entry is observed.
* @param host_key The host key of the lastseen entry.
* @param incoming Whether it is an incoming or outgoing entry.
* @return true if host entry was successfully acknowledged, otherwise false.
*/
bool LastSeenHostAcknowledge(const char *host_key, bool incoming);

#endif

0 comments on commit cf243d6

Please sign in to comment.