Skip to content

Commit 4b2de77

Browse files
committed
bdap: refactor wallet-dependent logic to their own files
1 parent 7e31544 commit 4b2de77

15 files changed

Lines changed: 1716 additions & 1005 deletions

src/Makefile.am

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ libdynamic_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(DYNAMIC_INCLUDES)
381381
libdynamic_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
382382
libdynamic_wallet_a_SOURCES = \
383383
bdap/stealth.cpp \
384+
bdap/wallet/keys.cpp \
385+
bdap/wallet/wallet.cpp \
386+
bdap/wallet/linkmanager.cpp \
387+
bdap/wallet/utils.cpp \
388+
bdap/wallet/vgp.cpp \
384389
keepass.cpp \
385390
policy/rbf.cpp \
386391
privatesend-client.cpp \
@@ -475,6 +480,7 @@ libdynamic_common_a_SOURCES = \
475480
base58.cpp \
476481
bdap/stealth.cpp \
477482
bip39.cpp \
483+
bdap/wallet/keys.cpp \
478484
chainparams.cpp \
479485
coins.cpp \
480486
compressor.cpp \
@@ -628,6 +634,7 @@ CLEANFILES = leveldb/libleveldb.a leveldb/libmemenv.a
628634
CLEANFILES += *.gcda *.gcno
629635
CLEANFILES += bdap/*.gcda bdap/*.gcno
630636
CLEANFILES += bdap/vgp/*.gcda bdap/vgp/*.gcno
637+
CLEANFILES += bdap/wallet/*.gcda bdap/wallet/*.gcno
631638
CLEANFILES += compat/*.gcda compat/*.gcno
632639
CLEANFILES += consensus/*.gcda consensus/*.gcno
633640
CLEANFILES += crypto/*.gcda crypto/*.gcno

src/bdap/linkmanager.cpp

Lines changed: 3 additions & 729 deletions
Large diffs are not rendered by default.

src/bdap/utils.cpp

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "bdap/utils.h"
66

7+
#include "base58.h"
78
#include "chainparams.h"
89
#include "coins.h"
910
#include "core_io.h"
@@ -198,24 +199,6 @@ bool GetBDAPData(const CTxOut& out, std::vector<unsigned char>& vchData, std::ve
198199
return GetBDAPData(out.scriptPubKey, vchData, vchHash);
199200
}
200201

201-
void CreateRecipient(const CScript& scriptPubKey, CRecipient& recipient)
202-
{
203-
CRecipient recp = {scriptPubKey, recipient.nAmount, false};
204-
recipient = recp;
205-
CTxOut txout(recipient.nAmount, scriptPubKey);
206-
size_t nSize = GetSerializeSize(txout, SER_DISK, 0) + 148u;
207-
recipient.nAmount = 3 * minRelayTxFee.GetFee(nSize);
208-
}
209-
210-
void CreateFeeRecipient(CScript& scriptPubKey, const std::vector<unsigned char>& data, CRecipient& recipient)
211-
{
212-
// add hash to data output (must match hash in inputs check with the tx scriptpubkey hash)
213-
uint256 hash = Hash(data.begin(), data.end());
214-
std::vector<unsigned char> vchHashRand = vchFromValue(hash.GetHex());
215-
scriptPubKey << vchHashRand;
216-
CRecipient recp = {scriptPubKey, 0, false};
217-
recipient = recp;
218-
}
219202

220203
void ToLowerCase(CharString& vchValue) {
221204
std::string strValue;
@@ -609,24 +592,6 @@ bool ExtractOpTypeValue(const CScript& script, std::string& strOpType, std::vect
609592
return true;
610593
}
611594

612-
bool GetScriptOpTypeValue(const std::vector<CRecipient>& vecSend, CScript& bdapOpScript, std::string& strOpType, std::vector<unsigned char>& vchValue)
613-
{
614-
LogPrint("bdap", "%s -- vecSend size = %u \n", __func__, vecSend.size());
615-
for (const CRecipient& rec : vecSend) {
616-
CScript script = rec.scriptPubKey;
617-
if (!script.IsUnspendable()) {
618-
if (ExtractOpTypeValue(script, strOpType, vchValue)) {
619-
bdapOpScript = script;
620-
break;
621-
}
622-
}
623-
}
624-
if (strOpType.size() > 0) {
625-
return true;
626-
}
627-
return false;
628-
}
629-
630595
bool GetTransactionOpTypeValue(const CTransaction& tx, CScript& bdapOpScript, std::string& strOpType, std::vector<unsigned char>& vchValue)
631596
{
632597
for (const CTxOut& out : tx.vout)

src/bdap/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class CCoinsViewCache;
1515
class CDynamicAddress;
16+
class CStealthAddress;
1617
struct CRecipient;
1718
class CScript;
1819
class CTxOut;

src/bdap/vgpmessage.cpp

Lines changed: 2 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "bdap/vgpmessage.h"
88

9+
#include "arith_uint256.h"
910
#include "base58.h"
1011
#include "bdap/linkmanager.h"
1112
#include "bdap/utils.h"
@@ -19,79 +20,16 @@
1920
#include "script/script.h"
2021
#include "streams.h"
2122
#include "timedata.h"
23+
#include "uint256.h"
2224
#include "util.h"
2325
#include "wallet/wallet.h"
2426

2527
#include <cstdlib>
2628

27-
static std::map<uint256, CVGPMessage> mapMyVGPMessages;
28-
static CCriticalSection cs_mapMyVGPMessages;
29-
static int nMyMessageCounter = 0;
30-
3129
static std::map<uint256, int64_t> mapRecentMessageLog;
3230
static CCriticalSection cs_mapRecentMessageLog;
3331
static int nMessageCounter = 0;
3432

35-
class CMessage
36-
{
37-
public:
38-
static const int CURRENT_VERSION = 1;
39-
int nMessageVersion;
40-
std::vector<unsigned char> vchMessageType;
41-
std::vector<unsigned char> vchMessage;
42-
std::vector<unsigned char> vchSenderFQDN;
43-
bool fKeepLast;
44-
45-
CMessage()
46-
{
47-
SetNull();
48-
}
49-
50-
CMessage(const int& version, const std::vector<unsigned char>& type, const std::vector<unsigned char>& message, const std::vector<unsigned char>& sender, bool keeplast)
51-
: nMessageVersion(version), vchMessageType(type), vchMessage(message), vchSenderFQDN(sender), fKeepLast(keeplast) {}
52-
53-
CMessage(const std::vector<unsigned char>& vchData)
54-
{
55-
UnserializeFromData(vchData);
56-
}
57-
58-
ADD_SERIALIZE_METHODS;
59-
60-
template <typename Stream, typename Operation>
61-
inline void SerializationOp(Stream& s, Operation ser_action)
62-
{
63-
READWRITE(nMessageVersion);
64-
READWRITE(vchMessageType);
65-
READWRITE(vchMessage);
66-
READWRITE(vchSenderFQDN);
67-
READWRITE(fKeepLast);
68-
}
69-
70-
void SetNull()
71-
{
72-
nMessageVersion = -1;
73-
vchMessageType.clear();
74-
vchMessage.clear();
75-
vchSenderFQDN.clear();
76-
fKeepLast = false;
77-
}
78-
79-
inline bool IsNull() const { return (nMessageVersion == -1); }
80-
81-
inline CMessage operator=(const CMessage& b)
82-
{
83-
nMessageVersion = b.nMessageVersion;
84-
vchMessageType = b.vchMessageType;
85-
vchMessage = b.vchMessage;
86-
vchSenderFQDN = b.vchSenderFQDN;
87-
fKeepLast = b.fKeepLast;
88-
return *this;
89-
}
90-
91-
void Serialize(std::vector<unsigned char>& vchData);
92-
bool UnserializeFromData(const std::vector<unsigned char>& vchData);
93-
};
94-
9533
void CMessage::Serialize(std::vector<unsigned char>& vchData)
9634
{
9735
CDataStream dsMessageData(SER_NETWORK, PROTOCOL_VERSION);
@@ -529,49 +467,6 @@ bool ReceivedMessage(const uint256& messageHash)
529467
return false;
530468
}
531469

532-
void CleanupMyMessageMap()
533-
{
534-
// map with message type, message sender and timestamp. Used to keep last message from a sender/type pair.
535-
std::map<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>, int64_t> mapMessageTypeFromTimestamp;
536-
int64_t nCurrentTimeStamp = GetAdjustedTime();
537-
std::map<uint256, CVGPMessage>::iterator itr = mapMyVGPMessages.begin();
538-
while (itr != mapMyVGPMessages.end())
539-
{
540-
CVGPMessage message = (*itr).second;
541-
CUnsignedVGPMessage unsignedMessage(message.vchMsg);
542-
if (!unsignedMessage.fEncrypted && nCurrentTimeStamp > unsignedMessage.nTimeStamp + KEEP_MY_MESSAGE_ALIVE_SECONDS)
543-
{
544-
CMessage message(unsignedMessage.vchMessageData);
545-
std::pair<std::vector<unsigned char>, std::vector<unsigned char>> pairTypeFrom = std::make_pair(message.vchMessageType, message.vchSenderFQDN);
546-
if (!message.fKeepLast) {
547-
itr = mapMyVGPMessages.erase(itr);
548-
}
549-
else {
550-
std::map<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>, int64_t>::iterator itTypeFrom = mapMessageTypeFromTimestamp.find(pairTypeFrom);
551-
if (itTypeFrom != mapMessageTypeFromTimestamp.end()) {
552-
if (itTypeFrom->second > unsignedMessage.nTimeStamp) {
553-
itr = mapMyVGPMessages.erase(itr);
554-
}
555-
else {
556-
mapMessageTypeFromTimestamp[pairTypeFrom] = unsignedMessage.nTimeStamp;
557-
++itr;
558-
}
559-
}
560-
else {
561-
mapMessageTypeFromTimestamp[pairTypeFrom] = unsignedMessage.nTimeStamp;
562-
++itr;
563-
}
564-
}
565-
}
566-
else
567-
{
568-
++itr;
569-
}
570-
}
571-
LogPrintf("%s -- Size %d\n", __func__, mapMyVGPMessages.size());
572-
}
573-
574-
#ifdef ENABLE_WALLET
575470
bool DecryptMessage(CUnsignedVGPMessage& unsignedMessage)
576471
{
577472
CLink link;
@@ -608,103 +503,6 @@ bool DecryptMessage(CUnsignedVGPMessage& unsignedMessage)
608503
return false;
609504
}
610505

611-
void AddMyMessage(const CVGPMessage& message)
612-
{
613-
bool fFound = false;
614-
CUnsignedVGPMessage unsignedMessage(message.vchMsg);
615-
LogPrint("bdap", "%s -- Message hash = %s, Link MessageID = %s\n", __func__, message.GetHash().ToString(), unsignedMessage.MessageID.ToString());
616-
CVGPMessage storeMessage;
617-
if (pwalletMain && pLinkManager && !pwalletMain->IsLocked() && unsignedMessage.fEncrypted)
618-
{
619-
if (DecryptMessage(unsignedMessage))
620-
fFound = true;
621-
}
622-
if (fFound)
623-
{
624-
CVGPMessage newMessage(unsignedMessage);
625-
storeMessage = newMessage;
626-
}
627-
else
628-
{
629-
storeMessage = message;
630-
}
631-
LOCK(cs_mapMyVGPMessages);
632-
mapMyVGPMessages[storeMessage.GetHash()] = storeMessage;
633-
nMyMessageCounter++;
634-
if ((nMyMessageCounter % 10) == 0)
635-
CleanupMyMessageMap();
636-
}
637-
638-
void GetMyLinkMessages(const uint256& subjectID, std::vector<CUnsignedVGPMessage>& vMessages)
639-
{
640-
LOCK(cs_mapMyVGPMessages);
641-
std::map<uint256, CVGPMessage>::iterator itr = mapMyVGPMessages.begin();
642-
while (itr != mapMyVGPMessages.end())
643-
{
644-
CVGPMessage message = (*itr).second;
645-
CUnsignedVGPMessage unsignedMessage(message.vchMsg);
646-
if (unsignedMessage.SubjectID == subjectID)
647-
{
648-
if (unsignedMessage.fEncrypted)
649-
{
650-
if (pwalletMain && !pwalletMain->IsLocked() && DecryptMessage(unsignedMessage))
651-
{
652-
vMessages.push_back(unsignedMessage);
653-
}
654-
}
655-
else
656-
{
657-
vMessages.push_back(unsignedMessage);
658-
}
659-
}
660-
itr++;
661-
}
662-
}
663-
664-
void GetMyLinkMessagesByType(const std::vector<unsigned char>& vchType, const std::vector<unsigned char>& vchRecipientFQDN, std::vector<CVGPMessage>& vMessages, bool& fKeepLast)
665-
{
666-
LOCK(cs_mapMyVGPMessages);
667-
std::map<uint256, CVGPMessage>::iterator itr = mapMyVGPMessages.begin();
668-
while (itr != mapMyVGPMessages.end())
669-
{
670-
CVGPMessage messageWrapper = (*itr).second;
671-
CUnsignedVGPMessage unsignedMessage(messageWrapper.vchMsg);
672-
if (unsignedMessage.fEncrypted && pwalletMain && !pwalletMain->IsLocked())
673-
{
674-
DecryptMessage(unsignedMessage);
675-
}
676-
if (!unsignedMessage.fEncrypted && (vchType.size() == 0 || vchType == unsignedMessage.Type()) && unsignedMessage.SenderFQDN() != vchRecipientFQDN)
677-
{
678-
if (unsignedMessage.KeepLast())
679-
fKeepLast = true;
680-
681-
vMessages.push_back(unsignedMessage);
682-
}
683-
itr++;
684-
}
685-
}
686-
687-
void GetMyLinkMessagesBySubjectAndSender(const uint256& subjectID, const std::vector<unsigned char>& vchSenderFQDN,
688-
const std::vector<unsigned char>& vchType, std::vector<CVGPMessage>& vchMessages, bool& fKeepLast)
689-
{
690-
LOCK(cs_mapMyVGPMessages);
691-
std::map<uint256, CVGPMessage>::iterator itr = mapMyVGPMessages.begin();
692-
while (itr != mapMyVGPMessages.end())
693-
{
694-
CVGPMessage messageWrapper = (*itr).second;
695-
CUnsignedVGPMessage unsignedMessage(messageWrapper.vchMsg);
696-
if (unsignedMessage.SubjectID == subjectID && unsignedMessage.SenderFQDN() == vchSenderFQDN && (vchType.size() == 0 || vchType == unsignedMessage.Type()))
697-
{
698-
if (unsignedMessage.KeepLast())
699-
fKeepLast = true;
700-
701-
vchMessages.push_back(messageWrapper);
702-
}
703-
itr++;
704-
}
705-
}
706-
#endif // ENABLE_WALLET
707-
708506
void KeepLastTypeBySender(std::vector<CVGPMessage>& vMessages)
709507
{
710508
std::map<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>, std::pair<CVGPMessage, int64_t> > mapFromMessageTime;

0 commit comments

Comments
 (0)