From 218977170c637f1a3557bdb09e70a0ce5078d895 Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 25 Jul 2016 15:45:49 -0400 Subject: [PATCH] added two new options to people page for handlign reputations: change the ban limit from friend opinions, and allow to auto-set positive opinions to contacts --- libretroshare/src/retroshare/rsreputations.h | 9 +- libretroshare/src/services/p3gxsreputation.cc | 117 ++++++++++++++--- libretroshare/src/services/p3gxsreputation.h | 123 +++++++++--------- .../src/gui/settings/PeoplePage.cpp | 21 ++- retroshare-gui/src/gui/settings/PeoplePage.ui | 45 ++++++- 5 files changed, 229 insertions(+), 86 deletions(-) diff --git a/libretroshare/src/retroshare/rsreputations.h b/libretroshare/src/retroshare/rsreputations.h index f620f3d5e..fdb89b8b5 100644 --- a/libretroshare/src/retroshare/rsreputations.h +++ b/libretroshare/src/retroshare/rsreputations.h @@ -51,9 +51,16 @@ public: virtual bool setOwnOpinion(const RsGxsId& key_id, const Opinion& op) =0; virtual bool getReputationInfo(const RsGxsId& id,const RsPgpId& owner_id,ReputationInfo& info) =0 ; + + // parameters + virtual void setNodeAutoBanThreshold(uint32_t n) =0; virtual uint32_t nodeAutoBanThreshold() =0; - + virtual void setNodeAutoPositiveOpinionForContacts(bool b) =0; + virtual bool nodeAutoPositiveOpinionForContacts() =0; + virtual float nodeAutoBanIdentitiesLimit() =0; + virtual void setNodeAutoBanIdentitiesLimit(float f) =0; + // This one is a proxy designed to allow fast checking of a GXS id. // it basically returns true if assessment is not ASSESSMENT_OK diff --git a/libretroshare/src/services/p3gxsreputation.cc b/libretroshare/src/services/p3gxsreputation.cc index 56c2d8cc2..5ccef637b 100644 --- a/libretroshare/src/services/p3gxsreputation.cc +++ b/libretroshare/src/services/p3gxsreputation.cc @@ -134,7 +134,7 @@ static const int ACTIVE_FRIENDS_UPDATE_PERIOD = 600 ; // 10 minu static const int ACTIVE_FRIENDS_ONLINE_DELAY = 86400*7 ; // 1 week. static const int kReputationRequestPeriod = 600; // 10 mins static const int kReputationStoreWait = 180; // 3 minutes. -static const float REPUTATION_ASSESSMENT_THRESHOLD_X1 = 0.5f ; // reputation under which the peer gets killed +static const float REPUTATION_ASSESSMENT_THRESHOLD_X1 = 0.5f ; // reputation under which the peer gets killed. Warning there's a 1 shift with what's shown in GUI. Be careful. 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 @@ -153,6 +153,9 @@ p3GxsReputation::p3GxsReputation(p3LinkMgr *lm) mLastActiveFriendsUpdate = time(NULL) - 0.5*ACTIVE_FRIENDS_UPDATE_PERIOD; // avoids doing it too soon since the TS from rsIdentity needs to be loaded already mAverageActiveFriends = 0 ; mLastBannedNodesUpdate = 0 ; + + mAutoBanIdentitiesLimit = REPUTATION_ASSESSMENT_THRESHOLD_X1; + mAutoSetPositiveOptionToContacts = true; // default } const std::string GXS_REPUTATION_APP_NAME = "gxsreputation"; @@ -232,6 +235,44 @@ uint32_t p3GxsReputation::nodeAutoBanThreshold() return mPgpAutoBanThreshold ; } +void p3GxsReputation::setNodeAutoPositiveOpinionForContacts(bool b) +{ + RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ + + if(b != mAutoSetPositiveOptionToContacts) + { + mLastBannedNodesUpdate = 0 ; + mAutoSetPositiveOptionToContacts = b ; + IndicateConfigChanged() ; + } +} +bool p3GxsReputation::nodeAutoPositiveOpinionForContacts() +{ + RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ + return mAutoSetPositiveOptionToContacts ; +} +float p3GxsReputation::nodeAutoBanIdentitiesLimit() +{ + RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ + return mAutoBanIdentitiesLimit - 1.0f; +} +void p3GxsReputation::setNodeAutoBanIdentitiesLimit(float f) +{ + RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ + + if(f < -1.0 || f >= 0.0) + { + std::cerr << "(EE) Unexpected value for auto ban identities limit: " << f << std::endl; + return ; + } + if(f != mAutoBanIdentitiesLimit) + { + mLastBannedNodesUpdate = 0 ; + mAutoBanIdentitiesLimit = f+1.0 ; + IndicateConfigChanged() ; + } +} + int p3GxsReputation::status() { return 1; @@ -292,6 +333,8 @@ void p3GxsReputation::updateIdentityFlags() to_update.push_back(rit->first) ; } + std::list should_set_to_positive ; + for(std::list::const_iterator rit(to_update.begin());rit!=to_update.end();++rit) { RsIdentityDetails details; @@ -303,32 +346,40 @@ void p3GxsReputation::updateIdentityFlags() #endif continue ; } + bool is_a_contact = rsIdentity->isARegularContact(*rit) ; - RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ + { + RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ + std::map::iterator it = mReputations.find(*rit) ; - std::map::iterator it = mReputations.find(*rit) ; + if(it == mReputations.end()) + { + std::cerr << " Weird situation: item " << *rit << " has been deleted from the list??" << std::endl; + continue ; + } + it->second.mIdentityFlags = 0 ; - if(it == mReputations.end()) - { - std::cerr << " Weird situation: item " << *rit << " has been deleted from the list??" << std::endl; - continue ; - } - it->second.mIdentityFlags = 0 ; + if(details.mFlags & RS_IDENTITY_FLAGS_PGP_LINKED) + { + it->second.mIdentityFlags |= REPUTATION_IDENTITY_FLAG_PGP_LINKED ; + it->second.mOwnerNode = details.mPgpId ; + } + if(details.mFlags & RS_IDENTITY_FLAGS_PGP_KNOWN ) it->second.mIdentityFlags |= REPUTATION_IDENTITY_FLAG_PGP_KNOWN ; - if(details.mFlags & RS_IDENTITY_FLAGS_PGP_LINKED) - { - it->second.mIdentityFlags |= REPUTATION_IDENTITY_FLAG_PGP_LINKED ; - it->second.mOwnerNode = details.mPgpId ; - } - if(details.mFlags & RS_IDENTITY_FLAGS_PGP_KNOWN ) it->second.mIdentityFlags |= REPUTATION_IDENTITY_FLAG_PGP_KNOWN ; + if(mAutoSetPositiveOptionToContacts && is_a_contact && it->second.mOwnOpinion == RsReputations::OPINION_NEUTRAL) + should_set_to_positive.push_back(*rit) ; #ifdef DEBUG_REPUTATION - std::cerr << " updated flags for " << *rit << " to " << std::hex << it->second.mIdentityFlags << std::dec << std::endl; + std::cerr << " updated flags for " << *rit << " to " << std::hex << it->second.mIdentityFlags << std::dec << std::endl; #endif - it->second.updateReputation() ; - IndicateConfigChanged(); + it->second.updateReputation() ; + IndicateConfigChanged(); + } } + + for(std::list::const_iterator it(should_set_to_positive.begin());it!=should_set_to_positive.end();++it) + setOwnOpinion(*it,RsReputations::OPINION_POSITIVE) ; } void p3GxsReputation::cleanup() @@ -748,7 +799,7 @@ bool p3GxsReputation::getReputationInfo(const RsGxsId& gxsid, const RsPgpId& own #endif info.mAssessment = RsReputations::ASSESSMENT_BAD ; } - else if(info.mOverallReputationScore <= REPUTATION_ASSESSMENT_THRESHOLD_X1) + else if(info.mOverallReputationScore <= mAutoBanIdentitiesLimit) info.mAssessment = RsReputations::ASSESSMENT_BAD ; else info.mAssessment = RsReputations::ASSESSMENT_OK ; @@ -901,7 +952,15 @@ bool p3GxsReputation::saveList(bool& cleanup, std::list &savelist) rs_sprintf(kv.value, "%d", mPgpAutoBanThreshold); vitem->tlvkvs.pairs.push_back(kv) ; - savelist.push_back(vitem) ; + kv.key = "AUTO_BAN_IDENTITIES_THRESHOLD" ; + rs_sprintf(kv.value, "%d", mAutoBanIdentitiesLimit); + vitem->tlvkvs.pairs.push_back(kv) ; + + kv.key = "AUTO_POSITIVE_CONTACTS" ; + kv.value = mAutoSetPositiveOptionToContacts?"YES":"NO"; + vitem->tlvkvs.pairs.push_back(kv) ; + + savelist.push_back(vitem) ; return true; } @@ -955,7 +1014,23 @@ bool p3GxsReputation::loadList(std::list& loadList) mLastBannedNodesUpdate = 0 ; // force update } }; - } + if(kit->key == "AUTO_BAN_IDENTITIES_THRESHOLD") + { + float val ; + if (sscanf(kit->value.c_str(), "%f", &val) == 1) + { + mAutoBanIdentitiesLimit = val ; + std::cerr << "Setting AutoBanIdentity threshold to " << val << std::endl ; + mLastBannedNodesUpdate = 0 ; // force update + } + }; + if(kit->key == "AUTO_POSITIVE_CONTACTS") + { + mAutoSetPositiveOptionToContacts = (kit->value == "YES"); + std::cerr << "Setting AutoPositiveContacts to " << kit->value << std::endl ; + mLastBannedNodesUpdate = 0 ; // force update + } + } delete (*it); } diff --git a/libretroshare/src/services/p3gxsreputation.h b/libretroshare/src/services/p3gxsreputation.h index 8dca8cd5a..6640ded97 100644 --- a/libretroshare/src/services/p3gxsreputation.h +++ b/libretroshare/src/services/p3gxsreputation.h @@ -91,76 +91,83 @@ public: class p3GxsReputation: public p3Service, public p3Config, public RsReputations /* , public pqiMonitor */ { - public: - p3GxsReputation(p3LinkMgr *lm); - virtual RsServiceInfo getServiceInfo(); +public: + p3GxsReputation(p3LinkMgr *lm); + virtual RsServiceInfo getServiceInfo(); - /***** Interface for RsReputations *****/ - virtual bool setOwnOpinion(const RsGxsId& key_id, const Opinion& op) ; - virtual bool getReputationInfo(const RsGxsId& id, const RsPgpId &owner_id, ReputationInfo& info) ; - virtual bool isIdentityBanned(const RsGxsId& id, const RsPgpId &owner_node) ; - - virtual void setNodeAutoBanThreshold(uint32_t n) ; - virtual uint32_t nodeAutoBanThreshold() ; - - /***** overloaded from p3Service *****/ - virtual int tick(); - virtual int status(); + /***** Interface for RsReputations *****/ + virtual bool setOwnOpinion(const RsGxsId& key_id, const Opinion& op) ; + virtual bool getReputationInfo(const RsGxsId& id, const RsPgpId &owner_id, ReputationInfo& info) ; + virtual bool isIdentityBanned(const RsGxsId& id, const RsPgpId &owner_node) ; - /*! - * Interface stuff. - */ + virtual void setNodeAutoBanThreshold(uint32_t n) ; + virtual uint32_t nodeAutoBanThreshold() ; + virtual void setNodeAutoPositiveOpinionForContacts(bool b) ; + virtual bool nodeAutoPositiveOpinionForContacts() ; + virtual float nodeAutoBanIdentitiesLimit() ; + virtual void setNodeAutoBanIdentitiesLimit(float f) ; + + /***** overloaded from p3Service *****/ + virtual int tick(); + virtual int status(); + + /*! + * Interface stuff. + */ + + /************* from p3Config *******************/ + virtual RsSerialiser *setupSerialiser() ; + virtual bool saveList(bool& cleanup, std::list&) ; + virtual void saveDone(); + virtual bool loadList(std::list& load) ; - /************* from p3Config *******************/ - virtual RsSerialiser *setupSerialiser() ; - virtual bool saveList(bool& cleanup, std::list&) ; - virtual void saveDone(); - virtual bool loadList(std::list& load) ; - private: - bool processIncoming(); + bool processIncoming(); - bool SendReputations(RsGxsReputationRequestItem *request); - bool RecvReputations(RsGxsReputationUpdateItem *item); - bool updateLatestUpdate(RsPeerId peerid, time_t latest_update); - void updateActiveFriends() ; - void updateBannedNodesList(); - - // internal update of data. Takes care of cleaning empty boxes. - void locked_updateOpinion(const RsPeerId &from, const RsGxsId &about, RsReputations::Opinion op); - bool loadReputationSet(RsGxsReputationSetItem *item, const std::set &peerSet); + bool SendReputations(RsGxsReputationRequestItem *request); + bool RecvReputations(RsGxsReputationUpdateItem *item); + bool updateLatestUpdate(RsPeerId peerid, time_t latest_update); + void updateActiveFriends() ; + void updateBannedNodesList(); - int sendPackets(); - void cleanup(); - void sendReputationRequests(); - int sendReputationRequest(RsPeerId peerid); - void debug_print() ; - void updateIdentityFlags(); + // internal update of data. Takes care of cleaning empty boxes. + void locked_updateOpinion(const RsPeerId &from, const RsGxsId &about, RsReputations::Opinion op); + bool loadReputationSet(RsGxsReputationSetItem *item, const std::set &peerSet); - private: - RsMutex mReputationMtx; + int sendPackets(); + void cleanup(); + void sendReputationRequests(); + int sendReputationRequest(RsPeerId peerid); + void debug_print() ; + void updateIdentityFlags(); - time_t mLastActiveFriendsUpdate; - time_t mRequestTime; - time_t mStoreTime; - time_t mLastBannedNodesUpdate ; - bool mReputationsUpdated; - uint32_t mAverageActiveFriends ; +private: + RsMutex mReputationMtx; - p3LinkMgr *mLinkMgr; + time_t mLastActiveFriendsUpdate; + time_t mRequestTime; + time_t mStoreTime; + time_t mLastBannedNodesUpdate ; + bool mReputationsUpdated; + uint32_t mAverageActiveFriends ; - // Data for Reputation. - std::map mConfig; - std::map mReputations; - std::multimap mUpdated; + float mAutoBanIdentitiesLimit ; + bool mAutoSetPositiveOptionToContacts; - // set of Reputations to send to p3IdService. - std::set mUpdatedReputations; - - // PGP Ids auto-banned. This is updated regularly. - std::set mBannedPgpIds ; - uint32_t mPgpAutoBanThreshold ; + p3LinkMgr *mLinkMgr; + + // Data for Reputation. + std::map mConfig; + std::map mReputations; + std::multimap mUpdated; + + // set of Reputations to send to p3IdService. + std::set mUpdatedReputations; + + // PGP Ids auto-banned. This is updated regularly. + std::set mBannedPgpIds ; + uint32_t mPgpAutoBanThreshold ; }; #endif //SERVICE_RSGXSREPUTATION_HEADER diff --git a/retroshare-gui/src/gui/settings/PeoplePage.cpp b/retroshare-gui/src/gui/settings/PeoplePage.cpp index f2df90bf3..197874d90 100644 --- a/retroshare-gui/src/gui/settings/PeoplePage.cpp +++ b/retroshare-gui/src/gui/settings/PeoplePage.cpp @@ -37,19 +37,30 @@ PeoplePage::~PeoplePage() /** Saves the changes on this page */ bool PeoplePage::save(QString &/*errmsg*/) { - if(!ui.identityBan_CB->isChecked()) - rsReputations->setNodeAutoBanThreshold(0) ; - else - rsReputations->setNodeAutoBanThreshold(ui.identityBanThreshold_SB->value()) ; + if(!ui.identityBan_CB->isChecked()) + rsReputations->setNodeAutoBanThreshold(0) ; + else + rsReputations->setNodeAutoBanThreshold(ui.identityBanThreshold_SB->value()) ; - return true; + if(!ui.autoPositiveOpinion_CB->isChecked()) + rsReputations->setNodeAutoPositiveOpinionForContacts(true) ; + else + rsReputations->setNodeAutoPositiveOpinionForContacts(false) ; + + rsReputations->setNodeAutoBanIdentitiesLimit(ui.autoBanIdentitiesLimit_SB->value()); + + return true; } /** Loads the settings for this page */ void PeoplePage::load() { uint32_t ban_limit = rsReputations->nodeAutoBanThreshold() ; + bool auto_positive_contacts = rsReputations->nodeAutoPositiveOpinionForContacts() ; + float node_auto_ban_identities_limit = rsReputations->nodeAutoBanIdentitiesLimit(); ui.identityBan_CB->setChecked(ban_limit > 0) ; ui.identityBanThreshold_SB->setValue(ban_limit) ; + ui.autoPositiveOpinion_CB->setChecked(auto_positive_contacts); + ui.autoBanIdentitiesLimit_SB->setValue(node_auto_ban_identities_limit); } diff --git a/retroshare-gui/src/gui/settings/PeoplePage.ui b/retroshare-gui/src/gui/settings/PeoplePage.ui index 60a4a4121..e7d05919b 100644 --- a/retroshare-gui/src/gui/settings/PeoplePage.ui +++ b/retroshare-gui/src/gui/settings/PeoplePage.ui @@ -16,7 +16,7 @@ Identities handling - + @@ -61,6 +61,49 @@ + + + + <html><head/><body><p>Anyone in your contact list will automatically have a positive opinion. This allows to automatically raise reputations of used nodes. </p></body></html> + + + automatically give "Positive" option to my contacts + + + true + + + + + + + + + Friend options below which identities are banned: + + + + + + + <html><head/><body><p>The default value of -0.6 needs 3 to 4 friends to set a negative opinion in order for that identity to be banned at your own node. This is a pretty conservative value. If you want to more easily get rid of banned identities, set the value to e.g. -0.2</p></body></html> + + + -1.000000000000000 + + + -0.010000000000000 + + + 0.010000000000000 + + + -0.600000000000000 + + + + +