2012-03-18 23:14:03 +01:00
# include "transactiondesc.h"
2011-06-10 15:05:51 +02:00
# include "guiutil.h"
2011-08-08 17:38:17 +02:00
# include "bitcoinunits.h"
2012-04-15 22:10:54 +02:00
# include "main.h"
# include "wallet.h"
# include "db.h"
2012-04-04 13:19:30 +02:00
# include "ui_interface.h"
2012-05-14 23:44:52 +02:00
# include "base58.h"
2013-07-22 08:50:39 +02:00
# include "paymentserver.h"
2011-06-10 15:05:51 +02:00
2013-01-23 21:51:02 +01:00
# include <string>
2011-08-08 17:38:17 +02:00
QString TransactionDesc : : FormatTxStatus ( const CWalletTx & wtx )
2011-06-10 15:05:51 +02:00
{
2013-01-08 13:17:15 +01:00
if ( ! IsFinalTx ( wtx ) )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
if ( wtx . nLockTime < LOCKTIME_THRESHOLD )
2013-01-09 09:14:48 +01:00
return tr ( " Open for %n more block(s) " , " " , wtx.nLockTime - nBestHeight + 1) ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
return tr ( " Open until %1 " ) . arg ( GUIUtil : : dateTimeStr ( wtx . nLockTime ) ) ;
2011-06-10 15:05:51 +02:00
}
else
{
int nDepth = wtx . GetDepthInMainChain ( ) ;
if ( GetAdjustedTime ( ) - wtx . nTimeReceived > 2 * 60 & & wtx . GetRequestCount ( ) = = 0 )
2012-07-03 14:37:51 +02:00
return tr ( " %1/offline " ) . arg ( nDepth ) ;
2011-06-10 15:05:51 +02:00
else if ( nDepth < 6 )
2011-08-08 17:38:17 +02:00
return tr ( " %1/unconfirmed " ) . arg ( nDepth ) ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
return tr ( " %1 confirmations " ) . arg ( nDepth ) ;
2011-06-10 15:05:51 +02:00
}
}
2013-08-24 15:07:17 +02:00
QString TransactionDesc : : toHTML ( CWallet * wallet , CWalletTx & wtx , int unit )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
QString strHTML ;
2012-04-06 18:39:12 +02:00
2011-06-10 15:05:51 +02:00
{
2012-04-06 18:39:12 +02:00
LOCK ( wallet - > cs_wallet ) ;
2011-06-10 15:05:51 +02:00
strHTML . reserve ( 4000 ) ;
strHTML + = " <html><font face='verdana, arial, helvetica, sans-serif'> " ;
2011-12-21 22:33:19 +01:00
int64 nTime = wtx . GetTxTime ( ) ;
int64 nCredit = wtx . GetCredit ( ) ;
int64 nDebit = wtx . GetDebit ( ) ;
int64 nNet = nCredit - nDebit ;
2011-06-10 15:05:51 +02:00
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Status " ) + " :</b> " + FormatTxStatus ( wtx ) ;
2011-06-10 15:05:51 +02:00
int nRequests = wtx . GetRequestCount ( ) ;
if ( nRequests ! = - 1 )
{
if ( nRequests = = 0 )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " , has not been successfully broadcast yet " ) ;
2012-07-03 14:37:51 +02:00
else if ( nRequests > 0 )
strHTML + = tr ( " , broadcast through %n node(s) " , " " , nRequests ) ;
2011-06-10 15:05:51 +02:00
}
strHTML + = " <br> " ;
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Date " ) + " :</b> " + ( nTime ? GUIUtil : : dateTimeStr ( nTime ) : " " ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
// From
//
if ( wtx . IsCoinBase ( ) )
{
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Source " ) + " :</b> " + tr ( " Generated " ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
2012-09-28 13:57:07 +02:00
else if ( wtx . mapValue . count ( " from " ) & & ! wtx . mapValue [ " from " ] . empty ( ) )
2011-06-10 15:05:51 +02:00
{
// Online transaction
2012-09-28 13:57:07 +02:00
strHTML + = " <b> " + tr ( " From " ) + " :</b> " + GUIUtil : : HtmlEscape ( wtx . mapValue [ " from " ] ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
else
{
// Offline transaction
if ( nNet > 0 )
{
// Credit
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
{
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2011-06-10 15:05:51 +02:00
{
2012-05-14 23:44:52 +02:00
CTxDestination address ;
if ( ExtractDestination ( txout . scriptPubKey , address ) & & IsMine ( * wallet , address ) )
2011-06-10 15:05:51 +02:00
{
2011-07-26 15:38:31 +02:00
if ( wallet - > mapAddressBook . count ( address ) )
2011-06-10 15:05:51 +02:00
{
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " From " ) + " :</b> " + tr ( " unknown " ) + " <br> " ;
strHTML + = " <b> " + tr ( " To " ) + " :</b> " ;
2012-05-14 23:44:52 +02:00
strHTML + = GUIUtil : : HtmlEscape ( CBitcoinAddress ( address ) . ToString ( ) ) ;
2013-07-15 07:20:50 +02:00
if ( ! wallet - > mapAddressBook [ address ] . name . empty ( ) )
strHTML + = " ( " + tr ( " own address " ) + " , " + tr ( " label " ) + " : " + GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] . name ) + " ) " ;
2011-06-10 15:05:51 +02:00
else
2012-07-03 14:37:51 +02:00
strHTML + = " ( " + tr ( " own address " ) + " ) " ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
}
break ;
}
}
}
}
//
// To
//
2012-09-28 13:57:07 +02:00
if ( wtx . mapValue . count ( " to " ) & & ! wtx . mapValue [ " to " ] . empty ( ) )
2011-06-10 15:05:51 +02:00
{
// Online transaction
2012-07-03 14:37:51 +02:00
std : : string strAddress = wtx . mapValue [ " to " ] ;
strHTML + = " <b> " + tr ( " To " ) + " :</b> " ;
2012-05-14 23:44:52 +02:00
CTxDestination dest = CBitcoinAddress ( strAddress ) . Get ( ) ;
2013-07-15 07:20:50 +02:00
if ( wallet - > mapAddressBook . count ( dest ) & & ! wallet - > mapAddressBook [ dest ] . name . empty ( ) )
strHTML + = GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ dest ] . name ) + " " ;
2011-11-11 10:13:25 +01:00
strHTML + = GUIUtil : : HtmlEscape ( strAddress ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
//
// Amount
//
if ( wtx . IsCoinBase ( ) & & nCredit = = 0 )
{
//
// Coinbase
//
2011-12-21 22:33:19 +01:00
int64 nUnmatured = 0 ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
nUnmatured + = wallet - > GetCredit ( txout ) ;
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " ;
2011-06-10 15:05:51 +02:00
if ( wtx . IsInMainChain ( ) )
2013-08-24 15:07:17 +02:00
strHTML + = BitcoinUnits : : formatWithUnit ( unit , nUnmatured ) + " ( " + tr ( " matures in %n more block(s) " , " " , wtx . GetBlocksToMaturity ( ) ) + " ) " ;
2011-06-10 15:05:51 +02:00
else
2012-07-03 14:37:51 +02:00
strHTML + = " ( " + tr ( " not accepted " ) + " ) " ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
else if ( nNet > 0 )
{
//
// Credit
//
2013-08-24 15:07:17 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , nNet ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
else
{
bool fAllFromMe = true ;
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
2011-06-26 19:23:24 +02:00
fAllFromMe = fAllFromMe & & wallet - > IsMine ( txin ) ;
2011-06-10 15:05:51 +02:00
bool fAllToMe = true ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
fAllToMe = fAllToMe & & wallet - > IsMine ( txout ) ;
2011-06-10 15:05:51 +02:00
if ( fAllFromMe )
{
//
// Debit
//
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
{
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2011-06-10 15:05:51 +02:00
continue ;
2012-09-28 13:57:07 +02:00
if ( ! wtx . mapValue . count ( " to " ) | | wtx . mapValue [ " to " ] . empty ( ) )
2011-06-10 15:05:51 +02:00
{
// Offline transaction
2012-05-14 23:44:52 +02:00
CTxDestination address ;
if ( ExtractDestination ( txout . scriptPubKey , address ) )
2011-06-10 15:05:51 +02:00
{
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " To " ) + " :</b> " ;
2013-07-15 07:20:50 +02:00
if ( wallet - > mapAddressBook . count ( address ) & & ! wallet - > mapAddressBook [ address ] . name . empty ( ) )
strHTML + = GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] . name ) + " " ;
2012-05-14 23:44:52 +02:00
strHTML + = GUIUtil : : HtmlEscape ( CBitcoinAddress ( address ) . ToString ( ) ) ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
}
2013-08-24 15:07:17 +02:00
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , - txout . nValue ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
if ( fAllToMe )
{
// Payment to self
2011-12-21 22:33:19 +01:00
int64 nChange = wtx . GetChange ( ) ;
int64 nValue = nCredit - nChange ;
2013-08-24 15:07:17 +02:00
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , - nValue ) + " <br> " ;
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , nValue ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
2013-01-08 13:17:15 +01:00
int64 nTxFee = nDebit - GetValueOut ( wtx ) ;
2011-06-10 15:05:51 +02:00
if ( nTxFee > 0 )
2013-08-24 15:07:17 +02:00
strHTML + = " <b> " + tr ( " Transaction fee " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , - nTxFee ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
else
{
//
// Mixed debit transaction
//
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txin ) )
2013-08-24 15:07:17 +02:00
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , - wallet - > GetDebit ( txin ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2013-08-24 15:07:17 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , wallet - > GetCredit ( txout ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
}
2013-08-24 15:07:17 +02:00
strHTML + = " <b> " + tr ( " Net amount " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , nNet , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
// Message
//
2012-09-28 13:57:07 +02:00
if ( wtx . mapValue . count ( " message " ) & & ! wtx . mapValue [ " message " ] . empty ( ) )
2012-07-03 14:37:51 +02:00
strHTML + = " <br><b> " + tr ( " Message " ) + " :</b><br> " + GUIUtil : : HtmlEscape ( wtx . mapValue [ " message " ] , true ) + " <br> " ;
2012-09-28 13:57:07 +02:00
if ( wtx . mapValue . count ( " comment " ) & & ! wtx . mapValue [ " comment " ] . empty ( ) )
2012-07-03 14:37:51 +02:00
strHTML + = " <br><b> " + tr ( " Comment " ) + " :</b><br> " + GUIUtil : : HtmlEscape ( wtx . mapValue [ " comment " ] , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Transaction ID " ) + " :</b> " + wtx . GetHash ( ) . ToString ( ) . c_str ( ) + " <br> " ;
2012-01-10 19:25:02 +01:00
2013-07-22 08:50:39 +02:00
//
// PaymentRequest info:
//
foreach ( const PAIRTYPE ( string , string ) & r , wtx . vOrderForm )
{
if ( r . first = = " PaymentRequest " )
{
PaymentRequestPlus req ;
req . parse ( QByteArray : : fromRawData ( r . second . c_str ( ) , r . second . size ( ) ) ) ;
QString merchant ;
if ( req . getMerchant ( PaymentServer : : getCertStore ( ) , merchant ) )
strHTML + = " <b> " + tr ( " Merchant " ) + " :</b> " + GUIUtil : : HtmlEscape ( merchant ) + " <br> " ;
}
}
2011-06-10 15:05:51 +02:00
if ( wtx . IsCoinBase ( ) )
2012-07-03 14:37:51 +02:00
strHTML + = " <br> " + tr ( " Generated coins must mature 120 blocks before they can be spent. When you generated this block, it was broadcast to the network to be added to the block chain. If it fails to get into the chain, its state will change to \" not accepted \" and it won't be spendable. This may occasionally happen if another node generates a block within a few seconds of yours. " ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
// Debug view
//
if ( fDebug )
{
2012-07-03 14:37:51 +02:00
strHTML + = " <hr><br> " + tr ( " Debug information " ) + " <br><br> " ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txin ) )
2013-08-24 15:07:17 +02:00
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , - wallet - > GetDebit ( txin ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2013-08-24 15:07:17 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , wallet - > GetCredit ( txout ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
2012-07-03 14:37:51 +02:00
strHTML + = " <br><b> " + tr ( " Transaction " ) + " :</b><br> " ;
2011-11-11 10:13:25 +01:00
strHTML + = GUIUtil : : HtmlEscape ( wtx . ToString ( ) , true ) ;
2011-06-10 15:05:51 +02:00
2012-07-03 14:37:51 +02:00
strHTML + = " <br><b> " + tr ( " Inputs " ) + " :</b> " ;
2011-07-29 23:11:40 +02:00
strHTML + = " <ul> " ;
2012-04-06 18:39:12 +02:00
2011-06-10 15:05:51 +02:00
{
2012-04-06 18:39:12 +02:00
LOCK ( wallet - > cs_wallet ) ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
{
COutPoint prevout = txin . prevout ;
2011-07-29 23:11:40 +02:00
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
CCoins prev ;
2012-07-06 16:33:34 +02:00
if ( pcoinsTip - > GetCoins ( prevout . hash , prev ) )
2011-06-10 15:05:51 +02:00
{
if ( prevout . n < prev . vout . size ( ) )
{
2011-07-29 23:11:40 +02:00
strHTML + = " <li> " ;
const CTxOut & vout = prev . vout [ prevout . n ] ;
2012-05-14 23:44:52 +02:00
CTxDestination address ;
if ( ExtractDestination ( vout . scriptPubKey , address ) )
2011-07-29 23:11:40 +02:00
{
2013-07-15 07:20:50 +02:00
if ( wallet - > mapAddressBook . count ( address ) & & ! wallet - > mapAddressBook [ address ] . name . empty ( ) )
strHTML + = GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] . name ) + " " ;
2012-05-14 23:44:52 +02:00
strHTML + = QString : : fromStdString ( CBitcoinAddress ( address ) . ToString ( ) ) ;
2011-07-29 23:11:40 +02:00
}
2013-08-24 15:07:17 +02:00
strHTML = strHTML + " " + tr ( " Amount " ) + " = " + BitcoinUnits : : formatWithUnit ( unit , vout . nValue ) ;
2012-07-03 14:37:51 +02:00
strHTML = strHTML + " IsMine= " + ( wallet - > IsMine ( vout ) ? tr ( " true " ) : tr ( " false " ) ) + " </li> " ;
2011-06-10 15:05:51 +02:00
}
}
}
}
2012-07-03 14:37:51 +02:00
2011-07-29 23:11:40 +02:00
strHTML + = " </ul> " ;
2011-06-10 15:05:51 +02:00
}
strHTML + = " </font></html> " ;
}
return strHTML ;
}