2015-05-27 12:08:46 +00:00
|
|
|
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
|
2014-08-13 10:38:35 +00:00
|
|
|
//
|
|
|
|
// This file is part of Bytecoin.
|
|
|
|
//
|
|
|
|
// Bytecoin is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Bytecoin is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
|
2014-06-25 17:21:42 +00:00
|
|
|
|
|
|
|
#include "WalletUnconfirmedTransactions.h"
|
2015-04-06 16:13:07 +00:00
|
|
|
#include "WalletSerialization.h"
|
|
|
|
|
2014-06-25 17:21:42 +00:00
|
|
|
#include "cryptonote_core/cryptonote_format_utils.h"
|
2015-04-06 16:13:07 +00:00
|
|
|
#include "serialization/ISerializer.h"
|
|
|
|
#include "serialization/SerializationOverloads.h"
|
2014-06-25 17:21:42 +00:00
|
|
|
|
|
|
|
namespace CryptoNote {
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
inline TransactionOutputId getOutputId(const TransactionOutputInformation& out) {
|
|
|
|
return std::make_pair(out.transactionPublicKey, out.outputInTransaction);
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
void WalletUnconfirmedTransactions::serialize(CryptoNote::ISerializer& s, const std::string& name) {
|
2015-04-06 16:13:07 +00:00
|
|
|
s.beginObject(name);
|
|
|
|
s(m_unconfirmedTxs, "transactions");
|
|
|
|
s.endObject();
|
2014-06-25 17:21:42 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
if (s.type() == CryptoNote::ISerializer::INPUT) {
|
2015-04-06 16:13:07 +00:00
|
|
|
collectUsedOutputs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WalletUnconfirmedTransactions::findTransactionId(const TransactionHash& hash, TransactionId& id) {
|
|
|
|
auto it = m_unconfirmedTxs.find(hash);
|
|
|
|
if (it == m_unconfirmedTxs.end()) {
|
2014-06-25 17:21:42 +00:00
|
|
|
return false;
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
2014-06-25 17:21:42 +00:00
|
|
|
|
|
|
|
id = it->second.transactionId;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
void WalletUnconfirmedTransactions::erase(const TransactionHash& hash) {
|
|
|
|
auto it = m_unconfirmedTxs.find(hash);
|
|
|
|
if (it == m_unconfirmedTxs.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& o : it->second.usedOutputs) {
|
|
|
|
m_usedOutputs.erase(o);
|
|
|
|
}
|
|
|
|
m_unconfirmedTxs.erase(it);
|
2014-06-25 17:21:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
void WalletUnconfirmedTransactions::add(const CryptoNote::Transaction& tx, TransactionId transactionId,
|
2015-04-06 16:13:07 +00:00
|
|
|
uint64_t amount, const std::list<TransactionOutputInformation>& usedOutputs) {
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
auto cryptoHash = CryptoNote::get_transaction_hash(tx);
|
2015-04-06 16:13:07 +00:00
|
|
|
TransactionHash hash = reinterpret_cast<const TransactionHash&>(cryptoHash);
|
|
|
|
|
|
|
|
UnconfirmedTransferDetails& utd = m_unconfirmedTxs[hash];
|
2014-06-25 17:21:42 +00:00
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
utd.amount = amount;
|
|
|
|
utd.sentTime = time(nullptr);
|
2014-06-25 17:21:42 +00:00
|
|
|
utd.tx = tx;
|
|
|
|
utd.transactionId = transactionId;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
uint64_t outsAmount = 0;
|
|
|
|
// process used outputs
|
|
|
|
utd.usedOutputs.reserve(usedOutputs.size());
|
|
|
|
for (const auto& out : usedOutputs) {
|
|
|
|
auto id = getOutputId(out);
|
|
|
|
utd.usedOutputs.push_back(id);
|
|
|
|
m_usedOutputs.insert(id);
|
|
|
|
outsAmount += out.amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
utd.outsAmount = outsAmount;
|
2014-06-25 17:21:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
void WalletUnconfirmedTransactions::updateTransactionId(const TransactionHash& hash, TransactionId id) {
|
|
|
|
auto it = m_unconfirmedTxs.find(hash);
|
|
|
|
if (it != m_unconfirmedTxs.end()) {
|
|
|
|
it->second.transactionId = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t WalletUnconfirmedTransactions::countUnconfirmedOutsAmount() const {
|
|
|
|
uint64_t amount = 0;
|
|
|
|
|
|
|
|
for (auto& utx: m_unconfirmedTxs)
|
|
|
|
amount+= utx.second.outsAmount;
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t WalletUnconfirmedTransactions::countUnconfirmedTransactionsAmount() const {
|
2014-06-25 17:21:42 +00:00
|
|
|
uint64_t amount = 0;
|
|
|
|
|
|
|
|
for (auto& utx: m_unconfirmedTxs)
|
2015-04-06 16:13:07 +00:00
|
|
|
amount+= utx.second.amount;
|
2014-06-25 17:21:42 +00:00
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
bool WalletUnconfirmedTransactions::isUsed(const TransactionOutputInformation& out) const {
|
|
|
|
return m_usedOutputs.find(getOutputId(out)) != m_usedOutputs.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WalletUnconfirmedTransactions::collectUsedOutputs() {
|
|
|
|
UsedOutputsContainer used;
|
|
|
|
for (const auto& kv : m_unconfirmedTxs) {
|
|
|
|
used.insert(kv.second.usedOutputs.begin(), kv.second.usedOutputs.end());
|
|
|
|
}
|
|
|
|
m_usedOutputs = std::move(used);
|
|
|
|
}
|
|
|
|
|
2015-07-09 14:52:47 +00:00
|
|
|
void WalletUnconfirmedTransactions::reset() {
|
|
|
|
m_unconfirmedTxs.clear();
|
|
|
|
m_usedOutputs.clear();
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2014-06-25 17:21:42 +00:00
|
|
|
} /* namespace CryptoNote */
|