return true;
}
+set<uint256> CWallet::GetConflicts(const uint256& txid) const
+{
+ set<uint256> result;
+ LOCK(cs_wallet);
+
+ std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(txid);
+ if (it == mapWallet.end())
+ return result;
+ const CWalletTx& wtx = it->second;
+
+ std::pair<TxConflicts::const_iterator, TxConflicts::const_iterator> range;
+
+ BOOST_FOREACH(const CTxIn& txin, wtx.vin)
+ {
+ range = mapTxConflicts.equal_range(txin.prevout);
+ for (TxConflicts::const_iterator it = range.first; it != range.second; ++it)
+ result.insert(it->second);
+ }
+ return result;
+}
+
+void CWallet::SyncMetaData(pair<TxConflicts::iterator, TxConflicts::iterator> range)
+{
+ // We want all the wallet transactions in range to have the same metadata as
+ // the oldest (smallest nOrderPos).
+ // So: find smallest nOrderPos:
+
+ int nMinOrderPos = std::numeric_limits<int>::max();
+ const CWalletTx* copyFrom = NULL;
+ for (TxConflicts::iterator it = range.first; it != range.second; ++it)
+ {
+ const uint256& hash = it->second;
+ int n = mapWallet[hash].nOrderPos;
+ if (n < nMinOrderPos)
+ {
+ nMinOrderPos = n;
+ copyFrom = &mapWallet[hash];
+ }
+ }
+ // Now copy data from copyFrom to rest:
+ for (TxConflicts::iterator it = range.first; it != range.second; ++it)
+ {
+ const uint256& hash = it->second;
+ CWalletTx* copyTo = &mapWallet[hash];
+ if (copyFrom == copyTo) continue;
+ copyTo->mapValue = copyFrom->mapValue;
+ copyTo->vOrderForm = copyFrom->vOrderForm;
+ // fTimeReceivedIsTxTime not copied on purpose
+ // nTimeReceived not copied on purpose
+ copyTo->nTimeSmart = copyFrom->nTimeSmart;
+ copyTo->fFromMe = copyFrom->fFromMe;
+ copyTo->strFromAccount = copyFrom->strFromAccount;
+ // vfSpent not copied on purpose
+ // nOrderPos not copied on purpose
+ // cached members not copied on purpose
+ }
+}
+
+void CWallet::AddToConflicts(const uint256& wtxhash)
+{
+ assert(mapWallet.count(wtxhash));
+ CWalletTx& thisTx = mapWallet[wtxhash];
+ if (thisTx.IsCoinBase())
+ return;
+
+ BOOST_FOREACH(const CTxIn& txin, thisTx.vin)
+ {
+ mapTxConflicts.insert(make_pair(txin.prevout, wtxhash));
+
+ pair<TxConflicts::iterator, TxConflicts::iterator> range;
+ range = mapTxConflicts.equal_range(txin.prevout);
+ if (range.first != range.second)
+ SyncMetaData(range);
+ }
+}
+
bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
{
if (IsCrypted())
}
}
-bool CWallet::AddToWallet(const CWalletTx& wtxIn)
+bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet)
{
uint256 hash = wtxIn.GetHash();
+ if (fFromLoadWallet)
+ {
+ mapWallet[hash] = wtxIn;
+ AddToConflicts(hash);
+ }
+ else
{
LOCK(cs_wallet);
// Inserts only if not already there, returns tx inserted or tx found
}
else
printf("AddToWallet() : found %s in block %s not in index\n",
- hash.ToString().substr(0,10).c_str(),
- wtxIn.hashBlock.ToString().c_str());
+ hash.ToString().c_str(),
+ wtxIn.hashBlock.ToString().c_str());
}
+ AddToConflicts(hash);
}
bool fUpdated = false;
// Sent/received.
BOOST_FOREACH(const CTxOut& txout, vout)
{
+ bool fIsMine;
+ // Only need to handle txouts if AT LEAST one of these is true:
+ // 1) they debit from us (sent)
+ // 2) the output is to us (received)
+ if (nDebit > 0)
+ {
+ // Don't report 'change' txouts
+ if (pwallet->IsChange(txout))
+ continue;
+ fIsMine = pwallet->IsMine(txout);
+ }
+ else if (!(fIsMine = pwallet->IsMine(txout)))
+ continue;
+
+ // In either case, we need to get the destination address
CTxDestination address;
- vector<unsigned char> vchPubKey;
if (!ExtractDestination(txout.scriptPubKey, address))
{
printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
this->GetHash().ToString().c_str());
+ address = CNoDestination();
}
- // Don't report 'change' txouts
- if (nDebit > 0 && pwallet->IsChange(txout))
- continue;
-
+ // If we are debited by the transaction, add the output as a "sent" entry
if (nDebit > 0)
listSent.push_back(make_pair(address, txout.nValue));
- if (pwallet->IsMine(txout))
+ // If we are receiving the output, add it as a "received" entry
+ if (fIsMine)
listReceived.push_back(make_pair(address, txout.nValue));
}
}
}
+set<uint256> CWalletTx::GetConflicts() const
+{
+ set<uint256> result;
+ if (pwallet != NULL)
+ {
+ uint256 myHash = GetHash();
+ result = pwallet->GetConflicts(myHash);
+ result.erase(myHash);
+ }
+ return result;
+}
+
void CWalletTx::RelayWalletTransaction()
{
CCoinsViewCache& coins = *pcoinsTip;
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
const CWalletTx* pcoin = &(*it).second;
- if (pcoin->IsConfirmed())
+ if (pcoin->IsTrusted())
nTotal += pcoin->GetAvailableCredit();
}
}
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
const CWalletTx* pcoin = &(*it).second;
- if (!pcoin->IsFinal() || !pcoin->IsConfirmed())
+ if (!pcoin->IsFinal() || (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0))
nTotal += pcoin->GetAvailableCredit();
}
}
{
const CWalletTx* pcoin = &(*it).second;
- if (!pcoin->IsFinal())
+ if (!pcoin->IsFinal()) {
+ continue;
+ }
+
+ if (fOnlyConfirmed && !pcoin->IsTrusted()) {
continue;
+ }
- if (fOnlyConfirmed && !pcoin->IsConfirmed())
+ if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0) {
continue;
+ }
- if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
+ int nDepth = pcoin->GetDepthInMainChain();
+ if (nDepth < 0) {
continue;
+ }
- if(pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0)
+ if(pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0) {
continue;
+ }
- for (unsigned int i = 0; i < pcoin->vout.size(); i++)
+ for (unsigned int i = 0; i < pcoin->vout.size(); i++) {
if (!(pcoin->IsSpent(i)) && IsMine(pcoin->vout[i]) && pcoin->vout[i].nValue >= nMinimumInputValue &&
(!coinControl || !coinControl->HasSelected() || coinControl->IsSelected((*it).first, i)))
- vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain()));
-
+ vCoins.push_back(COutput(pcoin, i, nDepth));
+ }
}
}
}
void CWallet::AvailableCoinsMinConf(vector<COutput>& vCoins, int nConf) const
{
vCoins.clear();
-
{
LOCK(cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
}
int64 nChange = nValueIn - nValue - nFeeRet;
+
// if sub-cent change is required, the fee must be raised to at least MIN_TX_FEE
// or until nChange becomes zero
// NOTE: this depends on the exact behaviour of GetMinFee
- if (nFeeRet < MIN_TX_FEE && nChange > 0 && nChange < CENT)
+ if (wtxNew.nTime < FEE_SWITCH_TIME && !fTestNet)
{
- int64 nMoveToFee = min(nChange, MIN_TX_FEE - nFeeRet);
- nChange -= nMoveToFee;
- nFeeRet += nMoveToFee;
- }
+ if (nFeeRet < CENT && nChange > 0 && nChange < CENT)
+ {
+ int64 nMoveToFee = min(nChange, CENT - nFeeRet);
+ nChange -= nMoveToFee;
+ nFeeRet += nMoveToFee;
+ }
- // sub-cent change is moved to fee
- if (nChange > 0 && nChange < MIN_TXOUT_AMOUNT)
- {
- nFeeRet += nChange;
- nChange = 0;
+ // sub-cent change is moved to fee
+ if (nChange > 0 && nChange < CENT)
+ {
+ nFeeRet += nChange;
+ nChange = 0;
+ }
}
if (nChange > 0)
// Check that enough fee is included
int64 nPayFee = nTransactionFee * (1 + (int64)nBytes / 1000);
bool fAllowFree = CTransaction::AllowFree(dPriority);
- int64 nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND);
+
+ // Disable free transactions until 1 July 2014
+ if (!fTestNet && wtxNew.nTime < FEE_SWITCH_TIME)
+ {
+ fAllowFree = false;
+ }
+
+ int64 nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND, nBytes);
if (nFeeRet < max(nPayFee, nMinFee))
{
nFeeRet = max(nPayFee, nMinFee);
return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, coinControl);
}
+void CWallet::GetStakeWeightFromValue(const int64& nTime, const int64& nValue, uint64& nWeight)
+{
+ int64 nTimeWeight = GetWeight(nTime, (int64)GetTime());
+
+ // If time weight is lower or equal to zero then weight is zero.
+ if (nTimeWeight <= 0)
+ {
+ nWeight = 0;
+ return;
+ }
+
+ CBigNum bnCoinDayWeight = CBigNum(nValue) * nTimeWeight / COIN / (24 * 60 * 60);
+ nWeight = bnCoinDayWeight.getuint64();
+}
+
+
// NovaCoin: get current stake weight
bool CWallet::GetStakeWeight(const CKeyStore& keystore, uint64& nMinWeight, uint64& nMaxWeight, uint64& nWeight)
{
return error("CreateCoinStake : exceeded coinstake size limit");
// Check enough fee is paid
- if (nMinFee < txNew.GetMinFee() - MIN_TX_FEE)
+ if (nMinFee < txNew.GetMinFee(1, false, GMF_BLOCK, nBytes, CENT) - CENT)
{
- nMinFee = txNew.GetMinFee() - MIN_TX_FEE;
+ nMinFee = txNew.GetMinFee(1, false, GMF_BLOCK, nBytes, CENT) - CENT;
continue; // try signing again
}
else
return DB_LOAD_OK;
}
+DBErrors CWallet::ZapWalletTx()
+{
+ if (!fFileBacked)
+ return DB_LOAD_OK;
+ DBErrors nZapWalletTxRet = CWalletDB(strWalletFile,"cr+").ZapWalletTx(this);
+ if (nZapWalletTxRet == DB_NEED_REWRITE)
+ {
+ if (CDB::Rewrite(strWalletFile, "\x04pool"))
+ {
+ LOCK(cs_wallet);
+ setKeyPool.clear();
+ // Note: can't top-up keypool here, because wallet is locked.
+ // User will be prompted to unlock wallet the next operation
+ // the requires a new key.
+ }
+ }
+
+ if (nZapWalletTxRet != DB_LOAD_OK)
+ return nZapWalletTxRet;
+
+ return DB_LOAD_OK;
+}
bool CWallet::SetAddressBookName(const CTxDestination& address, const string& strName)
{
{
CWalletTx *pcoin = &walletEntry.second;
- if (!pcoin->IsFinal() || !pcoin->IsConfirmed())
+ if (!pcoin->IsFinal() || !pcoin->IsTrusted())
continue;
if ((pcoin->IsCoinBase() || pcoin->IsCoinStake()) && pcoin->GetBlocksToMaturity() > 0)