Skip to content

Commit

Permalink
bdap: refactor wallet-dependent logic to their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Aug 30, 2021
1 parent 7e31544 commit 8a179d5
Show file tree
Hide file tree
Showing 14 changed files with 1,715 additions and 970 deletions.
7 changes: 7 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ libdynamic_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(DYNAMIC_INCLUDES)
libdynamic_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libdynamic_wallet_a_SOURCES = \
bdap/stealth.cpp \
bdap/wallet/keys.cpp \
bdap/wallet/wallet.cpp \
bdap/wallet/linkmanager.cpp \
bdap/wallet/utils.cpp \
bdap/wallet/vgp.cpp \
keepass.cpp \
policy/rbf.cpp \
privatesend-client.cpp \
Expand Down Expand Up @@ -475,6 +480,7 @@ libdynamic_common_a_SOURCES = \
base58.cpp \
bdap/stealth.cpp \
bip39.cpp \
bdap/wallet/keys.cpp \
chainparams.cpp \
coins.cpp \
compressor.cpp \
Expand Down Expand Up @@ -628,6 +634,7 @@ CLEANFILES = leveldb/libleveldb.a leveldb/libmemenv.a
CLEANFILES += *.gcda *.gcno
CLEANFILES += bdap/*.gcda bdap/*.gcno
CLEANFILES += bdap/vgp/*.gcda bdap/vgp/*.gcno
CLEANFILES += bdap/wallet/*.gcda bdap/wallet/*.gcno
CLEANFILES += compat/*.gcda compat/*.gcno
CLEANFILES += consensus/*.gcda consensus/*.gcno
CLEANFILES += crypto/*.gcda crypto/*.gcno
Expand Down
732 changes: 3 additions & 729 deletions src/bdap/linkmanager.cpp

Large diffs are not rendered by default.

37 changes: 1 addition & 36 deletions src/bdap/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "bdap/utils.h"

#include "base58.h"
#include "chainparams.h"
#include "coins.h"
#include "core_io.h"
Expand Down Expand Up @@ -198,24 +199,6 @@ bool GetBDAPData(const CTxOut& out, std::vector<unsigned char>& vchData, std::ve
return GetBDAPData(out.scriptPubKey, vchData, vchHash);
}

void CreateRecipient(const CScript& scriptPubKey, CRecipient& recipient)
{
CRecipient recp = {scriptPubKey, recipient.nAmount, false};
recipient = recp;
CTxOut txout(recipient.nAmount, scriptPubKey);
size_t nSize = GetSerializeSize(txout, SER_DISK, 0) + 148u;
recipient.nAmount = 3 * minRelayTxFee.GetFee(nSize);
}

void CreateFeeRecipient(CScript& scriptPubKey, const std::vector<unsigned char>& data, CRecipient& recipient)
{
// add hash to data output (must match hash in inputs check with the tx scriptpubkey hash)
uint256 hash = Hash(data.begin(), data.end());
std::vector<unsigned char> vchHashRand = vchFromValue(hash.GetHex());
scriptPubKey << vchHashRand;
CRecipient recp = {scriptPubKey, 0, false};
recipient = recp;
}

void ToLowerCase(CharString& vchValue) {
std::string strValue;
Expand Down Expand Up @@ -609,24 +592,6 @@ bool ExtractOpTypeValue(const CScript& script, std::string& strOpType, std::vect
return true;
}

bool GetScriptOpTypeValue(const std::vector<CRecipient>& vecSend, CScript& bdapOpScript, std::string& strOpType, std::vector<unsigned char>& vchValue)
{
LogPrint("bdap", "%s -- vecSend size = %u \n", __func__, vecSend.size());
for (const CRecipient& rec : vecSend) {
CScript script = rec.scriptPubKey;
if (!script.IsUnspendable()) {
if (ExtractOpTypeValue(script, strOpType, vchValue)) {
bdapOpScript = script;
break;
}
}
}
if (strOpType.size() > 0) {
return true;
}
return false;
}

bool GetTransactionOpTypeValue(const CTransaction& tx, CScript& bdapOpScript, std::string& strOpType, std::vector<unsigned char>& vchValue)
{
for (const CTxOut& out : tx.vout)
Expand Down
1 change: 1 addition & 0 deletions src/bdap/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

class CCoinsViewCache;
class CDynamicAddress;
class CStealthAddress;
struct CRecipient;
class CScript;
class CTxOut;
Expand Down
206 changes: 2 additions & 204 deletions src/bdap/vgpmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "bdap/vgpmessage.h"

#include "arith_uint256.h"
#include "base58.h"
#include "bdap/linkmanager.h"
#include "bdap/utils.h"
Expand All @@ -19,79 +20,16 @@
#include "script/script.h"
#include "streams.h"
#include "timedata.h"
#include "uint256.h"
#include "util.h"
#include "wallet/wallet.h"

#include <cstdlib>

static std::map<uint256, CVGPMessage> mapMyVGPMessages;
static CCriticalSection cs_mapMyVGPMessages;
static int nMyMessageCounter = 0;

static std::map<uint256, int64_t> mapRecentMessageLog;
static CCriticalSection cs_mapRecentMessageLog;
static int nMessageCounter = 0;

class CMessage
{
public:
static const int CURRENT_VERSION = 1;
int nMessageVersion;
std::vector<unsigned char> vchMessageType;
std::vector<unsigned char> vchMessage;
std::vector<unsigned char> vchSenderFQDN;
bool fKeepLast;

CMessage()
{
SetNull();
}

CMessage(const int& version, const std::vector<unsigned char>& type, const std::vector<unsigned char>& message, const std::vector<unsigned char>& sender, bool keeplast)
: nMessageVersion(version), vchMessageType(type), vchMessage(message), vchSenderFQDN(sender), fKeepLast(keeplast) {}

CMessage(const std::vector<unsigned char>& vchData)
{
UnserializeFromData(vchData);
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(nMessageVersion);
READWRITE(vchMessageType);
READWRITE(vchMessage);
READWRITE(vchSenderFQDN);
READWRITE(fKeepLast);
}

void SetNull()
{
nMessageVersion = -1;
vchMessageType.clear();
vchMessage.clear();
vchSenderFQDN.clear();
fKeepLast = false;
}

inline bool IsNull() const { return (nMessageVersion == -1); }

inline CMessage operator=(const CMessage& b)
{
nMessageVersion = b.nMessageVersion;
vchMessageType = b.vchMessageType;
vchMessage = b.vchMessage;
vchSenderFQDN = b.vchSenderFQDN;
fKeepLast = b.fKeepLast;
return *this;
}

void Serialize(std::vector<unsigned char>& vchData);
bool UnserializeFromData(const std::vector<unsigned char>& vchData);
};

void CMessage::Serialize(std::vector<unsigned char>& vchData)
{
CDataStream dsMessageData(SER_NETWORK, PROTOCOL_VERSION);
Expand Down Expand Up @@ -529,49 +467,6 @@ bool ReceivedMessage(const uint256& messageHash)
return false;
}

void CleanupMyMessageMap()
{
// map with message type, message sender and timestamp. Used to keep last message from a sender/type pair.
std::map<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>, int64_t> mapMessageTypeFromTimestamp;
int64_t nCurrentTimeStamp = GetAdjustedTime();
std::map<uint256, CVGPMessage>::iterator itr = mapMyVGPMessages.begin();
while (itr != mapMyVGPMessages.end())
{
CVGPMessage message = (*itr).second;
CUnsignedVGPMessage unsignedMessage(message.vchMsg);
if (!unsignedMessage.fEncrypted && nCurrentTimeStamp > unsignedMessage.nTimeStamp + KEEP_MY_MESSAGE_ALIVE_SECONDS)
{
CMessage message(unsignedMessage.vchMessageData);
std::pair<std::vector<unsigned char>, std::vector<unsigned char>> pairTypeFrom = std::make_pair(message.vchMessageType, message.vchSenderFQDN);
if (!message.fKeepLast) {
itr = mapMyVGPMessages.erase(itr);
}
else {
std::map<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>, int64_t>::iterator itTypeFrom = mapMessageTypeFromTimestamp.find(pairTypeFrom);
if (itTypeFrom != mapMessageTypeFromTimestamp.end()) {
if (itTypeFrom->second > unsignedMessage.nTimeStamp) {
itr = mapMyVGPMessages.erase(itr);
}
else {
mapMessageTypeFromTimestamp[pairTypeFrom] = unsignedMessage.nTimeStamp;
++itr;
}
}
else {
mapMessageTypeFromTimestamp[pairTypeFrom] = unsignedMessage.nTimeStamp;
++itr;
}
}
}
else
{
++itr;
}
}
LogPrintf("%s -- Size %d\n", __func__, mapMyVGPMessages.size());
}

#ifdef ENABLE_WALLET
bool DecryptMessage(CUnsignedVGPMessage& unsignedMessage)
{
CLink link;
Expand Down Expand Up @@ -608,103 +503,6 @@ bool DecryptMessage(CUnsignedVGPMessage& unsignedMessage)
return false;
}

void AddMyMessage(const CVGPMessage& message)
{
bool fFound = false;
CUnsignedVGPMessage unsignedMessage(message.vchMsg);
LogPrint("bdap", "%s -- Message hash = %s, Link MessageID = %s\n", __func__, message.GetHash().ToString(), unsignedMessage.MessageID.ToString());
CVGPMessage storeMessage;
if (pwalletMain && pLinkManager && !pwalletMain->IsLocked() && unsignedMessage.fEncrypted)
{
if (DecryptMessage(unsignedMessage))
fFound = true;
}
if (fFound)
{
CVGPMessage newMessage(unsignedMessage);
storeMessage = newMessage;
}
else
{
storeMessage = message;
}
LOCK(cs_mapMyVGPMessages);
mapMyVGPMessages[storeMessage.GetHash()] = storeMessage;
nMyMessageCounter++;
if ((nMyMessageCounter % 10) == 0)
CleanupMyMessageMap();
}

void GetMyLinkMessages(const uint256& subjectID, std::vector<CUnsignedVGPMessage>& vMessages)
{
LOCK(cs_mapMyVGPMessages);
std::map<uint256, CVGPMessage>::iterator itr = mapMyVGPMessages.begin();
while (itr != mapMyVGPMessages.end())
{
CVGPMessage message = (*itr).second;
CUnsignedVGPMessage unsignedMessage(message.vchMsg);
if (unsignedMessage.SubjectID == subjectID)
{
if (unsignedMessage.fEncrypted)
{
if (pwalletMain && !pwalletMain->IsLocked() && DecryptMessage(unsignedMessage))
{
vMessages.push_back(unsignedMessage);
}
}
else
{
vMessages.push_back(unsignedMessage);
}
}
itr++;
}
}

void GetMyLinkMessagesByType(const std::vector<unsigned char>& vchType, const std::vector<unsigned char>& vchRecipientFQDN, std::vector<CVGPMessage>& vMessages, bool& fKeepLast)
{
LOCK(cs_mapMyVGPMessages);
std::map<uint256, CVGPMessage>::iterator itr = mapMyVGPMessages.begin();
while (itr != mapMyVGPMessages.end())
{
CVGPMessage messageWrapper = (*itr).second;
CUnsignedVGPMessage unsignedMessage(messageWrapper.vchMsg);
if (unsignedMessage.fEncrypted && pwalletMain && !pwalletMain->IsLocked())
{
DecryptMessage(unsignedMessage);
}
if (!unsignedMessage.fEncrypted && (vchType.size() == 0 || vchType == unsignedMessage.Type()) && unsignedMessage.SenderFQDN() != vchRecipientFQDN)
{
if (unsignedMessage.KeepLast())
fKeepLast = true;

vMessages.push_back(unsignedMessage);
}
itr++;
}
}

void GetMyLinkMessagesBySubjectAndSender(const uint256& subjectID, const std::vector<unsigned char>& vchSenderFQDN,
const std::vector<unsigned char>& vchType, std::vector<CVGPMessage>& vchMessages, bool& fKeepLast)
{
LOCK(cs_mapMyVGPMessages);
std::map<uint256, CVGPMessage>::iterator itr = mapMyVGPMessages.begin();
while (itr != mapMyVGPMessages.end())
{
CVGPMessage messageWrapper = (*itr).second;
CUnsignedVGPMessage unsignedMessage(messageWrapper.vchMsg);
if (unsignedMessage.SubjectID == subjectID && unsignedMessage.SenderFQDN() == vchSenderFQDN && (vchType.size() == 0 || vchType == unsignedMessage.Type()))
{
if (unsignedMessage.KeepLast())
fKeepLast = true;

vchMessages.push_back(messageWrapper);
}
itr++;
}
}
#endif // ENABLE_WALLET

void KeepLastTypeBySender(std::vector<CVGPMessage>& vMessages)
{
std::map<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>, std::pair<CVGPMessage, int64_t> > mapFromMessageTime;
Expand Down
Loading

0 comments on commit 8a179d5

Please sign in to comment.