Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
transactionrecord.cpp
Go to the documentation of this file.
1 #include "transactionrecord.h"
2 
3 #include "wallet.h"
4 #include "base58.h"
5 
6 /* Return positive answer if transaction should be shown in list.
7  */
9 {
10  if (wtx.IsCoinBase())
11  {
12  // Ensures we show generated coins / mined transactions at depth 1
13  if (!wtx.IsInMainChain())
14  {
15  return false;
16  }
17  }
18  return true;
19 }
20 
21 /*
22  * Decompose CWallet transaction to model transaction records.
23  */
24 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
25 {
26  QList<TransactionRecord> parts;
27  int64 nTime = wtx.GetTxTime();
28  int64 nCredit = wtx.GetCredit(true);
29  int64 nDebit = wtx.GetDebit();
30  int64 nNet = nCredit - nDebit;
31  uint256 hash = wtx.GetHash();
32  std::map<std::string, std::string> mapValue = wtx.mapValue;
33 
34  if (nNet > 0 || wtx.IsCoinBase())
35  {
36  //
37  // Credit
38  //
39  BOOST_FOREACH(const CTxOut& txout, wtx.vout)
40  {
41  if(wallet->IsMine(txout))
42  {
43  TransactionRecord sub(hash, nTime);
45  sub.idx = parts.size(); // sequence number
46  sub.credit = txout.nValue;
47  if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
48  {
49  // Received by Bitcoin Address
51  sub.address = CBitcoinAddress(address).ToString();
52  }
53  else
54  {
55  // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
57  sub.address = mapValue["from"];
58  }
59  if (wtx.IsCoinBase())
60  {
61  // Generated
63  }
64 
65  parts.append(sub);
66  }
67  }
68  }
69  else
70  {
71  bool fAllFromMe = true;
72  BOOST_FOREACH(const CTxIn& txin, wtx.vin)
73  fAllFromMe = fAllFromMe && wallet->IsMine(txin);
74 
75  bool fAllToMe = true;
76  BOOST_FOREACH(const CTxOut& txout, wtx.vout)
77  fAllToMe = fAllToMe && wallet->IsMine(txout);
78 
79  if (fAllFromMe && fAllToMe)
80  {
81  // Payment to self
82  int64 nChange = wtx.GetChange();
83 
84  parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
85  -(nDebit - nChange), nCredit - nChange));
86  }
87  else if (fAllFromMe)
88  {
89  //
90  // Debit
91  //
92  int64 nTxFee = nDebit - wtx.GetValueOut();
93 
94  for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
95  {
96  const CTxOut& txout = wtx.vout[nOut];
97  TransactionRecord sub(hash, nTime);
98  sub.idx = parts.size();
99 
100  if(wallet->IsMine(txout))
101  {
102  // Ignore parts sent to self, as this is usually the change
103  // from a transaction sent back to our own address.
104  continue;
105  }
106 
108  if (ExtractDestination(txout.scriptPubKey, address))
109  {
110  // Sent to Bitcoin Address
112  sub.address = CBitcoinAddress(address).ToString();
113  }
114  else
115  {
116  // Sent to IP, or other non-address transaction like OP_EVAL
118  sub.address = mapValue["to"];
119  }
120 
121  int64 nValue = txout.nValue;
122  /* Add fee to first output */
123  if (nTxFee > 0)
124  {
125  nValue += nTxFee;
126  nTxFee = 0;
127  }
128  sub.debit = -nValue;
129 
130  parts.append(sub);
131  }
132  }
133  else
134  {
135  //
136  // Mixed debit transaction, can't break down payees
137  //
138  parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
139  }
140  }
141 
142  return parts;
143 }
144 
146 {
147  // Determine transaction status
148 
149  // Find the block the tx is in
150  CBlockIndex* pindex = NULL;
151  std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
152  if (mi != mapBlockIndex.end())
153  pindex = (*mi).second;
154 
155  // Sort order, unrecorded transactions sort to the top
156  status.sortKey = strprintf("%010d-%01d-%010u-%03d",
157  (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
158  (wtx.IsCoinBase() ? 1 : 0),
159  wtx.nTimeReceived,
160  idx);
161  status.confirmed = wtx.IsConfirmed();
164 
165  if (!wtx.IsFinal())
166  {
167  if (wtx.nLockTime < LOCKTIME_THRESHOLD)
168  {
170  status.open_for = wtx.nLockTime - nBestHeight + 1;
171  }
172  else
173  {
175  status.open_for = wtx.nLockTime;
176  }
177  }
178  else
179  {
180  if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
181  {
183  }
184  else if (status.depth < NumConfirmations)
185  {
187  }
188  else
189  {
191  }
192  }
193 
194  // For generated transactions, determine maturity
196  {
197  int64 nCredit = wtx.GetCredit(true);
198  if (nCredit == 0)
199  {
201 
202  if (wtx.IsInMainChain())
203  {
205 
206  // Check if the block was requested by anyone
207  if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
209  }
210  else
211  {
213  }
214  }
215  else
216  {
218  }
219  }
220 }
221 
223 {
225 }
226 
228 {
229  return hash.ToString() + strprintf("-%03d", idx);
230 }
231 
bool IsFinal(int nBlockHeight=0, int64 nBlockTime=0) const
Definition: main.h:520
#define strprintf(format,...)
Definition: util.h:169
int idx
Subtransaction index, for sort key.
bool IsMine(const CTxIn &txin) const
Definition: wallet.cpp:540
CScript scriptPubKey
Definition: main.h:404
std::string getTxID()
Return the unique identifier for this transaction (part)
uint256 hashBlock
Definition: main.h:1126
uint256 GetHash() const
Definition: main.h:515
static bool showTransaction(const CWalletTx &wtx)
Decompose CWallet transaction to model transaction records.
int64 GetCredit(bool fUseCache=true) const
Definition: wallet.h:565
mapValue_t mapValue
Definition: wallet.h:374
int GetBlocksToMaturity() const
Definition: main.cpp:931
int GetRequestCount() const
Definition: wallet.cpp:598
bool IsInMainChain() const
Definition: main.h:1165
UI model for a transaction.
bool IsConfirmed() const
Definition: wallet.h:640
TransactionStatus status
Status: can change with block chain update.
unsigned int nLockTime
Definition: main.h:486
static QList< TransactionRecord > decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
An input of a transaction.
Definition: main.h:323
std::vector< CTxOut > vout
Definition: main.h:485
std::vector< CTxIn > vin
Definition: main.h:484
int64 GetAdjustedTime()
Definition: util.cpp:1317
int64 GetDebit() const
Definition: wallet.h:554
void updateStatus(const CWalletTx &wtx)
Update status from core wallet tx.
std::string ToString() const
Definition: base58.h:229
An output of a transaction.
Definition: main.h:400
int64 GetValueOut() const
Amount of bitcoins spent by this transaction.
Definition: main.h:602
static const int NumConfirmations
Number of confirmation needed for transaction.
int cur_num_blocks
Current number of blocks (to know whether cached status is still valid)
int64 GetChange() const
Definition: wallet.h:620
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:367
int64 GetTxTime() const
Definition: wallet.cpp:592
Transaction will likely not mature because no nodes have confirmed.
int64 open_for
Timestamp if status==OpenUntilDate, otherwise number of additional blocks that need to be mined befor...
256-bit unsigned integer
Definition: uint256.h:537
int64 nValue
Definition: main.h:403
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:1626
std::string ToString() const
Definition: uint256.h:343
bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
Definition: script.cpp:1378
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Definition: script.cpp:1422
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:69
int GetDepthInMainChain(CBlockIndex *&pindexRet) const
Definition: main.cpp:905
bool statusUpdateNeeded()
Return whether a status update is needed.
bool IsCoinBase() const
Definition: main.h:566
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: script.h:62
unsigned int nTimeReceived
Definition: wallet.h:377
int nHeight
Definition: main.h:1639
int nBestHeight
Definition: main.cpp:41
map< uint256, CBlockIndex * > mapBlockIndex
Definition: main.cpp:37
long long int64
Definition: serialize.h:25