added auto-cleaning of reputation info for outdated/removed identities

This commit is contained in:
csoler 2016-07-03 18:59:30 -04:00
parent bf28ea7ac7
commit c98416dc28
4 changed files with 174 additions and 153 deletions

View File

@ -221,8 +221,8 @@ public:
RsIdentity(RsGxsIface *gxs): RsGxsIfaceHelper(gxs) { return; }
virtual ~RsIdentity() { return; }
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
// For Other Services....
// It should be impossible for them to get a message which we don't have the identity.
@ -231,37 +231,38 @@ public:
// We cache all identities, and provide alternative (instantaneous)
// functions to extract info, rather than the standard Token system.
//virtual bool getNickname(const RsGxsId &id, std::string &nickname) = 0;
virtual bool getIdDetails(const RsGxsId &id, RsIdentityDetails &details) = 0;
//virtual bool getNickname(const RsGxsId &id, std::string &nickname) = 0;
virtual bool getIdDetails(const RsGxsId &id, RsIdentityDetails &details) = 0;
// Fills up list of all own ids. Returns false if ids are not yet loaded.
virtual bool getOwnIds(std::list<RsGxsId> &ownIds) = 0;
virtual bool isOwnId(const RsGxsId& id) = 0;
// Fills up list of all own ids. Returns false if ids are not yet loaded.
virtual bool getOwnIds(std::list<RsGxsId> &ownIds) = 0;
virtual bool isOwnId(const RsGxsId& id) = 0;
//
virtual bool submitOpinion(uint32_t& token, const RsGxsId &id,
virtual bool submitOpinion(uint32_t& token, const RsGxsId &id,
bool absOpinion, int score) = 0;
virtual bool createIdentity(uint32_t& token, RsIdentityParameters &params) = 0;
virtual bool createIdentity(uint32_t& token, RsIdentityParameters &params) = 0;
virtual bool updateIdentity(uint32_t& token, RsGxsIdGroup &group) = 0;
virtual bool deleteIdentity(uint32_t& token, RsGxsIdGroup &group) = 0;
virtual bool updateIdentity(uint32_t& token, RsGxsIdGroup &group) = 0;
virtual bool deleteIdentity(uint32_t& token, RsGxsIdGroup &group) = 0;
virtual bool parseRecognTag(const RsGxsId &id, const std::string &nickname,
virtual bool parseRecognTag(const RsGxsId &id, const std::string &nickname,
const std::string &tag, RsRecognTagDetails &details) = 0;
virtual bool getRecognTagRequest(const RsGxsId &id, const std::string &comment,
virtual bool getRecognTagRequest(const RsGxsId &id, const std::string &comment,
uint16_t tag_class, uint16_t tag_type, std::string &tag) = 0;
virtual bool setAsRegularContact(const RsGxsId& id,bool is_a_contact) = 0 ;
virtual bool isARegularContact(const RsGxsId& id) = 0 ;
virtual bool isBanned(const RsGxsId& id) =0;
virtual time_t getLastUsageTS(const RsGxsId &id) =0;
// Specific RsIdentity Functions....
/* Specific Service Data */
/* We expose these initially for testing / GUI purposes.
*/
virtual bool getGroupData(const uint32_t &token, std::vector<RsGxsIdGroup> &groups) = 0;
//virtual bool getMsgData(const uint32_t &token, std::vector<RsGxsIdOpinion> &opinions) = 0;
virtual bool getGroupData(const uint32_t &token, std::vector<RsGxsIdGroup> &groups) = 0;
//virtual bool getMsgData(const uint32_t &token, std::vector<RsGxsIdOpinion> &opinions) = 0;
};

View File

@ -117,13 +117,13 @@
* 10 | 1.0 | 0 | 0.25 | 1.0
*
* To check:
* [ ] Opinions are saved/loaded accross restart
* [ ] Opinions are transmitted to friends
* [ ] Opinions are transmitted to friends when updated
* [X] Opinions are saved/loaded accross restart
* [X] Opinions are transmitted to friends
* [X] Opinions are transmitted to friends when updated
*
* To do:
* [ ] Add debug info
* [ ] Test the whole thing
* [X] Add debug info
* [X] Test the whole thing
* [X] Implement a system to allow not storing info when we don't have it
*/
@ -139,6 +139,7 @@ static const float REPUTATION_ASSESSMENT_THRESHOLD_X1 = 0.5f ; // reputat
static const uint32_t PGP_AUTO_BAN_THRESHOLD_DEFAULT = 2 ; // above this, auto ban any GXS id signed by this node
static const uint32_t IDENTITY_FLAGS_UPDATE_DELAY = 100 ; //
static const uint32_t BANNED_NODES_UPDATE_DELAY = 313 ; // update approx every 5 mins. Chosen to not be a multiple of IDENTITY_FLAGS_UPDATE_DELAY
static const uint32_t REPUTATION_INFO_KEEP_DELAY = 86400*35; // remove old reputation info 5 days after last usage limit, in case the ID would come back..
p3GxsReputation::p3GxsReputation(p3LinkMgr *lm)
:p3Service(), p3Config(),
@ -343,6 +344,7 @@ void p3GxsReputation::cleanup()
// identities, which would cause an excess of hits to the database.
bool updated = false ;
time_t now = time(NULL) ;
RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/
@ -358,6 +360,17 @@ void p3GxsReputation::cleanup()
#endif
updated = true ;
}
else if(rsIdentity->getLastUsageTS(it->first) + REPUTATION_INFO_KEEP_DELAY < now)
{
#ifdef DEBUG_REPUTATION
std::cerr << " Identity " << it->first << " has a last usage TS of " << now - rsIdentity->getLastUsageTS(it->first) << " secs ago: deleting it." << std::endl;
#endif
std::map<RsGxsId,Reputation>::iterator tmp(it) ;
++tmp ;
mReputations.erase(it) ;
it = tmp ;
updated = true ;
}
else
++it ;

View File

@ -503,6 +503,12 @@ bool p3IdService:: getNickname(const RsGxsId &id, std::string &nickname)
}
#endif
time_t p3IdService::getLastUsageTS(const RsGxsId &id)
{
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
return locked_getLastUsageTS(id) ;
}
bool p3IdService::getIdDetails(const RsGxsId &id, RsIdentityDetails &details)
{
#ifdef DEBUG_IDS

View File

@ -218,11 +218,11 @@ private:
class p3IdService: public RsGxsIdExchange, public RsIdentity, public GxsTokenQueue, public RsTickEvent, public p3Config
{
public:
public:
p3IdService(RsGeneralDataService* gds, RsNetworkExchangeService* nes, PgpAuxUtils *pgpUtils);
virtual RsServiceInfo getServiceInfo();
static uint32_t idAuthenPolicy();
virtual RsServiceInfo getServiceInfo();
static uint32_t idAuthenPolicy();
virtual void service_tick(); // needed for background processing.
@ -238,14 +238,14 @@ static uint32_t idAuthenPolicy();
/* Data Specific Interface */
// These are exposed via RsIdentity.
virtual bool getGroupData(const uint32_t &token, std::vector<RsGxsIdGroup> &groups);
//virtual bool getMsgData(const uint32_t &token, std::vector<RsGxsIdOpinion> &opinions);
virtual bool getGroupData(const uint32_t &token, std::vector<RsGxsIdGroup> &groups);
//virtual bool getMsgData(const uint32_t &token, std::vector<RsGxsIdOpinion> &opinions);
// These are local - and not exposed via RsIdentity.
virtual bool createGroup(uint32_t& token, RsGxsIdGroup &group);
virtual bool updateGroup(uint32_t& token, RsGxsIdGroup &group);
virtual bool deleteGroup(uint32_t& token, RsGxsIdGroup &group);
//virtual bool createMsg(uint32_t& token, RsGxsIdOpinion &opinion);
virtual bool createGroup(uint32_t& token, RsGxsIdGroup &group);
virtual bool updateGroup(uint32_t& token, RsGxsIdGroup &group);
virtual bool deleteGroup(uint32_t& token, RsGxsIdGroup &group);
//virtual bool createMsg(uint32_t& token, RsGxsIdOpinion &opinion);
/**************** RsIdentity External Interface.
* Notes:
@ -256,25 +256,26 @@ virtual bool deleteGroup(uint32_t& token, RsGxsIdGroup &group);
* Also need to handle Cache updates / invalidation from internal changes.
*
*/
//virtual bool getNickname(const RsGxsId &id, std::string &nickname);
virtual bool getIdDetails(const RsGxsId &id, RsIdentityDetails &details);
//virtual bool getNickname(const RsGxsId &id, std::string &nickname);
virtual bool getIdDetails(const RsGxsId &id, RsIdentityDetails &details);
//
virtual bool submitOpinion(uint32_t& token, const RsGxsId &id,
virtual bool submitOpinion(uint32_t& token, const RsGxsId &id,
bool absOpinion, int score);
virtual bool createIdentity(uint32_t& token, RsIdentityParameters &params);
virtual bool createIdentity(uint32_t& token, RsIdentityParameters &params);
virtual bool updateIdentity(uint32_t& token, RsGxsIdGroup &group);
virtual bool deleteIdentity(uint32_t& token, RsGxsIdGroup &group);
virtual bool updateIdentity(uint32_t& token, RsGxsIdGroup &group);
virtual bool deleteIdentity(uint32_t& token, RsGxsIdGroup &group);
virtual bool parseRecognTag(const RsGxsId &id, const std::string &nickname,
virtual bool parseRecognTag(const RsGxsId &id, const std::string &nickname,
const std::string &tag, RsRecognTagDetails &details);
virtual bool getRecognTagRequest(const RsGxsId &id, const std::string &comment,
virtual bool getRecognTagRequest(const RsGxsId &id, const std::string &comment,
uint16_t tag_class, uint16_t tag_type, std::string &tag);
virtual bool setAsRegularContact(const RsGxsId& id,bool is_a_contact) ;
virtual bool setAsRegularContact(const RsGxsId& id,bool is_a_contact) ;
virtual bool isARegularContact(const RsGxsId& id) ;
virtual bool isBanned(const RsGxsId& id) ;
virtual time_t getLastUsageTS(const RsGxsId &id) ;
/**************** RsGixs Implementation ***************/
@ -304,23 +305,23 @@ virtual bool setAsRegularContact(const RsGxsId& id,bool is_a_contact) ;
/**************** RsGixsReputation Implementation ****************/
// get Reputation.
virtual bool haveReputation(const RsGxsId &id);
virtual bool loadReputation(const RsGxsId &id, const std::list<RsPeerId>& peers);
virtual bool getReputation(const RsGxsId &id, GixsReputation &rep);
virtual bool haveReputation(const RsGxsId &id);
virtual bool loadReputation(const RsGxsId &id, const std::list<RsPeerId>& peers);
virtual bool getReputation(const RsGxsId &id, GixsReputation &rep);
protected:
protected:
/** Notifications **/
virtual void notifyChanges(std::vector<RsGxsNotify*>& changes);
virtual void notifyChanges(std::vector<RsGxsNotify*>& changes);
/** Overloaded to add PgpIdHash to Group Definition **/
virtual ServiceCreate_Return service_CreateGroup(RsGxsGrpItem* grpItem, RsTlvSecurityKeySet& keySet);
virtual ServiceCreate_Return service_CreateGroup(RsGxsGrpItem* grpItem, RsTlvSecurityKeySet& keySet);
// Overloaded from GxsTokenQueue for Request callbacks.
virtual void handleResponse(uint32_t token, uint32_t req_type);
virtual void handleResponse(uint32_t token, uint32_t req_type);
// Overloaded from RsTickEvent.
virtual void handle_event(uint32_t event_type, const std::string &elabel);
virtual void handle_event(uint32_t event_type, const std::string &elabel);
//===================================================//
// p3Config methods //
@ -334,9 +335,9 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel);
virtual RsSerialiser *setupSerialiser() ;
private:
private:
/************************************************************************
/************************************************************************
* This is the Cache for minimising calls to the DataStore.
*
*/
@ -360,7 +361,7 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel);
// Switching to RsMemCache for Key Caching.
RsMemCache<RsGxsId, RsGxsIdCache> mKeyCache;
/************************************************************************
/************************************************************************
* Refreshing own Ids.
*
*/
@ -369,7 +370,7 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel);
std::list<RsGxsId> mOwnIds;
/************************************************************************
/************************************************************************
* Test fns for Caching.
*
*/
@ -377,7 +378,7 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel);
bool cachetest_getlist();
bool cachetest_handlerequest(uint32_t token);
/************************************************************************
/************************************************************************
* for processing background tasks that use the serviceString.
* - must be mutually exclusive to avoid clashes.
*/
@ -387,7 +388,7 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel);
bool mBgSchedule_Active;
uint32_t mBgSchedule_Mode;
/************************************************************************
/************************************************************************
* pgphash processing.
*
*/
@ -403,7 +404,7 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel);
std::map<RsPgpId, PGPFingerprintType> mPgpFingerprintMap;
std::list<RsGxsIdGroup> mGroupsToProcess;
/************************************************************************
/************************************************************************
* recogn processing.
*
*/
@ -421,7 +422,7 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel);
void loadRecognKeys();
/************************************************************************
/************************************************************************
* opinion processing.
*
*/
@ -449,12 +450,12 @@ virtual void handle_event(uint32_t event_type, const std::string &elabel);
std::map<RsGxsId, RsGxsRecognSignerItem *> mRecognSignKeys;
std::map<RsGxsId, uint32_t> mRecognOldSignKeys;
/************************************************************************
/************************************************************************
* Below is the background task for processing opinions => reputations
*
*/
virtual void generateDummyData();
virtual void generateDummyData();
void generateDummy_OwnIds();
void generateDummy_FriendPGP();
void generateDummy_UnknownPGP();
@ -466,7 +467,7 @@ virtual void generateDummyData();
virtual void timeStampKey(const RsGxsId& id) ;
time_t locked_getLastUsageTS(const RsGxsId& gxs_id);
std::string genRandomId(int len = 20);
std::string genRandomId(int len = 20);
#if 0
bool reputation_start();
@ -497,11 +498,11 @@ std::string genRandomId(int len = 20);
std::list<RsGxsGroupId> mBgFullCalcGroups;
#endif
/************************************************************************
/************************************************************************
* Other Data that is protected by the Mutex.
*/
private:
private:
std::map<uint32_t, std::set<RsGxsGroupId> > mIdsPendingCache;
std::map<uint32_t, std::list<RsGxsGroupId> > mGroupNotPresent;