Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scraper: Shorten display representation of verification codes #1754

Merged
merged 2 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/neuralnet/beacon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ CBitcoinAddress Beacon::GetAddress() const
return CBitcoinAddress(CTxDestination(m_public_key.GetID()));
}

std::string Beacon::GetVerificationCode() const
{
const CKeyID key_id = GetId();

return EncodeBase58(key_id.begin(), key_id.end());
}

std::string Beacon::ToString() const
{
return EncodeBase64(
Expand Down
11 changes: 11 additions & 0 deletions src/neuralnet/beacon.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ class Beacon
//!
CBitcoinAddress GetAddress() const;

//!
//! \brief Get the code that the scrapers use to verify the beacon.
//!
//! World Community Grid allows no more than 30 characters for an account's
//! username field so we format the key ID using base58 encoding to produce
//! a shorter verification code.
//!
//! \return Base58 representation of the RIPEMD-160 hash of the public key.
//!
std::string GetVerificationCode() const;

//!
//! \brief Get the legacy string representation of a version 1 beacon
//! contract.
Expand Down
25 changes: 16 additions & 9 deletions src/neuralnet/superblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,19 @@ class ScraperStatsQuorumHasher
//!
void Reset(const ScraperPendingBeaconMap& verified_beacon_id_map)
{
uint160 key_id;

WriteCompactSize(m_hasher, verified_beacon_id_map.size());
std::vector<uint160> key_ids;
key_ids.reserve(verified_beacon_id_map.size());

for (const auto& entry_pair : verified_beacon_id_map) {
key_id.SetHex(entry_pair.first);
m_hasher << key_id;
key_ids.emplace_back(entry_pair.second.key_id);
}

// Base58 encoding on the ScraperPendingBeaconMap key results
// in different ordering of entries from that of the key IDs:
//
std::sort(key_ids.begin(), key_ids.end());

m_hasher << key_ids;
}

//!
Expand Down Expand Up @@ -973,12 +978,14 @@ void Superblock::VerifiedBeacons::Reset(
m_verified.clear();
m_verified.reserve(verified_beacon_id_map.size());

uint160 key_id;

for (const auto& entry_pair : verified_beacon_id_map) {
key_id.SetHex(entry_pair.first);
m_verified.emplace_back(key_id);
m_verified.emplace_back(entry_pair.second.key_id);
}

// Base58 encoding on the ScraperPendingBeaconMap key results in different
// ordering of entries from that of the actual key IDs:
//
std::sort(m_verified.begin(), m_verified.end());
}

// -----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/qt/researcher/researchermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,13 @@ QString ResearcherModel::formatBeaconAddress() const
return QString::fromStdString(m_beacon->GetAddress().ToString());
}

QString ResearcherModel::formatBeaconKeyId() const
QString ResearcherModel::formatBeaconVerificationCode() const
{
if (!m_pending_beacon) {
return QString();
}

return QString::fromStdString(m_pending_beacon->GetId().ToString());
return QString::fromStdString(m_pending_beacon->GetVerificationCode());
}

std::vector<ProjectRow> ResearcherModel::buildProjectTable(bool with_mag) const
Expand Down
2 changes: 1 addition & 1 deletion src/qt/researcher/researchermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ResearcherModel : public QObject
QString formatBeaconAge() const;
QString formatTimeToBeaconExpiration() const;
QString formatBeaconAddress() const;
QString formatBeaconKeyId() const;
QString formatBeaconVerificationCode() const;

std::vector<ProjectRow> buildProjectTable(bool with_mag = true) const;

Expand Down
2 changes: 1 addition & 1 deletion src/qt/researcher/researcherwizardauthpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void ResearcherWizardAuthPage::refresh()
return;
}

ui->verificationCodeLabel->setText(m_researcher_model->formatBeaconKeyId());
ui->verificationCodeLabel->setText(m_researcher_model->formatBeaconVerificationCode());
}

void ResearcherWizardAuthPage::on_copyToClipboardButton_clicked()
Expand Down
12 changes: 7 additions & 5 deletions src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,15 +610,17 @@ UniValue advertisebeacon(const UniValue& params, bool fHelp)

LOCK2(cs_main, pwalletMain->cs_wallet);

const NN::AdvertiseBeaconResult result = NN::Researcher::Get()->AdvertiseBeacon();
NN::AdvertiseBeaconResult result = NN::Researcher::Get()->AdvertiseBeacon();

if (auto public_key_option = result.TryPublicKey()) {
const NN::Beacon beacon(std::move(*public_key_option));

UniValue res(UniValue::VOBJ);

res.pushKV("result", "SUCCESS");
res.pushKV("cpid", NN::Researcher::Get()->Id().ToString());
res.pushKV("public_key", public_key_option->ToString());
res.pushKV("verification_code", public_key_option->GetID().ToString());
res.pushKV("public_key", beacon.m_public_key.ToString());
res.pushKV("verification_code", beacon.GetVerificationCode());

return res;
}
Expand Down Expand Up @@ -905,7 +907,7 @@ UniValue beaconstatus(const UniValue& params, bool fHelp)
entry.pushKV("address", beacon->GetAddress().ToString());
entry.pushKV("public_key", beacon->m_public_key.ToString());
entry.pushKV("magnitude", NN::Quorum::GetMagnitude(*cpid).Floating());
entry.pushKV("verification_code", beacon->GetId().ToString());
entry.pushKV("verification_code", beacon->GetVerificationCode());
entry.pushKV("is_mine", is_mine);

active.push_back(entry);
Expand All @@ -922,7 +924,7 @@ UniValue beaconstatus(const UniValue& params, bool fHelp)
entry.pushKV("address", beacon->GetAddress().ToString());
entry.pushKV("public_key", beacon->m_public_key.ToString());
entry.pushKV("magnitude", 0);
entry.pushKV("verification_code", beacon->GetId().ToString());
entry.pushKV("verification_code", beacon->GetVerificationCode());
entry.pushKV("is_mine", is_mine);

pending.push_back(entry);
Expand Down
3 changes: 3 additions & 0 deletions src/scraper/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,23 @@ typedef std::map<std::string, ScraperBeaconEntry> ScraperBeaconMap;
struct ScraperPendingBeaconEntry
{
std::string cpid;
uint160 key_id;
int64_t timestamp;

template<typename Stream>
void Serialize(Stream& stream) const
{
stream << cpid;
stream << timestamp;
stream << key_id;
}

template<typename Stream>
void Unserialize(Stream& stream)
{
stream >> cpid;
stream >> timestamp;
stream >> key_id;
}
};

Expand Down
44 changes: 37 additions & 7 deletions src/scraper/scraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ BeaconConsensus GetConsensusBeaconList()

beaconentry.timestamp = pending_beacon.m_timestamp;
beaconentry.cpid = pending_beacon.m_cpid.ToString();
beaconentry.key_id = key_id;

consensus.mPendingMap.emplace(key_id.ToString(), std::move(beaconentry));
consensus.mPendingMap.emplace(pending_beacon.GetVerificationCode(), std::move(beaconentry));
}

return consensus;
Expand Down Expand Up @@ -1983,7 +1984,7 @@ bool DownloadProjectRacFilesByCPID(const NN::WhitelistSnapshot& projectWhitelist
{
for (const auto& entry : Consensus.mPendingMap)
{
_log(logattribute::INFO, "DownloadProjectRacFilesByCPID", "Pending beacon address "
_log(logattribute::INFO, "DownloadProjectRacFilesByCPID", "Pending beacon verification code "
+ entry.first + ", CPID " + entry.second.cpid + ", "
+ DateTimeStrFormat("%Y%m%d%H%M%S", entry.second.timestamp));
}
Expand Down Expand Up @@ -2189,7 +2190,8 @@ bool ProcessProjectRacFileByCPID(const std::string& project, const fs::path& fil
{
const std::string username = ExtractXML(data, "<name>", "</name>");

if (username.size() != 40) continue;
// Base58-encoded beacon verification code sizes fall within:
if (username.size() < 26 || username.size() > 28) continue;

// The username has to be temporarily changed to the key, so that
// it matches the key of the mPendingMap, which is the string representation
Expand Down Expand Up @@ -3357,7 +3359,14 @@ ScraperStatsAndVerifiedBeacons GetScraperStatsByConvergedManifest(const Converge
{
CDataStream part(iter->second, SER_NETWORK, 1);

part >> VerifiedBeaconMap;
try
{
part >> VerifiedBeaconMap;
}
catch (const std::exception& e)
{
_log(logattribute::WARNING, __func__, "failed to deserialize verified beacons part: " + std::string(e.what()));
}

++exclude_parts_from_count;
}
Expand Down Expand Up @@ -3477,7 +3486,14 @@ ScraperStatsAndVerifiedBeacons GetScraperStatsFromSingleManifest(CScraperManifes
{
CDataStream part(iter->second, SER_NETWORK, 1);

part >> VerifiedBeaconMap;
try
{
part >> VerifiedBeaconMap;
}
catch (const std::exception& e)
{
_log(logattribute::WARNING, __func__, "failed to deserialize verified beacons part: " + std::string(e.what()));
}

++exclude_parts_from_count;
}
Expand Down Expand Up @@ -4914,7 +4930,14 @@ std::vector<uint160> GetVerifiedBeaconIDs(const ConvergedManifest& StructConverg
{
CDataStream part(iter->second, SER_NETWORK, 1);

part >> VerifiedBeaconMap;
try
{
part >> VerifiedBeaconMap;
}
catch (const std::exception& e)
{
_log(logattribute::WARNING, __func__, "failed to deserialize verified beacons part: " + std::string(e.what()));
}

for (const auto& entry : VerifiedBeaconMap)
{
Expand Down Expand Up @@ -4955,7 +4978,14 @@ ScraperStatsAndVerifiedBeacons GetScraperStatsAndVerifiedBeacons(const Converged
{
CDataStream part(iter->second, SER_NETWORK, 1);

part >> stats_and_verified_beacons.mVerifiedMap;
try
{
part >> stats_and_verified_beacons.mVerifiedMap;
}
catch (const std::exception& e)
{
_log(logattribute::WARNING, __func__, "failed to deserialize verified beacons part: " + std::string(e.what()));
}
}

stats_and_verified_beacons.mScraperStats = stats.mScraperConvergedStats;
Expand Down
34 changes: 33 additions & 1 deletion src/test/neuralnet/beacon_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,30 @@ struct TestKey
});
}

//!
//! \brief Create a key ID from the test public key.
//!
static CKeyID KeyId()
{
return Public().GetID();
}

//!
//! \brief Create an address from the test public key.
//!
static CBitcoinAddress Address()
{
return CBitcoinAddress(CTxDestination(Public().GetID()));
return CBitcoinAddress(CTxDestination(KeyId()));
}

//!
//! \brief Create a beacon verification code from the test public key.
//!
static std::string VerificationCode()
{
const CKeyID key_id = KeyId();

return EncodeBase58(key_id.begin(), key_id.end());
}

//!
Expand Down Expand Up @@ -165,13 +183,27 @@ BOOST_AUTO_TEST_CASE(it_determines_whether_a_beacon_is_renewable)
BOOST_CHECK(renewable.Renewable(NN::Beacon::RENEWAL_AGE + 1) == true);
}

BOOST_AUTO_TEST_CASE(it_produces_a_key_id)
{
const NN::Beacon beacon(TestKey::Public());

BOOST_CHECK(beacon.GetId() == TestKey::KeyId());
}

BOOST_AUTO_TEST_CASE(it_produces_a_rain_address)
{
const NN::Beacon beacon(TestKey::Public());

BOOST_CHECK(beacon.GetAddress() == TestKey::Address());
}

BOOST_AUTO_TEST_CASE(it_produces_a_verification_code)
{
const NN::Beacon beacon(TestKey::Public());

BOOST_CHECK(beacon.GetVerificationCode() == TestKey::VerificationCode());
}

BOOST_AUTO_TEST_CASE(it_represents_itself_as_a_legacy_string)
{
const NN::Beacon beacon(TestKey::Public());
Expand Down
26 changes: 19 additions & 7 deletions src/test/neuralnet/superblock_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "base58.h"
#include "compat/endian.h"
#include "neuralnet/superblock.h"
#include "streams.h"
Expand Down Expand Up @@ -387,12 +388,22 @@ const ScraperStatsAndVerifiedBeacons GetTestScraperStats(const ScraperStatsMeta&
p2.statsvalue.dMag = meta.p2_mag;
stats_and_verified_beacons.mScraperStats.emplace(p2.statskey, p2);

ScraperPendingBeaconEntry pendingBeaconEntry1;
pendingBeaconEntry1.cpid = meta.cpid1_str;
pendingBeaconEntry1.key_id = meta.beacon_id_1;
pendingBeaconEntry1.timestamp = 100;

ScraperPendingBeaconEntry pendingBeaconEntry2;
pendingBeaconEntry2.cpid = meta.cpid2_str;
pendingBeaconEntry2.key_id = meta.beacon_id_2;
pendingBeaconEntry2.timestamp = 200;

stats_and_verified_beacons.mVerifiedMap.emplace(
meta.beacon_id_1.ToString(),
ScraperPendingBeaconEntry());
EncodeBase58(meta.beacon_id_1.begin(), meta.beacon_id_1.end()),
pendingBeaconEntry1);
stats_and_verified_beacons.mVerifiedMap.emplace(
meta.beacon_id_2.ToString(),
ScraperPendingBeaconEntry());
EncodeBase58(meta.beacon_id_2.begin(), meta.beacon_id_2.end()),
pendingBeaconEntry2);

return stats_and_verified_beacons;
}
Expand All @@ -407,9 +418,10 @@ ConvergedScraperStats GetTestConvergence(
const ScraperStatsMeta& meta,
const bool by_parts = false)
{
const ScraperStatsAndVerifiedBeacons stats = GetTestScraperStats(meta);
ConvergedScraperStats convergence;

convergence.mScraperConvergedStats = GetTestScraperStats(meta).mScraperStats;
convergence.mScraperConvergedStats = stats.mScraperStats;

convergence.Convergence.bByParts = by_parts;
convergence.Convergence.nContentHash
Expand All @@ -423,8 +435,8 @@ ConvergedScraperStats GetTestConvergence(
CDataStream verified_beacons_part(SER_NETWORK, PROTOCOL_VERSION);
verified_beacons_part
<< ScraperPendingBeaconMap {
{ meta.beacon_id_1.ToString(), ScraperPendingBeaconEntry() },
{ meta.beacon_id_2.ToString(), ScraperPendingBeaconEntry() },
*stats.mVerifiedMap.begin(),
*++stats.mVerifiedMap.begin(),
};

convergence.Convergence.ConvergedManifestPartsMap.emplace(
Expand Down