switched structs to use shared_ptrs

This commit is contained in:
woodser
2019-06-04 10:41:42 -04:00
parent 7567609e05
commit ce0179d1a9
3 changed files with 119 additions and 209 deletions
+58 -96
View File
@@ -20,6 +20,8 @@ namespace monero {
/**
* Models a connection to a daemon.
*
* TODO: shared_ptr<string>?
*/
struct MoneroRpcConnection {
string uri;
@@ -38,42 +40,42 @@ namespace monero {
*/
struct MoneroTx {
shared_ptr<MoneroBlock> block;
string id;
string version;
bool isCoinbase;
string paymentId;
uint64_t fee;
uint32_t mixin;
bool doNotRelay;
bool isRelayed;
bool isConfirmed;
bool inTxPool;
uint64_t numConfirmations;
uint64_t unlockTime;
uint64_t lastRelayedTimestamp;
uint64_t receivedTimestamp;
bool isDoubleSpend;
string key;
string fullHex;
string prunedHex;
string prunableHex;
string prunableHash;
uint32_t size;
uint32_t weight;
shared_ptr<string> id;
shared_ptr<string> version;
shared_ptr<bool> isCoinbase;
shared_ptr<string> paymentId;
shared_ptr<uint64_t> fee;
shared_ptr<uint32_t> mixin;
shared_ptr<bool> doNotRelay;
shared_ptr<bool> isRelayed;
shared_ptr<bool> isConfirmed;
shared_ptr<bool> inTxPool;
shared_ptr<uint64_t> numConfirmations;
shared_ptr<uint64_t> unlockTime;
shared_ptr<uint64_t> lastRelayedTimestamp;
shared_ptr<uint64_t> receivedTimestamp;
shared_ptr<bool> isDoubleSpend;
shared_ptr<string> key;
shared_ptr<string> fullHex;
shared_ptr<string> prunedHex;
shared_ptr<string> prunableHex;
shared_ptr<string> prunableHash;
shared_ptr<uint32_t> size;
shared_ptr<uint32_t> weight;
vector<MoneroOutput> vins;
vector<MoneroOutput> vouts;
vector<uint32_t> outputIndices;
string metadata;
string commonTxSets;
shared_ptr<string> metadata;
shared_ptr<string> commonTxSets;
vector<uint32_t> extra;
string rctSignatures; // TODO: implement
string rctSigPrunable; // TODO: implement
bool isKeptByBlock;
bool isFailed;
shared_ptr<string> rctSignatures; // TODO: implement
shared_ptr<string> rctSigPrunable; // TODO: implement
shared_ptr<bool> isKeptByBlock;
shared_ptr<bool> isFailed;
shared_ptr<uint32_t> lastFailedHeight;
string lastFailedId;
shared_ptr<string> lastFailedId;
shared_ptr<uint32_t> maxUsedBlockHeight;
string maxUsedBlockId;
shared_ptr<string> maxUsedBlockId;
vector<string> signatures;
};
@@ -81,92 +83,52 @@ namespace monero {
* Models a Monero block header which contains information about the block.
*/
struct MoneroBlockHeader {
string id;
uint64_t height;
uint64_t timestamp;
uint64_t size;
uint64_t weight;
uint64_t longTermWeight;
uint64_t depth;
uint64_t difficulty;
uint64_t cumulativeDifficulty;
uint32_t majorVersion;
uint32_t minorVersion;
uint64_t nonce;
string coinbaseTxId;
uint32_t numTxs;
bool orphanStatus;
string prevId;
uint64_t reward;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(id)
KV_SERIALIZE(height)
KV_SERIALIZE(timestamp)
KV_SERIALIZE(size)
KV_SERIALIZE(weight)
KV_SERIALIZE(longTermWeight)
KV_SERIALIZE(depth)
KV_SERIALIZE(difficulty)
KV_SERIALIZE(cumulativeDifficulty)
KV_SERIALIZE(majorVersion)
KV_SERIALIZE(minorVersion)
KV_SERIALIZE(nonce)
KV_SERIALIZE(coinbaseTxId)
KV_SERIALIZE(numTxs)
KV_SERIALIZE(orphanStatus)
KV_SERIALIZE(prevId)
KV_SERIALIZE(reward)
END_KV_SERIALIZE_MAP()
shared_ptr<string> id;
shared_ptr<uint64_t> height;
shared_ptr<uint64_t> timestamp;
shared_ptr<uint64_t> size;
shared_ptr<uint64_t> weight;
shared_ptr<uint64_t> longTermWeight;
shared_ptr<uint64_t> depth;
shared_ptr<uint64_t> difficulty;
shared_ptr<uint64_t> cumulativeDifficulty;
shared_ptr<uint32_t> majorVersion;
shared_ptr<uint32_t> minorVersion;
shared_ptr<uint64_t> nonce;
shared_ptr<string> coinbaseTxId;
shared_ptr<uint32_t> numTxs;
shared_ptr<bool> orphanStatus;
shared_ptr<string> prevId;
shared_ptr<uint64_t> reward;
};
/**
* Models a Monero block in the blockchain.
*/
struct MoneroBlock : public MoneroBlockHeader {
string hex;
MoneroTx coinbaseTx;
shared_ptr<string> hex;
shared_ptr<MoneroTx> coinbaseTx;
vector<MoneroTx> txs;
vector<string> txIds;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(hex)
KV_SERIALIZE(coinbaseTx)
KV_SERIALIZE(txs)
KV_SERIALIZE(txIds)
END_KV_SERIALIZE_MAP()
};
/**
* Models a Monero key image.
*/
struct MoneroKeyImage {
string hex;
string signature;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(hex)
KV_SERIALIZE(signature)
END_KV_SERIALIZE_MAP()
shared_ptr<string> hex;
shared_ptr<string> signature;
};
/**
* Models a Monero transaction output.
*/
struct MoneroOutput {
MoneroTx* tx;
MoneroKeyImage keyImage;
uint64_t amount;
uint32_t index;
shared_ptr<MoneroTx> tx;
shared_ptr<MoneroKeyImage> keyImage;
shared_ptr<uint64_t> amount;
shared_ptr<uint32_t> index;
vector<uint32_t> ringOutputIndices;
string stealthPublicKey;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(keyImage)
KV_SERIALIZE(amount)
KV_SERIALIZE(index)
KV_SERIALIZE(ringOutputIndices)
KV_SERIALIZE(stealthPublicKey)
END_KV_SERIALIZE_MAP()
shared_ptr<string> stealthPublicKey;
};
}
+32 -28
View File
@@ -69,20 +69,20 @@ namespace monero {
// notify listener of block
if (this->listener != nullptr) {
MoneroBlock block;
block.height = height;
MoneroBlock block;
block.height = shared_ptr<uint64_t>(make_shared<uint64_t>(height));
listener->onNewBlock(block);
}
// notify listeners of sync progress
if (syncStartHeight != nullptr && height > *syncStartHeight) {
if (height > *syncEndHeight) *syncEndHeight = height; // increase end height if necessary
uint64_t numBlocksDone = height - *syncStartHeight + 1;
uint64_t numBlocksTotal = *syncEndHeight - *syncStartHeight + 1;
double percentDone = numBlocksDone / (double) numBlocksTotal;
string message = string("Synchronizing");
if (this->listener != nullptr) this->listener->onSyncProgress(*syncStartHeight, numBlocksDone, numBlocksTotal, percentDone, message);
if (this->syncListener != nullptr) this->syncListener->onSyncProgress(*syncStartHeight, numBlocksDone, numBlocksTotal, percentDone, message);
if (height > *syncEndHeight) *syncEndHeight = height; // increase end height if necessary
uint64_t numBlocksDone = height - *syncStartHeight + 1;
uint64_t numBlocksTotal = *syncEndHeight - *syncStartHeight + 1;
double percentDone = numBlocksDone / (double) numBlocksTotal;
string message = string("Synchronizing");
if (this->listener != nullptr) this->listener->onSyncProgress(*syncStartHeight, numBlocksDone, numBlocksTotal, percentDone, message);
if (this->syncListener != nullptr) this->syncListener->onSyncProgress(*syncStartHeight, numBlocksDone, numBlocksTotal, percentDone, message);
}
}
@@ -224,8 +224,8 @@ namespace monero {
// return indices in subaddress
MoneroSubaddress subaddress;
cryptonote::subaddress_index cnIndex = *index;
subaddress.accountIndex = cnIndex.major;
subaddress.index = cnIndex.minor;
subaddress.accountIndex = shared_ptr<uint32_t>(make_shared<uint32_t>(cnIndex.major));
subaddress.index = shared_ptr<uint32_t>(make_shared<uint32_t>(cnIndex.minor));
return subaddress;
}
@@ -333,10 +333,10 @@ namespace monero {
vector<MoneroAccount> accounts;
for (uint32_t accountIdx = 0; accountIdx < wallet2->get_num_subaddress_accounts(); accountIdx++) {
MoneroAccount account;
account.index = accountIdx;
account.primaryAddress = getAddress(accountIdx, 0);
account.balance = wallet2->balance(accountIdx);
account.unlockedBalance = wallet2->unlocked_balance(accountIdx);
account.index = shared_ptr<uint32_t>(make_shared<uint32_t>(accountIdx));
account.primaryAddress = shared_ptr<string>(make_shared<string>(getAddress(accountIdx, 0)));
account.balance = shared_ptr<uint64_t>(make_shared<uint64_t>(wallet2->balance(accountIdx)));
account.unlockedBalance = shared_ptr<uint64_t>(make_shared<uint64_t>(wallet2->unlocked_balance(accountIdx)));
if (includeSubaddresses) account.subaddresses = getSubaddressesAux(accountIdx, vector<uint32_t>(), transfers);
accounts.push_back(account);
}
@@ -357,10 +357,10 @@ namespace monero {
// build and return account
MoneroAccount account;
account.index = accountIdx;
account.primaryAddress = getAddress(accountIdx, 0);
account.balance = wallet2->balance(accountIdx);
account.unlockedBalance = wallet2->unlocked_balance(accountIdx);
account.index = shared_ptr<uint32_t>(make_shared<uint32_t>(accountIdx));
account.primaryAddress = shared_ptr<string>(make_shared<string>(getAddress(accountIdx, 0)));
account.balance = shared_ptr<uint64_t>(make_shared<uint64_t>(wallet2->balance(accountIdx)));
account.unlockedBalance = shared_ptr<uint64_t>(make_shared<uint64_t>(wallet2->unlocked_balance(accountIdx)));
if (includeSubaddresses) account.subaddresses = getSubaddressesAux(accountIdx, vector<uint32_t>(), transfers);
return account;
}
@@ -405,18 +405,18 @@ namespace monero {
// initialize subaddresses at indices
for (uint32_t subaddressIndicesIdx = 0; subaddressIndicesIdx < subaddressIndices.size(); subaddressIndicesIdx++) {
MoneroSubaddress subaddress;
subaddress.accountIndex = accountIdx;
subaddress.accountIndex = shared_ptr<uint32_t>(make_shared<uint32_t>(accountIdx));
uint32_t subaddressIdx = subaddressIndices.at(subaddressIndicesIdx);
subaddress.index = subaddressIdx;
subaddress.address = getAddress(accountIdx, subaddressIdx);
subaddress.index = shared_ptr<uint32_t>(make_shared<uint32_t>(subaddressIdx));
subaddress.address = shared_ptr<string>(make_shared<string>(getAddress(accountIdx, subaddressIdx)));
auto iter1 = balancePerSubaddress.find(subaddressIdx);
subaddress.balance = iter1 == balancePerSubaddress.end() ? 0 : iter1->second;
subaddress.balance = shared_ptr<uint64_t>(make_shared<uint64_t>(iter1 == balancePerSubaddress.end() ? 0 : iter1->second));
auto iter2 = unlockedBalancePerSubaddress.find(subaddressIdx);
subaddress.unlockedBalance = iter2 == unlockedBalancePerSubaddress.end() ? 0 : iter2->second.first;
subaddress.numBlocksToUnlock = iter2 == unlockedBalancePerSubaddress.end() ? 0 : iter2->second.second;
subaddress.unlockedBalance = shared_ptr<uint64_t>(make_shared<uint64_t>(iter2 == unlockedBalancePerSubaddress.end() ? 0 : iter2->second.first));
subaddress.numBlocksToUnlock = shared_ptr<uint32_t>(make_shared<uint32_t>(iter2 == unlockedBalancePerSubaddress.end() ? 0 : iter2->second.second));
cryptonote::subaddress_index index = {accountIdx, subaddressIdx};
subaddress.numUnspentOutputs = count_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == index; });
subaddress.isUsed = find_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return td.m_subaddr_index == index; }) != transfers.end();
subaddress.numUnspentOutputs = shared_ptr<uint32_t>(make_shared<uint32_t>(count_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == index; })));
subaddress.isUsed = shared_ptr<bool>(make_shared<bool>(find_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return td.m_subaddr_index == index; }) != transfers.end()));
subaddresses.push_back(subaddress);
}
@@ -475,8 +475,12 @@ namespace monero {
// sync wallet
wallet2Listener->onSyncStart(syncStartHeight, listener);
MoneroSyncResult result;
wallet2->refresh(wallet2->is_trusted_daemon(), syncStartHeight, result.numBlocksFetched, result.receivedMoney, true);
result.numBlocksFetched = shared_ptr<uint64_t>(new uint64_t());
result.receivedMoney = shared_ptr<bool>(new bool());
wallet2->refresh(wallet2->is_trusted_daemon(), syncStartHeight, *result.numBlocksFetched, *result.receivedMoney, true);
wallet2Listener->onSyncEnd();
return result;
}
+29 -85
View File
@@ -17,60 +17,38 @@ namespace monero {
* Models a result of syncing a wallet.
*/
struct MoneroSyncResult {
uint64_t numBlocksFetched;
bool receivedMoney;
shared_ptr<uint64_t> numBlocksFetched;
shared_ptr<bool> receivedMoney;
MoneroSyncResult() {}
MoneroSyncResult(const uint64_t numBlocksFetched, const bool receivedMoney) : numBlocksFetched(numBlocksFetched), receivedMoney(receivedMoney) {}
MoneroSyncResult(const shared_ptr<uint64_t> numBlocksFetched, const shared_ptr<bool> receivedMoney) : numBlocksFetched(numBlocksFetched), receivedMoney(receivedMoney) {}
};
/**
* Models a Monero subaddress.
*/
struct MoneroSubaddress {
uint32_t accountIndex;
uint32_t index;
string address;
string label;
uint64_t balance;
uint64_t unlockedBalance;
uint32_t numUnspentOutputs;
bool isUsed;
uint32_t numBlocksToUnlock;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(accountIndex)
KV_SERIALIZE(index)
KV_SERIALIZE(address)
KV_SERIALIZE(label)
KV_SERIALIZE(balance)
KV_SERIALIZE(unlockedBalance)
KV_SERIALIZE(numUnspentOutputs)
KV_SERIALIZE(isUsed)
KV_SERIALIZE(numBlocksToUnlock)
END_KV_SERIALIZE_MAP()
shared_ptr<uint32_t> accountIndex;
shared_ptr<uint32_t> index;
shared_ptr<string> address;
shared_ptr<string> label;
shared_ptr<uint64_t> balance;
shared_ptr<uint64_t> unlockedBalance;
shared_ptr<uint32_t> numUnspentOutputs;
shared_ptr<bool> isUsed;
shared_ptr<uint32_t> numBlocksToUnlock;
};
/**
* Models a Monero account.
*/
struct MoneroAccount {
uint32_t index;
string primaryAddress;
string label;
uint64_t balance;
uint64_t unlockedBalance;
string tag;
shared_ptr<uint32_t> index;
shared_ptr<string> primaryAddress;
shared_ptr<string> label;
shared_ptr<uint64_t> balance;
shared_ptr<uint64_t> unlockedBalance;
shared_ptr<string> tag;
vector<MoneroSubaddress> subaddresses;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(index)
KV_SERIALIZE(primaryAddress)
KV_SERIALIZE(label)
KV_SERIALIZE(balance)
KV_SERIALIZE(unlockedBalance)
KV_SERIALIZE(tag)
KV_SERIALIZE(subaddresses)
END_KV_SERIALIZE_MAP()
};
// forward declarations
@@ -81,8 +59,8 @@ namespace monero {
* Models an outgoing transfer destination.
*/
struct MoneroDestination {
string address;
uint64_t amount;
shared_ptr<string> address;
shared_ptr<uint64_t> amount;
};
/**
@@ -90,17 +68,17 @@ namespace monero {
*/
struct MoneroTransfer {
shared_ptr<MoneroTxWallet> tx;
uint64_t amount;
uint32_t accountIndex;
bool isIncoming;
shared_ptr<uint64_t> amount;
shared_ptr<uint32_t> accountIndex;
shared_ptr<bool> isIncoming;
};
/**
* Models an incoming transfer of funds to the wallet.
*/
struct MoneroIncomingTransfer : public MoneroTransfer {
uint32_t subaddressIndex;
string address;
shared_ptr<uint32_t> subaddressIndex;
shared_ptr<string> address;
};
/**
@@ -118,8 +96,8 @@ namespace monero {
struct MoneroTxWallet : public MoneroTx {
vector<MoneroIncomingTransfer> incomingTransfers;
shared_ptr<MoneroOutgoingTransfer> outgoingTransfer;
uint32_t numSuggestedConfirmations;
string note;
shared_ptr<uint32_t> numSuggestedConfirmations;
shared_ptr<string> note;
};
/**
@@ -131,7 +109,7 @@ namespace monero {
shared_ptr<uint64_t> amount;
shared_ptr<uint32_t> accountIndex;
shared_ptr<bool> isIncoming;
string address;
shared_ptr<string> address;
vector<string> addresses;
shared_ptr<uint32_t> subaddressIndex;
vector<uint32_t> subaddressIndices;
@@ -145,41 +123,7 @@ namespace monero {
*
* All transactions are returned except those that do not meet the criteria defined in this request.
*/
struct MoneroTxRequest {
shared_ptr<MoneroBlock> block;
string id;
string version;
shared_ptr<bool> isCoinbase;
string paymentId;
shared_ptr<uint64_t> fee;
shared_ptr<uint32_t> mixin;
shared_ptr<bool> doNotRelay;
shared_ptr<bool> isRelayed;
shared_ptr<bool> isConfirmed;
shared_ptr<bool> inTxPool;
shared_ptr<uint64_t> numConfirmations;
shared_ptr<uint64_t> unlockTime;
shared_ptr<uint64_t> lastRelayedTimestamp;
shared_ptr<uint64_t> receivedTimestamp;
shared_ptr<bool> isDoubleSpend;
string key;
string fullHex;
string prunedHex;
string prunableHex;
string prunableHash;
shared_ptr<uint32_t> size;
shared_ptr<uint32_t> weight;
vector<MoneroOutput> vins;
vector<MoneroOutput> vouts;
vector<uint32_t> outputIndices;
string metadata;
shared_ptr<bool> isFailed;
vector<MoneroIncomingTransfer> incomingTransfers;
shared_ptr<MoneroOutgoingTransfer> outgoingTransfer;
shared_ptr<uint32_t> numSuggestedConfirmations;
string note;
struct MoneroTxRequest : public MoneroTxWallet {
shared_ptr<bool> isOutgoing;
shared_ptr<bool> isIncoming;
vector<string> txIds;