@@ -783,19 +783,19 @@ bool CWallet::IsSpentKey(const uint256& hash, unsigned int n) const
783
783
return false ;
784
784
}
785
785
786
- bool CWallet::AddToWallet (const CWalletTx& wtxIn , bool fFlushOnClose )
786
+ CWalletTx* CWallet::AddToWallet (CTransactionRef tx, const CWalletTx::Confirmation& confirm, const UpdateWalletTxFn& update_wtx , bool fFlushOnClose )
787
787
{
788
788
LOCK (cs_wallet);
789
789
790
790
WalletBatch batch (*database, " r+" , fFlushOnClose );
791
791
792
- uint256 hash = wtxIn. GetHash ();
792
+ uint256 hash = tx-> GetHash ();
793
793
794
794
if (IsWalletFlagSet (WALLET_FLAG_AVOID_REUSE)) {
795
795
// Mark used destinations
796
796
std::set<CTxDestination> tx_destinations;
797
797
798
- for (const CTxIn& txin : wtxIn. tx ->vin ) {
798
+ for (const CTxIn& txin : tx->vin ) {
799
799
const COutPoint& op = txin.prevout ;
800
800
SetSpentKeyState (batch, op.hash , op.n , true , tx_destinations);
801
801
}
@@ -804,55 +804,51 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
804
804
}
805
805
806
806
// Inserts only if not already there, returns tx inserted or tx found
807
- std::pair<std::map<uint256, CWalletTx>::iterator, bool > ret = mapWallet.insert (std::make_pair (hash, wtxIn ));
807
+ auto ret = mapWallet.emplace (std::piecewise_construct, std::forward_as_tuple (hash), std::forward_as_tuple ( this , tx ));
808
808
CWalletTx& wtx = (*ret.first ).second ;
809
809
wtx.BindWallet (this );
810
810
bool fInsertedNew = ret.second ;
811
+ bool fUpdated = update_wtx && update_wtx (wtx, fInsertedNew );
811
812
if (fInsertedNew ) {
813
+ wtx.m_confirm = confirm;
812
814
wtx.nTimeReceived = chain ().getAdjustedTime ();
813
815
wtx.nOrderPos = IncOrderPosNext (&batch);
814
816
wtx.m_it_wtxOrdered = wtxOrdered.insert (std::make_pair (wtx.nOrderPos , &wtx));
815
817
wtx.nTimeSmart = ComputeTimeSmart (wtx);
816
818
AddToSpends (hash);
817
819
}
818
820
819
- bool fUpdated = false ;
820
821
if (!fInsertedNew )
821
822
{
822
- if (wtxIn. m_confirm .status != wtx.m_confirm .status ) {
823
- wtx.m_confirm .status = wtxIn. m_confirm .status ;
824
- wtx.m_confirm .nIndex = wtxIn. m_confirm .nIndex ;
825
- wtx.m_confirm .hashBlock = wtxIn. m_confirm .hashBlock ;
826
- wtx.m_confirm .block_height = wtxIn. m_confirm .block_height ;
823
+ if (confirm .status != wtx.m_confirm .status ) {
824
+ wtx.m_confirm .status = confirm .status ;
825
+ wtx.m_confirm .nIndex = confirm .nIndex ;
826
+ wtx.m_confirm .hashBlock = confirm .hashBlock ;
827
+ wtx.m_confirm .block_height = confirm .block_height ;
827
828
fUpdated = true ;
828
829
} else {
829
- assert (wtx.m_confirm .nIndex == wtxIn.m_confirm .nIndex );
830
- assert (wtx.m_confirm .hashBlock == wtxIn.m_confirm .hashBlock );
831
- assert (wtx.m_confirm .block_height == wtxIn.m_confirm .block_height );
832
- }
833
- if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe )
834
- {
835
- wtx.fFromMe = wtxIn.fFromMe ;
836
- fUpdated = true ;
830
+ assert (wtx.m_confirm .nIndex == confirm.nIndex );
831
+ assert (wtx.m_confirm .hashBlock == confirm.hashBlock );
832
+ assert (wtx.m_confirm .block_height == confirm.block_height );
837
833
}
838
834
// If we have a witness-stripped version of this transaction, and we
839
835
// see a new version with a witness, then we must be upgrading a pre-segwit
840
836
// wallet. Store the new version of the transaction with the witness,
841
837
// as the stripped-version must be invalid.
842
838
// TODO: Store all versions of the transaction, instead of just one.
843
- if (wtxIn. tx ->HasWitness () && !wtx.tx ->HasWitness ()) {
844
- wtx.SetTx (wtxIn. tx );
839
+ if (tx->HasWitness () && !wtx.tx ->HasWitness ()) {
840
+ wtx.SetTx (tx);
845
841
fUpdated = true ;
846
842
}
847
843
}
848
844
849
845
// // debug print
850
- WalletLogPrintf (" AddToWallet %s %s%s\n " , wtxIn. GetHash () .ToString (), (fInsertedNew ? " new" : " " ), (fUpdated ? " update" : " " ));
846
+ WalletLogPrintf (" AddToWallet %s %s%s\n " , hash .ToString (), (fInsertedNew ? " new" : " " ), (fUpdated ? " update" : " " ));
851
847
852
848
// Write to disk
853
849
if (fInsertedNew || fUpdated )
854
850
if (!batch.WriteTx (wtx))
855
- return false ;
851
+ return nullptr ;
856
852
857
853
// Break debit/credit balance caches:
858
854
wtx.MarkDirty ();
@@ -866,7 +862,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
866
862
867
863
if (!strCmd.empty ())
868
864
{
869
- boost::replace_all (strCmd, " %s" , wtxIn. GetHash () .GetHex ());
865
+ boost::replace_all (strCmd, " %s" , hash .GetHex ());
870
866
#ifndef WIN32
871
867
// Substituting the wallet name isn't currently supported on windows
872
868
// because windows shell escaping has not been implemented yet:
@@ -880,7 +876,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
880
876
}
881
877
#endif
882
878
883
- return true ;
879
+ return &wtx ;
884
880
}
885
881
886
882
void CWallet::LoadToWallet (CWalletTx& wtxIn)
@@ -960,13 +956,9 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, CWalletTx::Co
960
956
}
961
957
}
962
958
963
- CWalletTx wtx (this , ptx);
964
-
965
959
// Block disconnection override an abandoned tx as unconfirmed
966
960
// which means user may have to call abandontransaction again
967
- wtx.m_confirm = confirm;
968
-
969
- return AddToWallet (wtx, false );
961
+ return AddToWallet (MakeTransactionRef (tx), confirm, /* update_wtx= */ nullptr , /* fFlushOnClose= */ false );
970
962
}
971
963
}
972
964
return false ;
@@ -3007,29 +2999,30 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CTransac
3007
2999
void CWallet::CommitTransaction (CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm)
3008
3000
{
3009
3001
LOCK (cs_wallet);
3010
-
3011
- CWalletTx wtxNew (this , std::move (tx));
3012
- wtxNew.mapValue = std::move (mapValue);
3013
- wtxNew.vOrderForm = std::move (orderForm);
3014
- wtxNew.fTimeReceivedIsTxTime = true ;
3015
- wtxNew.fFromMe = true ;
3016
-
3017
- WalletLogPrintf (" CommitTransaction:\n %s" , wtxNew.tx ->ToString ()); /* Continued */
3002
+ WalletLogPrintf (" CommitTransaction:\n %s" , tx->ToString ()); /* Continued */
3018
3003
3019
3004
// Add tx to wallet, because if it has change it's also ours,
3020
3005
// otherwise just for transaction history.
3021
- AddToWallet (wtxNew);
3006
+ AddToWallet (tx, {}, [&](CWalletTx& wtx, bool new_tx) {
3007
+ CHECK_NONFATAL (wtx.mapValue .empty ());
3008
+ CHECK_NONFATAL (wtx.vOrderForm .empty ());
3009
+ wtx.mapValue = std::move (mapValue);
3010
+ wtx.vOrderForm = std::move (orderForm);
3011
+ wtx.fTimeReceivedIsTxTime = true ;
3012
+ wtx.fFromMe = true ;
3013
+ return true ;
3014
+ });
3022
3015
3023
3016
// Notify that old coins are spent
3024
- for (const CTxIn& txin : wtxNew. tx ->vin ) {
3017
+ for (const CTxIn& txin : tx->vin ) {
3025
3018
CWalletTx &coin = mapWallet.at (txin.prevout .hash );
3026
3019
coin.BindWallet (this );
3027
3020
NotifyTransactionChanged (this , coin.GetHash (), CT_UPDATED);
3028
3021
}
3029
3022
3030
3023
// Get the inserted-CWalletTx from mapWallet so that the
3031
3024
// fInMempool flag is cached properly
3032
- CWalletTx& wtx = mapWallet.at (wtxNew. GetHash ());
3025
+ CWalletTx& wtx = mapWallet.at (tx-> GetHash ());
3033
3026
3034
3027
if (!fBroadcastTransactions ) {
3035
3028
// Don't submit tx to the mempool
0 commit comments