danicoin/src/WalletLegacy/WalletUserTransactionsCache.cpp

306 lines
11 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
2014-06-25 17:21:42 +00:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2015-07-30 15:22:07 +00:00
#include "IWalletLegacy.h"
#include "Wallet/WalletErrors.h"
#include "WalletLegacy/WalletUserTransactionsCache.h"
#include "WalletLegacy/WalletLegacySerialization.h"
#include "WalletLegacy/WalletUtils.h"
#include "Serialization/ISerializer.h"
#include "Serialization/SerializationOverloads.h"
2014-06-25 17:21:42 +00:00
#include <algorithm>
2015-07-30 15:22:07 +00:00
using namespace Crypto;
2015-07-30 15:22:07 +00:00
namespace CryptoNote {
2015-08-05 13:09:05 +00:00
WalletUserTransactionsCache::WalletUserTransactionsCache(uint64_t mempoolTxLiveTime) : m_unconfirmedTransactions(mempoolTxLiveTime) {
}
2015-07-15 12:23:00 +00:00
bool WalletUserTransactionsCache::serialize(CryptoNote::ISerializer& s) {
2015-05-27 12:08:46 +00:00
if (s.type() == CryptoNote::ISerializer::INPUT) {
s(m_transactions, "transactions");
s(m_transfers, "transfers");
s(m_unconfirmedTransactions, "unconfirmed");
2015-08-05 13:09:05 +00:00
updateUnconfirmedTransactions();
2015-08-05 13:09:05 +00:00
deleteOutdatedTransactions();
} else {
UserTransactions txsToSave;
UserTransfers transfersToSave;
getGoodItems(txsToSave, transfersToSave);
s(txsToSave, "transactions");
s(transfersToSave, "transfers");
s(m_unconfirmedTransactions, "unconfirmed");
}
2015-07-15 12:23:00 +00:00
return true;
2014-06-25 17:21:42 +00:00
}
uint64_t WalletUserTransactionsCache::unconfirmedTransactionsAmount() const {
return m_unconfirmedTransactions.countUnconfirmedTransactionsAmount();
2014-06-25 17:21:42 +00:00
}
uint64_t WalletUserTransactionsCache::unconfrimedOutsAmount() const {
return m_unconfirmedTransactions.countUnconfirmedOutsAmount();
}
2014-06-25 17:21:42 +00:00
size_t WalletUserTransactionsCache::getTransactionCount() const {
2014-06-25 17:21:42 +00:00
return m_transactions.size();
}
size_t WalletUserTransactionsCache::getTransferCount() const {
2014-06-25 17:21:42 +00:00
return m_transfers.size();
}
TransactionId WalletUserTransactionsCache::addNewTransaction(
2015-07-30 15:22:07 +00:00
uint64_t amount, uint64_t fee, const std::string& extra, const std::vector<WalletLegacyTransfer>& transfers, uint64_t unlockTime) {
2015-07-30 15:22:07 +00:00
WalletLegacyTransaction transaction;
transaction.firstTransferId = insertTransfers(transfers);
transaction.transferCount = transfers.size();
transaction.totalAmount = -static_cast<int64_t>(amount);
transaction.fee = fee;
transaction.sentTime = time(nullptr);
transaction.isCoinbase = false;
transaction.timestamp = 0;
transaction.extra = extra;
2015-07-30 15:22:07 +00:00
transaction.blockHeight = WALLET_LEGACY_UNCONFIRMED_TRANSACTION_HEIGHT;
transaction.state = WalletLegacyTransactionState::Sending;
transaction.unlockTime = unlockTime;
return insertTransaction(std::move(transaction));
}
void WalletUserTransactionsCache::updateTransaction(
2015-05-27 12:08:46 +00:00
TransactionId transactionId, const CryptoNote::Transaction& tx, uint64_t amount, const std::list<TransactionOutputInformation>& usedOutputs) {
// update extra field from created transaction
auto& txInfo = m_transactions.at(transactionId);
txInfo.extra.assign(tx.extra.begin(), tx.extra.end());
m_unconfirmedTransactions.add(tx, transactionId, amount, usedOutputs);
}
void WalletUserTransactionsCache::updateTransactionSendingState(TransactionId transactionId, std::error_code ec) {
auto& txInfo = m_transactions.at(transactionId);
if (ec) {
2015-07-30 15:22:07 +00:00
txInfo.state = ec.value() == error::TX_CANCELLED ? WalletLegacyTransactionState::Cancelled : WalletLegacyTransactionState::Failed;
m_unconfirmedTransactions.erase(txInfo.hash);
} else {
txInfo.sentTime = time(nullptr); // update sending time
2015-07-30 15:22:07 +00:00
txInfo.state = WalletLegacyTransactionState::Active;
}
}
2015-07-30 15:22:07 +00:00
std::shared_ptr<WalletLegacyEvent> WalletUserTransactionsCache::onTransactionUpdated(const TransactionInformation& txInfo, int64_t txBalance) {
std::shared_ptr<WalletLegacyEvent> event;
2015-07-30 15:22:07 +00:00
TransactionId id = CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID;
if (!m_unconfirmedTransactions.findTransactionId(txInfo.transactionHash, id)) {
id = findTransactionByHash(txInfo.transactionHash);
} else {
m_unconfirmedTransactions.erase(txInfo.transactionHash);
}
bool isCoinbase = txInfo.totalAmountIn == 0;
2015-07-30 15:22:07 +00:00
if (id == CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID) {
WalletLegacyTransaction transaction;
transaction.firstTransferId = WALLET_LEGACY_INVALID_TRANSFER_ID;
transaction.transferCount = 0;
transaction.totalAmount = txBalance;
transaction.fee = isCoinbase ? 0 : txInfo.totalAmountIn - txInfo.totalAmountOut;
transaction.sentTime = 0;
transaction.hash = txInfo.transactionHash;
transaction.blockHeight = txInfo.blockHeight;
transaction.isCoinbase = isCoinbase;
transaction.timestamp = txInfo.timestamp;
transaction.extra.assign(txInfo.extra.begin(), txInfo.extra.end());
2015-07-30 15:22:07 +00:00
transaction.state = WalletLegacyTransactionState::Active;
transaction.unlockTime = txInfo.unlockTime;
id = insertTransaction(std::move(transaction));
// notification event
event = std::make_shared<WalletExternalTransactionCreatedEvent>(id);
} else {
2015-07-30 15:22:07 +00:00
WalletLegacyTransaction& tr = getTransaction(id);
tr.blockHeight = txInfo.blockHeight;
tr.timestamp = txInfo.timestamp;
2015-07-30 15:22:07 +00:00
tr.state = WalletLegacyTransactionState::Active;
// notification event
event = std::make_shared<WalletTransactionUpdatedEvent>(id);
}
return event;
}
2015-07-30 15:22:07 +00:00
std::shared_ptr<WalletLegacyEvent> WalletUserTransactionsCache::onTransactionDeleted(const Hash& transactionHash) {
TransactionId id = CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID;
if (m_unconfirmedTransactions.findTransactionId(transactionHash, id)) {
m_unconfirmedTransactions.erase(transactionHash);
2015-07-15 12:23:00 +00:00
// LOG_ERROR("Unconfirmed transaction is deleted: id = " << id << ", hash = " << transactionHash);
assert(false);
} else {
id = findTransactionByHash(transactionHash);
}
2015-07-30 15:22:07 +00:00
std::shared_ptr<WalletLegacyEvent> event;
if (id != CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID) {
WalletLegacyTransaction& tr = getTransaction(id);
tr.blockHeight = WALLET_LEGACY_UNCONFIRMED_TRANSACTION_HEIGHT;
tr.timestamp = 0;
2015-07-30 15:22:07 +00:00
tr.state = WalletLegacyTransactionState::Deleted;
event = std::make_shared<WalletTransactionUpdatedEvent>(id);
} else {
2015-07-15 12:23:00 +00:00
// LOG_ERROR("Transaction wasn't found: " << transactionHash);
assert(false);
}
return event;
}
2014-06-25 17:21:42 +00:00
TransactionId WalletUserTransactionsCache::findTransactionByTransferId(TransferId transferId) const
{
TransactionId id;
for (id = 0; id < m_transactions.size(); ++id) {
2015-07-30 15:22:07 +00:00
const WalletLegacyTransaction& tx = m_transactions[id];
2014-06-25 17:21:42 +00:00
2015-07-30 15:22:07 +00:00
if (tx.firstTransferId == WALLET_LEGACY_INVALID_TRANSFER_ID || tx.transferCount == 0)
2014-06-25 17:21:42 +00:00
continue;
if (transferId >= tx.firstTransferId && transferId < (tx.firstTransferId + tx.transferCount))
break;
}
if (id == m_transactions.size())
2015-07-30 15:22:07 +00:00
return WALLET_LEGACY_INVALID_TRANSACTION_ID;
2014-06-25 17:21:42 +00:00
return id;
}
2015-07-30 15:22:07 +00:00
bool WalletUserTransactionsCache::getTransaction(TransactionId transactionId, WalletLegacyTransaction& transaction) const
2014-06-25 17:21:42 +00:00
{
if (transactionId >= m_transactions.size())
return false;
transaction = m_transactions[transactionId];
return true;
}
2015-07-30 15:22:07 +00:00
bool WalletUserTransactionsCache::getTransfer(TransferId transferId, WalletLegacyTransfer& transfer) const
2014-06-25 17:21:42 +00:00
{
if (transferId >= m_transfers.size())
return false;
transfer = m_transfers[transferId];
return true;
}
2015-07-30 15:22:07 +00:00
TransactionId WalletUserTransactionsCache::insertTransaction(WalletLegacyTransaction&& Transaction) {
m_transactions.emplace_back(std::move(Transaction));
2014-06-25 17:21:42 +00:00
return m_transactions.size() - 1;
}
2015-07-30 15:22:07 +00:00
TransactionId WalletUserTransactionsCache::findTransactionByHash(const Hash& hash) {
auto it = std::find_if(m_transactions.begin(), m_transactions.end(), [&hash](const WalletLegacyTransaction& tx) { return tx.hash == hash; });
2014-06-25 17:21:42 +00:00
if (it == m_transactions.end())
2015-07-30 15:22:07 +00:00
return CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID;
2014-06-25 17:21:42 +00:00
return std::distance(m_transactions.begin(), it);
}
bool WalletUserTransactionsCache::isUsed(const TransactionOutputInformation& out) const {
return m_unconfirmedTransactions.isUsed(out);
2014-06-25 17:21:42 +00:00
}
2015-07-30 15:22:07 +00:00
WalletLegacyTransaction& WalletUserTransactionsCache::getTransaction(TransactionId transactionId) {
2014-06-25 17:21:42 +00:00
return m_transactions.at(transactionId);
}
void WalletUserTransactionsCache::getGoodItems(UserTransactions& transactions, UserTransfers& transfers) {
2014-06-25 17:21:42 +00:00
size_t offset = 0;
for (size_t txId = 0; txId < m_transactions.size(); ++txId) {
bool isGood =
2015-07-30 15:22:07 +00:00
m_transactions[txId].state != WalletLegacyTransactionState::Cancelled &&
m_transactions[txId].state != WalletLegacyTransactionState::Failed;
2014-06-25 17:21:42 +00:00
if (isGood) {
getGoodTransaction(txId, offset, transactions, transfers);
} else {
2015-07-30 15:22:07 +00:00
const WalletLegacyTransaction& t = m_transactions[txId];
offset += t.firstTransferId != WALLET_LEGACY_INVALID_TRANSFER_ID ? t.transferCount : 0;
2014-06-25 17:21:42 +00:00
}
}
}
void WalletUserTransactionsCache::getGoodTransaction(TransactionId txId, size_t offset, UserTransactions& transactions, UserTransfers& transfers) {
2014-06-25 17:21:42 +00:00
transactions.push_back(m_transactions[txId]);
2015-07-30 15:22:07 +00:00
WalletLegacyTransaction& tx = transactions.back();
2014-06-25 17:21:42 +00:00
2015-07-30 15:22:07 +00:00
if (tx.firstTransferId == WALLET_LEGACY_INVALID_TRANSFER_ID) {
2014-06-25 17:21:42 +00:00
return;
}
UserTransfers::const_iterator first = m_transfers.begin() + tx.firstTransferId;
UserTransfers::const_iterator last = first + tx.transferCount;
tx.firstTransferId -= offset;
std::copy(first, last, std::back_inserter(transfers));
}
void WalletUserTransactionsCache::getTransfersByTx(TransactionId id, UserTransfers& transfers) {
2015-07-30 15:22:07 +00:00
const WalletLegacyTransaction& tx = m_transactions[id];
2014-06-25 17:21:42 +00:00
2015-07-30 15:22:07 +00:00
if (tx.firstTransferId != WALLET_LEGACY_INVALID_TRANSFER_ID) {
2014-06-25 17:21:42 +00:00
UserTransfers::const_iterator first = m_transfers.begin() + tx.firstTransferId;
UserTransfers::const_iterator last = first + tx.transferCount;
std::copy(first, last, std::back_inserter(transfers));
}
}
2015-07-30 15:22:07 +00:00
TransferId WalletUserTransactionsCache::insertTransfers(const std::vector<WalletLegacyTransfer>& transfers) {
2014-06-25 17:21:42 +00:00
std::copy(transfers.begin(), transfers.end(), std::back_inserter(m_transfers));
return m_transfers.size() - transfers.size();
}
void WalletUserTransactionsCache::updateUnconfirmedTransactions() {
for (TransactionId id = 0; id < m_transactions.size(); ++id) {
2015-07-30 15:22:07 +00:00
if (m_transactions[id].blockHeight == WALLET_LEGACY_UNCONFIRMED_TRANSACTION_HEIGHT) {
m_unconfirmedTransactions.updateTransactionId(m_transactions[id].hash, id);
}
}
}
2015-07-30 15:22:07 +00:00
WalletLegacyTransfer& WalletUserTransactionsCache::getTransfer(TransferId transferId) {
2014-06-25 17:21:42 +00:00
return m_transfers.at(transferId);
}
2015-07-09 14:52:47 +00:00
void WalletUserTransactionsCache::reset() {
m_transactions.clear();
m_transfers.clear();
m_unconfirmedTransactions.reset();
}
2014-06-25 17:21:42 +00:00
2015-08-05 13:09:05 +00:00
std::vector<TransactionId> WalletUserTransactionsCache::deleteOutdatedTransactions() {
auto deletedTransactions = m_unconfirmedTransactions.deleteOutdatedTransactions();
for (auto id: deletedTransactions) {
assert(id < m_transactions.size());
m_transactions[id].state = WalletLegacyTransactionState::Deleted;
}
return deletedTransactions;
}
2014-06-25 17:21:42 +00:00
} //namespace CryptoNote