forked from gridcoin-community/Gridcoin-Research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresearcher.cpp
1437 lines (1184 loc) · 40.6 KB
/
researcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2014-2021 The Gridcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or https://opensource.org/licenses/mit-license.php.
#include "init.h"
#include "gridcoin/appcache.h"
#include "gridcoin/backup.h"
#include "gridcoin/beacon.h"
#include "gridcoin/boinc.h"
#include "gridcoin/contract/message.h"
#include "gridcoin/magnitude.h"
#include "gridcoin/project.h"
#include "gridcoin/quorum.h"
#include "gridcoin/researcher.h"
#include "gridcoin/support/xml.h"
#include "gridcoin/tally.h"
#include "span.h"
#include "node/ui_interface.h"
#include "util.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <optional>
#include <openssl/md5.h>
#include <set>
#include <univalue.h>
using namespace GRC;
extern CCriticalSection cs_main;
extern CCriticalSection cs_ScraperGlobals;
extern std::string msMiningErrors;
extern unsigned int nActiveBeforeSB;
std::vector<MiningPool> MiningPools::GetMiningPools()
{
return m_mining_pools;
}
MiningPools g_mining_pools;
namespace {
//!
//! \brief Global BOINC researcher mining context.
//!
ResearcherPtr g_researcher = std::make_shared<Researcher>();
//!
//! \brief Indicates whether the researcher context needs a refresh.
//!
std::atomic<bool> g_researcher_dirty(true);
//!
//! \brief Change investor mode and set the email address directive in the
//! read-write JSON settings file
//!
//! \param email The email address to update the directive to. If empty, set
//! the configuration to investor mode.
//!
//! \return \c false if an error occurs during the update.
//!
bool UpdateRWSettingsForMode(const ResearcherMode mode, const std::string& email)
{
std::vector<std::pair<std::string, util::SettingsValue>> settings;
if (mode == ResearcherMode::INVESTOR) {
settings.push_back(std::make_pair("email", util::SettingsValue(UniValue::VNULL)));
settings.push_back(std::make_pair("investor", "1"));
} else if (mode == ResearcherMode::SOLO) {
settings.push_back(std::make_pair("email", util::SettingsValue(email)));
settings.push_back(std::make_pair("investor", "0"));
}
return ::updateRwSettings(settings);
}
//!
//! \brief Convert a project name to lowercase change any underscores to spaces.
//!
//! NOTICE: Because the application merges the loaded BOINC project details into
//! the output of the "projects" RPC method, project whitelist contracts created
//! by the master key holder must embed the project names into the contract just
//! as output by this function or the method will omit projects with mismatching
//! names from the result.
//!
//! \return A "normalized" project name.
//!
std::string LowerUnderscore(std::string data)
{
data = ToLower(data);
boost::replace_all(data, "_", " ");
return data;
}
//!
//! \brief Extract the authority component (hostname:port) from a URL.
//!
//! \param URL to extract the authority component from.
//!
//! \return A view of the URL string for the authority component.
//!
Span<const char> ParseUrlHostname(const std::string& url)
{
const auto url_end = url.end();
auto domain_begin = url.begin();
auto scheme_end = std::find(domain_begin, url_end, ':');
if (std::distance(scheme_end, url_end) >= 3) {
if (*++scheme_end == '/' && *++scheme_end == '/') {
domain_begin = ++scheme_end;
}
}
auto domain_end = std::find(domain_begin, url_end, '/');
if (domain_end == url_end) {
domain_end = std::find(domain_begin, url_end, '?');
}
return Span<const char>(&*domain_begin, &*domain_end);
}
//!
//! \brief Determine whether two URLs contain the same authority component
//! (hostname and port number).
//!
//! \param url_1 First BOINC project website URL to compare.
//! \param url_2 Second BOINC project website URL to compare.
//!
//! \return \c true if both URLs contain the same authority component.
//!
bool CompareProjectHostname(const std::string& url_1, const std::string& url_2)
{
return ParseUrlHostname(url_1) == ParseUrlHostname(url_2);
}
//!
//! \brief Attempt to match the local project loaded from BOINC with a project
//! on the Gridcoin whitelist.
//!
//! \param project Project loaded from BOINC to compare.
//! \param whitelist A snapshot of the current projects on the whitelist.
//!
//! \return A pointer to the whitelist project if it matches.
//!
const Project* ResolveWhitelistProject(
const MiningProject& project,
const WhitelistSnapshot& whitelist)
{
for (const auto& whitelist_project : whitelist) {
if (project.m_name == whitelist_project.m_name) {
return &whitelist_project;
}
// Sometimes project whitelist contracts contain a name different from
// the project name specified in client_state.xml. The URLs will often
// match in this case. We just check the authority component:
//
if (CompareProjectHostname(project.m_url, whitelist_project.m_url)) {
return &whitelist_project;
}
}
return nullptr;
}
//!
//! \brief Determine whether the provided CPID belongs to a Gridcoin pool.
//!
//! \param cpid An external CPID for a project loaded from BOINC.
//!
//! \return \c true if the CPID matches a known Gridcoin pool's CPID.
//!
bool IsPoolCpid(const Cpid cpid)
{
for (const auto& pool : g_mining_pools.GetMiningPools()) {
if (pool.m_cpid == cpid) {
return true;
}
}
return false;
}
//!
//! \brief Determine whether the provided username belongs to a Gridcoin pool.
//!
//! \param cpid The BOINC account username for a project loaded from BOINC.
//!
//! \return \c true if the username matches a known Gridcoin pool's username.
//!
bool IsPoolUsername(const std::string& username)
{
for (const auto& pool : g_mining_pools.GetMiningPools()) {
if (pool.m_name == username) {
return true;
}
}
return false;
}
//!
//! \brief Fetch the contents of BOINC's client_state.xml file from disk.
//!
//! \return The entire client_state.xml file as a string if readable.
//!
std::optional<std::string> ReadClientStateXml()
{
const fs::path path = GetBoincDataDir();
std::string contents = GetFileContents(path / "client_state.xml");
if (contents != "-1") {
return std::make_optional(std::move(contents));
}
LogPrintf("WARNING: Unable to obtain BOINC CPIDs.");
if (!gArgs.GetArg("-boincdatadir", "").empty()) {
LogPrintf("Could not access configured BOINC data directory %s", path.string());
} else {
LogPrintf(
"BOINC data directory is not installed in the default location.\n"
"Please specify its current location in gridcoinresearch.conf.");
}
return std::nullopt;
}
//!
//! \brief Load a set of project XML sections from BOINC's client_state.xml
//! file to gather CPIDs from.
//!
//! \return Items containing the XML between each \c <project>...</project>
//! section present in the client_state.xml file or an empty container when
//! the file is not accessible or contains no project elements.
//!
std::vector<std::string> FetchProjectsXml()
{
const std::optional<std::string> client_state_xml = ReadClientStateXml();
if (!client_state_xml) {
return { };
}
std::vector<std::string> projects = split(*client_state_xml, "<project>");
if (projects.size() < 2) {
LogPrintf("BOINC is not attached to any projects. No CPIDs loaded.");
return { };
}
// Drop the first element which never contains a project:
//
std::swap(projects.front(), projects.back());
projects.pop_back();
return projects;
}
//!
//! \brief Determine whether BOINC projects should be checked for membership in
//! a whitelisted team before enabling the associated CPID.
//!
//! \return \c true when the protocol is configured to require team membership
//! or when no protocol directive exists.
//!
bool ShouldEnforceTeamMembership()
{
return ReadCache(Section::PROTOCOL, "REQUIRE_TEAM_WHITELIST_MEMBERSHIP").value != "false";
}
//!
//! \brief Fetch the current set of whitelisted teams.
//!
//! \return The set of whitelisted teams configured in the protocol or a set
//! with only team "gridcoin" when no protocol directive exists. Supplies an
//! empty set when the team requirement is not active.
//!
std::set<std::string> GetTeamWhitelist()
{
if (!ShouldEnforceTeamMembership()) {
return { };
}
const AppCacheEntry entry = ReadCache(Section::PROTOCOL, "TEAM_WHITELIST");
if (entry.value.empty()) {
return { "gridcoin" };
}
std::set<std::string> teams;
std::string delimiter = "<>";
// Handle the transition of the team whitelist delimiter from "|" to "<>":
if (entry.value.find(delimiter) == std::string::npos) {
delimiter = "|";
}
for (auto&& team_name : split(entry.value, delimiter)) {
if (team_name.empty()) {
continue;
}
team_name = ToLower(team_name);
teams.emplace(std::move(team_name));
}
if (teams.empty()) {
return { "gridcoin" };
}
return teams;
}
//!
//! \brief Select the provided project's CPID if the project passes the rules
//! for eligibility.
//!
//! \param mining_id Updated with the project CPID if eligible.
//! \param project Project with the CPID to determine eligibility for.
//!
void TryProjectCpid(MiningId& mining_id, const MiningProject& project)
{
switch (project.m_error) {
case MiningProject::Error::NONE:
break; // Suppress warning.
case MiningProject::Error::MALFORMED_CPID:
LogPrintf("Invalid external CPID for project %s.", project.m_name);
return;
case MiningProject::Error::MISMATCHED_CPID:
LogPrintf("CPID mismatch. Check email for %s.", project.m_name);
return;
case MiningProject::Error::INVALID_TEAM:
LogPrintf("Project %s's team is not whitelisted.", project.m_name);
return;
case MiningProject::Error::POOL:
LogPrintf("Project %s is attached to a pool.", project.m_name);
return;
}
mining_id = project.m_cpid;
LogPrintf(
"Found eligible project %s with CPID %s.",
project.m_name,
project.m_cpid.ToString());
}
//!
//! \brief Compute an external CPID from the supplied internal CPID and the
//! configured email address.
//!
//! A bug in BOINC sometimes results in an empty external CPID element in the
//! client_state.xml file. For these cases, we'll recompute the external CPID
//! of the project from the user's internal CPID and email address. This call
//! validates that the user's email address hash extracted from a project
//! XML node matches the email set in the Gridcoin configuration file so that
//! the wallet doesn't inadvertently generate an unowned CPID.
//!
//! \param email_hash MD5 digest of the email address to compare with
//! the configured email.
//! \param internal_cpid As extracted from client_state.xml. An input to the
//! hash that generates the external CPID.
//!
//! \return The computed external CPID if the hash of the configured email
//! address matches the supplied email address hash.
//!
std::optional<Cpid> FallbackToCpidByEmail(
const std::string& email_hash,
const std::string& internal_cpid)
{
if (email_hash.empty() || internal_cpid.empty()) {
return std::nullopt;
}
const std::string email = Researcher::Email();
std::vector<unsigned char> email_hash_bytes(16);
MD5(reinterpret_cast<const unsigned char*>(email.data()),
email.size(),
email_hash_bytes.data());
if (HexStr(email_hash_bytes) != email_hash) {
return std::nullopt;
}
return Cpid::Hash(internal_cpid, email);
}
//!
//! \brief Try to detect a split CPID and log a warning message.
//!
//! In the future, we can extend this to display a warning in the UI.
//!
//! \param projects Map of local projects loaded from BOINC's client_state.xml
//! file.
//!
void DetectSplitCpid(const MiningProjectMap& projects)
{
std::unordered_map<Cpid, std::string> eligible_cpids;
for (const auto& project_pair : projects) {
if (project_pair.second.Eligible()) {
eligible_cpids.emplace(
project_pair.second.m_cpid,
project_pair.second.m_name);
}
}
if (eligible_cpids.size() > 1) {
std::string warning = "WARNING: Detected potential CPID split. ";
warning += "Eligible CPIDs: \n";
for (const auto& cpid_pair : eligible_cpids) {
warning += " " + cpid_pair.first.ToString();
warning += " (" + cpid_pair.second + ")\n";
}
LogPrintf("%s", warning);
}
}
//!
//! \brief Set the global BOINC researcher context.
//!
//! \param context Contains the CPID and local projects loaded from BOINC.
//!
void StoreResearcher(Researcher context)
{
// TODO: this belongs in presentation layer code:
switch (context.Status()) {
case ResearcherStatus::ACTIVE:
msMiningErrors = _("Eligible for Research Rewards");
break;
case ResearcherStatus::POOL:
msMiningErrors = _("Staking Only - Pool Detected");
break;
case ResearcherStatus::NO_PROJECTS:
msMiningErrors = _("Staking Only - No Eligible Research Projects");
break;
case ResearcherStatus::NO_BEACON:
msMiningErrors = _("Staking Only - No active beacon");
break;
case ResearcherStatus::INVESTOR:
msMiningErrors = _("Staking Only - Investor Mode");
break;
}
std::atomic_store(
&g_researcher,
std::make_shared<Researcher>(std::move(context)));
g_researcher_dirty = false;
uiInterface.ResearcherChanged();
}
//!
//! \brief A piece of helper state that keeps track of recently-advertised
//! pending beacons.
//!
class RecentBeacons
{
public:
//!
//! \brief Load the set of pending beacons that match the node's private
//! keys.
//!
//! THREAD SAFETY: Lock cs_main and pwalletMain->cs_wallet before calling
//! this method.
//!
//! \param beacons Contains the set of pending beacons to import from.
//!
void ImportRegistry(const BeaconRegistry& beacons) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pwalletMain->cs_wallet)
{
AssertLockHeld(cs_main);
AssertLockHeld(pwalletMain->cs_wallet);
for (const auto& pending_pair : beacons.PendingBeacons()) {
const CKeyID& key_id = pending_pair.first;
const PendingBeacon beacon = static_cast<PendingBeacon>(*pending_pair.second);
if (pwalletMain->HaveKey(key_id)) {
auto iter_pair = m_pending.emplace(beacon.m_cpid, beacon);
iter_pair.first->second.m_timestamp = beacon.m_timestamp;
}
}
}
//!
//! \brief Fetch the pending beacon for the specified CPID if it exists.
//!
//! \param cpid CPID that the beacon was advertised for.
//!
//! \return A pointer to the pending beacon if one exists for the CPID.
//!
const PendingBeacon* Try(const Cpid cpid) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
const auto iter = m_pending.find(cpid);
if (iter == m_pending.end()) {
return nullptr;
}
if (iter->second.Expired(GetAdjustedTime())) {
m_pending.erase(iter);
return nullptr;
}
// If the pending beacon activated, don't report it as pending still:
//
if (const BeaconOption beacon = GetBeaconRegistry().Try(cpid)) {
if (beacon->m_public_key == iter->second.m_public_key) {
m_pending.erase(iter);
return nullptr;
}
}
return &iter->second;
}
//!
//! \brief Stash a beacon that the node just advertised to the network.
//!
//! \param cpid CPID that the beacon was advertised for.
//! \param result Contains the public key if the transaction succeeded.
//!
void Remember(const Cpid cpid, const AdvertiseBeaconResult& result) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pwalletMain->cs_wallet)
{
AssertLockHeld(cs_main);
if (const CPubKey* key = result.TryPublicKey()) {
auto iter_pair = m_pending.emplace(cpid, PendingBeacon(cpid, *key));
iter_pair.first->second.m_timestamp = GetAdjustedTime();
}
}
private:
std::map<Cpid, PendingBeacon> m_pending; //!< Known set of pending beacons.
}; // RecentBeacons
//!
//! \brief A cache of recently-advertised pending beacons.
//!
//! THREAD SAFETY: Lock cs_main before calling methods on this object.
//!
RecentBeacons g_recent_beacons;
//!
//! \brief Determine whether the wallet contains a valid private key that
//! matches the supplied beacon public key.
//!
//! \param public_key Identifies the corresponding private key.
//!
//! \return \c true if the wallet contains a matching private key.
//!
bool CheckBeaconPrivateKey(const CWallet* const wallet, const CPubKey& public_key)
{
CKey out_key;
if (!wallet->GetKey(public_key.GetID(), out_key)) {
return error("%s: Key not found", __func__);
}
if (!out_key.IsValid()) {
return error("%s: Invalid stored key", __func__);
}
if (out_key.GetPubKey() != public_key) {
return error("%s: Public key mismatch", __func__);
}
return true;
}
//!
//! \brief Generate a new beacon key pair.
//!
//! \param cpid The participant's current primary CPID.
//!
//! \return A variant that contains the new public key if successful or a
//! description of the error that occurred.
//!
AdvertiseBeaconResult GenerateBeaconKey(const Cpid& cpid)
{
LogPrintf("%s: Generating new keys for %s...", __func__, cpid.ToString());
CPubKey public_key;
if (!pwalletMain->GetKeyFromPool(public_key, false)) {
LogPrintf("ERROR: %s: Failed to get new key from wallet", __func__);
return BeaconError::MISSING_KEY;
}
// We label this key in the GUI to indicate to the user that it is a beacon key.
// The block number is also included because there are situations where there
// could be multiple beacon keys in the wallet due to expiration or forced re-
// advertisement.
const std::string address_label = strprintf(
"Beacon Address for CPID %s (at %" PRIu64 ")",
cpid.ToString(),
static_cast<uint64_t>(nBestHeight));
if (!pwalletMain->SetAddressBookName(public_key.GetID(), address_label)) {
LogPrintf("WARNING: %s: Failed to change beacon key label", __func__);
}
if (!CheckBeaconPrivateKey(pwalletMain, public_key)) {
LogPrintf("ERROR: %s: Failed to verify new beacon key", __func__);
return BeaconError::MISSING_KEY;
}
if (!BackupWallet(*pwalletMain, GetBackupFilename("wallet.dat"))) {
LogPrintf("WARNING: %s: Failed to backup wallet file", __func__);
}
return public_key;
}
//!
//! \brief Sign a new beacon payload with the beacon's private key.
//!
//! \param payload The beacon payload to sign.
//!
//! \return \c false if the key doesn't exist or fails to sign the payload.
//!
bool SignBeaconPayload(BeaconPayload& payload)
{
CKey out_key;
if (!pwalletMain->GetKey(payload.m_beacon.m_public_key.GetID(), out_key)) {
return error("%s: Key not found", __func__);
}
if (!payload.Sign(out_key)) {
return error("%s: Failed to sign payload", __func__);
}
return true;
}
//!
//! \brief Determine whether a wallet can send a beacon transaction.
//!
//! \param wallet The wallet that will hold the key and pay for the beacon.
//!
//! \return An error that describes why the wallet cannot send a beacon if
//! a transaction will not succeed.
//!
BeaconError CheckBeaconTransactionViable(const CWallet& wallet)
{
if (pwalletMain->IsLocked()) {
LogPrintf("WARNING: %s: Wallet locked.", __func__);
return BeaconError::WALLET_LOCKED;
}
// Ensure that the wallet contains enough coins to send a transaction for
// the contract.
//
// TODO: refactor wallet so we can determine this dynamically. For now, we
// require 1 GRC:
//
if (pwalletMain->GetBalance() < COIN) {
LogPrintf("WARNING: %s: Insufficient funds.", __func__);
return BeaconError::INSUFFICIENT_FUNDS;
}
return BeaconError::NONE;
}
//!
//! \brief Send a transaction that contains a beacon contract.
//!
//! \param cpid CPID to send a beacon for.
//! \param beacon Contains the CPID's beacon public key.
//! \param action Determines whether to add or remove a beacon.
//!
//! \return A variant that contains the new public key if successful or a
//! description of the error that occurred.
//!
AdvertiseBeaconResult SendBeaconContract(
const Cpid& cpid,
Beacon beacon,
ContractAction action = ContractAction::ADD)
{
const BeaconError error = CheckBeaconTransactionViable(*pwalletMain);
if (error != BeaconError::NONE) {
return error;
}
BeaconPayload payload(cpid, beacon);
if (!SignBeaconPayload(payload)) {
return BeaconError::MISSING_KEY;
}
const auto result_pair = SendContract(
MakeContract<BeaconPayload>(action, std::move(payload)));
if (!result_pair.second.empty()) {
return BeaconError::TX_FAILED;
}
return AdvertiseBeaconResult(std::move(beacon.m_public_key));
}
//!
//! \brief Generate keys for and send a new beacon contract.
//!
//! \param cpid The CPID to create the beacon for.
//!
//! \return A variant that contains the new public key if successful or a
//! description of the error that occurred.
//!
AdvertiseBeaconResult SendNewBeacon(const Cpid& cpid)
{
// First, determine whether we can successfully send a beacon contract. The
// wallet must be unlocked and hold a balance great enough to send a beacon
// transaction. Otherwise, we may create a bogus beacon key that lingers in
// the wallet:
//
const BeaconError error = CheckBeaconTransactionViable(*pwalletMain);
if (error != BeaconError::NONE) {
return error;
}
AdvertiseBeaconResult result = GenerateBeaconKey(cpid);
if (auto key_option = result.TryPublicKey()) {
result = SendBeaconContract(cpid, std::move(*key_option));
}
return result;
}
//!
//! \brief Send a contract that renews an existing beacon.
//!
//! \param cpid The CPID to create the beacon for.
//! \param beacon Contains the public key to renew.
//!
//! \return A variant that contains the public key if successful or a
//! description of the error that occurred.
//!
AdvertiseBeaconResult RenewBeacon(const Cpid& cpid, const Beacon& beacon)
{
if (!beacon.Renewable(GetAdjustedTime())) {
LogPrintf("%s: Beacon renewal not needed", __func__);
return BeaconError::NOT_NEEDED;
}
LogPrintf("%s: Renewing beacon for %s", __func__, cpid.ToString());
const BeaconError error = CheckBeaconTransactionViable(*pwalletMain);
if (error != BeaconError::NONE) {
return error;
}
// A participant may run the wallet on two computers, but only one computer
// holds the beacon private key. If BOINC also exists on both computers for
// the same CPID, both nodes will attempt to renew the beacon. Only allow a
// node with the private key to send the contract:
//
if (!CheckBeaconPrivateKey(pwalletMain, beacon.m_public_key)) {
LogPrintf("WARNING: %s: Missing or invalid private key", __func__);
return BeaconError::MISSING_KEY;
}
return SendBeaconContract(cpid, beacon);
}
} // anonymous namespace
// -----------------------------------------------------------------------------
// Class: MiningProject
// -----------------------------------------------------------------------------
MiningProject::MiningProject(std::string name,
Cpid cpid,
std::string team,
std::string url,
double rac)
: m_name(LowerUnderscore(std::move(name)))
, m_cpid(std::move(cpid))
, m_team(std::move(team))
, m_url(std::move(url))
, m_rac(std::move(rac))
, m_error(Error::NONE)
{
m_team = ToLower(m_team);
}
MiningProject MiningProject::Parse(const std::string& xml)
{
double rac = 0.0;
if (!ParseDouble(ExtractXML(xml, "<user_expavg_credit>", "</user_expavg_credit>"), &rac)) {
LogPrintf("WARN: %s: Unable to parse user RAC from legacy XML data.", __func__);
}
MiningProject project(
ExtractXML(xml, "<project_name>", "</project_name>"),
Cpid::Parse(ExtractXML(xml, "<external_cpid>", "</external_cpid>")),
ExtractXML(xml, "<team_name>", "</team_name>"),
ExtractXML(xml, "<master_url>", "</master_url>"),
rac);
if (IsPoolCpid(project.m_cpid) && !gArgs.GetBoolArg("-pooloperator", false)) {
project.m_error = MiningProject::Error::POOL;
return project;
}
if (project.m_cpid.IsZero()) {
const std::string external_cpid
= ExtractXML(xml, "<external_cpid>", "</external_cpid>");
// Old BOINC server versions may not provide an external CPID element
// in client_state.xml. For these cases, we'll recompute the external
// CPID of the project from the internal CPID and email address:
//
if (external_cpid.empty()) {
if (const std::optional<Cpid> cpid = FallbackToCpidByEmail(
ExtractXML(xml, "<email_hash>", "</email_hash>"),
ExtractXML(xml, "<cross_project_id>", "</cross_project_id>")))
{
project.m_cpid = *cpid;
return project;
}
}
// Since we cannot match the project using a solo cruncher's email
// address above, the project may be attached to a pool. We cannot
// compare the pool's external CPID so we check the username. This
// is not as accurate, but it prevents the GUI from displaying the
// "malformed CPID" notice to pool users for BOINC servers that do
// not reply with an external CPID:
//
if (IsPoolUsername(ExtractXML(xml, "<user_name>", "</user_name>"))
&& !gArgs.GetBoolArg("-pooloperator", false))
{
project.m_error = MiningProject::Error::POOL;
return project;
}
// For the extremely rare case that a BOINC project assigned a user a
// CPID that contains only zeroes, double check that a CPID parsed to
// zero is actually invalid:
//
if (MiningId::Parse(external_cpid).Which() != MiningId::Kind::CPID) {
project.m_error = MiningProject::Error::MALFORMED_CPID;
return project;
}
}
// We compare the digest of the internal CPID and email address to the
// external CPID as a smoke test to avoid running with corrupted CPIDs.
//
if (!project.m_cpid.Matches(
ExtractXML(xml, "<cross_project_id>", "</cross_project_id>"),
Researcher::Email()))
{
project.m_error = MiningProject::Error::MISMATCHED_CPID;
}
return project;
}
bool MiningProject::Eligible() const
{
return m_error == Error::NONE;
}
const Project* MiningProject::TryWhitelist(const WhitelistSnapshot& whitelist) const
{
return ResolveWhitelistProject(*this, whitelist);
}
bool MiningProject::Whitelisted(const WhitelistSnapshot& whitelist) const
{
return TryWhitelist(whitelist) != nullptr;
}
std::string MiningProject::ErrorMessage() const
{
switch (m_error) {
case Error::NONE: return "";
case Error::INVALID_TEAM: return _("Invalid team");
case Error::MALFORMED_CPID: return _("Malformed CPID");
case Error::MISMATCHED_CPID: return _("Project email mismatch");
case Error::POOL: return _("Pool");
}
return _("Unknown error");
}
// -----------------------------------------------------------------------------
// Class: MiningProjectMap
// -----------------------------------------------------------------------------
MiningProjectMap::MiningProjectMap() : m_has_pool_project(false)
{
}
MiningProjectMap MiningProjectMap::Parse(const std::vector<std::string>& xml)
{
MiningProjectMap projects;
for (const auto& project_xml : xml) {
MiningProject project = MiningProject::Parse(project_xml);
if (project.m_name.empty()) {
LogPrintf("Skipping invalid BOINC project with empty name.");
continue;
}
projects.Set(std::move(project));
}
return projects;
}
MiningProjectMap::const_iterator MiningProjectMap::begin() const
{
return m_projects.begin();
}
MiningProjectMap::const_iterator MiningProjectMap::end() const
{
return m_projects.end();
}
MiningProjectMap::size_type MiningProjectMap::size() const
{
return m_projects.size();
}
bool MiningProjectMap::empty() const
{
return m_projects.empty();
}
bool MiningProjectMap::ContainsPool() const
{
return m_has_pool_project;
}
ProjectOption MiningProjectMap::Try(const std::string& name) const
{
const auto iter = m_projects.find(name);
if (iter == m_projects.end()) {
return nullptr;
}
return &iter->second;
}
void MiningProjectMap::Set(MiningProject project)
{
m_has_pool_project |= project.m_error == MiningProject::Error::POOL;
m_projects.emplace(project.m_name, std::move(project));
}
void MiningProjectMap::ApplyTeamWhitelist(const std::set<std::string>& teams)
{
for (auto& project_pair : m_projects) {
MiningProject& project = project_pair.second;
switch (project.m_error) {
case MiningProject::Error::NONE:
if (!teams.empty() && !teams.count(project.m_team)) {
project.m_error = MiningProject::Error::INVALID_TEAM;
}
break;
case MiningProject::Error::INVALID_TEAM:
if (teams.empty() || teams.count(project.m_team)) {
project.m_error = MiningProject::Error::NONE;
}
break;
default:
continue;
}
}
}
// -----------------------------------------------------------------------------
// Class: AdvertiseBeaconResult
// -----------------------------------------------------------------------------
AdvertiseBeaconResult::AdvertiseBeaconResult(CPubKey public_key)
: m_result(std::move(public_key))