forked from litecoin-project/litecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxrecord.cpp
More file actions
207 lines (175 loc) · 6.92 KB
/
txrecord.cpp
File metadata and controls
207 lines (175 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <wallet/txrecord.h>
#include <wallet/wallet.h>
#include <wallet/txrecord.h>
#include <wallet/wallet.h>
#include <chain.h>
#include <core_io.h>
#include <interfaces/chain.h>
#include <key_io.h>
#include <util/check.h>
#include <chrono>
using namespace std::chrono;
bool WalletTxRecord::UpdateStatusIfNeeded(const uint256& block_hash)
{
assert(!block_hash.IsNull());
// Check if update is needed
if (status.m_cur_block_hash == block_hash && !status.needsUpdate) {
return false;
}
// Try locking the wallet
TRY_LOCK(m_pWallet->cs_wallet, locked_wallet);
if (!locked_wallet) {
return false;
}
const int last_block_height = m_pWallet->GetLastBlockHeight();
int64_t block_time = -1;
CHECK_NONFATAL(m_pWallet->chain().findBlock(m_pWallet->GetLastBlockHash(), interfaces::FoundBlock().time(block_time)));
// Sort order, unrecorded transactions sort to the top
// Sub-components sorted with standard outputs first, MWEB outputs second, then MWEB pegouts third.
std::string idx = strprintf("0%03d", 0);
if (index) {
if (index->type() == typeid(int)) {
idx = strprintf("0%03d", boost::get<int>(*index));
} else if (index->type() == typeid(mw::Hash)) {
idx = "1" + boost::get<mw::Hash>(*index).ToHex();
} else {
const PegoutIndex& pegout_idx = boost::get<PegoutIndex>(*index);
idx = "2" + pegout_idx.kernel_id.ToHex() + strprintf("%03d", pegout_idx.pos);
}
}
int block_height = m_wtx->m_confirm.block_height > 0 ? m_wtx->m_confirm.block_height : std::numeric_limits<int>::max();
int blocks_to_maturity = m_wtx->GetBlocksToMaturity();
status.sortKey = strprintf("%010d-%01d-%010u-%s",
block_height,
m_wtx->IsCoinBase() ? 1 : 0,
m_wtx->nTimeReceived,
idx);
status.countsForBalance = m_wtx->IsTrusted() && !(blocks_to_maturity > 0);
status.depth = m_wtx->GetDepthInMainChain();
status.m_cur_block_hash = block_hash;
if (m_wtx->IsInMainChain()) {
status.matures_in = blocks_to_maturity;
}
const int64_t time_since_epoch = (int64_t)duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
const bool up_to_date = (time_since_epoch - block_time < MAX_BLOCK_TIME_GAP);
if (up_to_date && !m_wtx->IsFinal()) {
if (m_wtx->tx->nLockTime < LOCKTIME_THRESHOLD) {
status.status = WalletTxStatus::OpenUntilBlock;
status.open_for = m_wtx->tx->nLockTime - last_block_height;
} else {
status.status = WalletTxStatus::OpenUntilDate;
status.open_for = m_wtx->tx->nLockTime;
}
}
// For generated transactions, determine maturity
else if (type == WalletTxRecord::Type::Generated || m_wtx->IsHogEx()) {
if (blocks_to_maturity > 0) {
status.status = WalletTxStatus::Immature;
if (!m_wtx->IsInMainChain()) {
status.status = WalletTxStatus::NotAccepted;
}
} else {
status.status = WalletTxStatus::Confirmed;
}
} else {
if (status.depth < 0) {
status.status = WalletTxStatus::Conflicted;
} else if (status.depth == 0) {
status.status = WalletTxStatus::Unconfirmed;
if (m_wtx->isAbandoned())
status.status = WalletTxStatus::Abandoned;
} else if (status.depth < RecommendedNumConfirmations) {
status.status = WalletTxStatus::Confirming;
} else {
status.status = WalletTxStatus::Confirmed;
}
}
status.needsUpdate = false;
return true;
}
const uint256& WalletTxRecord::GetTxHash() const
{
assert(m_wtx != nullptr);
return m_wtx->GetHash();
}
std::string WalletTxRecord::GetTxString() const
{
assert(m_wtx != nullptr);
if (m_wtx->IsPartialMWEB()) {
if (m_wtx->mweb_wtx_info->received_coin) {
const mw::Coin& received_coin = *m_wtx->mweb_wtx_info->received_coin;
return strprintf("MWEB Output(ID=%s, amount=%d)", received_coin.output_id.ToHex(), received_coin.amount);
} else if (m_wtx->mweb_wtx_info->spent_input) {
return strprintf("MWEB Input(ID=%s)\n", m_wtx->mweb_wtx_info->spent_input->ToHex());
}
}
return m_wtx->tx->ToString();
}
int64_t WalletTxRecord::GetTxTime() const
{
assert(m_wtx != nullptr);
return m_wtx->GetTxTime();
}
std::string WalletTxRecord::GetComponentIndex() const
{
std::string idx;
if (this->index) {
if (this->index->type() == typeid(int)) {
idx = std::to_string(boost::get<int>(*index));
} else if (index->type() == typeid(mw::Hash)) {
idx = "MWEB Output (" + boost::get<mw::Hash>(*index).ToHex() + ")";
} else {
const PegoutIndex& pegout_idx = boost::get<PegoutIndex>(*index);
idx = "Pegout (" + pegout_idx.kernel_id.ToHex() + ":" + std::to_string(pegout_idx.pos) + ")";
}
}
return idx;
}
std::string WalletTxRecord::GetType() const
{
switch (this->type) {
case Other: return "Other";
case Generated: return "Generated";
case SendToAddress: return "SendToAddress";
case SendToOther: return "SendToOther";
case RecvWithAddress: return "RecvWithAddress";
case RecvFromOther: return "RecvFromOther";
case SendToSelf: return "SendToSelf";
}
assert(false);
}
UniValue WalletTxRecord::ToUniValue() const
{
UniValue entry(UniValue::VOBJ);
entry.pushKV("type", GetType());
entry.pushKV("amount", ValueFromAmount(GetAmount()));
entry.pushKV("net", ValueFromAmount(GetNet()));
CTxDestination destination = DecodeDestination(this->address);
if (m_wtx->IsFromMe(ISMINE_WATCH_ONLY) || (IsValidDestination(destination) && (m_pWallet->IsMine(destination) & ISMINE_WATCH_ONLY))) {
entry.pushKV("involvesWatchonly", true);
}
if (IsValidDestination(destination)) {
const auto* address_book_entry = m_pWallet->FindAddressBookEntry(destination);
if (address_book_entry) {
entry.pushKV("label", address_book_entry->GetLabel());
}
}
if (!this->address.empty()) {
entry.pushKV("address", this->address);
}
if (this->index) {
if (this->index->type() == typeid(int)) {
entry.pushKV("vout", boost::get<int>(*this->index));
} else if (this->index->type() == typeid(mw::Hash)) {
entry.pushKV("mweb_out", boost::get<mw::Hash>(*this->index).ToHex());
} else {
const PegoutIndex& pegout_idx = boost::get<PegoutIndex>(*index);
entry.pushKV("pegout", pegout_idx.kernel_id.ToHex() + ":" + std::to_string(pegout_idx.pos));
}
}
if (this->debit > 0) {
entry.pushKV("fee", ValueFromAmount(-this->fee));
entry.pushKV("abandoned", m_wtx->isAbandoned());
}
return entry;
}