| 929 | } |
| 930 | |
| 931 | bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose) |
| 932 | { |
| 933 | LOCK(cs_wallet); |
| 934 | |
| 935 | WalletBatch batch(*database, "r+", fFlushOnClose); |
| 936 | |
| 937 | uint256 hash = wtxIn.GetHash(); |
| 938 | |
| 939 | // Inserts only if not already there, returns tx inserted or tx found |
| 940 | std::pair<std::map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(std::make_pair(hash, wtxIn)); |
| 941 | CWalletTx& wtx = (*ret.first).second; |
| 942 | wtx.BindWallet(this); |
| 943 | bool fInsertedNew = ret.second; |
| 944 | if (fInsertedNew) { |
| 945 | wtx.nTimeReceived = GetAdjustedTime(); |
| 946 | wtx.nOrderPos = IncOrderPosNext(&batch); |
| 947 | wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr))); |
| 948 | wtx.nTimeSmart = ComputeTimeSmart(wtx); |
| 949 | AddToSpends(hash); |
| 950 | } |
| 951 | |
| 952 | bool fUpdated = false; |
| 953 | if (!fInsertedNew) |
| 954 | { |
| 955 | // Merge |
| 956 | if (!wtxIn.hashUnset() && wtxIn.hashBlock != wtx.hashBlock) |
| 957 | { |
| 958 | wtx.hashBlock = wtxIn.hashBlock; |
| 959 | fUpdated = true; |
| 960 | } |
| 961 | // If no longer abandoned, update |
| 962 | if (wtxIn.hashBlock.IsNull() && wtx.isAbandoned()) |
| 963 | { |
| 964 | wtx.hashBlock = wtxIn.hashBlock; |
| 965 | fUpdated = true; |
| 966 | } |
| 967 | if (wtxIn.nIndex != -1 && (wtxIn.nIndex != wtx.nIndex)) |
| 968 | { |
| 969 | wtx.nIndex = wtxIn.nIndex; |
| 970 | fUpdated = true; |
| 971 | } |
| 972 | if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe) |
| 973 | { |
| 974 | wtx.fFromMe = wtxIn.fFromMe; |
| 975 | fUpdated = true; |
| 976 | } |
| 977 | // If we have a witness-stripped version of this transaction, and we |
| 978 | // see a new version with a witness, then we must be upgrading a pre-segwit |
| 979 | // wallet. Store the new version of the transaction with the witness, |
| 980 | // as the stripped-version must be invalid. |
| 981 | // TODO: Store all versions of the transaction, instead of just one. |
| 982 | if (wtxIn.tx->HasWitness() && !wtx.tx->HasWitness()) { |
| 983 | wtx.SetTx(wtxIn.tx); |
| 984 | fUpdated = true; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | //// debug print |