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

This commit is contained in:
csoler 2016-07-25 15:45:49 -04:00
parent efd503e7d4
commit 218977170c
5 changed files with 229 additions and 86 deletions

View File

@ -51,9 +51,16 @@ public:
virtual bool setOwnOpinion(const RsGxsId& key_id, const Opinion& op) =0; virtual bool setOwnOpinion(const RsGxsId& key_id, const Opinion& op) =0;
virtual bool getReputationInfo(const RsGxsId& id,const RsPgpId& owner_id,ReputationInfo& info) =0 ; virtual bool getReputationInfo(const RsGxsId& id,const RsPgpId& owner_id,ReputationInfo& info) =0 ;
// parameters
virtual void setNodeAutoBanThreshold(uint32_t n) =0; virtual void setNodeAutoBanThreshold(uint32_t n) =0;
virtual uint32_t nodeAutoBanThreshold() =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. // This one is a proxy designed to allow fast checking of a GXS id.
// it basically returns true if assessment is not ASSESSMENT_OK // it basically returns true if assessment is not ASSESSMENT_OK

View File

@ -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 ACTIVE_FRIENDS_ONLINE_DELAY = 86400*7 ; // 1 week.
static const int kReputationRequestPeriod = 600; // 10 mins static const int kReputationRequestPeriod = 600; // 10 mins
static const int kReputationStoreWait = 180; // 3 minutes. 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 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 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 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 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 ; mAverageActiveFriends = 0 ;
mLastBannedNodesUpdate = 0 ; mLastBannedNodesUpdate = 0 ;
mAutoBanIdentitiesLimit = REPUTATION_ASSESSMENT_THRESHOLD_X1;
mAutoSetPositiveOptionToContacts = true; // default
} }
const std::string GXS_REPUTATION_APP_NAME = "gxsreputation"; const std::string GXS_REPUTATION_APP_NAME = "gxsreputation";
@ -232,6 +235,44 @@ uint32_t p3GxsReputation::nodeAutoBanThreshold()
return mPgpAutoBanThreshold ; 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() int p3GxsReputation::status()
{ {
return 1; return 1;
@ -292,6 +333,8 @@ void p3GxsReputation::updateIdentityFlags()
to_update.push_back(rit->first) ; to_update.push_back(rit->first) ;
} }
std::list<RsGxsId> should_set_to_positive ;
for(std::list<RsGxsId>::const_iterator rit(to_update.begin());rit!=to_update.end();++rit) for(std::list<RsGxsId>::const_iterator rit(to_update.begin());rit!=to_update.end();++rit)
{ {
RsIdentityDetails details; RsIdentityDetails details;
@ -303,32 +346,40 @@ void p3GxsReputation::updateIdentityFlags()
#endif #endif
continue ; continue ;
} }
bool is_a_contact = rsIdentity->isARegularContact(*rit) ;
RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ {
RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/
std::map<RsGxsId,Reputation>::iterator it = mReputations.find(*rit) ;
std::map<RsGxsId,Reputation>::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()) if(details.mFlags & RS_IDENTITY_FLAGS_PGP_LINKED)
{ {
std::cerr << " Weird situation: item " << *rit << " has been deleted from the list??" << std::endl; it->second.mIdentityFlags |= REPUTATION_IDENTITY_FLAG_PGP_LINKED ;
continue ; it->second.mOwnerNode = details.mPgpId ;
} }
it->second.mIdentityFlags = 0 ; if(details.mFlags & RS_IDENTITY_FLAGS_PGP_KNOWN ) it->second.mIdentityFlags |= REPUTATION_IDENTITY_FLAG_PGP_KNOWN ;
if(details.mFlags & RS_IDENTITY_FLAGS_PGP_LINKED) if(mAutoSetPositiveOptionToContacts && is_a_contact && it->second.mOwnOpinion == RsReputations::OPINION_NEUTRAL)
{ should_set_to_positive.push_back(*rit) ;
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 ;
#ifdef DEBUG_REPUTATION #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 #endif
it->second.updateReputation() ; it->second.updateReputation() ;
IndicateConfigChanged(); IndicateConfigChanged();
}
} }
for(std::list<RsGxsId>::const_iterator it(should_set_to_positive.begin());it!=should_set_to_positive.end();++it)
setOwnOpinion(*it,RsReputations::OPINION_POSITIVE) ;
} }
void p3GxsReputation::cleanup() void p3GxsReputation::cleanup()
@ -748,7 +799,7 @@ bool p3GxsReputation::getReputationInfo(const RsGxsId& gxsid, const RsPgpId& own
#endif #endif
info.mAssessment = RsReputations::ASSESSMENT_BAD ; info.mAssessment = RsReputations::ASSESSMENT_BAD ;
} }
else if(info.mOverallReputationScore <= REPUTATION_ASSESSMENT_THRESHOLD_X1) else if(info.mOverallReputationScore <= mAutoBanIdentitiesLimit)
info.mAssessment = RsReputations::ASSESSMENT_BAD ; info.mAssessment = RsReputations::ASSESSMENT_BAD ;
else else
info.mAssessment = RsReputations::ASSESSMENT_OK ; info.mAssessment = RsReputations::ASSESSMENT_OK ;
@ -901,7 +952,15 @@ bool p3GxsReputation::saveList(bool& cleanup, std::list<RsItem*> &savelist)
rs_sprintf(kv.value, "%d", mPgpAutoBanThreshold); rs_sprintf(kv.value, "%d", mPgpAutoBanThreshold);
vitem->tlvkvs.pairs.push_back(kv) ; 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; return true;
} }
@ -955,7 +1014,23 @@ bool p3GxsReputation::loadList(std::list<RsItem *>& loadList)
mLastBannedNodesUpdate = 0 ; // force update 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); delete (*it);
} }

View File

@ -91,76 +91,83 @@ public:
class p3GxsReputation: public p3Service, public p3Config, public RsReputations /* , public pqiMonitor */ class p3GxsReputation: public p3Service, public p3Config, public RsReputations /* , public pqiMonitor */
{ {
public: public:
p3GxsReputation(p3LinkMgr *lm); p3GxsReputation(p3LinkMgr *lm);
virtual RsServiceInfo getServiceInfo(); virtual RsServiceInfo getServiceInfo();
/***** Interface for RsReputations *****/ /***** Interface for RsReputations *****/
virtual bool setOwnOpinion(const RsGxsId& key_id, const Opinion& op) ; virtual bool setOwnOpinion(const RsGxsId& key_id, const Opinion& op) ;
virtual bool getReputationInfo(const RsGxsId& id, const RsPgpId &owner_id, ReputationInfo& info) ; virtual bool getReputationInfo(const RsGxsId& id, const RsPgpId &owner_id, ReputationInfo& info) ;
virtual bool isIdentityBanned(const RsGxsId& id, const RsPgpId &owner_node) ; 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();
/*! virtual void setNodeAutoBanThreshold(uint32_t n) ;
* Interface stuff. 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<RsItem*>&) ;
virtual void saveDone();
virtual bool loadList(std::list<RsItem*>& load) ;
/************* from p3Config *******************/
virtual RsSerialiser *setupSerialiser() ;
virtual bool saveList(bool& cleanup, std::list<RsItem*>&) ;
virtual void saveDone();
virtual bool loadList(std::list<RsItem*>& load) ;
private: private:
bool processIncoming(); bool processIncoming();
bool SendReputations(RsGxsReputationRequestItem *request); bool SendReputations(RsGxsReputationRequestItem *request);
bool RecvReputations(RsGxsReputationUpdateItem *item); bool RecvReputations(RsGxsReputationUpdateItem *item);
bool updateLatestUpdate(RsPeerId peerid, time_t latest_update); bool updateLatestUpdate(RsPeerId peerid, time_t latest_update);
void updateActiveFriends() ; void updateActiveFriends() ;
void updateBannedNodesList(); 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<RsPeerId> &peerSet);
int sendPackets(); // internal update of data. Takes care of cleaning empty boxes.
void cleanup(); void locked_updateOpinion(const RsPeerId &from, const RsGxsId &about, RsReputations::Opinion op);
void sendReputationRequests(); bool loadReputationSet(RsGxsReputationSetItem *item, const std::set<RsPeerId> &peerSet);
int sendReputationRequest(RsPeerId peerid);
void debug_print() ;
void updateIdentityFlags();
private: int sendPackets();
RsMutex mReputationMtx; void cleanup();
void sendReputationRequests();
int sendReputationRequest(RsPeerId peerid);
void debug_print() ;
void updateIdentityFlags();
time_t mLastActiveFriendsUpdate; private:
time_t mRequestTime; RsMutex mReputationMtx;
time_t mStoreTime;
time_t mLastBannedNodesUpdate ;
bool mReputationsUpdated;
uint32_t mAverageActiveFriends ;
p3LinkMgr *mLinkMgr; time_t mLastActiveFriendsUpdate;
time_t mRequestTime;
time_t mStoreTime;
time_t mLastBannedNodesUpdate ;
bool mReputationsUpdated;
uint32_t mAverageActiveFriends ;
// Data for Reputation. float mAutoBanIdentitiesLimit ;
std::map<RsPeerId, ReputationConfig> mConfig; bool mAutoSetPositiveOptionToContacts;
std::map<RsGxsId, Reputation> mReputations;
std::multimap<time_t, RsGxsId> mUpdated;
// set of Reputations to send to p3IdService. p3LinkMgr *mLinkMgr;
std::set<RsGxsId> mUpdatedReputations;
// Data for Reputation.
// PGP Ids auto-banned. This is updated regularly. std::map<RsPeerId, ReputationConfig> mConfig;
std::set<RsPgpId> mBannedPgpIds ; std::map<RsGxsId, Reputation> mReputations;
uint32_t mPgpAutoBanThreshold ; std::multimap<time_t, RsGxsId> mUpdated;
// set of Reputations to send to p3IdService.
std::set<RsGxsId> mUpdatedReputations;
// PGP Ids auto-banned. This is updated regularly.
std::set<RsPgpId> mBannedPgpIds ;
uint32_t mPgpAutoBanThreshold ;
}; };
#endif //SERVICE_RSGXSREPUTATION_HEADER #endif //SERVICE_RSGXSREPUTATION_HEADER

View File

@ -37,19 +37,30 @@ PeoplePage::~PeoplePage()
/** Saves the changes on this page */ /** Saves the changes on this page */
bool PeoplePage::save(QString &/*errmsg*/) bool PeoplePage::save(QString &/*errmsg*/)
{ {
if(!ui.identityBan_CB->isChecked()) if(!ui.identityBan_CB->isChecked())
rsReputations->setNodeAutoBanThreshold(0) ; rsReputations->setNodeAutoBanThreshold(0) ;
else else
rsReputations->setNodeAutoBanThreshold(ui.identityBanThreshold_SB->value()) ; 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 */ /** Loads the settings for this page */
void PeoplePage::load() void PeoplePage::load()
{ {
uint32_t ban_limit = rsReputations->nodeAutoBanThreshold() ; 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.identityBan_CB->setChecked(ban_limit > 0) ;
ui.identityBanThreshold_SB->setValue(ban_limit) ; ui.identityBanThreshold_SB->setValue(ban_limit) ;
ui.autoPositiveOpinion_CB->setChecked(auto_positive_contacts);
ui.autoBanIdentitiesLimit_SB->setValue(node_auto_ban_identities_limit);
} }

View File

@ -16,7 +16,7 @@
<property name="title"> <property name="title">
<string>Identities handling</string> <string>Identities handling</string>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<item> <item>
@ -61,6 +61,49 @@
</item> </item>
</layout> </layout>
</item> </item>
<item>
<widget class="QCheckBox" name="autoPositiveOpinion_CB">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Anyone in your contact list will automatically have a positive opinion. This allows to automatically raise reputations of used nodes. &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>automatically give &quot;Positive&quot; option to my contacts</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Friend options below which identities are banned:</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="autoBanIdentitiesLimit_SB">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;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&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="minimum">
<double>-1.000000000000000</double>
</property>
<property name="maximum">
<double>-0.010000000000000</double>
</property>
<property name="singleStep">
<double>0.010000000000000</double>
</property>
<property name="value">
<double>-0.600000000000000</double>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>