Move RandBool() into random.h/cpp

This commit is contained in:
Alexander Block 2019-01-09 07:30:19 +01:00
parent e1901d24aa
commit 0dae46c2fb
3 changed files with 20 additions and 16 deletions

View File

@ -33,15 +33,6 @@ double justifyLieRate = 0;
double commitOmitRate = 0; double commitOmitRate = 0;
double commitLieRate = 0; double commitLieRate = 0;
static bool RandBool(double rate)
{
const uint64_t v = 100000000;
uint64_t r = GetRand(v + 1);
if (r <= v * rate)
return true;
return false;
}
CDKGLogger::CDKGLogger(CDKGSession& _quorumDkg, const std::string& _func) : CDKGLogger::CDKGLogger(CDKGSession& _quorumDkg, const std::string& _func) :
CDKGLogger(_quorumDkg.params.type, _quorumDkg.quorumHash, _quorumDkg.height, _quorumDkg.AreWeMember(), _func) CDKGLogger(_quorumDkg.params.type, _quorumDkg.quorumHash, _quorumDkg.height, _quorumDkg.AreWeMember(), _func)
{ {
@ -142,7 +133,7 @@ void CDKGSession::SendContributions()
logger.Printf("sending contributions\n"); logger.Printf("sending contributions\n");
if (RandBool(contributionOmitRate)) { if (GetRandBool(contributionOmitRate)) {
logger.Printf("omitting\n"); logger.Printf("omitting\n");
return; return;
} }
@ -161,7 +152,7 @@ void CDKGSession::SendContributions()
auto& m = members[i]; auto& m = members[i];
CBLSSecretKey skContrib = skContributions[i]; CBLSSecretKey skContrib = skContributions[i];
if (RandBool(contributionLieRate)) { if (GetRandBool(contributionLieRate)) {
logger.Printf("lying for %s\n", m->dmn->proTxHash.ToString()); logger.Printf("lying for %s\n", m->dmn->proTxHash.ToString());
skContrib.MakeNewKey(); skContrib.MakeNewKey();
} }
@ -310,7 +301,7 @@ void CDKGSession::ReceiveMessage(const uint256& hash, const CDKGContribution& qc
if (!qc.contributions->Decrypt(myIdx, *activeMasternodeInfo.blsKeyOperator, skContribution, PROTOCOL_VERSION)) { if (!qc.contributions->Decrypt(myIdx, *activeMasternodeInfo.blsKeyOperator, skContribution, PROTOCOL_VERSION)) {
logger.Printf("contribution from %s could not be decrypted\n", member->dmn->proTxHash.ToString()); logger.Printf("contribution from %s could not be decrypted\n", member->dmn->proTxHash.ToString());
complain = true; complain = true;
} else if (RandBool(complainLieRate)) { } else if (GetRandBool(complainLieRate)) {
logger.Printf("lying/complaining for %s\n", member->dmn->proTxHash.ToString()); logger.Printf("lying/complaining for %s\n", member->dmn->proTxHash.ToString());
complain = true; complain = true;
} }
@ -651,7 +642,7 @@ void CDKGSession::SendJustification(const std::set<uint256>& forMembers)
CBLSSecretKey skContribution = skContributions[i]; CBLSSecretKey skContribution = skContributions[i];
if (RandBool(justifyLieRate)) { if (GetRandBool(justifyLieRate)) {
logger.Printf("lying for %s\n", m->dmn->proTxHash.ToString()); logger.Printf("lying for %s\n", m->dmn->proTxHash.ToString());
skContribution.MakeNewKey(); skContribution.MakeNewKey();
} }
@ -659,7 +650,7 @@ void CDKGSession::SendJustification(const std::set<uint256>& forMembers)
qj.contributions.emplace_back(i, skContribution); qj.contributions.emplace_back(i, skContribution);
} }
if (RandBool(justifyOmitRate)) { if (GetRandBool(justifyOmitRate)) {
logger.Printf("omitting\n"); logger.Printf("omitting\n");
return; return;
} }
@ -917,7 +908,7 @@ void CDKGSession::SendCommitment()
return; return;
} }
if (RandBool(commitOmitRate)) { if (GetRandBool(commitOmitRate)) {
logger.Printf("omitting\n"); logger.Printf("omitting\n");
return; return;
} }
@ -955,7 +946,7 @@ void CDKGSession::SendCommitment()
qc.quorumVvecHash = ::SerializeHash(*vvec); qc.quorumVvecHash = ::SerializeHash(*vvec);
int lieType = -1; int lieType = -1;
if (RandBool(commitLieRate)) { if (GetRandBool(commitLieRate)) {
lieType = GetRandInt(5); lieType = GetRandInt(5);
logger.Printf("lying on commitment. lieType=%d\n", lieType); logger.Printf("lying on commitment. lieType=%d\n", lieType);
} }

View File

@ -177,6 +177,17 @@ uint256 GetRandHash()
return hash; return hash;
} }
bool GetRandBool(double rate)
{
if (rate == 0.0) {
return false;
}
const uint64_t v = 100000000;
uint64_t r = GetRand(v + 1);
return r <= v * rate;
}
FastRandomContext::FastRandomContext(bool fDeterministic) FastRandomContext::FastRandomContext(bool fDeterministic)
{ {
// The seed values have some unlikely fixed points which we avoid. // The seed values have some unlikely fixed points which we avoid.

View File

@ -21,6 +21,8 @@ uint64_t GetRand(uint64_t nMax);
int GetRandInt(int nMax); int GetRandInt(int nMax);
uint256 GetRandHash(); uint256 GetRandHash();
bool GetRandBool(double rate);
/** /**
* Function to gather random data from multiple sources, failing whenever any * Function to gather random data from multiple sources, failing whenever any
* of those source fail to provide a result. * of those source fail to provide a result.